Engineer's Asylum

IoT based Soil Moisture Monitoring and Control

In this tutorial we are going to implement an IoT based Soil Moisture Monitoring and Control system using ESP8266 WiFi Module i.e. NodeMCU.

Components Required for this project:

  • ESP8266 WiFi Module – Amazon (334/- INR)
  • Relay Module – Amazon (130/- INR)
  • 5V Submersible pump – Amazon (130/- INR)
  • Soil moisture Sensor – Amazon (160/- INR)
  • Jumpers – Amazon (120 PCs for 160/- INR)
  • 9V Battery + Snap – Amazon (40/- INR)

Total (Amazon) – 954/- INR

OR

Buy from Electronixity in 682/- INR

ESP8266 WiFi Module

ESP8266 WiFi Module Nodemcu

The development board equips the ESP-12E module containing ESP8266 chip having Tensilica Xtensa® 32-bit LX106 RISC microprocessor which operates at 80 to 160 MHz adjustable clock frequency and supports RTOS.

There’s also 128 KB RAM and 4MB of Flash memory (for program and data storage) just enough to cope with the large strings that make up web pages, JSON/XML data, and everything we throw at IoT devices nowadays.

The ESP8266 Integrates 802.11b/g/n HT40 Wifi transceiver, so it can not only connect to a WiFi network and interact with the Internet, but it can also set up a network of its own, allowing other devices to connect directly to it. This makes the ESP8266 NodeMCU even more versatile.

Relay Module

2 Channel Relay Board

A relay allows you to turn on or turn off a circuit using voltage and/or current much higher than what Arduino could handle. Relay provides complete isolation between the low-voltage circuit on Arduino side and the high-voltage side controlling the load. It gets activated using 5V from Arduino, which, in turn, controls electrical appliances like fans, lights and air-conditioners.

Soil Moisture Sensor

Soil Moisture Sensor by Alpha Electronz

Soil Moisture Sensor

This is Soil Moisture Meter, Soil Humidity Sensor, Water Sensor, Soil Hygrometer for Ardunio. With this module, you can tell when your plants need watering by how moist the soil is in your pot, garden, or yard. The two probes on the sensor act as variable resistors. Use it in a home automated watering system, hook it up to IoT, or just use it to find out when your plant needs a little love. Installing this sensor and its PCB will have you on your way to growing a green thumb!

The soil moisture sensor consists of two probes which are used to measure the volumetric content of water. The two probes allow the current to pass through the soil and then it gets the resistance value to measure the moisture value.

When there is more water, the soil will conduct more electricity which means that there will be less resistance. Therefore, the moisture level will be higher. Dry soil conducts electricity poorly, so when there will be less water, then the soil will conduct less electricity which means that there will be more resistance. Therefore, the moisture level will be lower.

Wiring Connection

VCC: 3.3V-5V

GND: GND

DO: Digital output interface(0 and 1)

AO: Analog output interface

Features :

  • Dual output mode, analog output more accurate
  • A fixed bolt hole for easy installation
  • With power indicator (red) and digital switching output indicator (green)
  • Having LM393 comparator chip, stable.

Circuit Diagram – IoT Based Soil Moisture Monitoring and Control System

Circuit Diagram - IoT Based Soil Moisture Monitoring and Control System

Circuit Diagram

The connections of the whole project are given above.

Power the ESP8266 WiFi Module through the USB Micro.

Download The ESP8266 Library from Here.

Having trouble installing ESP8266 Board in Arduino IDE? Checkout the tutorial

Code

/*
 Alpha Electronz - We provide services in Embedded Projects, 
 Website Development, App Development, Customised Electronic Solutions and Products.
 
 Topic - IoT Based Moisture Monitoring and Control
 Date - 24-12-19
 Source - http://alphaelectronz.com
 */
 
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
/*Put your SSID & Password*/
const char* ssid = "YOUR SSID";  // Enter SSID here
const char* password = "YOUR WIFI PASS";  //Enter Password here
ESP8266WebServer server(80);
             
String pumpv="";
int msv=0;
int pump=2;
int ms=A0;
 
void setup() 
{
  Serial.begin(115200);
  delay(100);
  pinMode(ms, INPUT);
  pinMode(pump, OUTPUT);
  Serial.println("Connecting to ");
  Serial.println(ssid);
  //connect to your local wi-fi network
  WiFi.begin(ssid, password);
  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) 
  {
  delay(1000);
  Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);
  server.begin();
  Serial.println("HTTP server started");
}
void loop() 
{  
  server.handleClient();  
}
void handle_OnConnect()
{
  msv=analogRead(0); 
  msv = map(msv, 500, 1023, 100, 0);
  Serial.println(msv);

   if(msv>50)
     {
     digitalWrite(pump,LOW);
     pumpv="OFF";
     }
   else
     {
      digitalWrite(pump,HIGH);
      pumpv="ON";
     }
  server.send(200, "text/html", SendHTML(pumpv,msv)); 
}
void handle_NotFound()
{
  server.send(404, "text/plain", "Not found");
}
  String SendHTML(String pumpvstat,float msvstat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<link href=\"https://fonts.googleapis.com/css?family=Open+Sans:300,400,600\" rel=\"stylesheet\">\n";
  ptr +="<title>MONITORING SYSTEM</title>\n";
  ptr +="<style>html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #333333;}\n";
  ptr +="body{margin-top: 50px;}\n";
  ptr +="h1 {margin: 50px auto 30px;}\n";
  ptr +=".side-by-side{display: inline-block;vertical-align: middle;position: relative;}\n";
  ptr +=".humidity-icon{background-color: #3498db;width: 30px;height: 30px;border-radius: 50%;line-height: 36px;}\n";
  ptr +=".humidity-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n";
  ptr +=".humidity{font-weight: 300;font-size: 60px;color: #3498db;}\n";
  ptr +=".temperature-icon{background-color: #f39c12;width: 30px;height: 30px;border-radius: 50%;line-height: 40px;}\n";
  ptr +=".temperature-text{font-weight: 600;padding-left: 15px;font-size: 19px;width: 160px;text-align: left;}\n";
  ptr +=".temperature{font-weight: 300;font-size: 60px;color: #f39c12;}\n";
  ptr +=".superscript{font-size: 17px;font-weight: 600;position: absolute;right: -20px;top: 15px;}\n";
  ptr +=".data{padding: 10px;}\n";
  ptr +="</style>\n";
/////////////////////////////////////////////////refreshing 
  ptr +="<script>\n";
  ptr +="setInterval(loadDoc,200);\n";
  ptr +="function loadDoc() {\n";
  ptr +="var xhttp = new XMLHttpRequest();\n";
  ptr +="xhttp.onreadystatechange = function() {\n";
  ptr +="if (this.readyState == 4 && this.status == 200) {\n";
  ptr +="document.getElementById(\"webpage\").innerHTML =this.responseText}\n";
  ptr +="};\n";
  ptr +="xhttp.open(\"GET\", \"/\", true);\n";
  ptr +="xhttp.send();\n";
  ptr +="}\n";
  ptr +="</script>\n";
///////////////////////////////////////////////////
  ptr +="</head>\n";
  ptr +="<body>\n";  
  ptr +="<div id=\"webpage\">\n";
  ptr +="<h1>SOIL MONITORING SYSTEM</h1>\n";
  ptr +="<div class=\"side-by-side temperature-text\">PUMP</div>\n";
  ptr +="<div class=\"side-by-side temperature\">";
  ptr +=(String)pumpvstat;
  ptr +="</div>\n";
   /////////
  ptr +="<div class=\"data\">\n";
  ptr +="<div class=\"side-by-side humidity-icon\">\n";
  ptr +="<svg version=\"1.1\" id=\"Layer_2\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n\"; width=\"12px\" height=\"17.955px\" viewBox=\"0 0 13 17.955\" enable-background=\"new 0 0 13 17.955\" xml:space=\"preserve\">\n";
  ptr +="<path fill=\"#FFFFFF\" d=\"M1.819,6.217C3.139,4.064,6.5,0,6.5,0s3.363,4.064,4.681,6.217c1.793,2.926,2.133,5.05,1.571,7.057\n";
  ptr +="c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107-6.252-4.681C-0.313,11.267,0.026,9.143,1.819,6.217\"></path>\n";
  ptr +="</svg>\n";
  ptr +="</div>\n";
  ptr +="<div class=\"side-by-side humidity-text\">MOISTURE</div>\n";
  ptr +="<div class=\"side-by-side humidity\">";
  ptr +=(int)msvstat;
  ptr +="<span class=\"superscript\">%</span></div>\n";
  ptr +="</div>\n";
  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}