-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenSprinkler.cpp
2265 lines (2017 loc) · 63.8 KB
/
OpenSprinkler.cpp
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
/* OpenSprinkler Unified (AVR/RPI/BBB/LINUX/ESP8266) Firmware
* Copyright (C) 2015 by Ray Wang ([email protected])
*
* OpenSprinkler library
* Feb 2015 @ OpenSprinkler.com
*
* This file is part of the OpenSprinkler library
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#if !defined(ARDUINO)
#include <netdb.h>
#endif
#include "OpenSprinkler.h"
#include "server.h"
#include "gpio.h"
#include "images.h"
#include "testmode.h"
/** Declare static data members */
NVConData OpenSprinkler::nvdata;
ConStatus OpenSprinkler::status;
ConStatus OpenSprinkler::old_status;
byte OpenSprinkler::hw_type;
byte OpenSprinkler::nboards;
byte OpenSprinkler::nstations;
byte OpenSprinkler::station_bits[MAX_EXT_BOARDS+1];
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__) || defined(ESP8266)
byte OpenSprinkler::engage_booster;
uint16_t OpenSprinkler::baseline_current;
#endif
ulong OpenSprinkler::sensor_lasttime;
ulong OpenSprinkler::flowcount_log_start;
ulong OpenSprinkler::flowcount_rt;
volatile ulong OpenSprinkler::flowcount_time_ms;
ulong OpenSprinkler::raindelay_start_time;
byte OpenSprinkler::button_timeout;
ulong OpenSprinkler::checkwt_lasttime;
ulong OpenSprinkler::checkwt_success_lasttime;
ulong OpenSprinkler::powerup_lasttime;
byte OpenSprinkler::weather_update_flag;
char tmp_buffer[TMP_BUFFER_SIZE+1]; // scratch buffer
const char wtopts_filename[] PROGMEM = WEATHER_OPTS_FILENAME;
const char stns_filename[] PROGMEM = STATION_ATTR_FILENAME;
const char ifkey_filename[] PROGMEM = IFTTT_KEY_FILENAME;
#ifdef ESP8266
const char wifi_filename[] PROGMEM = WIFI_FILENAME;
byte OpenSprinkler::state = OS_STATE_INITIAL;
WiFiConfig OpenSprinkler::wifi_config = {WIFI_MODE_AP, "", ""};
IOEXP* OpenSprinkler::expanders[(MAX_EXT_BOARDS+1)/2];
IOEXP* OpenSprinkler::mainio;
IOEXP* OpenSprinkler::drio;
RCSwitch OpenSprinkler::rfswitch;
extern ESP8266WebServerSecure *wifi_server;
extern char ether_buffer[];
#endif
#if defined(ARDUINO) && !defined(ESP8266)
LiquidCrystal OpenSprinkler::lcd;
#include <SdFat.h>
extern SdFat sd;
#elif defined(ESP8266)
#include <FS.h>
SSD1306Display OpenSprinkler::lcd(0x3c, SDA, SCL);
#else
// todo: LCD define for Linux-based systems
#endif
#if defined(OSPI)
byte OpenSprinkler::pin_sr_data = PIN_SR_DATA;
#endif
/** Option json names (stored in progmem) */
// IMPORTANT: each json name is strictly 5 characters
// with 0 fillings if less
#define OP_JSON_NAME_STEPSIZE 5
const char op_json_names[] PROGMEM =
"fwv\0\0"
"tz\0\0\0"
"ntp\0\0"
"dhcp\0"
"ip1\0\0"
"ip2\0\0"
"ip3\0\0"
"ip4\0\0"
"gw1\0\0"
"gw2\0\0"
"gw3\0\0"
"gw4\0\0"
"hp0\0\0"
"hp1\0\0"
"hwv\0\0"
"ext\0\0"
"seq\0\0"
"sdt\0\0"
"mas\0\0"
"mton\0"
"mtof\0"
"urs\0\0" // todo: rename to sn1t
"rso\0\0" // todo: rename to sn1o
"wl\0\0\0"
"den\0\0"
"ipas\0"
"devid"
"con\0\0"
"lit\0\0"
"dim\0\0"
"bst\0\0"
"uwt\0\0"
"ntp1\0"
"ntp2\0"
"ntp3\0"
"ntp4\0"
"lg\0\0\0"
"mas2\0"
"mton2"
"mtof2"
"fwm\0\0"
"fpr0\0"
"fpr1\0"
"re\0\0\0"
"dns1\0"
"dns2\0"
"dns3\0"
"dns4\0"
"sar\0\0"
"ife\0\0"
"sn2t\0"
"sn2o\0"
"reset";
/** Option promopts (stored in progmem, for LCD display) */
// Each string is strictly 16 characters
// with SPACE fillings if less
const char op_prompts[] PROGMEM =
"Firmware version"
"Time zone (GMT):"
"Enable NTP sync?"
"Enable DHCP? "
"Static.ip1: "
"Static.ip2: "
"Static.ip3: "
"Static.ip4: "
"Gateway.ip1: "
"Gateway.ip2: "
"Gateway.ip3: "
"Gateway.ip4: "
"HTTP Port: "
"----------------"
"Hardware version"
"# of exp. board:"
"----------------"
"Stn. delay (sec)"
"Master 1 (Mas1):"
"Mas1 on adjust:"
"Mas1 off adjust:"
"Sensor 1 type: "
"Normally open? "
"Watering level: "
"Device enabled? "
"Ignore password?"
"Device ID: "
"LCD contrast: "
"LCD brightness: "
"LCD dimming: "
"DC boost time: "
"Weather algo.: "
"NTP server.ip1: "
"NTP server.ip2: "
"NTP server.ip3: "
"NTP server.ip4: "
"Enable logging? "
"Master 2 (Mas2):"
"Mas2 on adjust:"
"Mas2 off adjust:"
"Firmware minor: "
"Pulse rate: "
"----------------"
"As remote ext.? "
"DNS server.ip1: "
"DNS server.ip2: "
"DNS server.ip3: "
"DNS server.ip4: "
"Special Refresh?"
"IFTTT Enable: "
"Sensor 2 type: "
"Normally open? "
"Factory reset? ";
/** Option maximum values (stored in progmem) */
const char op_max[] PROGMEM = {
0,
108,
1,
1,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
0,
MAX_EXT_BOARDS,
1,
255,
MAX_NUM_STATIONS,
255,
255,
255,
1,
250,
1,
1,
255,
255,
255,
255,
250,
255,
255,
255,
255,
255,
1,
MAX_NUM_STATIONS,
255,
255,
0,
255,
255,
1,
255,
255,
255,
255,
1,
255,
255,
1,
1
};
/** Option values (stored in RAM) */
byte OpenSprinkler::options[] = {
OS_FW_VERSION, // firmware version
GEN3PETIMEZONE, // default time zone: GMT-5
1, // 0: disable NTP sync, 1: enable NTP sync
1, // 0: use static ip, 1: use dhcp
0, // this and next 3 bytes define static ip
0,
0,
0,
0, // this and next 3 bytes define static gateway ip
0,
0,
0,
#if defined(ARDUINO) // on AVR, the default HTTP port is 80
144, // this and next byte define http port number
31,
#else // on RPI/BBB/LINUX, the default HTTP port is 8080
144,// this and next byte define http port number
31,
#endif
OS_HW_VERSION,
0, // number of 8-station extension board. 0: no extension boards
1, // the option 'sequential' is now retired
120,// station delay time (-10 minutes to 10 minutes).
0, // index of master station. 0: no master station
120,// master on time adjusted time (-10 minutes to 10 minutes)
120,// master off adjusted time (-10 minutes to 10 minutes)
0x02, // 3PE - use flow sensor by default sensor 1 type (see SENSOR_TYPE macro defines)
0, // sensor 1 option. 0: normally closed; 1: normally open.
100,// water level (default 100%),
1, // device enable
0, // 1: ignore password; 0: use password
0, // device id
150,// lcd contrast
100,// lcd backlight
50, // lcd dimming
80, // boost time (only valid to DC and LATCH type)
0, // weather algorithm (0 means not using weather algorithm)
50, // this and the next three bytes define the ntp server ip
97,
210,
169,
1, // enable logging: 0: disable; 1: enable.
0, // index of master2. 0: no master2 station
120,// master2 on adjusted time
120,// master2 off adjusted time
OS_FW_MINOR, // firmware minor version
100,// this and next byte define flow pulse rate (100x)
0, // default is 1.00 (100)
0, // set as remote extension
8, // this and the next three bytes define the custom dns server ip
8,
8,
8,
0, // special station auto refresh
0, // ifttt enable bits
0, // sensor 2 type
0, // sensor 2 option. 0: normally closed; 1: normally open.
0 // reset
};
static const uint8_t rsakey[] PROGMEM = {
#include "key.h"
};
static const uint8_t x509[] PROGMEM = {
#include "x509.h"
};
/** Weekday strings (stored in progmem, for LCD display) */
static const char days_str[] PROGMEM =
"Mon\0"
"Tue\0"
"Wed\0"
"Thu\0"
"Fri\0"
"Sat\0"
"Sun\0";
/** Calculate local time (UTC time plus time zone offset) */
time_t OpenSprinkler::now_tz() {
return now()+(int32_t)3600/4*(int32_t)(options[OPTION_TIMEZONE]-48);
}
#if defined(ARDUINO) // AVR network init functions
bool detect_i2c(int addr) {
Wire.beginTransmission(addr);
return (Wire.endTransmission()==0);
}
/** read hardware MAC */
#define MAC_CTRL_ID 0x50
bool OpenSprinkler::read_hardware_mac() {
#ifdef ESP8266
WiFi.macAddress((byte*)tmp_buffer);
return true;
#else
uint8_t ret;
ret = detect_i2c(MAC_CTRL_ID);
if (ret) return false;
Wire.beginTransmission(MAC_CTRL_ID);
Wire.write(0xFA); // The address of the register we want
Wire.endTransmission(); // Send the data
if(Wire.requestFrom(MAC_CTRL_ID, 6) != 6) return false; // Request 6 bytes from the EEPROM
for (ret=0;ret<6;ret++) {
tmp_buffer[ret] = Wire.read();
}
return true;
#endif
}
void(* resetFunc) (void) = 0; // AVR software reset function
/** Initialize network with the given mac address and http port */
byte OpenSprinkler::start_network() {
#ifdef ESP8266
lcd_print_line_clear_pgm(PSTR("Starting..."), 1);
if(wifi_server) delete wifi_server;
if(get_wifi_mode()==WIFI_MODE_AP) {
wifi_server = new ESP8266WebServerSecure(8080);
} else {
uint16_t httpport = (uint16_t)(options[OPTION_HTTPPORT_1]<<8) + (uint16_t)options[OPTION_HTTPPORT_0];
wifi_server = new ESP8266WebServerSecure(httpport);
}
wifi_server->setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
status.has_hwmac = 1;
#else
lcd_print_line_clear_pgm(PSTR("Connecting..."), 1);
// new from 2.2: read hardware MAC
if(!read_hardware_mac())
{
// if no hardware MAC exists, use software MAC
tmp_buffer[0] = 0x00;
tmp_buffer[1] = 0x69;
tmp_buffer[2] = 0x69;
tmp_buffer[3] = 0x2D;
tmp_buffer[4] = 0x31;
tmp_buffer[5] = options[OPTION_DEVICE_ID];
} else {
// has hardware MAC chip
status.has_hwmac = 1;
}
if(!ether.begin(ETHER_BUFFER_SIZE, (uint8_t*)tmp_buffer, PIN_ETHER_CS)) return 0;
// calculate http port number
ether.hisport = (unsigned int)(options[OPTION_HTTPPORT_1]<<8) + (unsigned int)options[OPTION_HTTPPORT_0];
if (options[OPTION_USE_DHCP]) {
// set up DHCP
// register with domain name "OS-xx" where xx is the last byte of the MAC address
if (!ether.dhcpSetup()) return 0;
// once we have valid DHCP IP, we write these into static IP / gateway IP
memcpy(options+OPTION_STATIC_IP1, ether.myip, 4);
memcpy(options+OPTION_GATEWAY_IP1, ether.gwip,4);
memcpy(options+OPTION_DNS_IP1, ether.dnsip, 4);
options_save();
} else {
// set up static IP
byte *staticip = options+OPTION_STATIC_IP1;
byte *gateway = options+OPTION_GATEWAY_IP1;
byte *dns = options+OPTION_DNS_IP1;
if (!ether.staticSetup(staticip, gateway, dns)) return 0;
}
#endif
return 1;
}
/** Reboot controller */
void OpenSprinkler::reboot_dev() {
lcd_print_line_clear_pgm(PSTR("Rebooting..."), 0);
#ifdef ESP8266
//ESP.restart(); Commented by [email protected] on 27 June 2019
#else
resetFunc();
#endif
}
#else // RPI/BBB/LINUX network init functions
#include "etherport.h"
#include <sys/reboot.h>
#include <stdlib.h>
#include "utils.h"
#include "server.h"
extern EthernetServer *m_server;
extern char ether_buffer[];
/** Initialize network with the given mac address and http port */
byte OpenSprinkler::start_network() {
unsigned int port = (unsigned int)(options[OPTION_HTTPPORT_1]<<8) + (unsigned int)options[OPTION_HTTPPORT_0];
#if defined(DEMO)
port = 80;
#endif
if(m_server) {
delete m_server;
m_server = 0;
}
m_server = new EthernetServer(port);
return m_server->begin();
}
/** Reboot controller */
void OpenSprinkler::reboot_dev() {
#if defined(DEMO)
// do nothing
#else
sync(); // add sync to prevent file corruption
reboot(RB_AUTOBOOT);
#endif
}
/** Launch update script */
void OpenSprinkler::update_dev() {
char cmd[1024];
sprintf(cmd, "cd %s & ./updater.sh", get_runtime_path());
system(cmd);
}
#endif // end network init functions
#if defined(ARDUINO)
/** Initialize LCD */
void OpenSprinkler::lcd_start() {
#ifdef ESP8266
// initialize SSD1306
lcd.init();
lcd.begin();
flash_screen();
#else
// initialize 16x2 character LCD
// turn on lcd
lcd.init(1, PIN_LCD_RS, 255, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7, 0,0,0,0);
lcd.begin();
if (lcd.type() == LCD_STD) {
// this is standard 16x2 LCD
// set PWM frequency for adjustable LCD backlight and contrast
#if OS_HW_VERSION==(OS_HW_VERSION_BASE+20) || OS_HW_VERSION==(OS_HW_VERSION_BASE+21) // 8MHz and 12MHz
TCCR1B = 0x01;
#else // 16MHz
TCCR1B = 0x02; // increase division factor for faster clock
#endif
// turn on LCD backlight and contrast
lcd_set_brightness();
lcd_set_contrast();
} else {
// for I2C LCD, we don't need to do anything
}
#endif
}
#endif
extern void flow_isr();
/** Initialize pins, controller variables, LCD */
void OpenSprinkler::begin() {
#if defined(ARDUINO)
Wire.begin(); // init I2C
#endif
hw_type = HW_TYPE_UNKNOWN;
#ifdef ESP8266
if(detect_i2c(ACDR_I2CADDR)) hw_type = HW_TYPE_AC;
else if(detect_i2c(DCDR_I2CADDR)) hw_type = HW_TYPE_DC;
else if(detect_i2c(LADR_I2CADDR)) hw_type = HW_TYPE_LATCH;
/* detect hardware revision type */
if(detect_i2c(MAIN_I2CADDR)) { // check if main PCF8574 exists
/* assign revision 0 pins */
PIN_BUTTON_1 = V0_PIN_BUTTON_1;
PIN_BUTTON_2 = V0_PIN_BUTTON_2;
PIN_BUTTON_3 = V0_PIN_BUTTON_3;
PIN_RFRX = V0_PIN_RFRX;
PIN_RFTX = V0_PIN_RFTX;
PIN_BOOST = V0_PIN_BOOST;
PIN_BOOST_EN = V0_PIN_BOOST_EN;
PIN_SENSOR1 = V0_PIN_SENSOR1;
PIN_SENSOR2 = V0_PIN_SENSOR2;
PIN_RAINSENSOR = V0_PIN_RAINSENSOR;
PIN_FLOWSENSOR = V0_PIN_FLOWSENSOR;
// on revision 0, main IOEXP and driver IOEXP are two separate PCF8574 chips
if(hw_type==HW_TYPE_DC) {
drio = new PCF8574(DCDR_I2CADDR);
} else if(hw_type==HW_TYPE_LATCH) {
drio = new PCF8574(LADR_I2CADDR);
} else {
drio = new PCF8574(ACDR_I2CADDR);
}
mainio = new PCF8574(MAIN_I2CADDR);
mainio->i2c_write(0, 0x0F); // set lower four bits of main PCF8574 (8-ch) to high
/*pcf_write(MAIN_I2CADDR, 0x0F);*/
digitalWriteExt(V0_PIN_PWR_TX, 1); // turn on TX power
digitalWriteExt(V0_PIN_PWR_RX, 1); // turn on RX power
pinModeExt(PIN_BUTTON_2, INPUT_PULLUP);
digitalWriteExt(PIN_BOOST, LOW);
digitalWriteExt(PIN_BOOST_EN, LOW);
digitalWriteExt(PIN_LATCH_COM, LOW);
} else {
/* assign revision 1 pins */
PIN_BUTTON_1 = V1_PIN_BUTTON_1;
PIN_BUTTON_2 = V1_PIN_BUTTON_2;
PIN_BUTTON_3 = V1_PIN_BUTTON_3;
PIN_RFRX = V1_PIN_RFRX;
PIN_RFTX = V1_PIN_RFTX;
PIN_IOEXP_INT = V1_PIN_IOEXP_INT;
PIN_BOOST = V1_PIN_BOOST;
PIN_BOOST_EN = V1_PIN_BOOST_EN;
PIN_LATCH_COM = V1_PIN_LATCH_COM;
PIN_SENSOR1 = V1_PIN_SENSOR1;
PIN_SENSOR2 = V1_PIN_SENSOR2;
PIN_RAINSENSOR = V1_PIN_RAINSENSOR;
PIN_FLOWSENSOR = V1_PIN_FLOWSENSOR;
if(hw_type==HW_TYPE_DC) {
drio = new PCA9555(DCDR_I2CADDR);
} else if(hw_type==HW_TYPE_LATCH) {
drio = new PCA9555(LADR_I2CADDR);
} else {
drio = new PCA9555(ACDR_I2CADDR);
}
// on revision 1, main IOEXP and driver IOEXP are combined into one single PCA9555 (16-ch) chip
mainio = drio;
mainio->i2c_write(NXP_CONFIG_REG, V1_IO_CONFIG);
mainio->i2c_write(NXP_OUTPUT_REG, V1_IO_OUTPUT);
}
for(byte i=0;i<(MAX_EXT_BOARDS+1)/2;i++)
expanders[i] = NULL;
detect_expanders();
#else
// shift register setup
pinMode(PIN_SR_OE, OUTPUT);
// pull shift register OE high to disable output
digitalWrite(PIN_SR_OE, HIGH);
pinMode(PIN_SR_LATCH, OUTPUT);
digitalWrite(PIN_SR_LATCH, HIGH);
pinMode(PIN_SR_CLOCK, OUTPUT);
#if defined(OSPI)
pin_sr_data = PIN_SR_DATA;
// detect RPi revision
unsigned int rev = detect_rpi_rev();
if (rev==0x0002 || rev==0x0003)
pin_sr_data = PIN_SR_DATA_ALT;
// if this is revision 1, use PIN_SR_DATA_ALT
pinMode(pin_sr_data, OUTPUT);
#else
pinMode(PIN_SR_DATA, OUTPUT);
#endif
#endif
// Reset all stations
clear_all_station_bits();
apply_all_station_bits();
#ifdef ESP8266
pinModeExt(PIN_SENSOR1, INPUT_PULLUP);
pinModeExt(PIN_SENSOR2, INPUT_PULLUP);
#else
// pull shift register OE low to enable output
digitalWrite(PIN_SR_OE, LOW);
// Rain sensor port set up
pinMode(PIN_RAINSENSOR, INPUT_PULLUP);
#endif
// Set up sensors
#if defined(ARDUINO)
#ifdef ESP8266
/* todo: handle two sensors */
if(mainio->type==IOEXP_TYPE_8574) {
attachInterrupt(PIN_FLOWSENSOR, flow_isr, FALLING);
} else if(mainio->type==IOEXP_TYPE_9555) {
mainio->i2c_read(NXP_INPUT_REG); // do a read to clear out current interrupt flag
attachInterrupt(PIN_IOEXP_INT, flow_isr, FALLING);
}
#else
//digitalWrite(PIN_RAINSENSOR, HIGH); // enabled internal pullup on rain sensor
attachInterrupt(PIN_FLOWSENSOR_INT, flow_isr, FALLING);
#endif
#else
// OSPI and OSBO use external pullups
attachInterrupt(PIN_FLOWSENSOR, "falling", flow_isr);
#endif
// Default controller status variables
// Static variables are assigned 0 by default
// so only need to initialize non-zero ones
status.enabled = 1;
status.safe_reboot = 0;
old_status = status;
nvdata.sunrise_time = 360; // 6:00am default sunrise
nvdata.sunset_time = 1080; // 6:00pm default sunset
nboards = 1;
nstations = 8;
// set rf data pin
pinModeExt(PIN_RFTX, OUTPUT);
digitalWriteExt(PIN_RFTX, LOW);
#if defined(ARDUINO) // AVR SD and LCD functions
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__) // OS 2.3 specific detections
uint8_t ret;
// detect hardware type
ret = detect_i2c(MAC_CTRL_ID);
if (!ret) {
Wire.requestFrom(MAC_CTRL_ID, 1);
ret = Wire.read();
if (ret == HW_TYPE_AC || ret == HW_TYPE_DC || ret == HW_TYPE_LATCH) {
hw_type = ret;
} else {
// hardware type is not assigned
}
}
if (hw_type == HW_TYPE_DC) {
pinMode(PIN_BOOST, OUTPUT);
digitalWrite(PIN_BOOST, LOW);
pinMode(PIN_BOOST_EN, OUTPUT);
digitalWrite(PIN_BOOST_EN, LOW);
}
// detect if current sensing pin is present
pinMode(PIN_CURR_DIGITAL, INPUT);
digitalWrite(PIN_CURR_DIGITAL, HIGH); // enable internal pullup
status.has_curr_sense = digitalRead(PIN_CURR_DIGITAL) ? 0 : 1;
digitalWrite(PIN_CURR_DIGITAL, LOW);
baseline_current = 0;
#elif defined(ESP8266) // OS3.0 specific detections
status.has_curr_sense = 1; // OS3.0 has current sensing capacility
// measure baseline current
baseline_current = 100;
#endif
lcd_start();
#if !defined(ESP8266)
// define lcd custom icons
byte _icon[8];
// WiFi icon
_icon[0] = B00000;
_icon[1] = B10100;
_icon[2] = B01000;
_icon[3] = B10101;
_icon[4] = B00001;
_icon[5] = B00101;
_icon[6] = B00101;
_icon[7] = B10101;
lcd.createChar(1, _icon);
_icon[1]=0;
_icon[2]=0;
_icon[3]=1;
lcd.createChar(0, _icon);
// uSD card icon
_icon[1] = B00000;
_icon[2] = B11111;
_icon[3] = B10001;
_icon[4] = B11111;
_icon[5] = B10001;
_icon[6] = B10011;
_icon[7] = B11110;
lcd.createChar(2, _icon);
// Rain icon
_icon[2] = B00110;
_icon[3] = B01001;
_icon[4] = B11111;
_icon[5] = B00000;
_icon[6] = B10101;
_icon[7] = B10101;
lcd.createChar(3, _icon);
// Connect icon
_icon[2] = B00111;
_icon[3] = B00011;
_icon[4] = B00101;
_icon[5] = B01000;
_icon[6] = B10000;
_icon[7] = B00000;
lcd.createChar(4, _icon);
// Remote extension icon
_icon[2] = B00000;
_icon[3] = B10001;
_icon[4] = B01011;
_icon[5] = B00101;
_icon[6] = B01001;
_icon[7] = B11110;
lcd.createChar(5, _icon);
// Flow sensor icon
_icon[2] = B00000;
_icon[3] = B11010;
_icon[4] = B10010;
_icon[5] = B11010;
_icon[6] = B10011;
_icon[7] = B00000;
lcd.createChar(6, _icon);
// Program switch icon
_icon[1] = B11100;
_icon[2] = B10100;
_icon[3] = B11100;
_icon[4] = B10010;
_icon[5] = B10110;
_icon[6] = B00010;
_icon[7] = B00111;
lcd.createChar(7, _icon);
// set sd cs pin high to release SD
pinMode(PIN_SD_CS, OUTPUT);
digitalWrite(PIN_SD_CS, HIGH);
if(sd.begin(PIN_SD_CS, SPI_HALF_SPEED)) {
status.has_sd = 1;
}
#else
/* create custom characters */
lcd.createChar(0, _iconimage_connected);
lcd.createChar(1, _iconimage_disconnected);
lcd.createChar(2, _iconimage_sdcard);
lcd.createChar(3, _iconimage_rain);
lcd.createChar(4, _iconimage_connect);
lcd.createChar(5, _iconimage_remotext);
lcd.createChar(6, _iconimage_flow);
lcd.createChar(7, _iconimage_pswitch);
lcd.setCursor(0,0);
lcd.print(F("Init file system"));
lcd.setCursor(0,1);
if(!SPIFFS.begin()) {
DEBUG_PRINTLN(F("SPIFFS failed"));
status.has_sd = 0;
} else {
status.has_sd = 1;
}
state = OS_STATE_INITIAL;
#endif
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__)
if(!status.has_sd) {
lcd.setCursor(0, 0);
lcd_print_pgm(PSTR("Error Code: 0x2D"));
while(1){}
}
#endif
// set button pins
// enable internal pullup
pinMode(PIN_BUTTON_1, INPUT_PULLUP);
pinMode(PIN_BUTTON_2, INPUT_PULLUP);
pinMode(PIN_BUTTON_3, INPUT_PULLUP);
// detect and check RTC type
RTC.detect();
#else
status.has_sd = 1;
DEBUG_PRINTLN(get_runtime_path());
#endif
}
/** Apply all station bits
* !!! This will activate/deactivate valves !!!
*/
void OpenSprinkler::apply_all_station_bits() {
#ifdef ESP8266
// Handle DC booster
if((hw_type==HW_TYPE_DC) && engage_booster) {
// for DC controller: boost voltage
digitalWriteExt(PIN_BOOST_EN, LOW); // disable output path
digitalWriteExt(PIN_BOOST, HIGH); // enable boost converter
delay((int)options[OPTION_BOOST_TIME]<<2); // wait for booster to charge
digitalWriteExt(PIN_BOOST, LOW); // disable boost converter
digitalWriteExt(PIN_BOOST_EN, HIGH); // enable output path
engage_booster = 0;
}
if(drio->type==IOEXP_TYPE_8574) {
/* revision 0 uses PCF8574 with active low logic, so all bits must be flipped */
drio->i2c_write(NXP_OUTPUT_REG, ~station_bits[0]);
} else if(drio->type==IOEXP_TYPE_9555) {
/* revision 1 uses PCA9555 with active high logic */
uint16_t reg = drio->i2c_read(NXP_OUTPUT_REG); // read current output reg value
reg = (reg&0xFF00) | station_bits[0]; // output channels are the low 8-bit
drio->i2c_write(NXP_OUTPUT_REG, reg); // write value to register
}
for(int i=0;i<MAX_EXT_BOARDS/2;i++) {
uint16_t data = station_bits[i*2+2];
data = (data<<8) + station_bits[i*2+1];
if(expanders[i]->type==IOEXP_TYPE_9555) {
expanders[i]->i2c_write(NXP_OUTPUT_REG, data);
} else {
expanders[i]->i2c_write(NXP_OUTPUT_REG, ~data);
//pcf_write16(EXP_I2CADDR_BASE+i, ~data);
}
}
/*if((hw_type==HW_TYPE_DC) && engage_booster) {
// for DC controller: enable output path
}*/
byte bid, s, sbits;
#else
digitalWrite(PIN_SR_LATCH, LOW);
byte bid, s, sbits;
// Shift out all station bit values
// from the highest bit to the lowest
for(bid=0;bid<=MAX_EXT_BOARDS;bid++) {
if (status.enabled)
sbits = station_bits[MAX_EXT_BOARDS-bid];
else
sbits = 0;
for(s=0;s<8;s++) {
digitalWrite(PIN_SR_CLOCK, LOW);
#if defined(OSPI) // if OSPI, use dynamically assigned pin_sr_data
digitalWrite(pin_sr_data, (sbits & ((byte)1<<(7-s))) ? HIGH : LOW );
#else
digitalWrite(PIN_SR_DATA, (sbits & ((byte)1<<(7-s))) ? HIGH : LOW );
#endif
digitalWrite(PIN_SR_CLOCK, HIGH);
}
}
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__)
if((hw_type==HW_TYPE_DC) && engage_booster) {
// for DC controller: boost voltage
digitalWrite(PIN_BOOST_EN, LOW); // disable output path
digitalWrite(PIN_BOOST, HIGH); // enable boost converter
delay((int)options[OPTION_BOOST_TIME]<<2); // wait for booster to charge
digitalWrite(PIN_BOOST, LOW); // disable boost converter
digitalWrite(PIN_BOOST_EN, HIGH); // enable output path
digitalWrite(PIN_SR_LATCH, HIGH);
engage_booster = 0;
} else {
digitalWrite(PIN_SR_LATCH, HIGH);
}
#else
digitalWrite(PIN_SR_LATCH, HIGH);
#endif
#endif
if(options[OPTION_SPE_AUTO_REFRESH]) {
// handle refresh of RF and remote stations
// each time apply_all_station_bits is called
// we refresh the station whose index is the current time modulo MAX_NUM_STATIONS
static byte last_sid = 0;
byte sid = now() % MAX_NUM_STATIONS;
if (sid != last_sid) { // avoid refreshing the same station twice in a roll
last_sid = sid;
bid=sid>>3;
s=sid&0x07;
switch_special_station(sid, (station_bits[bid]>>s)&0x01);
}
}
}
/** Read rain sensor status */
void OpenSprinkler::rainsensor_status() {
// options[OPTION_RS_TYPE]: 0 if normally closed, 1 if normally open
if(options[OPTION_SENSOR1_TYPE]!=SENSOR_TYPE_RAIN) return;
status.rain_sensed = (digitalReadExt(PIN_RAINSENSOR) == options[OPTION_SENSOR1_OPTION] ? 0 : 1);
}
/** Return program switch status */
bool OpenSprinkler::programswitch_status(ulong curr_time) {
if(options[OPTION_SENSOR1_TYPE]!=SENSOR_TYPE_PSWITCH) return false;
static ulong keydown_time = 0;
byte val = digitalReadExt(PIN_RAINSENSOR);
if(!val && !keydown_time) keydown_time = curr_time;
else if(val && keydown_time && (curr_time > keydown_time)) {
keydown_time = 0;
return true;
}
return false;
}
/** Read current sensing value
* OpenSprinkler 2.3 and above have a 0.2 ohm current sensing resistor.
* Therefore the conversion from analog reading to milli-amp is:
* (r/1024)*3.3*1000/0.2 (DC-powered controller)
* AC-powered controller has a built-in precision rectifier to sense
* the peak AC current. Therefore the actual current is discounted by 0.707
* ESP8266's analog reference voltage is 1.0 instead of 3.3, therefore
* it's further discounted by 1/3.3
*/
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__) || defined(ESP8266)
uint16_t OpenSprinkler::read_current() {
float scale = 1.0f;
if(status.has_curr_sense) {
if (hw_type == HW_TYPE_DC) {
#if defined(ESP8266)
scale = 4.88;
#else
scale = 16.11;
#endif
} else {
#if defined(ESP8266)
scale = 3.45;
#else
scale = 11.39;