Components and supplies
DHT11 Temperature & Humidity Sensor (4 pins)
SparkFun Soil Moisture Sensor (with Screw Terminals)
ESP32S
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
DHT11, Soil Moisture with NodeMCU
c_cpp
Copy and Paste the Code. Dont forget to add your Wifi username and password and connect your laptop with same wifi.
1#include <WiFi.h> 2#include <WiFiMulti.h> 3#include <HTTPClient.h> 4#include <Adafruit_Sensor.h> 5#include <DHT.h> 6#include <DHT_U.h> 7 8WiFiMulti WiFiMulti; 9HTTPClient ask; 10 11// user config: TODO 12#define MOISTURE_THRESHOLD 55 // moisture alert threshold 13 14const char* ssid = "***"; // enter wifi user name 15const char* password = "***"; // enter wifi password 16const char* apiKeyIn = "E5IiZ2835YBOgiiQwKjEErx7gaiaVIbw"; // Write your api key given from ask sensor 17const unsigned int writeInterval = 25000; // write interval (in ms) 18 19// ASKSENSORS config. 20const char* host = "api.asksensors.com"; // ASKSENSORS API host name 21const int httpPort = 80; // port 22 23// DHT config. 24#define DHTPIN 4 ////d4 Pin which is connected to the DHT sensor. 25// Uncomment the type of sensor in use: 26#define DHTTYPE DHT11 // DHT 11 27//#define DHTTYPE DHT22 // DHT 22 (AM2302) 28//#define DHTTYPE DHT21 // DHT 21 (AM2301) 29DHT_Unified dht(DHTPIN, DHTTYPE); 30uint32_t delayMS; 31int status = WL_IDLE_STATUS; 32float myTemperature = 0, myHumidity = 0; 33int moisture_Pin= A0; // Soil Moisture Sensor input at Analog PIN vp 34int moisture_value= 0, moisture_state = 0xFF; 35 36// create ASKSENSORS client 37// 38void setup() { 39 // open serial 40 Serial.begin(115200); 41 42 Serial.println("Wait for WiFi... "); 43 44 // connecting to the WiFi network 45 WiFiMulti.addAP(ssid, password); 46 while (WiFiMulti.run() != WL_CONNECTED) { 47 Serial.print("."); 48 delay(500); 49 } 50 // connected 51 Serial.println("WiFi connected"); 52 Serial.println("IP address: "); 53 Serial.println(WiFi.localIP()); 54 55 // Initialize device. 56 dht.begin(); 57 Serial.println("DHTxx Unified Sensor Example"); 58 // Print temperature sensor details. 59 sensor_t sensor; 60 dht.temperature().getSensor(&sensor); 61 Serial.println("------------------------------------"); 62 Serial.println("Temperature"); 63 Serial.print ("Sensor: "); Serial.println(sensor.name); 64 Serial.print ("Driver Ver: "); Serial.println(sensor.version); 65 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); 66 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" C"); 67 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" C"); 68 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" C"); 69 Serial.println("------------------------------------"); 70 // Print humidity sensor details. 71 dht.humidity().getSensor(&sensor); 72 Serial.println("------------------------------------"); 73 Serial.println("Humidity"); 74 Serial.print ("Sensor: "); Serial.println(sensor.name); 75 Serial.print ("Driver Ver: "); Serial.println(sensor.version); 76 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); 77 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%"); 78 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%"); 79 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%"); 80 Serial.println("------------------------------------"); 81 // Set delay between sensor readings based on sensor details. 82 delayMS = sensor.min_delay / 1000; 83} 84 85void loop() { 86 87 Serial.println(" Temperature, Humidity and Soil Moisture sensors "); 88 Serial.print("Moisture level : "); 89 moisture_value= analogRead(moisture_Pin); 90 moisture_value= moisture_value/10; 91 Serial.println(moisture_value); 92 if(moisture_value > MOISTURE_THRESHOLD) moisture_state = 0; 93 else moisture_state = 1; 94// Read data from DHT 95// Delay between measurements. 96 delay(delayMS); 97 // Get temperature event and print its value. 98 sensors_event_t event; 99 dht.temperature().getEvent(&event); 100 if (isnan(event.temperature)) { 101 Serial.println("Error reading temperature!"); 102 } 103 else { 104 // Update temperature and humidity 105 myTemperature = (float)event.temperature; 106 Serial.print("Temperature: "); 107 Serial.print(myTemperature); 108 Serial.println(" C"); 109 } 110 // Get humidity event and print its value. 111 dht.humidity().getEvent(&event); 112 if (isnan(event.relative_humidity)) { 113 Serial.println("Error reading humidity!"); 114 } 115 else { 116 myHumidity = (float)event.relative_humidity; 117 Serial.print("Humidity: "); 118 Serial.print(myHumidity); 119 Serial.println("%"); 120 } 121 122// Use WiFiClient class to create TCP connections 123 WiFiClient client; 124 125 126 if (!client.connect(host, httpPort)) { 127 Serial.println("connection failed"); 128 return; 129 }else { 130 131 // Create a URL for the request 132 String url = "https://api.asksensors.com/write/"; 133 url += apiKeyIn; 134 url += "?module1="; 135 url += myTemperature; 136 url += "&module2="; 137 url += myHumidity; 138 url += "&module3="; 139 url += moisture_value; 140 url += "&module4="; 141 url += moisture_state; 142 143 Serial.print(" requesting URL: "); 144 Serial.println(url); 145 146 ask.begin(url); //Specify the URL 147 Serial.println("Temperature, Humidity and Soil moisture data sent to Asksensors"); 148 149 //Check for the returning code 150 int httpCode = ask.GET(); 151 152 if (httpCode > 0) { 153 154 String payload = ask.getString(); 155 Serial.println(httpCode); 156 Serial.println(payload); 157 } else { 158 Serial.println("Error on HTTP request"); 159 } 160 161 ask.end(); //End 162 Serial.println(" End "); 163 164 } 165 166 client.stop(); // stop client 167 168 delay(writeInterval); // delay 169} 170
Downloadable files
DHT11, Soil Moisture with NodeMCU
Connect the circuit as shown in the figure
DHT11, Soil Moisture with NodeMCU
DHT11, Soil Moisture with NodeMCU
Connect the circuit as shown in the figure
DHT11, Soil Moisture with NodeMCU
Comments
Only logged in users can leave comments