Another illustration that demonstrate how to use the ESP8266 as a web-based Soil Moisture / Temperature and Humidity Monitor. If you’re new to ESP8266 please refer to this link. There are 3 main component are used to this project. The ESP8266, DHT11/12, and the Soil Moisture Sensor, as you can see the diagram below there are 3 sensors to accomplish this projects the ESP8266 WiFi Transceiver, two plate soil moisture, and the DHT11 Temperature & Humidity sensor. This project also tested in ESP8266 12E NodeMCU, ESP8266 Version 7, ESPDuino, WeMos Board.
Required Components
- ESP8266 12, 12E, ESP8266 NodeMCU, ESPDuino, WeMos.
- Soil Moisture Sensor Module or Build your own Sensor
- DHT11, DHT12 Sensor or DHT11 / DHT12 Module
- NPN Transistor (Optional)
- Two Conducted Plate (Optional)
- 2 Battery Case for 3.3v Out / 2 Double A Battery (For Stand Alone Option)
- LED (Any Color)
- Resistor (See the diagram below for value reference)
- Capacitors (See the diagram below for value reference)
- USB TTL UART (For Programming the ESP8266)
- Solder Less Bread Board
- Jumper Wires
Flashing Guide
For flashing using NodeMCU to ESP8266 12 or Flashing with ESP8266 Download Tool just follow this link & for ESP8266 Arduino Integration just follow also this link or if you are using MAC OS refer to this link Note: The AT command must be end with “\r\n”. For Arduino, must type the AT command like this > Serial1.print(“AT+GMR\r\n”);
Wiring Guide
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
/* ESP8266 Temperature Humidity & Moisture Soil Moisture moded by www.14core.com / Java Script by Google.com {https://www.gstatic.com/charts/loader.js} Note: You need to connect to the internet to get the guage which is transmit by the sensor . ********************************************************************************************** Ip address: 192.168.1.100 // Gatway: 192.168.1.1 // Check your AP gateway Subnet Mask: 255.255.255.0 */ #include <ESP8266WiFi.h> #include "DHT.h" #define DHTTYPE DHT11 const char* ssid = "14CORE"; //You can change the SSDI const char* password = "123456789"; //You can change the password as you like int WiFiStrength = 0; WiFiServer server(80); const int DHTPin = 5; // ~D6 const int moist = 14; // ~D5 const int ledStatus = 4;// ~D2 DHT dht (DHTPin, DHTTYPE); static char celsiusTemp[7]; static char fahrenheitTemp[7]; static char humidityTemp[7]; double moisture = 0.0; unsigned long timeHolder = 0; void setup() { Serial.begin(115200); delay(10); dht.begin(); pinMode(ledStatus, OUTPUT); pinMode(moisture, INPUT); analogWrite(ledStatus, 280); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); // This line should the same with your router/internet connection to parse the javascript which is provided by Google WiFi.config(IPAddress(192, 168, 1, 221), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0)); // connect to WiFi router while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("...."); } Serial.println("------------------------"); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started, setting up ESP8266 IP Address..."); Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } void loop() { digitalWrite(ledStatus, 0); delay(100); digitalWrite(ledStatus, 1); WiFiStrength = WiFi.RSSI(); // get decibels measured from the ESP8266 moisture = analogRead(moist); // set to get analogRead from variable moist float humid = dht.readHumidity(); float temp = dht.readTemperature(); float far = dht.readTemperature(true); int moistval = (moisture * 100) / 400; moistval = 100 - moistval; if (isnan(humid) || isnan(temp) || isnan(far)){ Serial.println("Failed to read from DHT sensor!"); strcpy(celsiusTemp,"Failed"); strcpy(fahrenheitTemp, "Failed"); strcpy(humidityTemp, "Failed"); } else { float thcal = dht.computeHeatIndex(temp, humid, false); dtostrf(thcal, 6, 2, celsiusTemp); float tfcal = dht.computeHeatIndex(far, humid); dtostrf(tfcal, 6, 2, fahrenheitTemp); dtostrf(humid, 6, 2, humidityTemp); Serial.print("Moist: "); Serial.println(moistval); Serial.print("Temperature: "); Serial.println(temp); Serial.print("Humidity: "); Serial.println(humid); Serial.print("Fahrenheit:"); Serial.println(far); Serial.print("TimeHolder: "); Serial.println(timeHolder); Serial.print("millis(): "); Serial.println(millis()); Serial.print("WiFi Strength: "); Serial.print(WiFiStrength); Serial.println("dBm"); Serial.println(" "); delay(1000); // slows amount of data sent via serial } // Serial data WiFiClient client = server.available(); //Cheack any web server request if (!client) { return; } Serial.println("Client Request..."); // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); // Return the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println(" <head>"); client.println("<meta http-equiv=\"refresh\" content=\"60\">"); client.println(" <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>"); client.println(" <script type=\"text/javascript\">"); client.println(" google.charts.load('current', {'packages':['gauge']});"); client.println(" google.charts.setOnLoadCallback(drawChart);"); client.println(" function drawChart() {"); // Temperature client.println(" var data = google.visualization.arrayToDataTable([ "); client.println(" ['Label', 'Value'], "); client.print(" ['Temp', "); client.print(temp); client.println(" ], "); client.println(" ]); "); // Humidity client.println(" var data = google.visualization.arrayToDataTable([ "); client.println(" ['Label', 'Value'], "); client.print(" ['Humid', "); client.print(humid); client.println(" ], "); client.println(" ]); "); // Moisture client.println(" var data = google.visualization.arrayToDataTable([ "); client.println(" ['Label', 'Value'], "); client.print(" ['Moisture', "); client.print(moistval); client.println(" ], "); client.println(" ]); "); // setup the google chart options here client.println(" var options = {"); client.println(" width: 500, height: 120,"); client.println(" redFrom: 0, redTo: 25,"); client.println(" yellowFrom: 25, yellowTo: 75,"); client.println(" greenFrom: 75, greenTo: 100,"); client.println(" minorTicks: 5"); client.println(" };"); client.println(" var chart = new google.visualization.Gauge(document.getElementById('gauge_div'));"); client.println(" chart.draw(data, options);"); //Temperature client.println(" setInterval(function() {"); client.print(" data.setValue(0, 1, "); client.print(temp); client.println(" );"); client.println(" chart.draw(data, options);"); client.println(" }, 13000);"); //Humidity client.println(" setInterval(function() {"); client.print(" data.setValue(0, 1, "); client.print(humid); client.println(" );"); client.println(" chart.draw(data, options);"); client.println(" }, 13000);"); // Moisture client.println(" setInterval(function() {"); client.print(" data.setValue(0, 1, "); client.print(moistval); client.println(" );"); client.println(" chart.draw(data, options);"); client.println(" }, 13000);"); client.println(" }"); client.println(" </script>"); client.println(" </head>"); client.println(" <body>"); client.println("<center>"); client.print("<h1 style=\"size:12px;\">ESP8266 Soil Moisture / Temperature <br/> Humidity Monitor</h1>"); client.print("WiFi Signal Strength: "); client.print(WiFiStrength); client.println("dBm<br>"); client.print("<br/>Soil Moisture :"); client.print(moistval); client.print(" - Temperature in Celsius:"); client.print(celsiusTemp); client.print("<br/> - Temperature in Fahrenheit: "); client.print(fahrenheitTemp); client.print(" - Humidity: "); client.print(humidityTemp); client.print("<br/><br/>"); client.println("<div id=\"gauge_div\" style=\"width: 300px; height: 120px;\"></div>"); client.println("<br><br><a href=\"/REFRESH\"\"><button>Refresh</button></br><br/></a>"); client.println("</center>"); client.println("</body>"); client.println("</html>"); } |
Downloads
- Download ESP8266 WifiClient & ESP8266Wifi | Zip
- Download ESP8266 WebServer | Zip
- Download ESP8266 Flash Tool | Link
- Download NodeMCU Flash Software | Link
Hi,
Google changed charts or something.. and you need to change code a bit.. can you update it? :)
Changed part needs to generate:
[‘Label’, ‘Value’],
[‘Temp’, 26.90 ],
[‘Humid’, 46.70 ],
[‘Moisture’, 15 ],
]);
Code:
// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“”); // do not forget this one
client.println(“”);
client.println(“”);
client.println(” “);
client.println(“”);
client.println(” “);
client.println(” “);
client.println(” google.charts.load(‘current’, {‘packages’:[‘gauge’]});”);
client.println(” google.charts.setOnLoadCallback(drawChart);”);
client.println(” function drawChart() {“);
// Temperature
client.println(” var data = google.visualization.arrayToDataTable([ “);
client.println(” [‘Label’, ‘Value’], “);
client.print(” [‘Temp’, “);
client.print(temp);
client.println(” ], “);
// Humidity
client.print(” [‘Humid’, “);
client.print(humid);
client.println(” ], “);
// Moisture
client.print(” [‘Moisture’, “);
client.print(moistval);
client.println(” ], “);
client.println(” ]); “);
// setup the google chart options here
Hi,
I think there is a problem with the wiring guide.
Ground connection to the esp is going trough a 100k resistor.
I think the ground connector from the battery should be on the other side of the resistor right?
//Roger
Hi Roger, I updated the diagram above i remove some unused connections to the pins, . thanks :)