IoT based Smart Irrigation

Hey Folks, I’m back again with a very interesting and almost most useful project related to irrigation. IoT based smart irrigation. People who love gardening feels it very annoying to go everyday for status of their plants. So today we gonna demonstrate how you can get real time status of your garden with the help of few components. We gonna use NodeMCU and Blynk app for real time data capture for viewing status of mostly all things. So let’s begin!!!!

NodeMCU:

If you are new to this site or a beginner so before starting read this brief info about NodeMCU. NodeMCu is basically same as other microcontrollers like Arduino Uno or Nano. But it has better speed, flash space, also has in-built Wi-Fi and bluetooth. There are various models in market which are very good and has both functionalities. But today we gonna use most common version available in the market i.e., NodeMCU (ESP8266MOD).

Also it runs on 3.3v logic level which reduces the chances of logic level problem we came across various sensors while using Arduino microcontroller. So to sum up I must say that it is pretty much everything for a beginner in IoT field. Remember to download CH340 driver for this module as sometimes it doesn’t get detected on computer or laptop at first. So install it from here.

Material Required:

  • NodeMCU (ESP8266MOD)
  • Soil-Moisture sensor
  • DHT11 sensor
  • Relay module
  • Water Pump
  • Green Led with resistor
  • 12V supply for pump( if needed)

Circuit Diagram:

PIN CONNECTIONS:

DHT11 —> D4

Soil-moisture —> A0

LED —> D6

Realy —> D7

rest are VCC and GND connections

Code:

In the beginning we include some libraries which are required in this project. Then we assign pins to which both sensor , including led and relay is connected. Along with this we also store our auth key received on email and wifi ssid & password.

After that we create a  function named sendSensor() to check DHT11 sensor and read value of temperature and humidity from it. Also if sensor fails to get these values it display error message on serial monitor.

  
 void sendSensor()  
 {  
  float h = dht.readHumidity();  
  float t = dht.readTemperature();  
  if (isnan(h) || isnan(t)) {  
   Serial.println("Failed to read from DHT sensor!");  
   return;  
  }  
  Blynk.virtualWrite(V5, h); //V5 is for Humidity  
  Blynk.virtualWrite(V6, t); //V6 is for Temperature  
 }  
 

Then we begin our setup() loop to initialize connection to our wifi and begin transmission between blynk app and NodeMCU. also initializes the DHT11sensor.

  
 void setup()  
 {  
  Serial.begin(9600);  
  Blynk.begin(auth, ssid, pass);  
  dht.begin();  
  timer.setInterval(1000L, sendSensor);  
  Serial.begin(115200);  
  Blynk.begin(auth, ssid, pass);  
   sensors.begin();  
 }  

Then we create another function senstemp() to check values from all sensor i.e., DHT11 and soil-moisture.

 
 void sendTemps()  
 {  
 sensor=analogRead(A0);  
 sensors.requestTemperatures();  
 float temp = sensors.getTempCByIndex(0);   
 Serial.println(temp);  
 Serial.println(sensor);  
 Blynk.virtualWrite(V1, temp);  
 Blynk.virtualWrite(V2,sensor);  
 delay(1000);  
 }  

Then we run all these codes in loop()

Here is complete code:

 //TECHATRONIC.COM  
 // BLYNK LIBRARY  
 // https://github.com/blynkkk/blynk-library  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 // DHT11 SENSOR LIBRARY  
 // https://github.com/adafruit/DHT-sensor-library  
 #define BLYNK_PRINT Serial    
 #include <SPI.h>  
 #include <ESP8266WiFi.h>  
 #include <BlynkSimpleEsp8266.h>  
 #include <SimpleTimer.h>  
 #include <DHT.h>  
 #define BLYNK_PRINT Serial  
 #include <OneWire.h>  
 #include <DallasTemperature.h>  
 #define ONE_WIRE_BUS D2  
 OneWire oneWire(ONE_WIRE_BUS);  
 DallasTemperature sensors(&oneWire);  
 char auth[] =" your auth code";   
 char ssid[] = "your wifi ssid";   
 char pass[] = "your wifi password";   
 #define DHTPIN 2    
 #define DHTTYPE DHT11   // DHT11 SENSOR CONNECT D4 PIN  
 DHT dht(DHTPIN, DHTTYPE);  
 SimpleTimer timer;  
 void sendSensor()  
 {  
  float h = dht.readHumidity();  
  float t = dht.readTemperature();  
  if (isnan(h) || isnan(t)) {  
   Serial.println("Failed to read from DHT sensor!");  
   return;  
  }  
  Blynk.virtualWrite(V5, h); //V5 is for Humidity  
  Blynk.virtualWrite(V6, t); //V6 is for Temperature  
 }  
 void setup()  
 {  
  Serial.begin(9600);  
  Blynk.begin(auth, ssid, pass);  
  dht.begin();  
  timer.setInterval(1000L, sendSensor);  
  Serial.begin(115200);  
  Blynk.begin(auth, ssid, pass);  
   sensors.begin();  
 }  
 int sensor=0;  
 void sendTemps()  
 {  
 sensor=analogRead(A0);  
 sensors.requestTemperatures();  
 float temp = sensors.getTempCByIndex(0);   
 Serial.println(temp);  
 Serial.println(sensor);  
 Blynk.virtualWrite(V1, temp);  
 Blynk.virtualWrite(V2,sensor);  
 delay(1000);  
 }  
 void loop()  
 {  
  Blynk.run();   
  timer.run();   
  sendTemps();  
 }  

Blynk App linking:

Download and install Blynk app from any App store Playstore | Apple appstore . Also login into App

You will see main screen after logging into the app

Then create a new project with any of your desired name(IoT base irrigation here)

An authentication token will be sent to your registered Email-ID. View the mail and copy & paste it in the code where auth key is required. Also fill up your Wi-Fi details so that Blynk app and NodeMCU can communicate.

You’ll see this type of screen after creating a project

Now add some Widgets to get the value from NodeMCU.

Add all eidgets as directed in above pictures

After completion you’ll see this type of interface

Now you are ready to grab values and control pump and light with Blynk app

You will see this type of output in serial monitor, If your connection is successful.

With this we have completed our IoT based smart irrigation project. Enjoy it & if you face any problem then ask me below.

Leave a Comment