-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthy-plant-iot-system-wifi-ble.ino
1477 lines (1273 loc) · 45.6 KB
/
healthy-plant-iot-system-wifi-ble.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Arduino.h>
#include <DHT.h>
#include <BH1750FVI.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLECharacteristic.h>
#include <BLE2902.h>
//#include <BLE2904.h>
#include <HTTPClient.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include "Ubidots.h"
#include "ArduinoJson.h"
//Dracaena deremensis 'Compacta'
/****** BLE fields *********************************/
BLECharacteristic* moistureCharacteristic;
BLECharacteristic* tempCharacteristic;
BLECharacteristic* humdCharacteristic;
BLECharacteristic* lightCharacteristic;
// moisture service
BLEUUID soilMoistureServiceID("2d566796-47df-11ec-81d3-0242ac130003");
// temperature service
BLEUUID tempServiceID("2d5669d0-47df-11ec-81d3-0242ac130003");
// humidity service
BLEUUID humdServiceID("2d566ac0-47df-11ec-81d3-0242ac130003");
// light intensity service
BLEUUID lightServiceID("2d566b7e-47df-11ec-81d3-0242ac130003");
// plant database service
BLEUUID plantDatabaseServiceID("9a2f7b48-59b2-11ec-bf63-0242ac130002");
// plant database characterictic
BLEUUID plantDatabaseCharID("9a2f7d96-59b2-11ec-bf63-0242ac130002");
// moisture characterictic
BLEUUID moistureCharID("2d566c3c-47df-11ec-81d3-0242ac130003");
// moisture minimum threshold characterictic
BLEUUID moistureMinCharID("2d566ef8-47df-11ec-81d3-0242ac130003");
// moisture maximum threshold characterictic !!
BLEUUID moistureMaxCharID("2d566fc0-47df-11ec-81d3-0242ac130003");
// temperature characterictic
BLEUUID tempCharID("ec922e1e-43da-11ec-81d3-0242ac130003");
// temperature minimum threshold characterictic
BLEUUID tempMinCharID("2d567128-47df-11ec-81d3-0242ac130003");
// temperature maximum threshold characterictic
BLEUUID tempMaxCharID("2d5671dc-47df-11ec-81d3-0242ac130003");
// humidity characterictic
BLEUUID humidCharID("ec922f90-43da-11ec-81d3-0242ac130003");
// humidity minimum threshold characterictic
BLEUUID humidMinCharID("2d56733a-47df-11ec-81d3-0242ac130003");
// humidity maximum threshold characterictic
BLEUUID humidMaxCharID("2d5673e4-47df-11ec-81d3-0242ac130003");
// light characterictic
BLEUUID lightCharID("2d5675ec-47df-11ec-81d3-0242ac130003");
// light minimum threshold characterictic
BLEUUID lightMinCharID("2d5676b4-47df-11ec-81d3-0242ac130003");
// light maximum threshold characterictic
BLEUUID lightMaxCharID("2d56775e-47df-11ec-81d3-0242ac130003");
// BLE server
BLEServer* pServer = NULL;
/****** BLE fields *****************************************/
/****** WiFi and UBidots fields *****************************************/
const char* WIFI_SSID = "Three_5G";
const char* WIFI_PASSWORD = "Varvara2010";
//const char* WIFI_SSID = "iPhone XS Max";
//const char* WIFI_PASSWORD = "123456789";
const char* UBIDOTS_TOKEN = "BBFF-zFr5hYpYnLlKINl15kl1kALvXonnfs";
Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP);
/****** WiFi and UBidots fields *****************************************/
/****** IFTTT fields *********************************/
const char* apiKey = "ugOtpsDjvzLpaMRaeNZPz";
const char* host = "maker.ifttt.com";
const int httpPort = 80;
/****** IFTTT fields *********************************/
/****** moisture sensor fields *********************************/
//pin
const uint8_t MOISTURE_PIN = A2;
const uint8_t LED_GREEN_PIN = 5;
// last moisture level
RTC_DATA_ATTR uint16_t moistureLevel = 0;
// threshold
RTC_DATA_ATTR uint16_t minMoist = 15;
RTC_DATA_ATTR uint16_t maxMoist = 60;
RTC_DATA_ATTR uint8_t isMoistTooHigh = false;
RTC_DATA_ATTR uint8_t isMoistTooLow = false;
RTC_DATA_ATTR uint8_t isMoistSensorFault = false;
/****** moisture sensor fields ******/
/****** temperature and humidity sensor fields *********************************/
const uint8_t DHTPIN = 4;
const uint8_t DHTTYPE = DHT11;
// last temperature level
RTC_DATA_ATTR int temperatureLevel = 0;
// threshold
RTC_DATA_ATTR int minTemp = 5;
RTC_DATA_ATTR int maxTemp = 35;
RTC_DATA_ATTR uint8_t isTempTooHigh = false;
RTC_DATA_ATTR uint8_t isTempTooLow = false;
RTC_DATA_ATTR uint8_t isTempSensorFault = false;
// last humidity level
RTC_DATA_ATTR uint16_t humidityLevel = 0;
// threshold
RTC_DATA_ATTR uint16_t minHumid = 30;
RTC_DATA_ATTR uint16_t maxHumid = 85;
RTC_DATA_ATTR uint8_t isHumidTooHigh = false;
RTC_DATA_ATTR uint8_t isHumidTooLow = false;
RTC_DATA_ATTR uint8_t isHumidSensorFault = false;
DHT dht(DHTPIN, DHTTYPE);
/****** temperature and humidity sensor fields *********************************/
/****** light intensity sensor fields *********************************/
// last light intensity level
RTC_DATA_ATTR uint16_t lightLevel = 0;
// threshold
RTC_DATA_ATTR uint16_t minLight = 3700;
RTC_DATA_ATTR uint16_t maxLight = 30000;
RTC_DATA_ATTR uint8_t isLightTooHigh = false;
RTC_DATA_ATTR uint8_t isLightTooLow = false;
RTC_DATA_ATTR uint8_t isLightSensorFault = false;
BH1750FVI LightSensor(BH1750FVI::k_DevModeContLowRes);
/****** light intensity sensor fields *********************************/
/****** Server callback fields *********************************/
RTC_DATA_ATTR uint8_t deviceConnected = false;
RTC_DATA_ATTR uint8_t oldDeviceConnected = false;
/****** Server callback fields *********************************/
/****** Deep Sleep fields *********************************/
const uint8_t DEEP_SLEEP_TIME = 120; // in minutes
const uint8_t SLEEP_DELAY_TIME = 10; // in minutes
/****** Deep Sleep fields *********************************/
/****** Awake fields *********************************/
const uint8_t AWAKE_INTERVAL = 15; // in minutes
uint32_t previousTime = 0;
/****** Awake fields *********************************/
//String for storing server response
String response = "";
//JSON document
DynamicJsonDocument doc(2048);
/*
* Checks a plant moisture.
* Returns a value from the moisture sensor.
*/
uint16_t getMoisture() {
uint16_t ml = analogRead(MOISTURE_PIN);
// delay(500);
return ml;
}
/*
* Checks the enviroment temperature around the plant.
* Returns a temperature value as Celsius from the DHT sensor.
*/
int getTemperature() {
int tp = dht.readTemperature();
// delay(500);
return tp;
}
/*
* Checks the enviroment humidity around the plant.
* Returns a humidity value from the DHT sensor.
*/
uint16_t getHumidity() {
uint16_t hm = dht.readHumidity();
// delay(500);
return hm;
}
/*
* Checks the enviroment light intensity around the plant.
* Returns a light intensity as Lux value from the light intensity sensor.
*/
uint16_t getLightIntensity() {
uint16_t lux = LightSensor.GetLightIntensity();
// delay(500);
return lux;
}
/*
* Check if the string consist of the numbers only.
* Returns true or false respectivly
* @param String to check
*/
bool is_number(const std::string& s) {
return !s.empty() && std::find_if(s.begin(),
s.end(), [] (unsigned char c) { return !std::isdigit(c); }) == s.end();
}
/*
* Checks if any of the sensors measurements are above the their threshold.
* Helps with switching the LED on or off.
*/
uint8_t checkIfProblem() {
if (isMoistTooHigh || isMoistTooLow || isTempTooHigh || isTempTooLow
|| isHumidTooHigh || isHumidTooLow || isLightTooHigh || isLightTooLow) {
return true;
}
else
return false;
}
/*
* Sends notifications via BLE to the client if any of the threshold are above the limit.
* Checks if device connected or not to the server.
*/
void sendBLENotificationToAll() {
if (deviceConnected) {
//checks moisture
if (isMoistTooLow) {
moistureCharacteristic->notify();
}
if (isMoistTooHigh) {
moistureCharacteristic->notify();
}
if (isMoistSensorFault) {
moistureCharacteristic->notify();
}
delay(10);
// checks temperature
if (isTempTooHigh) {
tempCharacteristic->notify();
}
if (isTempTooLow) {
tempCharacteristic->notify();
}
if (isTempSensorFault) {
tempCharacteristic->notify();
}
delay(10);
// checks humidity
if (isHumidTooHigh) {
humdCharacteristic->notify();
}
if (isHumidTooLow) {
humdCharacteristic->notify();
}
if (isHumidSensorFault) {
humdCharacteristic->notify();
}
delay(10);
// checks light inensity
if (isLightTooHigh) {
lightCharacteristic->notify();
}
if (isLightTooLow) {
lightCharacteristic->notify();
}
if (isLightSensorFault) {
lightCharacteristic->notify();
}
delay(10);
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500);
pServer->startAdvertising();
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
}
/*
* Sends notifications via BLE to the client if any of the threshold are above the limit.
* Checks if device connected or not to the server.
*/
void sendBLENotification(BLECharacteristic* &characteristic) {
if (deviceConnected) {
characteristic->notify();
delay(10);
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500);
pServer->startAdvertising();
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
}
/*
* Sends notifications via BLE to the client if any of the threshold are above the limit.
* Checks if device connected or not to the server.
*/
void sendToUbidots(int t, uint16_t m, uint16_t h, uint16_t l) {
ubidots.add("Temperature", t);
ubidots.add("Humidity", h);
ubidots.add("Soil Moisture", m);
ubidots.add("Light Intensity", l);
bool bufferSent = false;
bufferSent = ubidots.send(); // Will send data to a device label that matches the device Id
if (bufferSent) {
// Do something if values were sent properly
Serial.println("Values sent by the device");
}
else {
Serial.println("Values did not send by the device");
}
}
void sendEmailNotification() {
Serial.print("");
Serial.print("Sending notification to ");
Serial.println(host);
if(isTempTooLow) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/temperature/with/key/-xIO2yRF6XRohYRWrCEmi";
String temp = String(temperatureLevel);
String celsius = " °C";
String message = " - Temperature is to low!";
String v1 = temp + celsius + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
if(isTempTooHigh) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/temperature/with/key/-xIO2yRF6XRohYRWrCEmi";
String temp = String(temperatureLevel);
String celsius = " °C";
String message = " - Temperature is to high!";
String v1 = temp + celsius + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
if(isHumidTooHigh) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/humidity/with/key/-xIO2yRF6XRohYRWrCEmi";
String humid = String(humidityLevel);
String percentage = " %";
String message = " - Humidity is to high!";
String v1 = humid + percentage + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
if(isHumidTooLow) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/humidity/with/key/-xIO2yRF6XRohYRWrCEmi";
String humid = String(humidityLevel);
String percentage = " %";
String message = " - Humidity is to low!";
String v1 = humid + percentage + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
if(isMoistTooLow) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/moisture/with/key/-xIO2yRF6XRohYRWrCEmi";
String moist = String(moistureLevel);
String message = " - Moisture is to low!";
String v1 = moist + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
if(isMoistTooHigh) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/moisture/with/key/-xIO2yRF6XRohYRWrCEmi";
String moist = String(moistureLevel);
String message = " - Moisture is to low!";
String v1 = moist + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
if(isLightTooLow) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/light/with/key/-xIO2yRF6XRohYRWrCEmi";
String light = String(lightLevel);
String message = " - Light is to low!";
String v1 = light + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
if(isLightTooHigh) {
WiFiClient client;
Serial.println("");
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
Serial.println("notification did not send!");
} else {
Serial.println("Connected to ifttt!");
String url = "/trigger/light/with/key/-xIO2yRF6XRohYRWrCEmi";
String light = String(lightLevel);
String message = " - Light is to low!";
String v1 = light + message;
Serial.println("message: " + v1);
String df1 = "{\"value1\":";
String IFTTT_POST_DATA = df1 + "\"" + v1 + "\"" + "}" ;
String IFTTT_POST_DATA_SIZE = String(IFTTT_POST_DATA.length());
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"User-Agent: BuildFailureDetectorESP32\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length:" + IFTTT_POST_DATA_SIZE + "\r\n" +
"\r\n" +
IFTTT_POST_DATA + "\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("notification sent!");
}
}
}
/*
* Sends the unit to the deep sleep.
* Provides possibility to set the time to awake from sleep.
*/
void goToDeepSleep() {
Serial.println("Going to sleep now for " + String(DEEP_SLEEP_TIME) + " Minutes");
esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME * 600000);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
Serial.println("Slepping.....");
delay(1000);
esp_deep_sleep_start();
}
/*
* Method to print the reason by which ESP32
* has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0:
Serial.println("Wakeup caused by external signal using RTC_IO");
break;
case ESP_SLEEP_WAKEUP_EXT1:
Serial.println("Wakeup caused by external signal using RTC_CNTL");
break;
case ESP_SLEEP_WAKEUP_TIMER:
Serial.println("Wakeup caused by timer");
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
Serial.println("Wakeup caused by touchpad");
break;
case ESP_SLEEP_WAKEUP_ULP:
Serial.println("Wakeup caused by ULP program");
break;
default:
Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason);
break;
}
}
void printReadingsToMonitor(int t, uint16_t m, uint16_t h, uint16_t l) {
// moisture
Serial.println(F(""));
uint16_t moisture = m;
if (isnan(moisture)) Serial.println(F("Failed to read from moisture sensor!"));
else {
moistureLevel = moisture;
Serial.print(moisture);
//checks against it's thresholds
if (moisture < minMoist) Serial.println(F(" - Time to water your plant!"));
if (moisture > maxMoist) Serial.println(F(" - Too much water in your plant!"));
if (moisture >= minMoist && moisture <= maxMoist) Serial.println(F(" - Doesn't need watering!"));
}
//temperature
int temp = t;
if (isnan(temp)) Serial.println(F("Failed to read a temperature!"));
else {
temperatureLevel = temp;
Serial.print(temp);
Serial.print(F(" °C"));
//checks against it's thresholds
if (temp < minTemp) Serial.println(F(" - Temperature is to low!"));
if (temp > maxTemp) Serial.println(F(" - Temperature is too high!"));
if (temp >= minTemp && temp <= maxTemp) Serial.println(F(" - Temperature is ok!"));
}
// humidity
uint16_t humid = h;
if (isnan(humid)) Serial.println(F("Failed to read a humidity!"));
else {
humidityLevel = humid;
Serial.print(humid);
Serial.print(F(" %"));
//checks against it's thresholds
if (humid > maxHumid) Serial.println(F(" - Humidity is to high!"));
if (humid < minHumid) Serial.println(F(" - Humidity is too low!"));
if (humid >= minHumid && humid <= maxHumid) Serial.println(F(" - Humidity is ok!"));
}
// light intensity
uint16_t light = l;
if (isnan(light)) Serial.println(F("Failed to read a humidity!"));
else {
lightLevel = light;
Serial.print(light);
//checks against it's thresholds
if (light > maxLight) Serial.println(F(" - Light is to high!"));
if (light < minLight) Serial.println(F(" - Light is to low!"));
if (light <= maxLight && light >= minLight) Serial.println(F(" - Light is ok!"));
}
}
void checkAgainstThresholds(int t, uint16_t m, uint16_t h, uint16_t l) {
// moisture
if (isnan(m)) {
isMoistSensorFault = true;
}
else {
m < minMoist ? isMoistTooLow = true : isMoistTooLow = false;
m > maxMoist ? isMoistTooLow = true : isMoistTooHigh = false;
if (m >= minMoist && m <= maxMoist) {
isMoistTooLow = false;
isMoistTooHigh = false;
}
}
// temperature
if (isnan(t)) {
isTempSensorFault = true;
}
else {
t < minTemp ? isTempTooLow = true : isTempTooLow = false;
t > maxTemp ? isTempTooHigh = true : isTempTooHigh = false;
if (t >= minTemp && t <= maxTemp) {
isTempTooLow = false;
isTempTooHigh = false;
}
}
// humidity
if (isnan(h)) isHumidSensorFault = true;
else {
h > maxHumid ? isHumidTooHigh = true : isHumidTooHigh = false;
h < minHumid ? isHumidTooLow = true : isHumidTooLow = false;
if (h >= minHumid && h <= maxHumid) {
isHumidTooLow = false;
isHumidTooHigh = false;
}
}
// light intensity
if (isnan(l)) isLightSensorFault = true;
else {
l > maxLight ? isLightTooHigh = true : isLightTooHigh = false;
l < minLight ? isLightTooLow = true : isLightTooLow = false;
if (l <= maxLight && l >= minLight) {
isLightTooLow = false;
isLightTooHigh = false;
}
}
}
void findPlantSettings(String plantName) {
if (!WiFi.status()== WL_CONNECTED){
Serial.println("WiFi Disconnected");
Serial.println("Trying to connect again...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
////Initiate HTTP client
HTTPClient http;
//The API URL
String request = "https://open.plantbook.io/api/v1/plant/detail/" + plantName + "/";
//Start the request
http.begin(request);
http.addHeader("Authorization","Bearer TT4xiJEqm5xt0dTeHkesBKBfBiqrXT");
int httpResponseCode = http.GET();
Serial.println(F("Looking your plant in a database..."));
delay(500);
if (httpResponseCode == 200) {
Serial.println(F("Plant found in a database!"));
Serial.println(F(""));
delay(500);
String response = http.getString();
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, response);
if(error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
Serial.println(response);
} else {
const char* plantName = doc["display_pid"];
Serial.print(F("Plant name: "));
Serial.println(plantName);
const int plantMinTemp = doc["min_temp"];
Serial.print(F("minimum temperature: "));
Serial.print(plantMinTemp);
Serial.println(F("°C"));
const int plantMaxTemp = doc["max_temp"];
Serial.print(F("maximum temperature: "));
Serial.print(plantMaxTemp);
Serial.println(F("°C"));
const int plantMinHumid = doc["min_env_humid"];
Serial.print(F("minimum humidity: "));
Serial.print(plantMinHumid);
Serial.println(F("%"));
const int plantMaxHumid = doc["max_env_humid"];
Serial.print(F("maximum humidity: "));
Serial.print(plantMaxHumid);
Serial.println(F("%"));
const int plantMinMoist = doc["min_soil_moist"];
Serial.print(F("minimum soil moisture: "));
Serial.println(plantMinMoist);
const int plantMaxMoist = doc["max_soil_moist"];
Serial.print(F("maximium soil moisture: "));
Serial.println(plantMaxMoist);
const int plantMinLight = doc["min_light_lux"];
Serial.print(F("minimum light: "));
Serial.print(plantMinLight);
Serial.println(F(" lux"));
const int plantMaxLight = doc["max_light_lux"];
Serial.print(F("maximum light: "));
Serial.print(plantMaxLight);
Serial.println(F(" lux"));
}
}
if (httpResponseCode == 404) {
Serial.print("Plant not found in a database!");
Serial.println(F(""));
}
if (httpResponseCode != 200 && httpResponseCode != 404) {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
String response = http.getString();
Serial.print("Response: ");
Serial.println(response);
}
//Close connection
http.end();
}
/*
* The callback function that handles receiving data being sent from the client(phone) and Bluetooth connection status.
*/
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
/*
* A class definition for handling characterictics callbacks
*/
class MyCallbacks : public BLECharacteristicCallbacks {
//this method will be call to perform writes to characteristic
void onWrite(BLECharacteristic* pCharacteristic) {
// moisture minimum threshold
if (moistureMinCharID.equals(pCharacteristic->getUUID())) {
if (is_number(pCharacteristic->getValue().c_str())) {
uint16_t i = atoi((pCharacteristic->getValue().c_str()));
minMoist = i;
Serial.print(F("New minimum moisture threshold has been set: "));
Serial.println(minMoist);
uint16_t ml = getMoisture();
if (ml < minMoist) {
isMoistTooLow = true;
} else {
isMoistTooLow = false;
}
}
else {
Serial.println(F(" "));
Serial.println(F("not a number"));
}
}
// moisture maximum threshold
if (moistureMaxCharID.equals(pCharacteristic->getUUID())) {
if (is_number(pCharacteristic->getValue().c_str())) {
uint16_t i = atoi((pCharacteristic->getValue().c_str()));
maxMoist = i;
Serial.print(F("New maximum moisture threshold has been set: "));
Serial.println(maxMoist);
uint16_t ml = getMoisture();
if (ml > maxMoist) {
isMoistTooHigh = true;
} else {
isMoistTooHigh = false;
}
}
else {
Serial.println(F("not a number"));
}
}
// temperature minimum threshold
if (tempMinCharID.equals(pCharacteristic->getUUID())) {
if (is_number(pCharacteristic->getValue().c_str())) {
int i = atoi((pCharacteristic->getValue().c_str()));
minTemp = i;
Serial.print(F("New minimum temperature threshold has been set: "));
Serial.println(minTemp);
int t = getTemperature();
delay(500);
if (t < minTemp) {
isTempTooLow = true;
} else {
isTempTooLow = false;
}
}
else {
Serial.println(F("not a number"));
}
}
// temperature maximum threshold
if (tempMaxCharID.equals(pCharacteristic->getUUID())) {
if (is_number(pCharacteristic->getValue().c_str())) {
int i = atoi((pCharacteristic->getValue().c_str()));
maxTemp = i;
Serial.print(F("New maximum temperature threshold has been set: "));
Serial.println(maxTemp);
int t = getTemperature();
if (t > maxTemp) {
isTempTooHigh = true;
}else {
isTempTooHigh = false;
}
}
else {
Serial.println(F("not a number"));
}
}
// humidity minimum threshold
if (humidMinCharID.equals(pCharacteristic->getUUID())) {
if (is_number(pCharacteristic->getValue().c_str())) {
uint16_t i = atoi((pCharacteristic->getValue().c_str()));