IOT based Soil Moisture Monitoring on Arduino Cloud using Arduino Nano ESP32
Here we are going to build a smart Irrigation project using Arduino NANO ESP 32, DHT22 (temperature and humidity sensor), soil moisture sensor and Arduino cloud (cloud server and dashboard).
Here we are going to build a smart Irrigation project using Arduino NANO ESP 32, DHT22 (temperature and humidity sensor), soil moisture sensor and Arduino cloud (cloud server and dashboard). Realtime data generated by sensors can be monitored from anywhere on Mobile app as well as web dashboard. We can also operate switch from anywhere using mobile app or dashboard Â
So now let’s start Â
So first we know about
What is Arduino NANO ESP 32 Cloud?
Arduino cloud is cloud-based service provided by Arduino (an open-source electronic platform).it allows us to connect our Arduino devices to internet and manage them remotely Â
Some of the key features of Arduino cloud areÂ
- Remote Monitoring and controlÂ
- IOT IntegrationÂ
- Data Storage and AnalysisÂ
- User-friendly interfaceÂ
- SecurityÂ
- Automation & AlertsÂ
Table of contentÂ
-
- Hardware requirementÂ
-
- Software requirementÂ
-
- Creating thing and dashboard on Arduino cloudÂ
-
- Code and librariesÂ
-
- Circuit diagramÂ
-
- Hardware interfacing Â
-
- Final working of projectÂ
- ConclusionÂ
Hardware requirement of this projectÂ
- Arduino NANO ESP32Â Â
- DHT22 (Temperature and Humidity) SensorÂ
- Soil Moisture SensorÂ
- LEDÂ
- Universal PCBÂ
- BreadboardÂ
- Jumper Wire Â
- USB data cableÂ
Software requirement of this project Â
- Â Arduino IDEÂ
- Arduino cloud WebÂ
- Arduino IOT Remote AppÂ
- Arduino create agent for cloud Â
So, let’s start creating DashboardÂ
- Create/login to Arduino cloud accountÂ
- Click on Things > CREAT THINGÂ
-
- Click on Add variables Â
 Name > Humidity >Declaration> CloudRelativeHumidityhumidity Type> Relative humidity > Variable Permission> Read Only >Update Policy> On changeÂ
Create all variable as mentioned below follow same steps shown above Â
Name >LED> Declaration>Â CloudLightlED> Type>Â Switch> Variable Permission>Â Read & Write> Update Policy>Â On changeÂ
Name > Moisture > Declaration> floatmoisture > Type> Floating point number > Variable Permission> Read Only > Update Policy> On changeÂ
Name > Temperature > Declaration> CloudTemperatureSensortemperature Type> Temperature sensor (°c) > Variable Permission> Read Only > Update Policy> On change
4. Once all variables are created Select and configure associate device  Â
 Here we have Selected Arduino Nano ESP 32 Once device is selected properlyÂ
The device ID and Device Secret key will Displayed we have to Copy it and save It Â
After selecting the device, we have to configure the network. (Providing name and credential information of Wi-Fi or Hotspot & Device secrete key) to which we are supposed to connect our Arduino
Nano ESP32
Follow the steps as shown belowÂ
 5.Configure network >Wi-Fi Name>Wi-Fi Password>Device Secrete keyÂ
6. Create a dashboardÂ
After configuring the network, we create a dashboard where our data will be displayed and Monitored Â
Follow the steps Â
Click on Dashboards > CREATE DASHBOARD > ADDÂ
      Select variable; Switch > Link variable      Â
Things >LED>LINK VARIABLE>DONEÂ
In this step we create a dashboard icon and connect those icons to variables which we had created earlierÂ
Follow above steps for linking all the variables mentioned belowÂ
Similarly; ADD>GAUGE> Link variable > Things >Temperature>LINK VARIABLE>DONEÂ
Similarly; ADD>Percentage> Link variable > Things >Humidity>LINK VARIABLE>DONEÂ
Similarly; ADD> Percentage > Link variable > Things >Moisture>LINK VARIABLE>DONEÂ
Similarly; ADD> Chart > Link variable > Things >Temperature>LINK VARIABLE>DONEÂ
 7.Now Open Sketch in full Editor Â
 8.Edit the code according to variablesÂ
9.Download Arduino Create Agent & Connect Arduino Nano ESP32 board in bootloader modeÂ
10. Select the proper Arduino Board and portÂ
11. Verify and upload the codeÂ
(NOTE: – Be sure Arduino NANO ESP32 is in bootloader mode, Arduino Create Agent is active &other Arduino IDE  are closed while uploading code otherwise board will throw ERROR)Â
CODE:
#include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/187bfad8-5590-4e74-8cfc-15e06c1344b5
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float moisture;
CloudLight lED;
CloudTemperatureSensor temperature;
CloudRelativeHumidity humidity;
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"
#include <DHT.h>;
//Constants
#define LED_PIN 2
#define DHTPIN 13 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);//// Initialize DHT sensor for normal 16mhz Arduino
int SENSOR_PIN = A0; // Analog pin defined for soil moisture sensor
float percentage;
float map_low = 4100;
float map_high = 2700;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(DHTPIN,INPUT);
pinMode(SENSOR_PIN,INPUT);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
dht.begin();
// Defined in thingProperties.h
initProperties();
// 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
float m = analogRead(SENSOR_PIN);
moisture = m;
Serial.print("Raw: ");
Serial.print(moisture);
percentage = map(moisture, map_low, map_high, 0, 100);
moisture = percentage;
if (percentage > 50) {
digitalWrite(LED_PIN, LOW);
Serial.print("Water Pump is OFF ");
} else {
digitalWrite(LED_PIN, HIGH);
Serial.print("Water Pump is ON");
}
Serial.print(" | Percentage: ");
Serial.print(percentage);
Serial.println("%");
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
temperature = t;
humidity = h;
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
}else{
Serial.print(F("Temprature- "));Serial.print(t);
Serial.print(F("Humidity- "));Serial.print(h);
delay(2000);
}
}
/*
Since LED is READ_WRITE variable, onLEDChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLEDChange() {
if (lED == 0 )
{
digitalWrite(LED_PIN, LOW);
}
else
{
digitalWrite(LED_PIN, HIGH);
}
// Add your code here to act upon LED change
}
Libraries:
If your using Arduino cloud for uploading code Arduino cloud agent will download all necessary libraries automaticallyÂ
If not Â
Download Â
Arduino_ESP_32_OTA from Library manager of Arduino IDE
If you want to upload code from Arduino IDE Download it from Arduino cloud sketchÂ
Add network credential properly with device secret key.Â
(NOTE: Don’t forget to pause Arduino create agent else IDE can throw ERROR)Â
Circuit diagram:
Interface the components as per circuit diagramÂ
DHT22 sensorÂ
DHT22 Sensor pins | Arduino NANO ESP32 PINS |
+VE | 3.3v |
-Ve | GND |
OUTÂ | D13Â |
Soil Moisture sensorÂ
Soil moisture sensor | Arduino NANO ESP32 PINS  |
VCCÂ | 3.3VÂ |
GNDÂ | GNDÂ |
A0Â | A0Â |
AND connect LED to D2 pinÂ
Hardware interfacing
We have made the connection on PCB as shown in circuit diagramÂ
Assembled all the components on PCB and provided supply to Arduino Nano ESP32 boardÂ
As code is already uploaded, we just need to verify all connections are proper and our Wi-Fi is operational and Arduino Nano ESP32 board is connected to Wi-Fi & Cloud dashboard Â
Demonstration /how it worksÂ
Sensor is in moist soil moisture is optimum in soil, so LED is OFFÂ
Other Climatic factors like temperature and humidity are  measured are displayed as shown on mobile screenÂ
Sensor is out of moist soil no moisture is sensed by sensor so LED is ON, and it will remain ON until moisture reaches optimum level Â
Conclusion:
In this way we have created a project for Monitoring Temperature, Humidity and soil moisture Using Arduino Nano ESP 32 & Arduino cloud for smart irrigation. By using the power of cloud technology this project offers wide range of benefits, from improving water usage efficiency to real-time monitoring & control capabilities which can empower farmers to make data-driven decision, leading to increase in crop yield.Â