#include #include #include // library for getting data from DHT #include #include int j=0; int k=0; int d=0; int q=0; char auth[] = "_mv_FyIWMqpduhhE2IuNFhcHKVjAWDqm"; // Blynk project const char* ssid = "FTTH-79Q7"; const char* password = "Mateen@2016"; const char* host = "api.thingspeak.com"; const char* APIKey = "I0C0N8CJSO8NF3E0"; const int relay = D4; #define DHTPIN D2 //pin where the DHT11 is connected DHT dht(DHTPIN, DHT11); BLYNK_WRITE(4) // Lamp remote control { int i=param.asInt(); if (i==1) { Blynk.virtualWrite(V3, "Motor Is ON"); j=1; Serial.println(j); //aplyCmd(); } else { Blynk.virtualWrite(V3, "Motor Is OFF"); j=0; Serial.println(j); } } BLYNK_WRITE(5) // Lamp remote control { int z=param.asInt(); if (z==1) { d=1; Serial.println("notefecation is"); Serial.println(d); //aplyCmd(); } else { d=0; Serial.println("notefecation is"); Serial.println(d); } } void setup() { Serial.begin(115200); delay(10); dht.begin(); Blynk.begin(auth,ssid,password); // We start by connecting to a WiFi network pinMode(relay, OUTPUT); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); // Serial.println(WiFi.localIP()); } int value = 0; void loop() { Blynk.run(); float h = dht.readHumidity(); // Reading Temperature form DHT sensor float t = dht.readTemperature(); // Reading Humidity form DHT sensor if (isnan(h) || isnan(t)) // { Serial.println("Failed to read from DHT sensor!"); //return; // } else { Serial.println(""); Serial.print("humidity is = "); Serial.print(h); Serial.println(""); Blynk.virtualWrite(V1, h); Serial.print("tempreture is = "); Serial.print(t); Serial.println(""); Blynk.virtualWrite(V0, t); } delay(5000); value = analogRead(A0); Serial.print("Soil Sensor reading is ="); Serial.print(value); Blynk.virtualWrite(V2, value); if (value > 900 || h < 20 || j>0 ) // Emergency state water is needed imeditely { digitalWrite(relay, LOW); delay(5000); Serial.print("-->reley is on"); Blynk.virtualWrite(V6, "Motor Is ON Actually"); q=1; } else if ((value >550)&& (value<=900)) // garden may need water { Serial.print("inside may be "); k++; if (k==1 && q==0) Blynk.notify(" garden may need water press ok to irrigate it"); if (d>0){ digitalWrite(relay, LOW); delay(5000); Serial.print("garden on "); Blynk.virtualWrite(V6, "Motor Is ON Actually"); } } else if (value<550 || h>50 || j==0 || d==0 ) // every thing is good no more water is needed { digitalWrite(relay, HIGH); delay(5000); Serial.print("-->reley is off "); Blynk.virtualWrite(V6, "Motor Is OFF Actually"); k=0; d=0; q=0; } // Serial.print("connecting to "); //Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { // Serial.println("connection failed"); return; } // We now create a URI for the request String url = "/update"; url += "?api_key="; url += APIKey; url += "&field1="; url += value; url += "&field2="; url += h; url += "&field3="; url += t; // Serial.print("Requesting URL: "); // Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial while(client.available()) { String line = client.readStringUntil('\r'); // Serial.print(line); } // Serial.println(); Serial.println("\nclosing connection"); }