· 5 years ago · Jun 26, 2020, 11:42 AM
1
2//-----------------------------------------------------------------
3//include Libries
4//-----------------------------------------------------------------
5
6#include <ESP8266WiFi.h>
7#include <ESP8266WebServer.h>
8#include <ESP8266HTTPClient.h>
9#include <SoftwareSerial.h>
10#include <DHT.h>
11#include <WiFiClient.h>;
12 #include <ThingSpeak.h>;
13
14DHT dht2(2,DHT11);
15//-----------------------------------------------------------------
16//variable declaration
17//-----------------------------------------------------------------
18float percentage;
19String sensor05;
20String sensor10;
21String sensor20;
22String sensor30;
23String sensor40;
24
25
26WiFiClient client;
27
28unsigned long myChannelNumber = 1089564; //Your Channel Number (Without Brackets)
29
30const char * myWriteAPIKey = "OFNR2T1J2HPE1RGG"; //Your Write API Key
31//-----------------------------------------------------------------
32//Credentials of the Access Point
33//-----------------------------------------------------------------
34const char* APssid = "IoT_Sensor_Server";
35const char* APpassword = "12345678";
36const char* ssid = "LiFi";
37const char* password = "11110000";
38int max_connection = 8;
39
40//-----------------------------------------------------------------
41//Static IP address declation
42//-----------------------------------------------------------------
43IPAddress staticIP(10,10,10,10);
44IPAddress gateway(10,10,10,1);
45IPAddress subnet(255,0,0,0);
46
47//-----------------------------------------------------------------
48//HTTP variable declation
49//-----------------------------------------------------------------
50
51String ClientRequest;
52String httpurl;
53String TheHiddenAnswerOfClient;
54HTTPClient http;
55WiFiServer server(80);
56
57//-----------------------------------------------------------------
58//Function declation
59//-----------------------------------------------------------------
60
61String SendWithAnswer(String IPcache, String monmessagecache) {
62httpurl = "http://";
63httpurl+=IPcache;
64httpurl+="/";
65httpurl+=monmessagecache;
66http.begin(httpurl);
67http.GET();
68TheHiddenAnswerOfClient = (http.getString());
69http.end();
70return TheHiddenAnswerOfClient;
71}
72
73//-----------------------------------------------------------------
74//Set-up of ESP8266
75//-----------------------------------------------------------------
76
77void setup()
78{
79
80 sensor05 = "";
81
82 sensor10 = "";
83
84 sensor20 = "";
85
86 sensor30 = "";
87
88 sensor40 = "";
89 //---------------------------------------------------------------
90 {
91 Serial.begin(115200);
92 WiFi.begin(ssid, password);
93 WiFi.softAPConfig(staticIP, gateway, subnet);
94 //delay(10);
95
96 // Mengatur WiFi ----------------------------------------------------------
97 Serial.println();
98 Serial.print("Configuring access point...");
99
100 WiFi.mode(WIFI_AP_STA);
101 //WiFi.softAP(APssid, APpassword);
102 WiFi.softAP(APssid, APpassword, 1, false, max_connection);
103
104
105 while (WiFi.status() != WL_CONNECTED) {
106 delay(500);
107 Serial.print(".");
108 }
109
110 Serial.println("");
111 Serial.println("WiFi connected");
112
113 // Start the server -------------------------------------------------------
114 server.begin();
115 Serial.println("Server Started");
116
117 // Print the IP address ---------------------------------------------------
118 Serial.println(WiFi.localIP());
119 Serial.println(WiFi.softAPIP());
120 ThingSpeak.begin(client);
121}
122
123//-----------------------------------------------------------------
124//ESP8266 as Acess Point mode
125//-----------------------------------------------------------------
126 //delay(1000);
127 //Serial.begin(115200);
128 //WiFi.softAPConfig(staticIP, gateway, subnet);
129 //WiFi.mode(WIFI_AP);
130 //Serial.println("Setup Access point");
131 //Serial.println("Disconnect from any other modes");
132 //WiFi.disconnect();
133 //Serial.println("stating access point with SSID --" +String(ssid));
134 //WiFi.softAP(ssid, password, 1, false, max_connection);
135 //Serial.println("Connected");
136
137 //Serial.println("Your IP is");
138 //Serial.println((WiFi.localIP().toString()));
139 //IPAddress myIP = WiFi.softAPIP();
140 //Serial.print("AP IP address: ");
141 //Serial.println(myIP);
142//-----------------------------------------------------------------
143//Starting of the Server
144//-----------------------------------------------------------------
145 //server.begin();
146 //Serial.println("Web server started!");
147}
148
149
150void loop()
151{
152//-----------------------------------------------------------------
153//Server-Client Communication through HTTP Request
154//-----------------------------------------------------------------
155 WiFiClient client = server.available();
156 if (!client) { return; }
157 while(!client.available()){ delay(10); }
158 client.flush();
159 sensor20 = (SendWithAnswer("10.10.10.20","Yes"));
160 sensor20.remove(0, 5);
161 sensor20.remove(sensor20.length()-9,9);
162 sensor30 = (SendWithAnswer("10.10.10.30","Yes"));
163 sensor30.remove(0, 5);
164 sensor30.remove(sensor30.length()-9,9);
165 sensor40 = (SendWithAnswer("10.10.10.40","Yes"));
166 sensor40.remove(0, 5);
167 sensor40.remove(sensor40.length()-9,9);
168
169//-----------------------------------------------------------------
170//Configuaration of Soil Moisture Sensor
171//-----------------------------------------------------------------
172
173 float reading = analogRead(A0);
174 percentage = 100 - (reading/1024) * 100;
175 Serial.println("Moisture of the soil is:");
176 Serial.println(percentage);
177//-----------------------------------------------------------------
178//Configuaration of Hummidity and Temperature Sensor
179//-----------------------------------------------------------------
180 sensor05 = (dht2.readTemperature( ));
181 sensor10 = (dht2.readHumidity( ));
182 Serial.println("Temperature is:");
183 Serial.println(sensor05);
184 Serial.println("Humidity is:");
185 Serial.println(sensor10);
186 Serial.println("Soil Moisture is:");
187 Serial.println(sensor20);
188 Serial.println("Soil Moisture is:");
189 Serial.println(sensor30);
190 Serial.println("Soil Moisture is:");
191 Serial.println(sensor40);
192//-----------------------------------------------------------------
193//Configuaration of WEBPAGE
194//-----------------------------------------------------------------
195
196 client.println("HTTP/1.1 200 OK");
197 client.println("Content-Type: text/html");
198 client.println("");
199 client.println("<!DOCTYPE HTML>");
200 client.println("<html>");
201//-----------------------------------------------------------------
202//Title of the WEBPAGE
203//-----------------------------------------------------------------
204 client.println("<head>");
205 client.println("<title align=center>Sensor Network</title>");
206 client.println("</head>");
207//-----------------------------------------------------------------
208//Body of the WEBPAGE
209//-----------------------------------------------------------------
210 client.println("<body>");
211 client.println("<center>");
212 client.println("<h1 style=""color:#3333ff"">"); // Blue Colour
213 client.println("Sensor Network Page");
214 client.println("</h1>");
215
216 client.println("<span style=""color:#660000;font-size:20px"">"); //Brown Colour 20
217 client.println("The program Started Since:");
218 client.println("</span>");
219
220 client.println("<span style=""color:#000000;font-size:16px"">"); // Black Colour
221 client.println((millis()/1000));
222 client.println("</span>");
223//--------------------------------------------------------- Hummidity & Temperature Sensor
224 client.println("<h2 style=""color:#006600"">"); //Green Colour
225 client.println("----------Temperature Sensor----------");
226 client.println("</h2>");
227
228 client.println("<h2 style=""color:#009900"">"); // Light Green Colour
229 client.println("The temperature is:");
230 client.println("</h2>");
231
232 client.println("<span style=""color:#000000;font-size:18px"">"); // Black Colour
233 client.println(sensor05);
234 client.print("C");
235 client.println("</span>");
236
237 client.println("<h2 style=""color:#006600"">"); //Green Colour
238 client.println("----------Hummidity Sensor----------");
239 client.println("</h2>");
240
241 client.println("<h2 style=""color:#3333ff"">"); // Blue Colour
242 client.println("The moisture in the air is:");
243 client.println("</h2>");
244
245 client.println("<span style=""color:#000000;font-size:18px"">"); // Black Colour
246 client.println(sensor10);
247 client.print("%");
248 client.println("</span>");
249//--------------------------------------------------------- Soil Sensor 1
250 client.println("<h2 style=""color:#006600"">"); //Green Colour
251 client.println("----------Soil Moisture Sensor 1-----------");
252 client.println("</h2>");
253
254 client.println("<h2 style=""color:#ff6600"">"); // Orange Colour
255 client.println("The moisture of the soil from Soil Moisture Sensor 1 :");
256 client.println("</h2>");
257
258 client.println("<span style=""color:#000000;font-size:25px"">"); // Black Colour
259 client.println(percentage);
260 client.print("%");
261 client.println("</span>");
262
263
264 client.println("<h3 style=""color:#660000;font-size:20px"">"); // Brown Colour
265 client.println("Action:");
266 client.println("</h3>");
267
268
269 if(percentage >= 15.0)
270 {
271 client.println("<h4 style=""color:#006600;font-size:18px"">"); // Green Colour
272 client.println("Enough moisture available : No watering is needed.");
273 client.println("</h4>");
274 }
275 else
276 {
277 client.println("<h4 style=""color:#ff0000;font-size:18px"">"); // Red Colour
278 client.print("Not Enough moisture available : Immediate watering is needed.");
279 client.println("</h4>");
280 }
281
282//--------------------------------------------------------- Soil Sensor 2
283 client.println("<h2 style=""color:#006600"">"); //Green Colour
284 client.println("----------Soil Moisture Sensor 2----------");
285 client.println("</h2>");
286
287 client.println("<h2 style=""color:#ff6600"">"); //Orange Colour
288 client.println("The moisture of the soil from Soil Moisture Sensor 2 :");
289 client.println("</h2>");
290
291 client.println("<span style=""color:#000000;font-size:25px"">"); // Black Colour
292 client.println(sensor20);
293 client.println("</span>");
294
295
296//--------------------------------------------------------- Soil Sensor 3
297 client.println("<h2 style=""color:#006600"">"); //Green Colour
298 client.println("----------Soil Moisture Sensor 3------------");
299 client.println("</h2>");
300
301 client.println("<h2 style=""color:#ff6600"">"); //Orange Colour
302 client.println("The moisture of the soil from Soil Moisture Sensor 3 :");
303 client.println("</h2>");
304
305 client.println("<span style=""color:#000000;font-size:25px"">"); // Black Colour
306 client.println(sensor30);
307 client.println("</span>");
308
309
310//--------------------------------------------------------- Soil Sensor 4
311 client.println("<h2 style=""color:#006600"">"); //Green Colour
312 client.println("----------Soil Moisture Sensor 4----------");
313 client.println("</h2>");
314
315 client.println("<h2 style=""color:#ff6600"">"); //Orange Colour
316 client.println("The moisture of the soil from Soil Moisture Sensor 4 :");
317 client.println("</h2>");
318
319 client.println("<span style=""color:#000000;font-size:25px"">"); // Black Colour
320 client.println(sensor40);
321 client.println("</span>");
322
323 client.println("</center>");
324 client.println("</body>");
325 client.println("</html>");
326 //client.stop();
327 delay(10);
328ThingSpeak.writeField(myChannelNumber, 1,percentage, myWriteAPIKey);
329}
330// Green Colour
331//client.println("<h2 style=""color:#006600"">");
332
333//Black Colour
334//client.println("<h2 style=""color:#000000"">");
335
336//Red Colour
337//client.println("<h2 style=""color:#ff0000"">");
338
339//Brown Colour
340//client.println("<h2 style=""color:#660000"">");
341
342//Blue Colour
343//client.println("<h2 style=""color:#3333ff"">");
344
345//Light Green Colour
346//client.println("<h2 style=""color:#009900"">");
347
348//Orange Colour
349//client.println("<h2 style=""color:#ff6600"">");