-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlantInoWifi-src.ino
491 lines (441 loc) · 12.1 KB
/
PlantInoWifi-src.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Plant Monitor and Watering System
// Lab4D 2019
//
// Written for NodeMCU E12
// Requires ESP8266FS SPIFFS plugin for webserver
//Libraries
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "DHTesp.h"
#include <FS.h> //Include File System Headers
const char* imagefile = "/image.png";
const char* htmlfile = "/index.html";
//ThingSpeak Connection Settings
const char* server = "api.thingspeak.com";
String apiKey = "APIKEY";
String Channel = "CHANNELNAME";
//Variable Variables™
const char* MY_SSID = "WIFI NAME";
const char* MY_PWD = "WIFI PASSWORD";
bool uploadData = true; //UPLOAD TO THINGSPEAK?
long onlineLoop = 420000; //Send interval in millisec - 15000 minimum (def: 420000)
long sensorLoop = 4200; //Sensor Interval
int offlineLoop = 120; // Loop time when not sending data
int flipTimer = 200;
int waterTimer = 10000;
int sensLimit = 600; // 600 default
// Pin Variables
#define voltageFlipPinA 0 //D3
#define voltageFlipPinB 2 //D4
#define sensorPin 0 // A0
int DHTPin = 16; //D0
int LDRPin = 5; //D1
int PumpPin = 4; //D2
int Device1 = 14; // D5
int LEDL = 12; // D6
int LEDA = 13; // D7
int LEDB = 15; // D8
// Internal Variables
float temperature;
float humidity;
bool light;
int moisture;
unsigned long previousMillis = 0;
unsigned long sensMillis = 0;
bool firstRun = true;
int sent =1;
int unsent;
String webPage;
bool water = false;
bool deviceStatus;
int wattempt;
MDNSResponder mdns;
ESP8266WebServer srv(80);
DHTesp dht;
// On http request
void handleRoot() {
srv.sendHeader("Location", "/index.html",true); //Redirect to html web page
srv.send(302, "text/plane","");
}
/// Web Request Handling
void handleWebRequests(){
if(loadFromSpiffs(srv.uri())) return;
String message = "File Not Detected\n\n";
message += "URI: ";
message += srv.uri();
message += "\nMethod: ";
message += (srv.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += srv.args();
message += "\n";
for (uint8_t i=0; i<srv.args(); i++){
message += " NAME:"+srv.argName(i) + "\n VALUE:" + srv.arg(i) + "\n";
}
srv.send(404, "text/plain", message);
Serial.println(message);
}
void handleWater (){
pumpWater(waterTimer);
}
/// Device State Upload Handling
void handleRDevice() {
String d = String(deviceStatus);
srv.send(200, "text/plane", d); //Send to web page
}
/// Moisture Upload Handling
void handleRUpload() {
String u = String(uploadData);
srv.send(200, "text/plane", u); //Send to web page
}
/// Moisture Handling
void handleMoisture() {
String m = String(moisture);
srv.send(200, "text/plane", m); //Send to web page
}
/// Pump Handling
void handlePump() {
String s = String(water);
srv.send(200, "text/plane", s); //Send to web page
}
/// Temperature Handling
void handleTemp() {
String t = String(temperature);
srv.send(200, "text/plane", t); //Send to web page
}
/// Humidity Handling
void handleHum() {
String h = String(humidity);
srv.send(200, "text/plane", h); //Send to web page
}
/// Light Handling
void handleLight() {
String l = String(light);
srv.send(200, "text/plane", l); //Send to web page
}
/// Device State Button Handling
void handleDevice() {
String deviceState1 = "OFF";
String t_state = srv.arg("deviceState1");
if(t_state == "1")
{
digitalWrite(Device1, HIGH); //LED ON
deviceState1 = "ON"; //Feedback parameter
deviceStatus = true;
Serial.print("D+");
}
else
{
digitalWrite(Device1, LOW); //LED OFF
deviceState1 = "OFF"; //Feedback parameter
deviceStatus = false;
Serial.print("D-");
}
srv.send(200, "text/plane", deviceState1); //Send to web page
}
/// Upload Handling
void handleUpload() {
String uploadState = "OFF";
String t_state = srv.arg("upload");
if(t_state == "1")
{
digitalWrite(Device1, HIGH); //LED ON
uploadState = "ON"; //Feedback parameter
uploadData = true;
Serial.print("U+");
}
else
{
digitalWrite(Device1, LOW); //LED OFF
uploadState = "OFF"; //Feedback parameter
uploadData = false;
Serial.print("U-");
}
srv.send(200, "text/plane", uploadState); //Send to web page
}
/// Upload Timer Setting
void handleUploadTime() {
String t_state = srv.arg("uploadTime");
Serial.print("U=" + t_state);
onlineLoop = t_state.toInt();
//srv.send(200, "text/plane", t_state); //Send to web page
}
/// Handle Upload Timer
void handleRUploadTime() {
String t = String(onlineLoop);
srv.send(200, "text/plane", t); //Send to web page
}
/// Moisture Limit Setting
void handleMoistureLim() {
String t_state = srv.arg("moistureLim");
Serial.print("ML=" + t_state);
sensLimit = t_state.toInt();
//srv.send(200, "text/plane", t_state); //Send to web page
}
/// Handle Upload Timer
void handleRMoistureLim() {
String t = String(sensLimit);
srv.send(200, "text/plane", t); //Send to web page
}
/// Pump Timer Setting
void handlePumpTime() {
String t_state = srv.arg("pumpTime");
Serial.print("P=" + t_state);
waterTimer = t_state.toInt();
//srv.send(200, "text/plane", t_state); //Send to web page
}
/// Handle Pump Timer
void handleRPumpTime() {
String w = String(waterTimer);
srv.send(200, "text/plane", w); //Send to web page
}
//Print Serial Sensors
void printSerial(){
Serial.println();
Serial.print ("Light: ");
Serial.println(light);
Serial.print("Temperature: ");
Serial.println(String(temperature));
Serial.print("Humidity: ");
Serial.println(String(humidity));
Serial.print("Moisture : ");
Serial.println(String(moisture) + " / " + sensLimit);
Serial.print("Pump : ");
Serial.println(String(water));
Serial.print("Device : ");
Serial.println(String(deviceStatus));
}
void setup() {
pinMode(Device1, OUTPUT);
pinMode(LEDA, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(LEDL, OUTPUT);
pinMode(PumpPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(voltageFlipPinA, OUTPUT);
pinMode(voltageFlipPinB, OUTPUT);
dht.setup(DHTPin, DHTesp::DHT22);
Serial.begin(115200);
SPIFFS.begin();
connectWifi();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
srv.on("/", handleRoot);
srv.onNotFound(handleWebRequests);
srv.on("/readUpload", handleRUpload);
srv.on("/setUpload", handleUpload);
srv.on("/readDevice", handleRDevice);
srv.on("/setDevice", handleDevice);
srv.on("/setUploadTime", handleUploadTime);
srv.on("/readUploadTime", handleRUploadTime);
srv.on("/setMoistureLim", handleMoistureLim);
srv.on("/readMoistureLim", handleRMoistureLim);
srv.on("/setPumpTime", handlePumpTime);
srv.on("/readPumpTime", handleRPumpTime);
srv.on("/readTemp", handleTemp);
srv.on("/readHum", handleHum);
srv.on("/readLight", handleLight);
srv.on("/readMoisture", handleMoisture);
srv.on("/readPump", handlePump);
srv.on("/waterPlant", handleWater);
srv.begin();
Serial.println("Server Started");
}
void loop() {
srv.handleClient();
// Get Sensor Data
unsigned long sensMillis2 = millis();
if (sensMillis2 - sensMillis >= sensorLoop) {
sensMillis = sensMillis2;
temperature = dht.getTemperature();
humidity = dht.getHumidity();
light = digitalRead(LDRPin);
senseSoil();
//digitalWrite(LEDL, !light);
//Serial.print("s");
printSerial();
}
else {
Serial.print(".");
}
// Send to ThingSpeak
if (firstRun){
senseSoil();
firstRun = false;
}
if (uploadData){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= onlineLoop) {
previousMillis = currentMillis;
digitalWrite(LEDL, HIGH);
senseSoil();
Serial.println("");
// Send Data to ThingSpeak
sendThing();
digitalWrite(LEDL, LOW);
if (water == true){
pumpWater(waterTimer);
}
else {
Serial.println("No water needed <3 ");
}
}
}
else {
unsent++;
Serial.println("+++DATA UPLOAD: OFF - "+ String(unsent));
printSerial();
if (water == true){
pumpWater(waterTimer);
}
else {
Serial.println("No water needed <3");
}
}
sensorOff();
if (uploadData){
delay(220);
}
else {
delay(offlineLoop);
}
}
/// Connect to Wifi
void connectWifi()
{
delay(500);
Serial.println("");
Serial.println("Initializing");
delay(100);
Serial.print("Connecting to " + *MY_SSID);
delay(500);
WiFi.begin(MY_SSID, MY_PWD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
wattempt++;
if (wattempt >= 10){
Serial.println("Connection failed, exiting...)");
wattempt=0;
return;
}
}
Serial.println("");
delay(500);
Serial.println("Connected to "+ *MY_SSID);
Serial.println("");
if (mdns.begin("esp8266", WiFi.localIP()))
Serial.println("MDNS responder started");
}
/// Update ThingSpeak
void sendThing() {
WiFiClient client;
Serial.println("+++DATA UPLOAD: ON - " + String(sent));
// Check if Wifi is OK
if (WiFi.status() != WL_CONNECTED){
WiFi.disconnect(true);
Serial.println("Connection Lost - Resetting Wifi");
connectWifi();
delay(500);
}
if (client.connect(server, 80)) {
String postStr = apiKey;
postStr += "&field1=";
postStr += String(light);
postStr += "&field2=";
postStr += String(temperature);
postStr += "&field3=";
postStr += String(humidity);
postStr += "&field4=";
postStr += String(moisture);
postStr += "&field5=";
postStr += String(water);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
sent++;
printSerial();
}
else {
printSerial();
Serial.println("No connection - Data Not Sent");
WiFi.disconnect(true);
Serial.println("Connection Lost - Resetting Wifi");
connectWifi();
delay(500);
}
client.stop();
}//end send
// Read Soil Sensor
void senseSoil(){
setSensorPolarity(true);
delay(flipTimer);
int valA1 = analogRead(sensorPin);
delay(flipTimer);
setSensorPolarity(false);
delay(flipTimer);
// invert the reading
int valA2 = 1023 - analogRead(sensorPin);
moisture = (valA1 + valA2) / 2;
if (moisture <= sensLimit){
water = true;
digitalWrite(LEDA, LOW);
digitalWrite(LEDB, HIGH);
}
else {
water = false;
digitalWrite(LEDA, HIGH);
digitalWrite(LEDB, LOW);
}
}
void setSensorPolarity(boolean flip){
if(flip){
digitalWrite(voltageFlipPinA, HIGH);
digitalWrite(voltageFlipPinB, LOW);
}else{
digitalWrite(voltageFlipPinA, LOW);
digitalWrite(voltageFlipPinB, HIGH);
}
}
void sensorOff (){
digitalWrite(voltageFlipPinA, LOW);
digitalWrite(voltageFlipPinB, LOW);
}
void pumpWater(int time){
Serial.print("Watering... ");
digitalWrite(PumpPin, HIGH);
delay(time);
digitalWrite(PumpPin, LOW);
Serial.println(" Done!");
}
/// SPIFFS File Handling
bool loadFromSpiffs(String path){
String dataType = "text/plain";
if(path.endsWith("/")) path += "index.htm";
if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
else if(path.endsWith(".html")) dataType = "text/html";
else if(path.endsWith(".htm")) dataType = "text/html";
else if(path.endsWith(".css")) dataType = "text/css";
else if(path.endsWith(".js")) dataType = "application/javascript";
else if(path.endsWith(".png")) dataType = "image/png";
else if(path.endsWith(".gif")) dataType = "image/gif";
else if(path.endsWith(".jpg")) dataType = "image/jpeg";
else if(path.endsWith(".ico")) dataType = "image/x-icon";
else if(path.endsWith(".xml")) dataType = "text/xml";
else if(path.endsWith(".pdf")) dataType = "application/pdf";
else if(path.endsWith(".zip")) dataType = "application/zip";
File dataFile = SPIFFS.open(path.c_str(), "r");
if (srv.hasArg("download")) dataType = "application/octet-stream";
if (srv.streamFile(dataFile, dataType) != dataFile.size()) {
}
dataFile.close();
return true;
}