Arduino Cloud Design

arduino-cloud.png

Casing Design:

Untitled

Prototype With 3D Printed Casing

IMG_20240622_070501.jpg

IMG_20240622_070421.jpg

Arduino IDE Code:

#include "thingProperties.h"
#include <DHT.h>

#define relayPin 12
#define moisturePin A0
#define DHTPIN 2
#define DHTTYPE DHT11
#define debugPin 4

#define wet 210
#define dry 510

DHT dht(DHTPIN, DHTTYPE);

unsigned long lastSensorRead = 0;
const unsigned long sensorReadInterval = 2000; // Read sensors every 2 seconds
const unsigned long pumpOnDuration = 5000; // Minimum pump on duration in milliseconds

bool pumpState = false;
unsigned long pumpActivatedTime = 0;

void setup() {
  Serial.begin(9600);
  delay(1500);

  pinMode(relayPin, OUTPUT);
  pinMode(debugPin, INPUT);
  digitalWrite(relayPin, HIGH);

  dht.begin();

  // Initialize properties
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  // Set debug message level
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();

  Serial.print("Connecting to WiFi: ");
  Serial.println(SSID);

  // Wait for WiFi to connect
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println();
  Serial.print("Connected to WiFi, IP address: ");
  Serial.println(WiFi.localIP());

  // Wait for the IoT Cloud connection
  Serial.println("Connecting to Arduino IoT Cloud...");
  while (ArduinoCloud.connected() != 1) {
    ArduinoCloud.update();
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to Arduino IoT Cloud");
}

void loop() {
  ArduinoCloud.update();

  // Only read sensor values and control the pump if connected to the IoT Cloud
  if (ArduinoCloud.connected() && (millis() - lastSensorRead >= sensorReadInterval)) {
    lastSensorRead = millis();

    sensorValue = analogRead(moisturePin);
    int moisturePercent = map(sensorValue, dry, wet, 0, 100);

    temperature = dht.readTemperature();
    humidity = dht.readHumidity();

    if (isnan(temperature) || isnan(humidity)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }

    Serial.print("Moisture: ");
    Serial.print(moisturePercent);
    Serial.print("%, Temperature: ");
    Serial.print(temperature);
    Serial.print("°C, Humidity: ");
    Serial.print(humidity);
    Serial.println("%");

    // Automatic pump control based on soil moisture
    if ((moisturePercent < 30 || digitalRead(debugPin) == HIGH) && !pumpState) { // Adjust this threshold based on your needs
      // Turn pump on
      digitalWrite(relayPin, LOW);
      relay = true;
      pumpState = true;
      pumpActivatedTime = millis();

      if (digitalRead(debugPin) == HIGH) {
        Serial.println("DebugPin Triggered");
      }
    } else if ((moisturePercent > 40 && pumpState) && (millis() - pumpActivatedTime >= pumpOnDuration)) { // Adjust this threshold based on your needs
      // Turn pump off
      digitalWrite(relayPin, HIGH);
      relay = false;
      pumpState = false;
    }
  }
}

// Arduino IoT Cloud callback function
void onRelayChange() {
  if (relay) {
    digitalWrite(relayPin, LOW);
  } else {
    digitalWrite(relayPin, HIGH);
  }
}

void onSensorChange() {
  // Placeholder for sensor change logic if needed
}