forked from OpenSprinkler/OpenSprinkler-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenSprinkler.cpp
2643 lines (2370 loc) · 71.5 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/>.
*/
#include "OpenSprinkler.h"
#include "opensprinkler_server.h"
#include "gpio.h"
#include "testmode.h"
/** Declare static data members */
OSMqtt OpenSprinkler::mqtt;
NVConData OpenSprinkler::nvdata;
ConStatus OpenSprinkler::status;
ConStatus OpenSprinkler::old_status;
byte OpenSprinkler::hw_type;
byte OpenSprinkler::hw_rev;
byte OpenSprinkler::nboards;
byte OpenSprinkler::nstations;
byte OpenSprinkler::station_bits[MAX_NUM_BOARDS];
byte OpenSprinkler::engage_booster;
uint16_t OpenSprinkler::baseline_current;
ulong OpenSprinkler::sensor1_on_timer;
ulong OpenSprinkler::sensor1_off_timer;
ulong OpenSprinkler::sensor1_active_lasttime;
ulong OpenSprinkler::sensor2_on_timer;
ulong OpenSprinkler::sensor2_off_timer;
ulong OpenSprinkler::sensor2_active_lasttime;
ulong OpenSprinkler::raindelay_on_lasttime;
ulong OpenSprinkler::flowcount_log_start;
ulong OpenSprinkler::flowcount_rt;
byte OpenSprinkler::button_timeout;
ulong OpenSprinkler::checkwt_lasttime;
ulong OpenSprinkler::checkwt_success_lasttime;
ulong OpenSprinkler::powerup_lasttime;
uint8_t OpenSprinkler::last_reboot_cause = REBOOT_CAUSE_NONE;
byte OpenSprinkler::weather_update_flag;
// todo future: the following attribute bytes are for backward compatibility
byte OpenSprinkler::attrib_mas[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_igs[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_mas2[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_igs2[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_igrd[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_dis[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_seq[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_spe[MAX_NUM_BOARDS];
extern char tmp_buffer[];
extern char ether_buffer[];
#if defined(ESP8266)
SSD1306Display OpenSprinkler::lcd(0x3c, SDA, SCL);
byte OpenSprinkler::state = OS_STATE_INITIAL;
byte OpenSprinkler::prev_station_bits[MAX_NUM_BOARDS];
IOEXP* OpenSprinkler::expanders[MAX_NUM_BOARDS/2];
IOEXP* OpenSprinkler::mainio; // main controller IO expander object
IOEXP* OpenSprinkler::drio; // driver board IO expander object
RCSwitch OpenSprinkler::rfswitch;
String OpenSprinkler::wifi_ssid="";
String OpenSprinkler::wifi_pass="";
byte OpenSprinkler::wifi_testmode = 0;
#elif defined(ARDUINO)
LiquidCrystal OpenSprinkler::lcd;
extern SdFat sd;
#else
#if defined(OSPI)
byte OpenSprinkler::pin_sr_data = PIN_SR_DATA;
#endif
// todo future: LCD define for Linux-based systems
#endif
/** Option json names (stored in PROGMEM to reduce RAM usage) */
// IMPORTANT: each json name is strictly 5 characters
// with 0 fillings if less
#define OP_JSON_NAME_STEPSIZE 5
// for Integer options
const char iopt_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"
"rso\0\0"
"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"
"sn1t\0"
"sn1o\0"
"sn2t\0"
"sn2o\0"
"sn1on"
"sn1of"
"sn2on"
"sn2of"
"subn1"
"subn2"
"subn3"
"subn4"
"wimod"
"reset"
;
// for String options
/*
const char sopt_json_names[] PROGMEM =
"dkey\0"
"loc\0\0"
"jsp\0\0"
"wsp\0\0"
"wtkey"
"wto\0\0"
"ifkey"
"ssid\0"
"pass\0"
"mqtt\0"
"apass";
*/
/** Option promopts (stored in PROGMEM to reduce RAM usage) */
// Each string is strictly 16 characters
// with SPACE fillings if less
const char iopt_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:"
"----------------"
"----------------"
"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 1 type: "
"Normally open? "
"Sensor 2 type: "
"Normally open? "
"Sn1 on adjust: "
"Sn1 off adjust: "
"Sn2 on adjust: "
"Sn2 off adjust: "
"Subnet mask1: "
"Subnet mask2: "
"Subnet mask3: "
"Subnet mask4: "
"WiFi mode? "
"Factory reset? ";
// string options do not have prompts
/** Option maximum values (stored in PROGMEM to reduce RAM usage) */
const byte iopt_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,
255,
1,
255,
255,
255,
255,
255,
255,
255,
255,
255,
1
};
// string options do not have maximum values
/** Integer option values (stored in RAM) */
byte OpenSprinkler::iopts[] = {
OS_FW_VERSION, // firmware version
28, // 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
80, // this and next byte define http port number
0,
#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)
0, // urs (retired)
0, // rso (retired)
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)
0, // this and the next three bytes define the ntp server ip
0,
0,
0,
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 1 type (see SENSOR_TYPE macro defines)
1, // sensor 1 option. 0: normally closed; 1: normally open. default 1.
0, // sensor 2 type
1, // sensor 2 option. 0: normally closed; 1: normally open. default 1.
0, // sensor 1 on delay
0, // sensor 1 off delay
0, // sensor 2 on delay
0, // sensor 2 off delay
255,// subnet mask 1
255,// subnet mask 2
255,// subnet mask 3
0,
WIFI_MODE_AP, // wifi mode
0 // reset
};
/** String option values (stored in RAM) */
const char *OpenSprinkler::sopts[] = {
DEFAULT_PASSWORD,
DEFAULT_LOCATION,
DEFAULT_JAVASCRIPT_URL,
DEFAULT_WEATHER_URL,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING
};
/** Weekday strings (stored in PROGMEM to reduce RAM usage) */
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)(iopts[IOPT_TIMEZONE]-48);
}
#if defined(ARDUINO) // AVR network init functions
bool detect_i2c(int addr) {
Wire.beginTransmission(addr);
return (Wire.endTransmission()==0); // successful if received 0
}
/** read hardware MAC into tmp_buffer */
#define MAC_CTRL_ID 0x50
bool OpenSprinkler::load_hardware_mac(byte* buffer, bool wired) {
#if defined(ESP8266)
WiFi.macAddress((byte*)buffer);
// if requesting wired Ethernet MAC, flip the last byte to create a modified MAC
if(wired) buffer[5] = ~buffer[5];
return true;
#else
// initialize the buffer by assigning software mac
buffer[0] = 0x00;
buffer[1] = 0x69;
buffer[2] = 0x69;
buffer[3] = 0x2D;
buffer[4] = 0x31;
buffer[5] = iopts[IOPT_DEVICE_ID];
if (detect_i2c(MAC_CTRL_ID)==false) 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; // if not enough data, return false
for(byte ret=0;ret<6;ret++) {
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() {
lcd_print_line_clear_pgm(PSTR("Starting..."), 1);
uint16_t httpport = (uint16_t)(iopts[IOPT_HTTPPORT_1]<<8) + (uint16_t)iopts[IOPT_HTTPPORT_0];
if(m_server) { delete m_server; m_server = NULL; }
if (start_ether()) {
m_server = new EthernetServer(httpport);
m_server->begin();
//udp = new EthernetUDP();
//udp->begin((httpport==8000) ? 8888 : 8000); // start udp on a different port than httpport
#if defined(ESP8266)
// turn off WiFi when ether is active
// todo: add option to keep both ether and wifi active
WiFi.mode(WIFI_OFF);
#endif
return 1;
} else {
#if defined(ESP8266)
if(wifi_server) { delete wifi_server; wifi_server = NULL; }
if(get_wifi_mode()==WIFI_MODE_AP) {
wifi_server = new ESP8266WebServer(80);
} else {
wifi_server = new ESP8266WebServer(httpport);
}
//udp = new WiFiUDP();
//udp->begin((httpport==8000) ? 8888 : 8000); // start udp on a different port than httpport
return 1;
#endif
}
return 0;
}
byte OpenSprinkler::start_ether() {
#if defined(ESP8266)
if(hw_rev<2) return 0; // ethernet capability is only available after hw_rev 2
#endif
Ethernet.init(PIN_ETHER_CS); // make sure to call this before any Ethernet calls
if(Ethernet.hardwareStatus()==EthernetNoHardware) return 0;
load_hardware_mac((uint8_t*)tmp_buffer, true);
lcd_print_line_clear_pgm(PSTR("Start wired link"), 1);
if (iopts[IOPT_USE_DHCP]) {
if(!Ethernet.begin((uint8_t*)tmp_buffer)) return 0;
memcpy(iopts+IOPT_STATIC_IP1, &(Ethernet.localIP()[0]), 4);
memcpy(iopts+IOPT_GATEWAY_IP1, &(Ethernet.gatewayIP()[0]),4);
memcpy(iopts+IOPT_DNS_IP1, &(Ethernet.dnsServerIP()[0]), 4);
memcpy(iopts+IOPT_SUBNET_MASK1, &(Ethernet.subnetMask()[0]), 4);
iopts_save();
} else {
IPAddress staticip(iopts+IOPT_STATIC_IP1);
IPAddress gateway(iopts+IOPT_GATEWAY_IP1);
IPAddress dns(iopts+IOPT_DNS_IP1);
IPAddress subn(iopts+IOPT_SUBNET_MASK1);
Ethernet.begin((uint8_t*)tmp_buffer, staticip, dns, gateway, subn);
}
return 1;
}
bool OpenSprinkler::network_connected(void) {
#if defined (ESP8266)
if(m_server) {
return (Ethernet.linkStatus()==LinkON);
} else {
return (get_wifi_mode()==WIFI_MODE_STA && WiFi.status()==WL_CONNECTED && state==OS_STATE_CONNECTED);
}
#else
return (Ethernet.linkStatus()==LinkON);
#endif
}
/** Reboot controller */
void OpenSprinkler::reboot_dev(uint8_t cause) {
lcd_print_line_clear_pgm(PSTR("Rebooting..."), 0);
if(cause) {
nvdata.reboot_cause = cause;
nvdata_save();
}
#if defined(ESP8266)
ESP.restart();
//ESP.reset();
#else
resetFunc();
#endif
}
#else // RPI/BBB/LINUX network init functions
#include "etherport.h"
#include <sys/reboot.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "utils.h"
#include "opensprinkler_server.h"
/** Initialize network with the given mac address and http port */
byte OpenSprinkler::start_network() {
unsigned int port = (unsigned int)(iopts[IOPT_HTTPPORT_1]<<8) + (unsigned int)iopts[IOPT_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();
}
bool OpenSprinkler::network_connected(void) {
return true;
}
// Return mac of first recognised interface and fallback to software mac
// Note: on OSPi, operating system handles interface allocation so 'wired' ignored
bool OpenSprinkler::load_hardware_mac(byte* mac, bool wired) {
const char * if_names[] = { "eth0", "eth1", "wlan0", "wlan1" };
struct ifreq ifr;
int fd;
// Fallback to asoftware mac if interface not recognised
mac[0] = 0x00;
mac[1] = 0x69;
mac[2] = 0x69;
mac[3] = 0x2D;
mac[4] = 0x31;
mac[5] = iopts[IOPT_DEVICE_ID];
if (m_server == NULL) return true;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return true;
// Returns the mac address of the first interface if multiple active
for (int i = 0; i < sizeof(if_names)/sizeof(const char *); i++) {
strncpy(ifr.ifr_name, if_names[i], sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFHWADDR, &ifr) != -1) {
memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
break;
}
}
close(fd);
return true;
}
/** Reboot controller */
void OpenSprinkler::reboot_dev(uint8_t cause) {
nvdata.reboot_cause = cause;
nvdata_save();
#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[1000];
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() {
#if defined(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
TCCR1B = 0x02; // increase division factor for faster clock
// 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;
hw_rev = 0;
#if defined(ESP8266)
/* check hardware type */
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;
// 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
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 {
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);
}
mainio = drio;
pinMode(16, INPUT);
if(digitalRead(16)==LOW) {
// revision 1
hw_rev = 1;
mainio->i2c_write(NXP_CONFIG_REG, V1_IO_CONFIG);
mainio->i2c_write(NXP_OUTPUT_REG, V1_IO_OUTPUT);
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;
} else {
// revision 2
hw_rev = 2;
mainio->i2c_write(NXP_CONFIG_REG, V2_IO_CONFIG);
mainio->i2c_write(NXP_OUTPUT_REG, V2_IO_OUTPUT);
PIN_BUTTON_1 = V2_PIN_BUTTON_1;
PIN_BUTTON_2 = V2_PIN_BUTTON_2;
PIN_BUTTON_3 = V2_PIN_BUTTON_3;
PIN_RFTX = V2_PIN_RFTX;
PIN_BOOST = V2_PIN_BOOST;
PIN_BOOST_EN = V2_PIN_BOOST_EN;
PIN_LATCH_COMK = V2_PIN_LATCH_COMK; // os3.2latch uses H-bridge separate cathode and anode design
PIN_LATCH_COMA = V2_PIN_LATCH_COMA;
PIN_SENSOR1 = V2_PIN_SENSOR1;
PIN_SENSOR2 = V2_PIN_SENSOR2;
}
}
/* detect expanders */
for(byte i=0;i<(MAX_NUM_BOARDS)/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();
#if defined(ESP8266)
// OS 3.0 has two independent sensors
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_SENSOR1, INPUT_PULLUP);
#if defined(PIN_SENSOR2)
pinMode(PIN_SENSOR2, INPUT_PULLUP);
#endif
#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
nvdata.reboot_cause = REBOOT_CAUSE_POWERON;
nboards = 1;
nstations = nboards*8;
// set rf data pin
pinModeExt(PIN_RFTX, OUTPUT);
digitalWriteExt(PIN_RFTX, LOW);
#if defined(ARDUINO) // AVR SD and LCD functions
#if defined(ESP8266) // OS3.0 specific detections
status.has_curr_sense = 1; // OS3.0 has current sensing capacility
// measure baseline current
baseline_current = 80;
#else // OS 2.3 specific detections
// detect hardware type
if (detect_i2c(MAC_CTRL_ID)) {
Wire.beginTransmission(MAC_CTRL_ID);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(MAC_CTRL_ID, 1);
byte ret = Wire.read();
if (ret == HW_TYPE_AC || ret == HW_TYPE_DC || ret == HW_TYPE_LATCH) {
hw_type = ret;
} else {
hw_type = HW_TYPE_AC; // if type not supported, make it AC
}
}
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;
#endif
lcd_start();
lcd.createChar(ICON_CONNECTED, _iconimage_connected);
lcd.createChar(ICON_DISCONNECTED, _iconimage_disconnected);
lcd.createChar(ICON_REMOTEXT, _iconimage_remotext);
lcd.createChar(ICON_RAINDELAY, _iconimage_raindelay);
lcd.createChar(ICON_RAIN, _iconimage_rain);
lcd.createChar(ICON_SOIL, _iconimage_soil);
#if defined(ESP8266)
/* create custom characters */
lcd.createChar(ICON_ETHER_CONNECTED, _iconimage_ether_connected);
lcd.createChar(ICON_ETHER_DISCONNECTED, _iconimage_ether_disconnected);
lcd.setCursor(0,0);
lcd.print(F("Init file system"));
lcd.setCursor(0,1);
if(!SPIFFS.begin()) {
// !!! flash init failed, stall as we cannot proceed
lcd.setCursor(0, 0);
lcd_print_pgm(PSTR("Error Code: 0x2D"));
delay(5000);
}
state = OS_STATE_INITIAL;
#else
// 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)) {
// !!! sd card not detected, stall as we cannot proceed
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
DEBUG_PRINTLN(get_runtime_path());
#endif
}
#if defined(ESP8266)
/** LATCH boost voltage
*
*/
void OpenSprinkler::latch_boost() {
digitalWriteExt(PIN_BOOST, HIGH); // enable boost converter
delay((int)iopts[IOPT_BOOST_TIME]<<2); // wait for booster to charge
digitalWriteExt(PIN_BOOST, LOW); // disable boost converter
}
/** Set all zones (for LATCH controller)
* This function sets all zone pins (including COM) to a specified value
*/
void OpenSprinkler::latch_setallzonepins(byte value) {
digitalWriteExt(PIN_LATCH_COM, value); // set latch com pin
// Handle driver board (on main controller)
if(drio->type==IOEXP_TYPE_9555) { // LATCH contorller only uses PCA9555, no other type
uint16_t reg = drio->i2c_read(NXP_OUTPUT_REG); // read current output reg value
if(value) reg |= 0x00FF; // first 8 zones are the lowest 8 bits of main driver board
else reg &= 0xFF00;
drio->i2c_write(NXP_OUTPUT_REG, reg); // write value to register
}
// Handle all expansion boards
for(byte i=0;i<MAX_EXT_BOARDS/2;i++) { //
if(expanders[i]->type==IOEXP_TYPE_9555) {
expanders[i]->i2c_write(NXP_OUTPUT_REG, value?0xFFFF:0x0000);
}
}
}
void OpenSprinkler::latch_disable_alloutputs_v2() {
digitalWriteExt(PIN_LATCH_COMA, LOW);
digitalWriteExt(PIN_LATCH_COMK, LOW);
// latch v2 has a pca9555 the lowest 8 bits of which control all h-bridge anode pins
drio->i2c_write(NXP_OUTPUT_REG, drio->i2c_read(NXP_OUTPUT_REG) & 0xFF00);
// latch v2 has a 74hc595 which controls all h-bridge cathode pins
drio->shift_out(V2_PIN_SRLAT, V2_PIN_SRCLK, V2_PIN_SRDAT, 0x00);
// todo: handle expander
}
/** Set one zone (for LATCH controller)
* This function sets one specified zone pin to a specified value
*/
void OpenSprinkler::latch_setzonepin(byte sid, byte value) {
if(sid<8) { // on main controller
if(drio->type==IOEXP_TYPE_9555) { // LATCH contorller only uses PCA9555, no other type
uint16_t reg = drio->i2c_read(NXP_OUTPUT_REG); // read current output reg value
if(value) reg |= (1<<sid);
else reg &= (~(1<<sid));
drio->i2c_write(NXP_OUTPUT_REG, reg); // write value to register
}
} else { // on expander
byte bid=(sid-8)>>4;
uint16_t s=(sid-8)&0x0F;
if(expanders[bid]->type==IOEXP_TYPE_9555) {
uint16_t reg = expanders[bid]->i2c_read(NXP_OUTPUT_REG); // read current output reg value
if(value) reg |= (1<<s);
else reg &= (~(1<<s));
expanders[bid]->i2c_write(NXP_OUTPUT_REG, reg);
}
}
}
void OpenSprinkler::latch_setzoneoutput_v2(byte sid, byte A, byte K) {