smart irrigation system project
(Last Updated On: June 3, 2021)

Table of Contents:

Overview:

The Smart Irrigation System has wide scope to automate the complete irrigation system. Here we are building an IoT based Smart Irrigation System using the ESP8266 NodeMCU Module and Soil-moisture sensor.

The main motto of the project is to automatically irrigate the water based on the moisture level in the soil and send the Data to ThingSpeak Server to keep track of the land condition.

The System will consist of Two devices, one is Sensor node and other is Central node. There will be wireless communication between the two nodes. Sensor node will fetch the data from the Soil-Moisture sensor and transfer to the Central node. The central node will receive data and perform analytics, based on the soil moisture data water irrigation will take place.  the same data will be transferred to the Thingspeak over the internet keep track of the entire crop moisture level.

You can watch the following video to look into the real-time implementation of Smart Irrigation System Project

Required Components :

  1. NodeMCU -2                                                   Buy from Amazon
  2. Soil Moisture sensor                                        Buy from Amazon
  3.  Two DC motors(10RPM)/ Solenoid Valve      Buy from Amazon
  4. Motor driver/Relay                                          Buy from Amazon
  5. Lithium-ion Batteries.                                      Buy from Amazon
  6. Aluminum sheet for making the casing.        Buy from Amazon

Circuit Diagram:

Smart Irrigation System Sensor Node:

Sensor node contains NodeMCU-1, Soil moisture sensor-1, Battery (Sufficient enough to drive nodeMCU)-1. We have used custom-designed Sensor, for representation I am using Sparkfun soil moisture sensor.

Smart Irrigation System

Smart Irrigation System Central node:

Central node Contains NodeMCU-1,  L293d Motor driver, DC motors(10RPM)-2, Battery (Sufficient enough to drive two DC motors)-1.

Smart Irrigation System

In this project, I have designed a custom valve mechanism to turn On and Off the water supply using the existing valve as shown in the below figure.

Valve Mechanism

You can use the solenoid valve to full fill the same requirement which is readily available in the market. find the circuit diagram to interface the solenoid with NodeMCU using the relay.

Smart Irrigation System Central node with Solenoid valve:

This circuit contains NodeMCU-1, Relay, Solenoid valve, Battery (Sufficient enough to drive both NodeMCU & Solenoid valve)-1.

Smart Irrigation System

 

NodeMCU Source Code/Program:

For getting started with NodeMCU and Arduino IDE check: NodeMCU programming with ArduinoIDE in 5min.

This tutorial will help you if you are a beginner with NODEMCU.

Source Code for Sensor Node:
/* Sensor Node Program for NodeMCU-ESP8266
* Project Smart irrigation system
* Program made by M V Subrahmanyam,
* www.electronicsinnovation.com
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
IPAddress ip (192,168,4,1);  //ip address of Acces Point
WiFiUDP udp;
char incomingPacket[255];   //buffer for storing incoming packet
char buf[50];
int flag=0;
int moisturevalue=0;
void setup(){
   Serial.begin(9600);
   delay(10);
   WiFi.mode(WIFI_STA);
   pinMode(A0,INPUT);// taking input from moisture sensor
   while(1)
   {
    {
      WiFi.begin("Electronics_Innovation","subscribe");// WiFi.begin("ssid","password"); 
        Serial.print("Connecting to AP");
        while(WiFi.status() != WL_CONNECTED)
        {
          Serial.print("."); //waiting for connection
          delay(500);
        }
      break;
    }
   } 
   Serial.println("WiFi connected");
   Serial.println("IP address: ");
   Serial.println(WiFi.localIP());
   Serial.println("Moisture---------------\n\n");
   udp.begin(8100); //add 100 to get a new port
}
 
void loop(){
  //Start of Program
     moisturevalue=analogRead(A0);
    if(WiFi.status()!=WL_CONNECTED)
    {
      ESP.restart(); // trying to connect to the UDP Servr.
    }
    String m=String(moisturevalue);
    
    Serial.println(moisturevalue);
    m.toCharArray(buf,m.length()+1);
    Serial.println("my string in buf is ");
    Serial.println(buf);
  
    udp.beginPacket(ip, 8100); // remote IP and port
    udp.write(buf);
    udp.endPacket();
    int packetSize = udp.parsePacket();
   // to recieve conformation from server when data is recieved 
    if(packetSize)
    {
      Serial.printf("received reply from %s, port %d\n",udp.remoteIP().toString().c_str(), udp.remotePort());
      int len = udp.read(incomingPacket, 255);
     
      if(len>0)
      {
        incomingPacket[len]=0;
      }
      Serial.printf("udp reply: %s\n",incomingPacket);
      Serial.println(buf);
      delay(100);
     flag++;
     Serial.println(flag);
    }
    // to send Sensor node to the Sleeping mode
    if(flag==1)
    {
      Serial.println("Entering DeepSleep Mode for 3600 sec(60min)");
      ESP.deepSleep(3600e6);// DeepSleep Mode for 3600 sec(60min)
    }
}

 

Source Code for Central Node:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
String apiKey = "RZ6VWJ5T3P8350QH"; // api key of Thingsspeak channel
int x = 0; // shifting between sensor node and internet
int j;
void ThingSpeak();// function to send data to the Thingspeak
// Access point

const char *ssid_ap = "Electronics_Innovation"; //SSID of the central node
const char *password_ap = "subscribe"; // its password
const char* server = "api.thingspeak.com";

// As client

IPAddress ip (192,168,4,1); //ip address of the central node
WiFiUDP udp;

uint8_t buf1[50];
//uint8_t i1=0;
const char *ssid = "VeeruSubbuAmi"; // SSID of your home WiFi
const char *password = "How_dare_you_to ask_me???";

void configClient()
{
WiFi.mode(WIFI_STA);

WiFi.begin(ssid,password);
Serial.print("Waiting for WiFi");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
x=0;
j++;
Serial.print(j);
if ( j==10 ) break ;
}
j=0;
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

udp.begin(localUdpPort);
delay(500);
}

void setup() {
pinMode(D5,OUTPUT);// Motor in put
pinMode(D6,OUTPUT);// Motor in put
pinMode(D7,OUTPUT);// Motor in put
pinMode(D8,OUTPUT);// Motor in put
digitalWrite(D5,LOW); // to stop the motors initially
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,LOW);
delay(100);
Serial.begin(9600);// for debug
}
void loop() {
float incomingPacket=analogRead(A0);

if (incomingPacket > 800 &&incomingPacke) <= 1026)
{
digitalWrite(D5, HIGH); // to open the valve.
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,HIGH);
delay(10000); // 10 secondes are sufficient for my mechanism,
//please change the value according to your mechanism.
}
else if (incomingPacket >0 && incomingPacket < 800)
{
digitalWrite(D7, LOW); //To close the valve.
digitalWrite(D5,HIGH);
digitalWrite(D6,HIGH);
digitalWrite(D7,LOW);
delay(10000); // 10 secondes are sufficient for my mechanism,
//please change the value according to your mechanism.

}
Serial.println("Client mode:");
configClient();

if(WiFi.status() != WL_CONNECTED)
{
configClient();
}
ThingSpeak();
// now send to WiFi:

}
void ThingSpeak()
{

if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
// concatination of string which needs to send to the Thingspeak
String postStr = apiKey;
postStr +="&field1=";
postStr += String(incomingPacket);
postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print(postStr);
Serial.print("moisture: ");
Serial.print(incomingPacket);
Serial.println("%. Send to Thingspeak.");
}
client.stop();

Serial.println("Waiting...");

// thingspeak needs a minimum 15-sec delay between updates, I've set it to 30 seconds
delay(10000);
}

</pre>
&nbsp;
<h5>Source Code for Central Node with Relay &amp; Solenoid Valve:</h5>
<pre data-enlighter-language="js">#include 
#include 
String apiKey = "RZ6VWJ5T3P8350QH";  // api key of Thingsspeak channel
int x = 0; // shifting between sensor node and internet 
int j;
void ThingSpeak();// function to send data to the Thingspeak
// Access point

const char *ssid_ap = "Electronics_Innovation";   //SSID of the central node
const char *password_ap = "subscribe"; // its password
const char* server = "api.thingspeak.com";

// As client

IPAddress ip (192,168,4,1);  //ip address of the central node
WiFiUDP udp;

uint8_t buf1[50];
//uint8_t i1=0;
const char *ssid = "VeeruSubbuAmi";               // SSID of your home WiFi
const char *password = "How_dare_you_to ask_me???";

void configClient()
{
  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid,password);
  Serial.print("Waiting for WiFi");
  while(WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
    x=0;
    j++;
    Serial.print(j);
    if ( j==10 ) break ;
  }
j=0;
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());

 udp.begin(localUdpPort);
 delay(500);  
}

void setup() {
 pinMode(D4,OUTPUT);// Motor in put
 digitalWrite(D4,LOW); // to stop the motors initially
 delay(100);
 Serial.begin(9600);// for debug
}
void loop() {
  float incomingPacket=analogRead(A0);


           if (incomingPacket &gt; 800 &amp;&amp;incomingPacke) &lt;= 1026) { digitalWrite(D4, HIGH); // to open the slenoid valve. } else if (incomingPacket &gt;0 &amp;&amp; incomingPacket &lt; 800) 
            {
            digitalWrite(D4, LOW); //To close the slenoid valve.                      
            }   
      Serial.println("Client mode:");
      configClient();

    if(WiFi.status() != WL_CONNECTED) 
    {     
      configClient();
    }
    ThingSpeak();
        // now send to WiFi:  
        
}
void ThingSpeak()
{

                         if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
                      {  
                            // concatination of string which needs to send to the Thingspeak
                             String postStr = apiKey;
                             postStr +="&amp;field1=";
                             postStr += String(incomingPacket);
                             postStr += "\r\n\r\n";
 
                             client.print("POST /update HTTP/1.1\n");
                             client.print("Host: api.thingspeak.com\n");
                             client.print("Connection: close\n");
                             client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
                             client.print("Content-Type: application/x-www-form-urlencoded\n");
                             client.print("Content-Length: ");
                             client.print(postStr.length());
                             client.print("\n\n");
                             client.print(postStr);
                             Serial.print(postStr);
                             Serial.print("moisture: ");
                             Serial.print(incomingPacket);
                             Serial.println("%. Send to Thingspeak.");
                        }
          client.stop();
 
          Serial.println("Waiting...");
  
  // thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
  delay(10000);
}

 

Sensor node and Central node placement in the field:

I have used one Central node and two Sensor nodes to cover the total land, moreover to get the reliable sensor Data. Sensor node1 and Sensor node2 will fetch the data from the field using the soil moisture sensor. The moisture data will be transferred to the Central node. The received data will be analyzed.

 

Sensor_placement_Smart Irrigation System Project

Side view of the sensor placement:

I have placed the sensors diagonally to the field to get more accurate soil moisture data.

Sensor_placement_Smart Irrigation System Project

Sensor probe placement. I have designed my own Soil moisture sensor to reduce the sensor’s cost. why because this project’s aim is to help farmers. Farmers cannot invest more on this. The sensors are designed in such a way that every farmer can afford and use.

Sensor probe

 

Water supply based on the moisture level:

The central node will analyze the data received from the sensor nodes and control the water supply.
if the moisture content is low it will release the water, else stops the water supply and save the water. For this, we have designed a simple mechanism which is compatible with existing infrastructure. Again I am going with the custom design to reduce the cost and make our solution farmer affordable.

The mechanism is shown in the below pictures.

Top View:

Valve Mechanism

Side View:

Valve mechanism

 

Healthy plants with sims:

Healthy plants in sims

Smart irrigation system project has caught every drop of the water and delivered to the plant where ever it is needed. Smart Irrigation System has reduced the 80% of the water wastage and help the farmer to use the same ware to scaleup the cultivation land.

Smart Irrigation System_water drops

The true achievement of the SIMS.

Before and After Smart Irrigation

Here take a look at the total project. which includes the water tank, water dispense mechanism, Central node, Sensor nodes, Sensors, Healthy plants.

Smart Irrigation System Project

Cheers,

If you feel the project was helpful, give a thumbs-up and spread it around.

Follow me on social media for more interesting projects.

You can watch the following video to look into the real-time implementation of Smart Irrigation System Project

 

By Veeru

8 thoughts on “Smart Irrigation System Project Using IoT- Realtime-Implementation”
  1. whats the distance between the two sensors and what bases you decided

    actually i am working on similar kind of project please help

  2. Can more than two sensor nodes be used ? Is same code used for each sensor or Will it be different to detect different sensor values ?
    As per mentioned at this site (coding part) only one senor value is detected at A0 pin and same is given to central node.

Leave a Reply