#include SoftwareSerial espSerial = SoftwareSerial(2,3); #include #define DHTPIN 5 // Connect the signal pin of DHT11 sensor to digital pin 5 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); int sensor_pin=A0; //setting the sensor pin int outputpin=13; //setting the output pin int output_value ; String apiKey = ""; //I put the channel's thingspeak WRITE API key String ssid=""; // I put Wifi network SSID String password =""; //I put Wifi network password boolean DEBUG=true; //======================================================================== showResponce void showResponse(int waitTime){ long t=millis(); char c; while (t+waitTime>millis()){ if (espSerial.available()){ c=espSerial.read(); if (DEBUG) Serial.print(c); } } } //======================================================================== boolean thingSpeakWrite(float value1, float value2){ String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection cmd += "184.106.153.149"; // api.thingspeak.com cmd += "\",80"; espSerial.println(cmd); if (DEBUG) Serial.println(cmd); if(espSerial.find("Error")){ if (DEBUG) Serial.println("AT+CIPSTART error"); return false; } String getStr = "GET /update?api_key="; // prepare GET string getStr += apiKey; getStr +="&field1="; getStr += String(value1); getStr +="&field2="; getStr += String(value2); // getStr +="&field3="; // getStr += String(value3); // ... getStr += "\r\n\r\n"; // send data length cmd = "AT+CIPSEND="; cmd += String(getStr.length()); espSerial.println(cmd); if (DEBUG) Serial.println(cmd); delay(100); espSerial.print(getStr); if (DEBUG) Serial.print(getStr); return true; } //================================================================================ setup void setup() { DEBUG=true; // enable debug serial Serial.begin(9600); dht.begin(); // Start DHT sensor espSerial.begin(115200); // enable software serial // esp8266 module's speed is probably at 115200. //soil moisture sensonsor Serial.println("Reading From the Sensor ...");//reads froms the sensor pinMode(13,OUTPUT); delay(2000); espSerial.println("AT+CWMODE=1"); // set esp8266 as client showResponse(1000); espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password showResponse(5000); if (DEBUG) Serial.println("Setup completed"); } // ====================================================================== loop void loop() { // Read sensor values float t = dht.readTemperature(); float h = dht.readHumidity(); if (DEBUG) Serial.println("Temp="+String(t)+" *C"); if (DEBUG) Serial.println("Humidity="+String(h)+" %"); thingSpeakWrite(t,h); // Write values to thingspeak // thingspeak needs 15 sec delay between updates, delay(20000); output_value= analogRead(sensor_pin); output_value = map(output_value,550,0,0,100); if(output_value<= -70) //checks if the moisture of the soil is low or high then opens and closes the valve { digitalWrite(13,HIGH); } else { digitalWrite(13,LOW); } Serial.print("Mositure : "); Serial.print(output_value); Serial.println("%"); delay(1000); }