-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathOpenSprinkler.cpp
3223 lines (2910 loc) · 89.1 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) 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 "Defines.h"
#include "OpenSprinkler.h"
#include "Gpio.h"
#if !defined(ESP8266) && !defined(ESP32)
#include <avr/eeprom.h>
#else
#ifdef EEPROM_ESP
#include "Eeprom_ESP.h"
#else
#include "eeprom_mio.h"
#endif //EEPROM_ESP
#endif
#if defined(ESP8266) || defined(ESP32)
//#include "PCF8574/PCF8574.h"
#include "PCF8574Mio.h"
#ifdef LORA
#include "LoraStation.h"
LoraStation Lora;
void switch_LoraStation(byte * buf, bool turnon);
#endif
#endif
#ifdef PCF8574_M
PCF8574 PCF[10];//_38(0x3F); // add switches to lines (used as input)
//PCF8574 PCF_39(0x3F); // add leds to lines (used as output)
//
// pin HACKING FUNCTIONS
// if PIN_NUMBER > MAX MCU PINS (16 for ESP8266) PINs are assigned to PCF 8574 I/O lines
// (16 pins reserved for each IC) (in the order given by ESP8266 address)
//
#define MAX_MCU_PINS 32 //Esp8266 setting
#ifdef ESP32
#define MAX_MCU_PINS 48 //Esp32 setting
#endif
#define pinMode(x,y) if(x<MAX_MCU_PINS) pinMode(x,y);else{DEBUG_PRINT(x); DEBUG_PRINT(" at PCF_addr ");DEBUG_PRINTLN((x - MAX_MCU_PINS) / 16);}//verify pin on Ic??
#define digitalWrite(x,y) (x<MAX_MCU_PINS)? digitalWrite(x,y) :PCF[(x - MAX_MCU_PINS) / 16].write((byte)(x - MAX_MCU_PINS) % 16, y)//;DEBUG_PRINT("<W");DEBUG_PRINT(x);DEBUG_PRINT(">");DEBUG_PRINT(y) //verify pin on Ic??
#define digitalRead(x) (x<MAX_MCU_PINS)? digitalRead(x): PCF[(x-MAX_MCU_PINS)/16].read((x-MAX_MCU_PINS)%16)
#else
#define pinMode(x, y) pinMode(x, y) //verify pin on Ic??
#define digitalWrite(x,y) {digitalWrite(x,y);if(nDB>300 ) if(x==(nDB-300)){ if(y>0){Serial.print("-");}else {Serial.print("_");}}}
#define digitalRead(x) digitalRead(x)
#endif
#ifndef ESP32 ////--
extern "C" {
#include "user_interface.h"
uint16 readvdd33(void);
bool wifi_set_sleep_type(sleep_type_t);
sleep_type_t wifi_get_sleep_type(void);
}
#endif
/** 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__)
byte OpenSprinkler::engage_booster;
#endif
ulong OpenSprinkler::sensor_lasttime;
ulong OpenSprinkler::flowcount_log_start;
ulong OpenSprinkler::flowcount_rt;
ulong OpenSprinkler::flowcount_time_ms;
ulong OpenSprinkler::raindelay_start_time;
byte OpenSprinkler::button_timeout;
ulong OpenSprinkler::checkwt_lasttime;
ulong OpenSprinkler::checkwt_success_lasttime;
char tmp_buffer[TMP_BUFFER_SIZE+1]; // scratch buffer
#ifdef OPENSPRINKLER_ARDUINO_DISCRETE
int OpenSprinkler::station_pins[16] =
{
PIN_STN_S01, PIN_STN_S02, PIN_STN_S03, PIN_STN_S04, PIN_STN_S05, PIN_STN_S06, PIN_STN_S07, PIN_STN_S08
#ifdef PIN_STN_S09
,PIN_STN_S09, PIN_STN_S10, PIN_STN_S11, PIN_STN_S12, PIN_STN_S13, PIN_STN_S14, PIN_STN_S15, PIN_STN_S16
#endif
};
#endif // OPENSPRINKLER_ARDUINO_DISCRETE
//#ifndef OPENSPRINKLER_ARDUINO // Moved to opensprinkler.h
#if PROGM
prog_char wtopts_filename[] PROGMEM = WEATHER_OPTS_FILENAME;
prog_char stns_filename[] PROGMEM = STATION_ATTR_FILENAME;
#else
char wtopts_filename[] = WEATHER_OPTS_FILENAME;
char stns_filename[] = STATION_ATTR_FILENAME;
#endif
//#endif
#if defined(ARDUINO)
#ifdef OPENSPRINKLER_ARDUINO_FREETRONICS_LCD
LiquidCrystal OpenSprinkler::lcd ( PIN_LCD_RS, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7 );
#else
#ifdef LCDI2C
#ifdef LCD_SH1106
Adafruit_SH1106 OpenSprinkler::lcd(SDA_PIN, SCL_PIN);
#elif defined( LCD_SSD1306)
#ifndef LCD_RST
#define LCD_RST -1
#endif
Adafruit_SSD1306 OpenSprinkler::lcd(LCD_RST);
#else
#define BACKLIGHT_PIN PIN_LCD_BACKLIGHT
//LiquidCrystal_I2C OpenSprinkler::lcd(0X27, 2, 1, 0, 4, 5, 6, 7);
LiquidCrystal_I2C OpenSprinkler::lcd(LCD_ADDR, PIN_LCD_EN, PIN_LCD_RW, PIN_LCD_RS, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7);
#endif
#else
LiquidCrystal OpenSprinkler::lcd;
#endif
#endif
#if defined(ESP8266) || defined(ESP32)
#ifndef SDFAT
#ifdef ESP8266
#include <FS.h>
#elif defined(ESP32)
#include <SPIFFS.h>
#else
#include <SD.h>
#include <SdFat.h>
extern SdFat sd;
#endif
#endif
#endif
#elif defined(OSPI)
//
: LCD define for OSPi
#endif
#ifdef BATTERY
static Adafruit_INA219 ina(0x040);
#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
//#ifndef OPENSPRINKLER_ARDUINO // Moved to opensprinkler.h
#if PROGM
prog_char op_json_names[] PROGMEM =
#else
char op_json_names[] =
#endif
"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"
#ifdef OS217
"dns1\0"
"dns2\0"
"dns3\0"
"dns4\0"
"sar\0\0"
"ife\0\0"
#endif
//___________________________________________________________________________________________________________
"reset"
#ifdef BATTERY
"DsleD"
"DsleS"
"DsleI"
"DsleV"
"LsleV"
"LsleP"
"batAh"
#endif
;
// OPENSPRINKLER_ARDUINO
/** Option promopts (stored in progmem, for LCD display) */
// Each string is strictly 16 characters
// with SPACE fillings if less
#if PROGM
prog_char op_prompts[] /*PROGMEM*/ =
#else
char op_prompts[] =
#endif
"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 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.? "
#ifdef OS217
"DNS server.ip1: "
"DNS server.ip2: "
"DNS server.ip3: "
"DNS server.ip4: "
"Special Refresh?"
"IFTTT Enable: "
#endif
//___________________________________________________________________________________________________________
"Factory reset? "
#ifdef BATTERY
"deep sleep dur. "
"deep sleep start"
"deep sleep inter"
"min bat v Dsleep"
"min V L sleep "
"lightslepp delay"
"battery capacity"
#endif
;
/** Option maximum values (stored in progmem) */
//#ifndef OPENSPRINKLER_ARDUINO // Moved to opensprinkler.h
#if PROGM
prog_char op_max[] PROGMEM =
{
#else
char op_max[] =
{
#endif
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,
#ifdef OS217
255,
255,
255,
255,
1,
255,
#endif
//___________________________________________________________________________________________________________
1
#ifdef BATTERY
, 255, //min deep sleep duration D_SLEEP_DURATION
255, //h deep sleep start D_SLEEP_START_TIME
255, //h deep sleep interval D_SLEEP_INTERVAL
255, //mV minimum battery voltage start of deep sleeMIN_VOLTS_D_SLEEP
255, //mV voltage start of light sleep MIN_VOLTS_L_SLEEP
255, //s light sleep delay time L_SLEEP_DURATION
255 //mAh battery capacity BATTERY_mAH
#endif
};
//#endif // OPENSPRINKLER_ARDUINO
#define DB_MASK 1
/** Option values (stored in RAM) */
byte OpenSprinkler::options[] =
{
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,
1, // number of 8-station extension board. 0: no extension boards
1, // the option 'sequential' is now retired
#ifdef OS217
120,// station delay time (-10 minutes to 10 minutes).
0, // index of master station. 0: no master station
120, // master on time [-10,10] min (x-120)*240 sec
120, // master off time [-10,10] min
#else
128,// station delay time (-59 minutes to 59 minutes).
0, // index of master station. 0: no master station
0, // master on time [0,60] seconds
60, // master off time [-60,60] seconds
#endif
0, // sensor function (see SENSOR_TYPE macro defines)
0, // rain sensor type. 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
15, // lcd dimming
80, // boost time (only valid to DC and LATCH type)
0, // weather algorithm (0 means not using weather algorithm)
132, // this and the next three bytes define the ntp server ip
163,
4,
101,
1, // enable logging: 0: disable; 1: enable.
0, // index of master 2. 0: no master2 station
0,
60,
OS_FW_MINOR, // firmware minor version
100,// this and next byte define flow pulse rate (100x)
0,
0, // set as remote extension
#ifdef OS217
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
#endif
//___________________________________________________________________________________________________________
0 // reset
//
#ifdef BATTERY
,
// options for battery operated
60, //m deep sleep duration D_SLEEP_DURATION
22, //h deep sleep start D_SLEEP_START_TIME
8, //h deep sleep interval D_SLEEP_INTERVAL
32, //V*10 minimum battery voltage start of deep sleeMIN_VOLTS_D_SLEEP
34, //V*10 voltage start of light sleep MIN_VOLTS_L_SLEEP
30, //s light sleep delay time L_SLEEP_DURATION
48 //10*Ah battery capacity BATTERY_mAH
//
#endif
};
static float charge=0;
unsigned long chargeTime=0;
/** Weekday strings (stored in progmem, for LCD display) */
static prog_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
//static PCF8574 PCF[10];
enum I2Cdev_t
{
PCF8574_TYPE,
PCF8574A_TYPE,
RTC_TYPE,
EEPROM_TYPE,
};
byte ADR[4] = {
0x20,
0x38,
0x64,
0x50
};
byte ADR_R[4] = {
8,
8,
16,
8
};
bool ADR_TYP[4] = {
true,
true,
false,
false
};
String ADR_DEV_NAME[4] = {
"PCF8574",
"PCF8574A",
"DS3231",
"AT24Cxx"
};
#ifdef DS1307RTC==I2CRTC
extern I2CRTC RTC; //MOD_RTC
#else
extern DS1307RTC RTC;
#endif
void ScanI2c()
{
byte address[20];
byte error;
int nDevices;
#ifdef DS1307RTC==I2CRTC
Serial.println("detecting RTC....");
RTC.detect();
#endif
Serial.println("Scanning..I2C.....");
nDevices = 0;
for (byte addres = 1; addres < 127; addres++)
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(addres);
error = Wire.endTransmission();
bool trov = false;
byte trova = 255;
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (addres < 16)
Serial.print("0");
Serial.print(addres, HEX);
Serial.print("\t");
byte id = 0;
while (id < 4) {
if (addres >= ADR[id] && addres < ADR[id] + ADR_R[id]) {
Serial.println(ADR_DEV_NAME[id]); trov = true;
trova = id;
}
id++;
}
if (!trov) Serial.println(" unknown");
}
if (trova != 255&&trov)
if (ADR_TYP[trova]) {
#ifdef LCD_ADDR //do not count LCD controller if LCD i2c library is used
if (addres == LCD_ADDR)Serial.println("found LCD Controller");
else
#endif
{
address[nDevices] = addres;
nDevices++;
#ifdef PCF8574_M
DEBUG_PRINT("PCF expander "); DEBUG_PRINT(nDevices - 1); DEBUG_PRINT("at"); DEBUG_PRINTLN(addres);
PCF[nDevices - 1].begin(addres);
#else
Serial.println("error Pcf_dev set: #define PCF8574_M in Pins.h");
#endif
}
}
else if (error == 4)
{
Serial.print("Unknow error at address 0x");
if (address[nDevices]<16)
Serial.print("0");
Serial.println(address[nDevices], HEX);
}
}
if (nDevices == 0)
{
#ifdef PCF8574_M
Serial.println("No PCF8574 devices found\n cannot continue!");
write_message("No PCF devices found!!");
delay(60000);
#endif
}
else
Serial.println("done\n");
// return nDevices;
}
/** read hardware MAC */
#define MAC_CTRL_ID 0x50
bool OpenSprinkler::read_hardware_mac()
{
#if defined(ESP8266) || defined(ESP32)
byte mac[6];
if(!WiFi.macAddress(mac)) return false;
for (byte i = 0; i < 6; i++) tmp_buffer[i ]= mac[i];
#else
uint8_t ret;
Wire.beginTransmission ( MAC_CTRL_ID );
Wire.write ( ( uint8_t ) ( 0x00 ) );
ret = Wire.endTransmission();
if ( ret ) return false;
Wire.beginTransmission ( MAC_CTRL_ID );
Wire.write ( 0xFA ); // The address of the register we want
Wire.endTransmission(); // Send the data
Wire.requestFrom ( MAC_CTRL_ID, 6 ); // Request 6 bytes from the EEPROM
while ( !Wire.available() ); // Wait for the response
for ( ret=0; ret<6; ret++ )
{
tmp_buffer[ret] = Wire.read();
}
#endif
return true;
}
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 ( "Connecting..." ), 1 );
DEBUG_PRINTLN ( F ( "Connecting..." ) );
// new from 2.2: read hardware MAC
#if defined( OPENSPRINKLER_ARDUINO) && !defined(ESP8266)
// always use software MAC
DEBUG_PRINTLN ( F ( "Setting 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
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;
}
#endif
if ( !ether.begin ( ETHER_BUFFER_SIZE, ( uint8_t* ) tmp_buffer, PIN_ETHER_CS ) )
{
DEBUG_PRINTLN ( F ( "Ethernet initialisation failed - check wiring" ) );
return 0;
}
else
{
DEBUG_PRINTLN ( F ( "Ethernet initialised" ) );
}
// calculate http port number
ether.hisport = ( unsigned int ) ( options[OPTION_HTTPPORT_1]<<8 ) + ( unsigned int ) options[OPTION_HTTPPORT_0];
DEBUG_PRINT ( F ( "Using http port " ) );
DEBUG_PRINTLN ( ether.hisport );
if ( options[OPTION_USE_DHCP] )
{
// set up DHCP
// register with domain name "OS-xx" where xx is the last byte of the MAC address
DEBUG_PRINT(F("DHCP IP setup"));
if ( !ether.dhcpSetup() )
{
DEBUG_PRINTLN ( F ( " failed" ) );
write_message("DHCP conn. failed");
return 0;
}
// once we have valid DHCP IP, we write these into static IP / gateway IP
byte *ip = ether.myip;
options[OPTION_STATIC_IP1] = ip[0];
options[OPTION_STATIC_IP2] = ip[1];
options[OPTION_STATIC_IP3] = ip[2];
options[OPTION_STATIC_IP4] = ip[3];
ip = ether.gwip;
options[OPTION_GATEWAY_IP1] = ip[0];
options[OPTION_GATEWAY_IP2] = ip[1];
options[OPTION_GATEWAY_IP3] = ip[2];
options[OPTION_GATEWAY_IP4] = ip[3];
options_save();
DEBUG_PRINTLN ( F ( "DHCP IP setup successful" ) );
}
else
{
// set up static IP
byte staticip[] =
{
options[OPTION_STATIC_IP1],
options[OPTION_STATIC_IP2],
options[OPTION_STATIC_IP3],
options[OPTION_STATIC_IP4]
};
byte gateway[] =
{
options[OPTION_GATEWAY_IP1],
options[OPTION_GATEWAY_IP2],
options[OPTION_GATEWAY_IP3],
options[OPTION_GATEWAY_IP4]
};
DEBUG_PRINT(F("Static IP setup "));
if ( !ether.staticSetup ( staticip, gateway, gateway ) )
{
DEBUG_PRINTLN ( F ( "failed" ) );
write_message("Static conn. failed");
return 0;
}
else
{
DEBUG_PRINTLN ( F ( "successful" ) );
}
}
DEBUG_PRINTLN ( F ( "Ethernet setup complete" ) );
return 1;
}
/** Reboot controller */
void OpenSprinkler::reboot_dev()
{
#if defined(ESP8266) || defined(ESP32)
// eeprom_write_byte((unsigned char *)(NVM_SIZE - 1), byte(charge / 25));
write_message("restart");
ESP.restart();
#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
}
LiquidCrystal_I2C OpenSprinkler::lcd(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte l)
{
return LiquidCrystal_I2C();
}
/** 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 OPENSPRINKLER_ARDUINO_FREETRONICS_LCD
//set up the LCD number of columns and rows:
lcd.begin ( 16, 2 );
#else
// turn on lcd
#ifndef LCDI2C
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
}
#else //for i2c library have to turn backligth on
#ifndef LCD_SSD1306
lcd.begin(20, 4);
lcd.setBacklightPin(BACKLIGHT_PIN, PIN_BACKLIGHT_MODE);
lcd.setBacklight(HIGH);
lcd.home();
lcd.print("start..V.2.1.6");
#else
#if defined(LCD_SH1106)
lcd.begin(SH1106_SWITCHCAPVCC, 0x3c);
#elif defined(LCD_SSD1306)
lcd.begin(SSD1306_SWITCHCAPVCC, 0x3c);
#endif
lcd.display();
delay(2000);
lcd.clearDisplay();
lcd.setTextColor(WHITE,BLACK); // white char clear background
lcd.print("start..V.2.1.6");
lcd.display();
#endif
#endif
#endif
}
#endif
extern void flow_isr();
/** Initialize pins, controller variables, LCD */
void OpenSprinkler::begin()
{
// Init I2C
#if defined(ESP8266) || defined(ESP32)
Wire.begin(SDA_PIN, SCL_PIN);
DEBUG_PRINT("wire begin("); DEBUG_PRINT(SDA_PIN); DEBUG_PRINT(','); DEBUG_PRINT(SCL_PIN); DEBUG_PRINTLN(')');
#else
Wire.begin();
#endif
delay(2000);
ScanI2c();
#ifdef LORA
while (!Serial.available());
byte n=Serial.read()-'0';
byte k = Serial.read()-'0';
SPI.begin(SPI_SCLK, SPI_MISO, SPI_MOSI,SPI_CS);
Lora.begin(true);
Lora.findRouting(n, true, k);
#endif
#ifdef OPENSPRINKLER_ARDUINO_DISCRETE
#ifdef OSBEE
#if OSBEE == 1
setallpins(HIGH);
#else
// initialize the Digital IO pins as outputs:
for (int i = 0; i < (PIN_EXT_BOARDS * 8); i++)
{ pinMode(station_pins[i], OUTPUT);
}
// for (int i = 0; i < MAX_NUMBER_ZONES; i++) BeeClose(i);
#endif
#endif
#else
// shift register setup
#ifdef PIN_SR_OE
PINMODE ( PIN_SR_OE, OUTPUT );
// pull shift register OE high to disable output
digitalWrite ( PIN_SR_OE, HIGH );
#endif
DEBUG_PRINTLN(PIN_SR_LATCH);
pinMode ( PIN_SR_LATCH, OUTPUT );
digitalWrite ( PIN_SR_LATCH, HIGH );
pinMode ( PIN_SR_CLOCK, OUTPUT );
#endif // OPENSPRINKLER_ARDUINO_DISCRETE
#if defined(OSPI)
pin_sr_data = PIN_SR_DATA;
// detect RPi revisionOPENSPRINKLER_ARDUINO_DISCRETE
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
#ifndef OPENSPRINKLER_ARDUINO_DISCRETE
pinMode ( PIN_SR_DATA, OUTPUT );
#endif
#endif
// Reset all stations
clear_all_station_bits();
apply_all_station_bits();
#ifndef OPENSPRINKLER_ARDUINO_DISCRETE
#ifdef PIN_SR_OE
// pull shift register OE low to enable output
digitalWrite ( PIN_SR_OE, LOW );
#endif
#endif // OPENSPRINKLER_ARDUINO_DISCRETE
#ifdef PIN_RAINSENSOR
// Rain sensor port set up
pinMode ( PIN_RAINSENSOR, INPUT );
// Set up sensors
#if defined(ARDUINO)
digitalWrite ( PIN_RAINSENSOR, HIGH ); // enabled internal pullup on rain sensor //ESP32_ER
attachInterrupt ( PIN_FLOWSENSOR_INT, flow_isr, FALLING );
#else
// OSPI and OSBO use external pullups
attachInterrupt ( PIN_FLOWSENSOR, "falling", flow_isr );
#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
nboards = 1;
nstations = 8;
#ifdef PIN_RF_DATA
// set rf data pin
PINMODE ( PIN_RF_DATA, OUTPUT );
digitalWrite ( PIN_RF_DATA, LOW );
#endif