-
Notifications
You must be signed in to change notification settings - Fork 286
/
OpenSprinkler.cpp
3097 lines (2786 loc) · 86.7 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 (ray@opensprinkler.com)
*
* 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"
#include "program.h"
#include "ArduinoJson.hpp"
/** Declare static data members */
OSMqtt OpenSprinkler::mqtt;
NVConData OpenSprinkler::nvdata;
ConStatus OpenSprinkler::status;
ConStatus OpenSprinkler::old_status;
unsigned char OpenSprinkler::hw_type;
unsigned char OpenSprinkler::hw_rev;
unsigned char OpenSprinkler::nboards;
unsigned char OpenSprinkler::nstations;
unsigned char OpenSprinkler::station_bits[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::engage_booster;
uint16_t OpenSprinkler::baseline_current;
time_os_t OpenSprinkler::sensor1_on_timer;
time_os_t OpenSprinkler::sensor1_off_timer;
time_os_t OpenSprinkler::sensor1_active_lasttime;
time_os_t OpenSprinkler::sensor2_on_timer;
time_os_t OpenSprinkler::sensor2_off_timer;
time_os_t OpenSprinkler::sensor2_active_lasttime;
time_os_t OpenSprinkler::raindelay_on_lasttime;
ulong OpenSprinkler::pause_timer;
ulong OpenSprinkler::flowcount_log_start;
ulong OpenSprinkler::flowcount_rt;
unsigned char OpenSprinkler::button_timeout;
time_os_t OpenSprinkler::checkwt_lasttime;
time_os_t OpenSprinkler::checkwt_success_lasttime;
time_os_t OpenSprinkler::powerup_lasttime;
uint8_t OpenSprinkler::last_reboot_cause = REBOOT_CAUSE_NONE;
unsigned char OpenSprinkler::weather_update_flag;
// todo future: the following attribute bytes are for backward compatibility
unsigned char OpenSprinkler::attrib_mas[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::attrib_igs[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::attrib_mas2[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::attrib_igs2[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::attrib_igrd[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::attrib_dis[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::attrib_spe[MAX_NUM_BOARDS];
unsigned char OpenSprinkler::attrib_grp[MAX_NUM_STATIONS];
unsigned char OpenSprinkler::masters[NUM_MASTER_ZONES][NUM_MASTER_OPTS];
extern char tmp_buffer[];
extern char ether_buffer[];
extern ProgramData pd;
#if defined(ESP8266)
SSD1306Display OpenSprinkler::lcd(0x3c, SDA, SCL);
unsigned char OpenSprinkler::state = OS_STATE_INITIAL;
unsigned char 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="";
unsigned char OpenSprinkler::wifi_bssid[6]={0};
unsigned char OpenSprinkler::wifi_channel=255;
unsigned char OpenSprinkler::wifi_testmode = 0;
#elif defined(ARDUINO)
LiquidCrystal OpenSprinkler::lcd;
extern SdFat sd;
#else
#if defined(OSPI)
unsigned char OpenSprinkler::pin_sr_data = PIN_SR_DATA;
#endif
// todo future: LCD define for Linux-based systems
#endif
#if defined(USE_OTF)
OTCConfig OpenSprinkler::otc;
#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"
"fwire"
"resv1"
"resv2"
"resv3"
"resv4"
"resv5"
"resv6"
"resv7"
"resv8"
"wimod"
"reset"
;
/** Option prompts (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?"
"Notif 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: "
"Force wired? "
"Reserved 1 "
"Reserved 2 "
"Reserved 3 "
"Reserved 4 "
"Reserved 5 "
"Reserved 6 "
"Reserved 7 "
"Reserved 8 "
"WiFi mode? "
"Factory reset? ";
// string options do not have prompts
/** Option maximum values (stored in PROGMEM to reduce RAM usage) */
const unsigned char 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,
1,
255,
255,
255,
255,
255,
255,
255,
255,
255,
1
};
// string options do not have maximum values
/** Integer option values (stored in RAM) */
unsigned char 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
15, // 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, // notif 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,
1, // force wired connection
0, // reserved 1
0, // reserved 2
0, // reserved 3
0, // reserved 4
0, // reserved 5
0, // reserved 6
0, // reserved 7
0, // reserved 8
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, // SOPT_WEATHER_OPTS
DEFAULT_EMPTY_STRING, // SOPT_IFTTT_KEY
DEFAULT_EMPTY_STRING, // SOPT_STA_SSID
DEFAULT_EMPTY_STRING, // SOPT_STA_PASS
DEFAULT_EMPTY_STRING, // SOPT_MQTT_OPTS
DEFAULT_EMPTY_STRING, // SOPT_OTC_OPTS
DEFAULT_DEVICE_NAME,
DEFAULT_EMPTY_STRING, // SOPT_STA_BSSID_CHL
DEFAULT_EMPTY_STRING, // SOPT_EMAIL_OPTS
};
/** 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";
#if !defined(ARDUINO)
static inline int32_t now() {
time_t rawtime;
time(&rawtime);
return rawtime;
}
#endif
/** Calculate local time (UTC time plus time zone offset) */
time_os_t OpenSprinkler::now_tz() {
return now()+(int32_t)3600/4*(int32_t)(iopts[IOPT_TIMEZONE]-48);
}
#if defined(ARDUINO)
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(unsigned char* buffer, bool wired) {
#if defined(ESP8266)
WiFi.macAddress((unsigned char*)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(unsigned char 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 */
unsigned char 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 defined(ESP8266)
if (start_ether()) {
useEth = true;
WiFi.mode(WIFI_OFF);
} else {
useEth = false;
}
if((useEth || get_wifi_mode()==WIFI_MODE_STA) && otc.en>0 && otc.token.length()>=DEFAULT_OTC_TOKEN_LENGTH) {
otf = new OTF::OpenThingsFramework(httpport, otc.server, otc.port, otc.token, false, ether_buffer, ETHER_BUFFER_SIZE);
DEBUG_PRINTLN(F("Started OTF with remote connection"));
} else {
otf = new OTF::OpenThingsFramework(httpport, ether_buffer, ETHER_BUFFER_SIZE);
DEBUG_PRINTLN(F("Started OTF with just local connection"));
}
extern DNSServer *dns;
if(get_wifi_mode() == WIFI_MODE_AP) dns = new DNSServer();
if(update_server) { delete update_server; update_server = NULL; }
update_server = new ESP8266WebServer(8080);
DEBUG_PRINT(F("Started update server"));
return 1;
#else
if (start_ether()) {
if(m_server) { delete m_server; m_server = NULL; }
m_server = new EthernetServer(httpport);
m_server->begin();
useEth = true;
return 1;
} else {
useEth = false;
return 0;
}
#endif
}
unsigned char OpenSprinkler::start_ether() {
#if defined(ESP8266)
if(hw_rev<2) return 0; // ethernet capability is only available when hw_rev>=2
eth.isW5500 = (hw_rev==2)?false:true; // os 3.2 uses enc28j60 and 3.3 uses w5500
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setFrequency(4000000);
if(eth.isW5500) {
DEBUG_PRINTLN(F("detect existence of W5500"));
/* this is copied from w5500.cpp wizchip_sw_reset
* perform a software reset and see if we get a correct response
* without this, eth.begin will crash if W5500 is not connected
* ideally wizchip_sw_reset should return a value but since it doesn't
* we have to extract it code here
* */
static const uint8_t AccessModeRead = (0x00 << 2);
static const uint8_t AccessModeWrite = (0x01 << 2);
static const uint8_t BlockSelectCReg = (0x00 << 3);
pinMode(PIN_ETHER_CS, OUTPUT);
// ==> setMR(MR_RST)
digitalWrite(PIN_ETHER_CS, LOW);
SPI.transfer((0x00 & 0xFF00) >> 8);
SPI.transfer((0x00 & 0x00FF) >> 0);
SPI.transfer(BlockSelectCReg | AccessModeWrite);
SPI.transfer(0x80);
digitalWrite(PIN_ETHER_CS, HIGH);
// ==> ret = getMR()
uint8_t ret;
digitalWrite(PIN_ETHER_CS, LOW);
SPI.transfer((0x00 & 0xFF00) >> 8);
SPI.transfer((0x00 & 0x00FF) >> 0);
SPI.transfer(BlockSelectCReg | AccessModeRead);
ret = SPI.transfer(0);
digitalWrite(PIN_ETHER_CS, HIGH);
if(ret!=0) return 0; // ret is expected to be 0
} else {
/* this is copied from enc28j60.cpp geterevid
* check to see if the hardware revision number if expected
* */
DEBUG_PRINTLN(F("detect existence of ENC28J60"));
#define MAADRX_BANK 0x03
#define EREVID 0x12
#define ECON1 0x1f
// ==> setregbank(MAADRX_BANK);
pinMode(PIN_ETHER_CS, OUTPUT);
uint8_t r;
digitalWrite(PIN_ETHER_CS, LOW);
SPI.transfer(0x00 | (ECON1 & 0x1f));
r = SPI.transfer(0);
digitalWrite(PIN_ETHER_CS, HIGH);
digitalWrite(PIN_ETHER_CS, LOW);
SPI.transfer(0x40 | (ECON1 & 0x1f));
SPI.transfer((r & 0xfc) | (MAADRX_BANK & 0x03));
digitalWrite(PIN_ETHER_CS, HIGH);
// ==> r = readreg(EREVID);
digitalWrite(PIN_ETHER_CS, LOW);
SPI.transfer(0x00 | (EREVID & 0x1f));
r = SPI.transfer(0);
digitalWrite(PIN_ETHER_CS, HIGH);
if(r==0 || r==255) return 0; // r is expected to be a non-255 revision number
}
load_hardware_mac((uint8_t*)tmp_buffer, true);
if (iopts[IOPT_USE_DHCP]==0) { // config static IP before calling eth.begin
IPAddress staticip(iopts+IOPT_STATIC_IP1);
IPAddress gateway(iopts+IOPT_GATEWAY_IP1);
IPAddress dns(iopts+IOPT_DNS_IP1);
IPAddress subn(iopts+IOPT_SUBNET_MASK1);
eth.config(staticip, gateway, subn, dns);
}
eth.setDefault();
if(!eth.begin((uint8_t*)tmp_buffer)) return 0;
lcd_print_line_clear_pgm(PSTR("Start wired link"), 1);
lcd_print_line_clear_pgm(eth.isW5500 ? PSTR(" [w5500] ") : PSTR(" [enc28j60] "), 2);
ulong timeout = millis()+60000; // 60 seconds time out
unsigned char timecount = 1;
while (!eth.connected() && millis()<timeout) {
DEBUG_PRINT(".");
lcd.setCursor(13, 2);
lcd.print(timecount);
delay(1000);
timecount++;
}
lcd_print_line_clear_pgm(PSTR(""), 2);
if(eth.connected()) {
// if wired connection is successful at this point, copy the network ips to config
DEBUG_PRINTLN();
DEBUG_PRINT("eth.ip:");
DEBUG_PRINTLN(eth.localIP());
DEBUG_PRINT("eth.dns:");
DEBUG_PRINTLN(WiFi.dnsIP());
if (iopts[IOPT_USE_DHCP]) {
memcpy(iopts+IOPT_STATIC_IP1, &(eth.localIP()[0]), 4);
memcpy(iopts+IOPT_GATEWAY_IP1, &(eth.gatewayIP()[0]),4);
memcpy(iopts+IOPT_DNS_IP1, &(WiFi.dnsIP()[0]), 4); // todo: lwip need dns ip
memcpy(iopts+IOPT_SUBNET_MASK1, &(eth.subnetMask()[0]), 4);
iopts_save();
}
return 1;
} else {
// if wired connection has failed at this point, return depending on whether the user wants to force wired
return (iopts[IOPT_FORCE_WIRED] ? 1 : 0);
}
#else
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;
#endif
}
bool OpenSprinkler::network_connected(void) {
#if defined (ESP8266)
if(useEth)
return eth.connected();
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();
#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 */
unsigned char OpenSprinkler::start_network() {
unsigned int port = (unsigned int)(iopts[IOPT_HTTPPORT_1]<<8) + (unsigned int)iopts[IOPT_HTTPPORT_0];
#if defined(DEMO)
#if defined(HTTP_PORT)
port = HTTP_PORT;
#else
port = 80;
#endif
#endif
if(otc.en>0 && otc.token.length()>=DEFAULT_OTC_TOKEN_LENGTH) {
otf = new OTF::OpenThingsFramework(port, otc.server.c_str(), otc.port, otc.token.c_str(), false, ether_buffer, ETHER_BUFFER_SIZE);
DEBUG_PRINTLN(F("Started OTF with remote connection"));
} else {
otf = new OTF::OpenThingsFramework(port, ether_buffer, ETHER_BUFFER_SIZE);
DEBUG_PRINTLN(F("Started OTF with just local connection"));
}
return 1;
}
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(unsigned char* 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 ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return true;
// Returns the mac address of the first interface if multiple active
for (unsigned 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[500];
snprintf(cmd, 500, "cd %s && ./updater.sh", get_data_dir());
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) // ESP8266 specific initializations
/* 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 and 3
if(detect_i2c(EEPROM_I2CADDR)) { // revision 3 has a I2C EEPROM
hw_rev = 3;
} else {
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(unsigned char 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, unless it is not being used
if(PIN_RFTX != 255) {
pinModeExt(PIN_RFTX, OUTPUT);
digitalWriteExt(PIN_RFTX, LOW);
}