· 6 years ago · Jan 04, 2020, 06:40 PM
1//Welcome to the E for Engineer
2#include <DHT.h> // DHT.h library
3#include <ESP8266WiFi.h> // ESP8266WiFi.h library
4
5int sensor=2;
6float soilvalue=0;
7#define DHTPIN 0 //Not D0 Pin it is D3 Pin, Here 0 means GPIO 0.
8#define DHTTYPE DHT11
9#define PIN 14
10
11const char* ssid = "supernova";// replace subscribe with your WiFi SSID(Name)
12const char* password = "12345678";//replace with Your Wifi Password name
13const char* host = "api.thingspeak.com";
14const char* writeAPIKey = "IWSPQLEBT1EKDTSK"; //copy yout ThingSpeak channel API Key.
15
16DHT dht(DHTPIN, DHTTYPE, 15);
17
18void setup() {
19 // Initialize sensor
20 pinMode(PIN,OUTPUT);
21 pinMode(sensor,INPUT);
22 Serial.begin(115200);
23 dht.begin();
24 delay(1000);
25 Serial.println("Connecting to ");
26 Serial.println(ssid);
27// Connect to WiFi network
28 WiFi.begin(ssid, password);
29 while (WiFi.status() != WL_CONNECTED) {
30 delay(500);
31 Serial.print(".");
32 }
33 Serial.println("");
34 Serial.println("WiFi connected");
35}
36
37void loop() {
38 Serial.println("Y1");
39 float humidity = dht.readHumidity();
40 float temperature = dht.readTemperature();
41 soilvalue=analogRead(sensor);
42
43/* if (isnan(humidity) || isnan(temperature)) {
44 return;
45 }*/
46 soilvalue=(100-((soilvalue/1023)*100));
47 if(soilvalue<50)
48 {
49 digitalWrite(PIN,HIGH);
50 }
51Serial.println("Y2");
52// make TCP connections
53 WiFiClient client;
54 const int httpPort = 80;
55 if (!client.connect(host, httpPort)) {
56 return;
57 }
58
59 String url = "/update?key=";
60 url+=writeAPIKey;
61 url+="&field1=";
62 url+=String(temperature);
63 url+="&field2=";
64 url+=String(humidity);
65 url+="&field3=";
66 url+=String(soilvalue);
67 url+="\r\n";
68
69 // Request to the server
70 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
71 "Host: " + host + "\r\n" +
72 "Connection: close\r\n\r\n");
73 Serial.print("Temperature:");
74 Serial.print(temperature);
75 Serial.print("\n");
76 Serial.print("Humidity:");
77 Serial.println(humidity);
78 Serial.print("Soil moisture:");
79 Serial.println(soilvalue);
80 Serial.println("Send to ThingSpeak.\n");
81client.stop();
82 Serial.println("Wait for 15 sec to update next datapack in thingSpeak");
83 delay(1000);
84
85}