#include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/3604a9f3-d22a-4569-a9fd-8f0000d5d9c8
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float temperature;
int humidity;
int waterTime;
bool pumpSwitch;
bool waterStatus;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
// --------- Pump Relay ----------
const byte pumpRelayPin = 4; // Replay Pin for controlling Pump
bool relayLogic = LOW; // Relay working logic change this to HIGH if your relay works with HIGH Logic
// ---------- DHT 11 Sensor ------------
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 11 Sensor Type
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
// ----------- Soil Moisture Sensor ----------
const byte soilMoisturePin = 32; // Soil Moisture sensor pin
const byte drySoilValue = 780; // Dry Soil value received by sensor
const byte wetSoilValue = 220; // Wet soil value received by sensor
int moistureValue = 0; // Variable to hold moisture value
// ------------ Water Status -----------
const byte waterStatusPin = 5; // Water status sensor pin
int totalWaterTime = 0; // variable to read waterTime from slider
unsigned long waterStartTime = 0; // variable to hold water start time
bool pumpStatus = LOW; // Flag to check if pump is on/off
unsigned long updateTime = 2000; //Update values on cloud after every 2 seconds (2 seconds = 2000 milliseconds)Update values on cloud after every 10 seconds (10 seconds = 10000 milliseconds)
unsigned long lastUpdateTime = millis(); // variable to hold last values update time
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(pumpRelayPin, OUTPUT); // Set pump relay pin as output
digitalWrite(pumpRelayPin, !relayLogic); // Initially Turn off relay
// Defined in thingProperties.h
initProperties();
dht.begin(); // initialize DHT sensor
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
int rawMoisture = analogRead(soilMoisturePin); // Read Raw soil mositure value
int moistureValue = map(rawMoisture, wetSoilValue, drySoilValue, 100, 0);
if (humidity != moistureValue) { // update
humidity = moistureValue;
}
if (temperature != dht.readTemperature()) {
Serial.print("Update temperature:");
Serial.println(dht.readTemperature());
temperature = dht.readTemperature();
}
waterStatus = digitalRead(waterStatusPin); // Update water status based on status sensor pin
// if ((millis() - lastUpdateTime) > updateTime) { // Update values on cloud after every 10 seconds (10 seconds = 10000 milliseconds)
// lastUpdateTime = millis(); // note current update time
// Serial.println("Update Values");
// // ---- Read Water Status ---------
// }
if ((pumpStatus) && ((millis() - waterStartTime) / 1000.0 > waterTime)) { // Check if user has manually turned on pump and time has reached turn off pump
digitalWrite(pumpRelayPin, relayLogic); // turn off relay
pumpStatus = LOW; // Reset pump status
pumpSwitch = false; // Reset pump switch status
}
}
/*
Since WaterTime is READ_WRITE variable, onWaterTimeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onWaterTimeChange() {
// Add your code here to act upon WaterTime change
totalWaterTime = waterTime; // Read water time from slider
Serial.print("Set water time:");
Serial.println(totalWaterTime);
}
/*
Since PumpSwitch is READ_WRITE variable, onPumpSwitchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPumpSwitchChange() {
// Add your code here to act upon PumpSwitch change
if (pumpSwitch == true) { // if user selected on
digitalWrite(pumpRelayPin, relayLogic); // Turn on Relay
waterStartTime = millis(); // note current water time
pumpStatus = HIGH; // set pump status
Serial.println("Turn On Pump");
} else { // If user selected off
digitalWrite(pumpRelayPin, !relayLogic); // turn off relay
pumpStatus = LOW; // Reset pump status
Serial.println("Turn off Pump");
}
}