This repository was archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSmartFarm_DeepSleep.ino
1634 lines (1395 loc) · 45.6 KB
/
SmartFarm_DeepSleep.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
/****************************************************************************************************************************
* SmartFarm_DeepSleep.ino
* SmartFarm for ESP32 and ESP8266, using configurable DeepSleep, and many other parameters
* For ESP8266 / ESP32 boards
* Written by Khoi Hoang
* Copyright (c) 2019 Khoi Hoang
*
* Built by Khoi Hoang https://github.com/khoih-prog/SmallProjects/SmartFarm_DeepSleep
* Licensed under MIT license
* Version: 1.0.4
*
* Now we can use these new 16 ISR-based timers, while consuming only 1 hardware Timer.
* Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
* The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
* Therefore, their executions are not blocked by bad-behaving functions / tasks.
* This important feature is absolutely necessary for mission-critical tasks.
*
* Notes:
* Special design is necessary to share data between interrupt code and the rest of your program.
* Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
* variable can not spontaneously change. Because your function may change variables while your program is using them,
* the compiler needs this hint. But volatile alone is often not enough.
* When accessing shared variables, usually interrupts must be disabled. Even with volatile,
* if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
* If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
* or the entire sequence of your code which accesses the data.
*
* Version Modified By Date Comments
* ------- ----------- ---------- -----------
* 1.0.0 K Hoang 18/09/2019 Initial coding for ESP32
* 1.0.1 K Hoang 25/09/2019 Add ESP8266 and SSL support
* 1.0.2 K Hoang 20/10/2019 Use Blynk_WM for easy management and test
* 1.0.3 K Hoang 07/01/2020 Use Blynk_WM v1.0.4 with personalized DHCP hostname
* 1.0.4 K Hoang 07/07/2020 Use new Blynk_WM v1.0.16+ with new USE_DYNAMIC_PARAMETERS and LOAD_DEFAULT_CONFIG_DATA
*****************************************************************************************************************************/
/****************************************************************************************************************************
* To use ESP32 Dev Module, CPU 240MHz (WiFi/BT), QIO, Flash 4MB/80MHz, Upload 921600
* KH Mods in .../hardware/espressif/esp32/libraries/WiFi/src/WiFiScan.cpp
* KH Mods in ~/Arduino/libraries/WiFiManager/WiFiManager.cpp
* Later check why using latest esp32 (arduino-esp32-master) -> crash. Already fixed in WiFiManager.h/cpp
*
* Issues:
* 1) SPIFFS OK now. Follow intruction at https://github.com/me-no-dev/arduino-esp32fs-plugin
* To upload firmware, press BOOT button just after uploading started.
* Press EN button if booting can't connect to WiFi
*****************************************************************************************************************************/
#include "SmartFarm_DeepSleep.h"
#define USE_DEEPSLEEP true
//#define USE_DEEPSLEEP false
#define USE_BLYNK_RTC true
#if USE_BLYNK_WM
String ssid = "";
#else
#define SMART_FARM_BOARD_NO 6
#if (SMART_FARM_BOARD_NO == 1)
String blynk_token = "board1-token";
#elif (SMART_FARM_BOARD_NO == 2)
String blynk_token = "board2-token";
#elif (SMART_FARM_BOARD_NO == 3)
String blynk_token = "board3-token";
#elif (SMART_FARM_BOARD_NO == 4)
String blynk_token = "board4-token";
#elif (SMART_FARM_BOARD_NO == 5)
String blynk_token = "board5-token";
#elif (SMART_FARM_BOARD_NO == 6)
String blynk_token = "board6-token";
#endif
String ssid = "****";
String pass = "****";
#endif
int16_t curr_RSSI;
int16_t percent_RSSI;
float humDHT = 0;
float tempDHT = 0;
float tempF_DHT = 32;
float HeatIndexDHT = 0;
float HeatIndexF_DHT = 32;
float soilMoist = 0;
boolean pumpStatus = STOP;
boolean pumpModeAuto = true;
boolean pumpModeNotice = true;
boolean tempNormal = true;
boolean humidNormal = true;
boolean moistNormal = true;
boolean RSSINormal = true;
uint moist_alarm_level = 50;
boolean moist_alarm = false;
BlynkTimer timer;
Ticker relay_ticker;
Ticker aux_ticker;
WidgetLED blynk_led_pump_on(BLYNK_PIN_PUMP_ON);
WidgetLED blynk_led_pump_off(BLYNK_PIN_PUMP_OFF);
WidgetRTC rtc;
WidgetLCD blynk_lcd(BLYNK_PIN_LCD);
DHTesp* dht;
String firmware_version = String(FWV / 100 % 10) + String(FWV / 10 % 10) + String(FWV % 10) + FWSV;
String get_last_ip()
{
String ip = "x.";
IPAddress _ip = WiFi.localIP();
ip += _ip[3];
return ip;
}
#define DEBUG_LOOP 1
#define DHT_DEBUG 0
#define DEBUG_BLYNK_RTC 1
#define CURRENT_TIME_LENGTH 12
#define CURRENT_DAY_LENGTH 15
char currentTime[CURRENT_TIME_LENGTH]; //prepared for AM/PM time
char currentDay [CURRENT_DAY_LENGTH];
char dayOfWeek[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
#if (DEBUG_BLYNK_RTC > 0)
char activatedTime[15];
#endif
bool clockSync = false;
static void restart(void)
{
ESP.restart();
//digitalWrite(PIN_RESET, LOW);
}
char* currentTimeAM_PM(char *inputTime)
{
int hourAM_PM = hour();
if (hourAM_PM >= 12)
{
if (hourAM_PM > 12)
hourAM_PM -= 12;
sprintf(inputTime, "%02d:%02d:%02d PM", hourAM_PM, minute(), second());
}
else
{
if (hourAM_PM == 0)
hourAM_PM = 12;
sprintf(inputTime, "%02d:%02d:%02d AM", hourAM_PM, minute(), second());
}
return inputTime;
}
void clockDisplay()
{
if (year() != 1970)
{
sprintf(currentDay, "%s %02d/%02d/%04d", dayOfWeek[weekday() - 1], day(), month(), year());
blynk_lcd.print(0, 0, currentTimeAM_PM(currentTime));
blynk_lcd.print(0, 1, currentDay);
if (clockSync == false)
{
#if (DEBUG_BLYNK_RTC > 0)
//Only print the first time after sync
sprintf(activatedTime, "%s %02d/%02d/%04d", dayOfWeek[weekday() - 1], day(), month(), year());
Serial.printf("First sync at %s on %s\n", currentTimeAM_PM(currentTime), activatedTime);
#endif
//blynk_lcd.clear();
//Null terminated string
currentTime[CURRENT_TIME_LENGTH - 1] = 0;
currentDay[CURRENT_DAY_LENGTH - 1] = 0;
clockSync = true;
}
}
}
#if (USE_DEEPSLEEP)
// 2 days
#define DAY_IN_MINS 1440
uint64_t deepSleepMax_mins = (2 * DAY_IN_MINS);
#define USE_BITFIELD true
#if USE_BITFIELD
/* define a structure with bit fields */
struct RTC_BoolData
{
uint32_t reserved : 29;
uint32_t MW33 : 1;
uint32_t sensorCapacitive : 1;
uint32_t USE_CELCIUS : 1;
};
#endif
#if (USE_ESP32)
RTC_DATA_ATTR uint32_t bootCount = 0;
RTC_DATA_ATTR uint32_t RTC_DEEPSLEEP_INTERVAL_FACTOR;
RTC_DATA_ATTR uint32_t RTC_TIME_TO_DEEPSLEEP;
RTC_DATA_ATTR uint32_t RTC_DRY_SOIL;
RTC_DATA_ATTR DHTesp::DHT_MODEL_t RTC_DHTTYPE;
#if USE_BITFIELD
RTC_DATA_ATTR RTC_BoolData RTC_Data;
#else
// Better to use bit field for boolean data
RTC_DATA_ATTR boolean RTC_MW33;
RTC_DATA_ATTR boolean RTC_sensorCapacitive;
RTC_DATA_ATTR boolean RTC_USE_CELCIUS;
#endif
#else //(USE_ESP32)
/*
ESP.rtcUserMemoryWrite(offset, &data, sizeof(data)) and ESP.rtcUserMemoryRead(offset, &data, sizeof(data)) allow data to be stored in and
retrieved from the RTC user memory of the chip respectively. offset is measured in blocks of 4 bytes and can range from 0 to 127 blocks
(total size of RTC memory is 512 bytes). data should be 4-byte aligned. The stored data can be retained between deep sleep cycles, but might
be lost after power cycling the chip. Data stored in the first 32 blocks will be lost after performing an OTA update, because they are used by
the Core internals.
*/
#define bootCountOffset 32
uint32_t bootCount = 0;
uint32_t RTC_DEEPSLEEP_INTERVAL_FACTOR;
uint32_t RTC_TIME_TO_DEEPSLEEP;
uint32_t RTC_DRY_SOIL;
uint32_t RTC_DHTTYPE;
#if USE_BITFIELD
/* define a structure with bit fields */
RTC_BoolData RTC_Data;
#else
// Better to use bit field for boolean data
uint32_t RTC_MW33;
uint32_t RTC_sensorCapacitive;
uint32_t RTC_USE_CELCIUS;
#endif
#endif //(USE_ESP32)
#endif //(USE_DEEPSLEEP)
BLYNK_CONNECTED()
{
rtc.begin();
//synchronize the state of widgets with hardware states
blynk_lcd.clear();
Blynk.virtualWrite(BLYNK_PIN_FWV, firmware_version);
ssid = WiFi.SSID();
Blynk.virtualWrite(BLYNK_PIN_SSID, ssid);
Blynk.virtualWrite(BLYNK_PIN_IP, get_last_ip());
#if (USE_DEEPSLEEP)
Blynk.setProperty(BLYNK_PIN_TIME_TO_DEEPSLEEP, "max", deepSleepMax_mins);
Blynk.virtualWrite(BLYNK_PIN_DEEPSLEEP_BOOTCOUNT, String(bootCount));
#endif
//change color to GREEN
tempNormal = true;
humidNormal = true;
moistNormal = true;
RSSINormal = true;
Blynk.setProperty(BLYNK_PIN_TEMP, "color", BLYNK_GREEN);
Blynk.setProperty(BLYNK_PIN_HUMID, "color", BLYNK_GREEN);
Blynk.setProperty(BLYNK_PIN_MOIST, "color", BLYNK_GREEN);
Blynk.setProperty(BLYNK_PIN_RSSI, "color", BLYNK_GREEN);
moist_alarm = false;
updateBlynkStatus();
Blynk.syncAll();
}
BLYNK_WRITE(BLYNK_PIN_RESET)
{
if (param.asInt())
{
restart();
}
}
DHTesp::DHT_MODEL_t DHTTYPE = DHTesp::AUTO_DETECT; // default Auto Detect
DHTesp::DHT_MODEL_t lastDHTTYPE;
//MW33 is one special type of DHT11. Needs conversion. Init is the same.
boolean MW33 = false;
BLYNK_WRITE(BLYNK_PIN_DHT_TYPE)
{
switch (param.asInt())
{
case 1:
{ // Item 1 "Auto Detect"
DHTTYPE = DHTesp::AUTO_DETECT;
break;
}
case 2:
{ // Item 2
DHTTYPE = DHTesp::DHT11;
MW33 = false;
break;
}
case 3:
{ // Item 3
DHTTYPE = DHTesp::DHT11;
MW33 = true;
break;
}
case 4:
{ // Item 4
DHTTYPE = DHTesp::DHT22;
break;
}
case 5:
{ // Item 5
DHTTYPE = DHTesp::AM2302;
break;
}
case 6:
{ // Item 6
DHTTYPE = DHTesp::RHT03;
break;
}
}
}
boolean sensorCapacitive = true;
BLYNK_WRITE(BLYNK_PIN_MOIST_SENSOR_TYPE)
{
switch (param.asInt())
{
case 1:
{ // Item 1 "Resistive"
if (sensorCapacitive)
{
sensorCapacitive = false;
}
break;
}
case 2:
{ // Item 2
if (!sensorCapacitive)
{
sensorCapacitive = true;
}
break;
}
}
}
BLYNK_WRITE(BLYNK_PIN_PUMP_MODE)
{
switch (param.asInt())
{
case 1:
{ // Item 1 "Auto & Notice"
pumpModeAuto = true;
pumpModeNotice = true;
break;
}
case 2:
{ // Item 2 "Auto & No Notice"
pumpModeAuto = true;
pumpModeNotice = false;
break;
}
case 3:
{ // Item 3 "Manual & Notice"
pumpModeAuto = false;
pumpModeNotice = true;
break;
}
case 4:
{ // Item 4 "Manual & No Notice"
pumpModeAuto = false;
pumpModeNotice = false;
break;
}
}
}
// Default Send alarm msg of low moist every hour, in seconds. If 0 => No alarm
uint32_t alarm_moist_interval = 3600;
BLYNK_WRITE(BLYNK_PIN_MOIST_ALARM_INTERVAL)
{
switch (param.asInt())
{
case 1:
{ // Item 1 "No Low Moisture Alarm"
alarm_moist_interval = 0;
break;
}
case 2:
{ // Item 2 "Every 1 hour"
alarm_moist_interval = 3600;
break;
}
case 3:
{ // Item 3 "Every 6 hrs"
alarm_moist_interval = 3600 * 6;
break;
}
case 4:
{ // Item 4 "Every 24 hrs"
alarm_moist_interval = 3600 * 24;
break;
}
}
}
BLYNK_WRITE(BLYNK_PIN_MOIST_ALARM_LEVEL)
{
if (moist_alarm_level != param.asInt())
{
moist_alarm_level = param.asInt();
}
}
int MIN_AIR_TEMPC_ALM = 10;
int MAX_AIR_TEMPC_ALM = 50;
int MIN_AIR_TEMPF_ALM = 50;
int MAX_AIR_TEMPF_ALM = 122;
#define MIN_TEMPC_ALM_LORANGE 0
#define MIN_TEMPC_ALM_HIRANGE 30
#define MAX_TEMPC_ALM_LORANGE 30
#define MAX_TEMPC_ALM_HIRANGE 50
#define MIN_TEMPF_ALM_LORANGE 32
#define MIN_TEMPF_ALM_HIRANGE 86
#define MAX_TEMPF_ALM_LORANGE 86
#define MAX_TEMPF_ALM_HIRANGE 122
boolean USE_CELCIUS = true;
BLYNK_WRITE(BLYNK_PIN_USE_CELCIUS)
{
switch (param.asInt())
{
case 1:
{ // Item 1 "Celcius"
if (!USE_CELCIUS)
{
USE_CELCIUS = true;
Blynk.setProperty(BLYNK_PIN_MIN_AIR_TEMP, "min", MIN_TEMPC_ALM_LORANGE);
Blynk.setProperty(BLYNK_PIN_MIN_AIR_TEMP, "max", MIN_TEMPC_ALM_HIRANGE);
MIN_AIR_TEMPC_ALM = (uint) ((MIN_AIR_TEMPF_ALM - 32) * 5 ) / 9;
Blynk.setProperty(BLYNK_PIN_MAX_AIR_TEMP, "min", MAX_TEMPC_ALM_LORANGE);
Blynk.setProperty(BLYNK_PIN_MAX_AIR_TEMP, "max", MAX_TEMPC_ALM_HIRANGE);
MAX_AIR_TEMPC_ALM = (uint) ((MAX_AIR_TEMPF_ALM - 32) * 5 ) / 9;
}
break;
}
case 2:
{ // Item 2
if (USE_CELCIUS)
{
USE_CELCIUS = false;
Blynk.setProperty(BLYNK_PIN_MIN_AIR_TEMP, "min", MIN_TEMPF_ALM_LORANGE);
Blynk.setProperty(BLYNK_PIN_MIN_AIR_TEMP, "max", MIN_TEMPF_ALM_HIRANGE);
MIN_AIR_TEMPF_ALM = (uint) ((MIN_AIR_TEMPC_ALM * 9) / 5 ) + 32;
Blynk.setProperty(BLYNK_PIN_MAX_AIR_TEMP, "min", MAX_TEMPF_ALM_LORANGE);
Blynk.setProperty(BLYNK_PIN_MAX_AIR_TEMP, "max", MAX_TEMPF_ALM_HIRANGE);
MAX_AIR_TEMPF_ALM = (uint) ((MAX_AIR_TEMPC_ALM * 9) / 5 ) + 32;
}
break;
}
}
}
uint moist_adj_factor = 100;
BLYNK_WRITE(BLYNK_PIN_MOIST_ADJ_FACTOR)
{
if (moist_adj_factor != param.asInt())
{
moist_adj_factor = param.asInt();
}
}
uint DHT_ADJ_FACTOR = 100;
BLYNK_WRITE(BLYNK_PIN_DHT_ADJ_FACTOR)
{
DHT_ADJ_FACTOR = param.asInt(); // Get value as integer
}
BLYNK_WRITE(BLYNK_PIN_MIN_AIR_TEMP)
{
// Get value as integer
if (USE_CELCIUS)
MIN_AIR_TEMPC_ALM = param.asInt();
else
MIN_AIR_TEMPF_ALM = param.asInt();
}
BLYNK_WRITE(BLYNK_PIN_MAX_AIR_TEMP)
{
// Get value as integer
if (USE_CELCIUS)
MAX_AIR_TEMPC_ALM = param.asInt();
else
MAX_AIR_TEMPF_ALM = param.asInt();
}
int MIN_AIR_HUMIDITY = 30;
BLYNK_WRITE(BLYNK_PIN_MIN_AIR_HUMIDITY)
{
MIN_AIR_HUMIDITY = param.asInt(); // Get value as integer
}
int MAX_AIR_HUMIDITY = 95;
BLYNK_WRITE(BLYNK_PIN_MAX_AIR_HUMIDITY)
{
MAX_AIR_HUMIDITY = param.asInt(); // Get value as integer
}
uint32_t DRY_SOIL = 68;
BLYNK_WRITE(BLYNK_PIN_DRY_SOIL)
{
DRY_SOIL = param.asInt(); // Get value as integer
}
int WET_SOIL = 90;
BLYNK_WRITE(BLYNK_PIN_WET_SOIL)
{
WET_SOIL = param.asInt(); // Get value as integer
}
unsigned long TIME_PUMP_ON = 15000L;
BLYNK_WRITE(BLYNK_PIN_TIME_PUMP_ON)
{
TIME_PUMP_ON = param.asInt() * 1000; // Get value as seconds, converting to ms
}
// Use button V2 (BLYNK_PIN_PUMP_BUTTON) to control pump manually
BLYNK_WRITE(BLYNK_PIN_PUMP_BUTTON)
{
if (param.asInt())
{
pumpStatus = !pumpStatus;
control_pump(pumpStatus);
}
}
#if USE_DEEPSLEEP
//Default 0 mins => disable deepsleep
static uint32_t DEEPSLEEP_INTERVAL_FACTOR = 0L;
BLYNK_WRITE(BLYNK_PIN_DEEPSLEEP_INTERVAL_FACTOR)
{
if (DEEPSLEEP_INTERVAL_FACTOR != param.asInt())
{
DEEPSLEEP_INTERVAL_FACTOR = param.asInt();
//Force interval between 1-60 (1-60 minutes)
if (DEEPSLEEP_INTERVAL_FACTOR < 0)
DEEPSLEEP_INTERVAL_FACTOR = 0;
else if (DEEPSLEEP_INTERVAL_FACTOR > 60)
DEEPSLEEP_INTERVAL_FACTOR = 60;
}
}
// In minutes, default no deepsleep = 0 mins
static uint32_t TIME_TO_DEEPSLEEP = 0L;
BLYNK_WRITE(BLYNK_PIN_TIME_TO_DEEPSLEEP)
{
if (TIME_TO_DEEPSLEEP != param.asInt())
{
TIME_TO_DEEPSLEEP = param.asInt();
// Force deepsleep time between 1-2 days (1 min -> 2 days/2880 minutes).
// TIME_TO_DEEPSLEEP = 0 => No DeepSleep
if (TIME_TO_DEEPSLEEP > deepSleepMax_mins)
{
TIME_TO_DEEPSLEEP = deepSleepMax_mins;
Blynk.virtualWrite(BLYNK_PIN_TIME_TO_DEEPSLEEP, deepSleepMax_mins);
}
}
}
BLYNK_WRITE(BLYNK_PIN_FORCE_DEEPSLEEP)
{
if (param.asInt())
{
//Enter forced deepsleep only when pump is STOP
safe_deepsleep();
}
}
#endif
void updateBlynkStatus(void)
{
// Update only when data is valid
if (tempDHT != 0)
{
if ( (((tempDHT > MAX_AIR_TEMPC_ALM) || (tempDHT < MIN_AIR_TEMPC_ALM)) && USE_CELCIUS) ||
(((tempF_DHT > MAX_AIR_TEMPF_ALM) || (tempF_DHT < MIN_AIR_TEMPF_ALM)) && !USE_CELCIUS) )
{
if (tempNormal)
{
tempNormal = false;
#if (DEBUG_LOOP > 1)
Serial.println("Temp. to RED");
#endif
//change color to RED
Blynk.setProperty(BLYNK_PIN_TEMP, "color", BLYNK_RED);
}
}
else
{
if (!tempNormal)
{
tempNormal = true;
#if (DEBUG_LOOP > 1)
Serial.println("Temp. to GREEN");
#endif
//change color to GREEN
Blynk.setProperty(BLYNK_PIN_TEMP, "color", BLYNK_GREEN);
}
}
}
// Update only when data is valid
if (humDHT != 0)
{
if ((humDHT > MAX_AIR_HUMIDITY) || (humDHT < MIN_AIR_HUMIDITY))
{
if (humidNormal)
{
humidNormal = false;
#if (DEBUG_LOOP > 1)
Serial.println("Humid. to RED");
#endif
//change color to RED
Blynk.setProperty(BLYNK_PIN_HUMID, "color", BLYNK_RED);
}
}
else
{
if (!humidNormal)
{
humidNormal = true;
#if (DEBUG_LOOP > 1)
Serial.println("Humid. to GREEN");
#endif
//change color to GREEN
Blynk.setProperty(BLYNK_PIN_HUMID, "color", BLYNK_GREEN);
}
}
}
if (((soilMoist < moist_alarm_level) || (soilMoist > WET_SOIL)) && soilMoist > 0)
{
if (!moist_alarm)
{
moist_alarm = true;
#if (DEBUG_LOOP > 1)
Serial.println("MoistAlarm ON");
#endif
}
}
else
{
if (moist_alarm)
{
moist_alarm = false;
#if (DEBUG_LOOP > 1)
Serial.println("MoistAlarm OFF");
#endif
}
}
if (((soilMoist > WET_SOIL) || (soilMoist < DRY_SOIL)) && soilMoist > 0)
{
if (moistNormal)
{
moistNormal = false;
#if (DEBUG_LOOP > 1)
Serial.println("Moist to RED");
#endif
//change color to RED
Blynk.setProperty(BLYNK_PIN_MOIST, "color", BLYNK_RED);
}
}
else
{
if (!moistNormal)
{
moistNormal = true;
#if (DEBUG_LOOP > 1)
Serial.println("Moist to GREEN");
#endif
//change color to GREEN
Blynk.setProperty(BLYNK_PIN_MOIST, "color", BLYNK_GREEN);
}
}
#define TESTING_RSSI_BOUNDARY false
#if TESTING_RSSI_BOUNDARY
static int testCount = 0;
curr_RSSI = -(testCount);
Serial.printf("curr_RSSI = %d\n", curr_RSSI);
testCount = (testCount + 5) % 100;
#endif
if (curr_RSSI < RSSI_WEAK)
{
if (RSSINormal)
{
RSSINormal = false;
#if (DEBUG_LOOP > 1)
Serial.println("RSSI to RED");
#endif
//change color to RED
Blynk.setProperty(BLYNK_PIN_RSSI, "color", BLYNK_RED);
}
}
else
{
if (!RSSINormal)
{
RSSINormal = true;
#if (DEBUG_LOOP > 1)
Serial.println("RSSI to GREEN");
#endif
//change color to GREEN
Blynk.setProperty(BLYNK_PIN_RSSI, "color", BLYNK_GREEN);
}
}
}
float fmap(float x, long in_min, long in_max, long out_min, long out_max)
{
return (float) ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
}
void getSoilMoist(void)
{
#define KAVG 10
static int i;
static int temp_soilMoist;
temp_soilMoist = 0;
for (i = 0; i < KAVG; i++) //
{
temp_soilMoist += analogRead(SOIL_MOIST_PIN);
delay(20);
}
temp_soilMoist = temp_soilMoist / KAVG;
#if 0
soilMoist = (fmap(temp_soilMoist, 1024, 0, 0, 100) * moist_adj_factor) / 100.0;
#else
if (sensorCapacitive)
{
#if USE_ESP32
// Capacitive Sensor testing: Air => 3250/3390, Water => 1600/1520. Adjust the range correspondingly
soilMoist = (fmap(temp_soilMoist, 3250, 1600, 0, 100) * moist_adj_factor) / 100.0;
//soilMoist = (fmap(temp_soilMoist, 4095, 0, 0, 100) * moist_adj_factor) / 100.0;
#else
// Capacitive Sensor testing: Air => 830, Water => 360. Adjust the range correspondingly
soilMoist = (fmap(temp_soilMoist, 830, 360, 0, 100) * moist_adj_factor) / 100.0;
//soilMoist = (fmap(temp_soilMoist, 1024, 360, 0, 100) * moist_adj_factor) / 100.0;
#endif
}
else
{
// Resistive Sensor testing: Air => 1024, Water => 350. Adjust the range correspondingly
soilMoist = (fmap(temp_soilMoist, 1024, 350, 0, 100) * moist_adj_factor) / 100.0;
}
#endif
if (soilMoist < 0)
soilMoist = 0.0;
if (soilMoist > 100.0)
soilMoist = 100.0;
#if (DEBUG_LOOP > 1)
Serial.printf("getSoilMoist: Analog Read temp_soilMoist = %d, soilMoist = %4.1f\n", temp_soilMoist, soilMoist);
#endif
#define TESTING_MOIST_BOUNDARY false
#if TESTING_MOIST_BOUNDARY
static int testCount = 0;
soilMoist = testCount;
Serial.printf("soilMoist = %5.2f\n", soilMoist);
testCount = (testCount + 3) % 100;
#endif
}
void getDhtData(void)
{
static int8_t thisTemp;
static float v;
tempDHT = 0;
humDHT = 0;
if (dht)
{
TempAndHumidity th = dht->getTempAndHumidity();
#if DHT_DEBUG
if (isnan(th.temperature) || isnan(th.humidity))
Serial.println("read_TH_sensor: Temp. or Humid. is NAN");
else
Serial.printf("read_TH_sensor: Temp. = %5.2f, Humidity = %5.2f\n", th.temperature, th.humidity);
#endif
v = th.temperature;
if (!isnan(v))
{
if (!MW33)
{
tempDHT = v;
}
else
{
// MW33 temp. from -20 -> 60 degC, have to take care of negative
// Lower then 0x7F => positive. Over 0x80 => Negative
thisTemp = (int8_t) v;
if (thisTemp & 0x80)
{
thisTemp = - (thisTemp & 0x7F);
if (thisTemp < -20)
thisTemp = -20;
}
else if (thisTemp > 60)
thisTemp = 60;
tempDHT = (float) thisTemp;
}
tempF_DHT = tempDHT * 1.8 + 32.0;
}
v = th.humidity;
if (!isnan(v))
{
if (!MW33)
humDHT = v;
else
{
#define MW33_HUMID_OFFSET 15
#define MW33_MIN_HUMID (float) 5.0
#define MW33_MAX_HUMID (float) 95.0
#define DHT11_MIN_HUMID (float) 20.0
#define DHT11_MAX_HUMID (float) 90.0
//(DHT11_MAX_HUMID - DHT11_MIN_HUMID) / (MW33_MAX_HUMID - MW33_MIN_HUMID)
#define DHT11_MW33_COEF (float) 0.7777778
// Scaling
humDHT = (v - MW33_MIN_HUMID) * ( DHT11_MW33_COEF ) + DHT11_MIN_HUMID + MW33_HUMID_OFFSET;
}
//Adjustment
humDHT = (humDHT * DHT_ADJ_FACTOR) / 100;
if (humDHT > 95.0)
humDHT = 95.0;
}
// Update only when data is valid
HeatIndexDHT = 0;
if ((tempDHT != 0) && (humDHT != 0))
{
HeatIndexDHT = dht->computeHeatIndex(tempDHT, humDHT, false);
HeatIndexF_DHT = HeatIndexDHT * 1.8 + 32;
}
}
#define TESTING_DHT_BOUNDARY false
#if TESTING_DHT_BOUNDARY
static int testCount = 0;
tempDHT = testCount;
tempF_DHT = tempDHT * 1.8 + 32;
humDHT = (testCount + 5) % 100;
Serial.printf("tempDHT = %5.2f, tempF_DHT = %5.2, humDHT = %5.2f\n", tempDHT, tempF_DHT, humDHT);
testCount = (testCount + 3) % 100;
#endif
}
void set_led(byte status)
{
digitalWrite(PIN_LED, status);
}
void control_pump(boolean action)
{
#if RELAY_ACTIVE_HIGH
digitalWrite(PUMP_RELAY_PIN, action);
if (digitalRead(PUMP_RELAY_PIN) == HIGH)
#else
digitalWrite(PUMP_RELAY_PIN, !action);
if (digitalRead(PUMP_RELAY_PIN) == LOW)
#endif //RELAY_ACTIVE_HIGH
{
#if (DEBUG_LOOP > 1)
Serial.println("Turn Pump ON");
#endif
pumpStatus = RUN;
if (pumpModeNotice)
Blynk.notify("Pump ON");
}
else
{
#if (DEBUG_LOOP > 1)
Serial.println("Turn Pump OFF");
#endif
pumpStatus = STOP;
if (pumpModeNotice)
Blynk.notify("Pump OFF");
}
}
void addWater()
{
control_pump(RUN);
// TIME_PUMP_ON = 15s (selected from from 10-50s)
relay_ticker.once_ms(TIME_PUMP_ON, control_pump, STOP);
}
void controlMoisture(void)
{
if ((soilMoist < DRY_SOIL) && (soilMoist > 0 ))
{
#if (DEBUG_LOOP > 1)
Serial.printf("soilMoist = %5.2f < DRY_SOIL\n", soilMoist);
#endif
addWater();
}
#if (DEBUG_LOOP > 1)
else
Serial.printf("soilMoist = %5.2f => OK\n", soilMoist);
#endif
}
#if USE_DEEPSLEEP
#if USE_ESP32
//Function that prints 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();
#if (DEBUG_LOOP > 1)
Serial.print("Wakeup by ");
switch (wakeup_reason)
{
case 1 : Serial.println("RTC_IO"); break;
case 2 : Serial.println("RTC_CNTL"); break;
case 3 : Serial.println("timer"); break;
case 4 : Serial.println("touchpad"); break;
case 5 : Serial.println("ULP"); break;
default : Serial.println("not deepsleep"); break;
}
#endif
}
#endif
uint64_t uS_TO_SLEEP = 0;
void deepsleep()
{
if (TIME_TO_DEEPSLEEP > 0)
{
uS_TO_SLEEP = (uint64_t) TIME_TO_DEEPSLEEP * uS_TO_MIN_FACTOR;
#if 1 //(DEBUG_LOOP > 1)
Serial.printf("Blynk disconnecting.Start DeepSleep. TIME_TO_DEEPSLEEP = %ld, Time = %lld us\n", TIME_TO_DEEPSLEEP, uS_TO_SLEEP);
#endif