forked from SmartThingsCommunity/SmartThingsPublic
/
Sonoff.ino
3478 lines (3127 loc) · 109 KB
/
Sonoff.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ESP8266WiFi.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>
#include <ESP8266SSDP.h>
#include <ESP8266HTTPUpdateServer.h>
ESP8266HTTPUpdateServer httpUpdater;
#include <Arduino.h>
//#define SONOFF
//#define SONOFF_TH
//#define SONOFF_S20
//#define SONOFF_TOUCH //ESP8285 !!!!!!!!!!
//#define SONOFF_SV
//#define SONOFF_POW
//#define SONOFF_DUAL
//#define SONOFF_4CH //ESP8285 !!!!!!!!!!
//#define ECOPLUG
//#define SONOFF_IFAN02 //ESP8285 !!!!!!!!!!
#define SHELLY
#ifdef SONOFF_POW
#include "HLW8012.h"
#endif
#if defined SONOFF_TH || defined SONOFF
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#endif
String message = "";
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int currentStatus = LOW;
boolean needUpdate1 = true;
boolean needUpdate2 = true;
boolean needUpdate3 = true;
boolean needUpdate4 = true;
boolean inAutoOff1 = false;
boolean inAutoOff2 = false;
boolean inAutoOff3 = false;
boolean inAutoOff4 = false;
boolean needReboot = false;
boolean shortPress = false;
int previousFanSpeed = -1;
//stores if the switch was high before at all
int state1 = LOW;
int state2 = LOW;
int state3 = LOW;
int state4 = LOW;
int state_ext = LOW;
//stores the time each button went high or low
unsigned long current_high1;
unsigned long current_low1;
unsigned long current_high2;
unsigned long current_low2;
unsigned long current_high3;
unsigned long current_low3;
unsigned long current_high4;
unsigned long current_low4;
unsigned long current_high_ext;
unsigned long current_low_ext;
#if defined SONOFF || defined ECOPLUG
const char * projectName = "Sonoff";
String softwareVersion = "2.0.5";
#endif
#ifdef SONOFF_S20
const char * projectName = "Sonoff S20";
String softwareVersion = "2.0.5";
#endif
#ifdef SONOFF_TOUCH
const char * projectName = "Sonoff Touch";
String softwareVersion = "2.0.5";
#endif
#ifdef SONOFF_POW
const char * projectName = "Sonoff POW";
String softwareVersion = "2.0.5";
HLW8012 hlw8012;
#endif
#ifdef SONOFF_TH
const char * projectName = "Sonoff TH";
String softwareVersion = "2.0.5";
#endif
#ifdef SONOFF_DUAL
const char * projectName = "Sonoff Dual";
String softwareVersion = "2.0.5";
#endif
#ifdef SONOFF_4CH
const char * projectName = "Sonoff 4CH";
String softwareVersion = "2.0.5";
#endif
#ifdef SONOFF_IFAN02
const char * projectName = "Sonoff IFan02";
String softwareVersion = "2.0.5";
#endif
#if defined SHELLY
const char * projectName = "Shelly";
String softwareVersion = "2.0.5";
#endif
const char compile_date[] = __DATE__ " " __TIME__;
unsigned long connectionFailures;
#define FLASH_EEPROM_SIZE 4096
extern "C" {
#include "spi_flash.h"
}
extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _SPIFFS_end;
extern "C" uint32_t _SPIFFS_page;
extern "C" uint32_t _SPIFFS_block;
unsigned long failureTimeout = millis();
long debounceDelay = 20; // the debounce time (in ms); increase if false positives
unsigned long timer1s = 0;
unsigned long timer5s = 0;
unsigned long timer5m = 0;
unsigned long timer1m = 0;
unsigned long timerW = 0;
unsigned long timerUptime;
unsigned long autoOffTimer1 = 0;
unsigned long autoOffTimer2 = 0;
unsigned long autoOffTimer3 = 0;
unsigned long autoOffTimer4 = 0;
unsigned long currentmillis = 0;
#if defined SONOFF_TH || defined SONOFF || defined SHELLY
unsigned long timerT = 0;
unsigned long timerH = 0;
#endif
#ifdef SONOFF_POW
unsigned long timerV = 0;
unsigned long timerA = 0;
unsigned long timerVA = 0;
unsigned long timerPF = 0;
#define REL_PIN1 12 // GPIO 12 = Red Led and Relay (0 = Off, 1 = On)
#define LED_PIN1 15 // GPIO 13 = Green Led (0 = On, 1 = Off)
#define KEY_PIN1 0 // GPIO 00 = Button
#define SEL_PIN 5
#define CF1_PIN 13
#define CF_PIN 14
#define CURRENT_MODE HIGH
#define CURRENT_RESISTOR 0.001
#define VOLTAGE_RESISTOR_UPSTREAM ( 5 * 470000 ) // Real: 2280k
#define VOLTAGE_RESISTOR_DOWNSTREAM ( 1000 ) // Real 1.009k
float W;
float V;
float A;
float VA;
float PF;
// redirect interrupt to HLW library
void ICACHE_RAM_ATTR hlw8012_cf1_interrupt() {
hlw8012.cf1_interrupt();
}
void ICACHE_RAM_ATTR hlw8012_cf_interrupt() {
hlw8012.cf_interrupt();
}
// Library expects an interrupt on both edges
void setInterrupts() {
attachInterrupt(CF1_PIN, hlw8012_cf1_interrupt, CHANGE);
attachInterrupt(CF_PIN, hlw8012_cf_interrupt, CHANGE);
}
void calibrate(int voltage = 120) {
// Let some time to register values
unsigned long timeout = millis();
while ((millis() - timeout) < 10000) {
delay(1);
}
//hlw8012.resetMultipliers();
// Calibrate using a 60W bulb (pure resistive) on a 230V line
//hlw8012.expectedActivePower(60.0);
//hlw8012.expectedVoltage(110.0);
//hlw8012.expectedCurrent(60.0 / 110.0);
//hlw8012.expectedActivePower(53.0);
hlw8012.expectedVoltage(voltage);
//hlw8012.expectedCurrent(53.0 / 120.0);
// Show corrected factors
//message += hlw8012.getCurrentMultiplier();
//message += " ";
//message += hlw8012.getVoltageMultiplier();
//message += " ";
//message += hlw8012.getPowerMultiplier();
//Serial.println();
}
#endif
#if defined SONOFF || defined SONOFF_TOUCH || defined SONOFF_S20 || defined SONOFF_DUAL
#define REL_PIN1 12
#define LED_PIN1 13
#define KEY_PIN1 0
#define EXT_PIN 14
#endif
#if defined SHELLY
#define REL_PIN1 4
#define KEY_PIN1 0
#define EXT_PIN 5
#define LED_PIN1 14
#endif
#if defined SONOFF_4CH || defined SONOFF_IFAN02
#define REL_PIN1 12
#define KEY_PIN1 0
#define REL_PIN2 5
#define KEY_PIN2 9
#define REL_PIN3 4
#define KEY_PIN3 10
#define REL_PIN4 15
#define KEY_PIN4 14
#define LED_PIN1 13
#endif
#if defined ECOPLUG
#define REL_PIN1 2
#define LED_PIN1 13
#define KEY_PIN1 0
#define EXT_PIN 14
#endif
#ifdef SONOFF_TH
#define REL_PIN1 12
#define LED_PIN1 13
#define KEY_PIN1 0
#endif
#if defined SONOFF || defined SONOFF_TH
#define EXT_PIN 14
#define DS_PIN 14
#define DHTTYPE DHT22
DHT dht(EXT_PIN, DHTTYPE);
float temperature;
float humidity;
OneWire oneWire(DS_PIN);
DallasTemperature ds18b20(&oneWire);
double _dsTemperature = 0;
double getDSTemperature() {
return _dsTemperature;
}
void dsSetup() {
ds18b20.begin();
}
#endif
#if defined SONOFF || defined SONOFF_S20 || defined SONOFF_TOUCH || defined SONOFF_TH || defined ECOPLUG || defined SONOFF_DUAL || defined SONOFF_4CH || defined SONOFF_IFAN02 || defined SHELLY
#define LEDoff1 digitalWrite(LED_PIN1,HIGH)
#define LEDon1 digitalWrite(LED_PIN1,LOW)
#else
#define LEDoff1 digitalWrite(LED_PIN1,LOW)
#define LEDon1 digitalWrite(LED_PIN1,HIGH)
#endif
#if defined SONOFF
#define Relayoff1 {\
if (Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,LOW); \
Settings.currentState1 = false; \
LEDoff1; \
}
#define Relayon1 {\
if (!Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,HIGH); \
Settings.currentState1 = true; \
LEDon1; \
}
#elif defined SONOFF_DUAL
#define Relayoff1 {\
byte byteValue; \
if (Settings.currentState1) needUpdate1 = true; \
if (Settings.currentState2) byteValue = 0x02; \
else byteValue = 0x00; \
Serial.flush(); \
Serial.write(0xA0); \
Serial.write(0x04); \
Serial.write(byteValue); \
Serial.write(0xA1); \
Serial.flush(); \
Settings.currentState1 = false; \
}
#define Relayon1 {\
byte byteValue; \
if (!Settings.currentState1) needUpdate1 = true; \
if (Settings.currentState2) byteValue = 0x03; \
else byteValue = 0x01; \
Serial.flush(); \
Serial.write(0xA0); \
Serial.write(0x04); \
Serial.write(byteValue); \
Serial.write(0xA1); \
Serial.flush(); \
Settings.currentState1 = true; \
}
#define Relayoff2 {\
byte byteValue; \
if (Settings.currentState2) needUpdate2 = true; \
if (Settings.currentState1) byteValue = 0x01; \
else byteValue = 0x00; \
Serial.flush(); \
Serial.write(0xA0); \
Serial.write(0x04); \
Serial.write(byteValue); \
Serial.write(0xA1); \
Serial.flush(); \
Settings.currentState2 = false; \
}
#define Relayon2 {\
byte byteValue; \
if (!Settings.currentState2) needUpdate2 = true; \
if (Settings.currentState1) byteValue = 0x03; \
else byteValue = 0x02; \
Serial.flush(); \
Serial.write(0xA0); \
Serial.write(0x04); \
Serial.write(byteValue); \
Serial.write(0xA1); \
Serial.flush(); \
Settings.currentState2 = true; \
}
#elif defined SONOFF_IFAN02
#define Relayoff1 {\
if (Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,LOW); \
Settings.currentState1 = false; \
}
#define Relayon1 {\
if (!Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,HIGH); \
Settings.currentState1 = true; \
}
#define Relayoff2 {\
digitalWrite(REL_PIN2,LOW); \
Settings.currentState2 = false; \
}
#define Relayon2 {\
digitalWrite(REL_PIN2,HIGH); \
Settings.currentState2 = true; \
}
#define Relayoff3 {\
digitalWrite(REL_PIN3,LOW); \
Settings.currentState3 = false; \
}
#define Relayon3 {\
digitalWrite(REL_PIN3,HIGH); \
Settings.currentState3 = true; \
}
#define Relayoff4 {\
digitalWrite(REL_PIN4,LOW); \
Settings.currentState4 = false; \
}
#define Relayon4 {\
digitalWrite(REL_PIN4,HIGH); \
Settings.currentState4 = true; \
}
#elif defined SONOFF_4CH
#define Relayoff1 {\
if (Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,LOW); \
Settings.currentState1 = false; \
}
#define Relayon1 {\
if (!Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,HIGH); \
Settings.currentState1 = true; \
}
#define Relayoff2 {\
if (Settings.currentState2) needUpdate2 = true; \
digitalWrite(REL_PIN2,LOW); \
Settings.currentState2 = false; \
}
#define Relayon2 {\
if (!Settings.currentState2) needUpdate2 = true; \
digitalWrite(REL_PIN2,HIGH); \
Settings.currentState2 = true; \
}
#define Relayoff3 {\
if (Settings.currentState3) needUpdate3 = true; \
digitalWrite(REL_PIN3,LOW); \
Settings.currentState3 = false; \
}
#define Relayon3 {\
if (!Settings.currentState3) needUpdate3 = true; \
digitalWrite(REL_PIN3,HIGH); \
Settings.currentState3 = true; \
}
#define Relayoff4 {\
if (Settings.currentState4) needUpdate4 = true; \
digitalWrite(REL_PIN4,LOW); \
Settings.currentState4 = false; \
}
#define Relayon4 {\
if (!Settings.currentState4) needUpdate4 = true; \
digitalWrite(REL_PIN4,HIGH); \
Settings.currentState4 = true; \
}
#else
#define Relayoff1 {\
if (Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,LOW); \
Settings.currentState1 = false; \
}
#define Relayon1 {\
if (!Settings.currentState1) needUpdate1 = true; \
digitalWrite(REL_PIN1,HIGH); \
Settings.currentState1 = true; \
}
#endif
byte mac[6];
boolean WebLoggedIn = false;
int WebLoggedInTimer = 2;
String printWebString = "";
boolean printToWeb = false;
#define DEFAULT_HAIP "0.0.0.0"
#define DEFAULT_HAPORT 39500
#define DEFAULT_RESETWIFI false
#define DEFAULT_POS 0
#define DEFAULT_CURRENT STATE ""
#define DEFAULT_IP "0.0.0.0"
#define DEFAULT_GATEWAY "0.0.0.0"
#define DEFAULT_SUBNET "0.0.0.0"
#define DEFAULT_DNS "0.0.0.0"
#define DEFAULT_USE_STATIC false
#define DEFAULT_LONG_PRESS false
#define DEFAULT_REALLY_LONG_PRESS false
#define DEFAULT_USE_PASSWORD false
#define DEFAULT_USE_PASSWORD_CONTROL false
#define DEFAULT_PASSWORD ""
#define DEFAULT_PORT 80
#define DEFAULT_SWITCH_TYPE 0
#define DEFAULT_AUTO_OFF1 0
#define DEFAULT_AUTO_OFF2 0
#define DEFAULT_AUTO_OFF3 0
#define DEFAULT_AUTO_OFF4 0
#define DEFAULT_UREPORT 60
#define DEFAULT_DEBOUNCE 20
#define DEFAULT_HOSTNAME ""
#ifdef SONOFF_POW
#define DEFAULT_WREPORT 60
#define DEFAULT_VREPORT 60
#define DEFAULT_AREPORT 60
#define DEFAULT_VAREPORT 120
#define DEFAULT_PFREPORT 240
#define DEFAULT_VOLTAGE 120
#endif
#ifdef SONOFF_TH
#define DEFAULT_SENSOR_TYPE 0
#define DEFAULT_USE_FAHRENHEIT true
#define DEFAULT_TREPORT 300
#define DEFAULT_HREPORT 300
#endif
#define DEFAULT_EXT_TYPE 0
struct SettingsStruct
{
byte haIP[4];
unsigned int haPort;
boolean resetWifi;
int powerOnState;
boolean currentState1;
byte IP[4];
byte Gateway[4];
byte Subnet[4];
byte DNS[4];
boolean useStatic;
boolean longPress;
boolean reallyLongPress;
boolean usePassword;
boolean usePasswordControl;
#if defined SONOFF || defined SONOFF_TOUCH || defined SONOFF_S20 || defined ECOPLUG || defined SHELLY
int usePort;
int switchType;
int autoOff1;
int uReport;
int debounce;
int externalType;
char hostName[26];
#endif
#ifdef SONOFF_POW
int wReport;
int vReport;
int aReport;
int vaReport;
int pfReport;
int usePort;
int switchType;
int autoOff1;
int uReport;
int debounce;
int externalType;
char hostName[26];
int voltage;
#endif
#ifdef SONOFF_TH
boolean useFahrenheit;
int usePort;
boolean settingsReboot;
int sensorType;
int switchType;
int autoOff1;
int uReport;
int tReport;
int hReport;
int debounce;
int externalType;
char hostName[26];
#endif
#if defined SONOFF_DUAL
int usePort;
int switchType;
int autoOff1;
boolean currentState2;
int autoOff2;
int uReport;
int debounce;
int externalType;
char hostName[26];
#endif
#if defined SONOFF_4CH || defined SONOFF_IFAN02
int usePort;
int switchType;
int autoOff1;
boolean currentState2;
int autoOff2;
boolean currentState3;
int autoOff3;
boolean currentState4;
int autoOff4;
int uReport;
int debounce;
int externalType;
char hostName[26];
#endif
} Settings;
struct SecurityStruct
{
char Password[26];
int settingsVersion;
} SecuritySettings;
// Start WiFi Server
std::unique_ptr<ESP8266WebServer> server;
String padHex(String hex) {
if (hex.length() == 1) {
hex = "0" + hex;
}
return hex;
}
void handleRoot() {
server->send(200, "application/json", "{\"message\":\"Sonoff Wifi Switch\"}");
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server->uri();
message += "\nMethod: ";
message += (server->method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server->args();
message += "\n";
for (uint8_t i = 0; i < server->args(); i++) {
message += " " + server->argName(i) + ": " + server->arg(i) + "\n";
}
server->send(404, "text/plain", message);
}
void addHeader(boolean showMenu, String& str)
{
boolean cssfile = false;
str += F("<script language=\"javascript\"><!--\n");
str += F("function dept_onchange(frmselect) {frmselect.submit();}\n");
str += F("//--></script>");
str += F("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>");
str += projectName;
str += F("</title>");
str += F("<style>");
str += F("* {font-family:sans-serif; font-size:12pt;}");
str += F("h1 {font-size:16pt; color:black;}");
str += F("h6 {font-size:10pt; color:black; text-align:center;}");
str += F(".button-menu {background-color:#ffffff; color:#000000; margin: 10px; text-decoration:none}");
str += F(".button-link {border-radius:0.3rem; padding:5px 15px; background-color:#000000; color:#fff; border:solid 1px #fff; text-decoration:none}");
str += F(".button-menu:hover {background:#ddddff;}");
str += F(".button-link:hover {background:#707070;}");
str += F("th {border-radius:0.3rem; padding:10px; background-color:black; color:#ffffff;}");
str += F("td {padding:7px;}");
str += F("table {color:black;}");
str += F(".div_l {float: left;}");
str += F(".div_r {float: right; margin: 2px; padding: 1px 10px; border-radius: 7px; background-color:#080; color:white;}");
str += F(".div_br {clear: both;}");
str += F("</style>");
str += F("</head>");
str += F("<center>");
}
void addFooter(String& str)
{
str += F("<h6><a href=\"http://smartlife.tech\">smartlife.tech</a></h6></body></center>");
}
void addMenu(String& str)
{
str += F("<a class=\"button-menu\" href=\".\">Main</a>");
str += F("<a class=\"button-menu\" href=\"advanced\">Advanced</a>");
str += F("<a class=\"button-menu\" href=\"control\">Control</a>");
str += F("<a class=\"button-menu\" href=\"update\">Firmware</a>");
}
void addRebootBanner(String& str)
{
if (needReboot == true) {
str += F("<TR><TD bgcolor='#A9A9A9' colspan='2' align='center'><font color='white'>Reboot needed for changes to take effect. <a href='/r'>Reboot</a></font>");
}
}
void relayControl(int relay, int value) {
switch (relay)
{
case 0: //All Switches
{
if (value == 0) {
Relayoff1;
#if defined SONOFF_DUAL
Relayoff2;
#endif
#if defined SONOFF_4CH || defined SONOFF_IFAN02
Relayoff2;
Relayoff3;
Relayoff4;
#endif
}
if (value == 1) {
if (!inAutoOff1) {
autoOffTimer1 = millis();
inAutoOff1 = true;
}
Relayon1;
#if defined SONOFF_DUAL
if (!inAutoOff2) {
autoOffTimer2 = millis();
inAutoOff2 = true;
}
Relayon2;
#endif
#if defined SONOFF_4CH || defined SONOFF_IFAN02
if (!inAutoOff2) {
autoOffTimer2 = millis();
inAutoOff2 = true;
}
Relayon2;
if (!inAutoOff3) {
autoOffTimer3 = millis();
inAutoOff3 = true;
}
Relayon3;
if (!inAutoOff4) {
autoOffTimer4 = millis();
inAutoOff4 = true;
}
Relayon4;
#endif
}
if (value == 2) {
if (Settings.currentState1) {
Relayoff1;
} else {
if (!inAutoOff1) {
autoOffTimer1 = millis();
inAutoOff1 = true;
}
Relayon1;
}
#if defined SONOFF_DUAL
if (Settings.currentState2) {
Relayoff2;
} else {
if (!inAutoOff2) {
autoOffTimer2 = millis();
inAutoOff2 = true;
}
Relayon2;
}
#endif
#if defined SONOFF_4CH || defined SONOFF_IFAN02
if (Settings.currentState2) {
Relayoff2;
} else {
if (!inAutoOff2) {
autoOffTimer2 = millis();
inAutoOff2 = true;
}
Relayon2;
}
if (Settings.currentState3) {
Relayoff3;
} else {
if (!inAutoOff3) {
autoOffTimer3 = millis();
inAutoOff3 = true;
}
Relayon3;
}
if (Settings.currentState4) {
Relayoff4;
} else {
if (!inAutoOff4) {
autoOffTimer4 = millis();
inAutoOff4 = true;
}
Relayon4;
}
#endif
}
break;
}
case 1: //Relay 1
{
if (value == 0) {
Relayoff1;
#ifdef SONOFF
LEDoff1;
#endif
}
if (value == 1) {
if (!inAutoOff1) {
autoOffTimer1 = millis();
inAutoOff1 = true;
}
Relayon1;
#ifdef SONOFF
LEDon1;
#endif
}
if (value == 2) {
if (Settings.currentState1) {
Relayoff1;
} else {
if (!inAutoOff1) {
autoOffTimer1 = millis();
inAutoOff1 = true;
}
Relayon1;
}
}
break;
}
case 2: //Relay 2
{
#if defined SONOFF_DUAL || defined SONOFF_4CH || defined SONOFF_IFAN02
if (value == 0) {
Relayoff2;
}
if (value == 1) {
if (!inAutoOff2) {
autoOffTimer2 = millis();
inAutoOff2 = true;
}
Relayon2;
}
if (value == 2) {
if (Settings.currentState2) {
Relayoff2;
} else {
if (!inAutoOff2) {
autoOffTimer2 = millis();
inAutoOff2 = true;
}
Relayon2;
}
}
#endif
break;
}
case 3: //Relay 3
{
#if defined SONOFF_4CH || defined SONOFF_IFAN02
if (value == 0) {
Relayoff3;
}
if (value == 1) {
if (!inAutoOff3) {
autoOffTimer3 = millis();
inAutoOff3 = true;
}
Relayon3;
}
if (value == 2) {
if (Settings.currentState3) {
Relayoff3;
} else {
if (!inAutoOff3) {
autoOffTimer3 = millis();
inAutoOff3 = true;
}
Relayon3;
}
}
#endif
break;
}
case 4: //Relay 4
{
#if defined SONOFF_4CH || defined SONOFF_IFAN02
if (value == 0) {
Relayoff4;
}
if (value == 1) {
if (!inAutoOff4) {
autoOffTimer4 = millis();
inAutoOff4 = true;
}
Relayon4;
}
if (value == 2) {
if (Settings.currentState4) {
Relayoff4;
} else {
if (!inAutoOff4) {
autoOffTimer4 = millis();
inAutoOff4 = true;
}
Relayon4;
}
}
#endif
break;
}
}
if (Settings.powerOnState == 2 || Settings.powerOnState == 3)
{
SaveSettings();
}
}
void relayToggle1() {
int reading = digitalRead(KEY_PIN1);
if (reading == LOW) {
current_low1 = millis();
state1 = LOW;
}
if (reading == HIGH && state1 == LOW)
{
current_high1 = millis();
state1 = HIGH;
if ((current_high1 - current_low1) > (Settings.debounce? Settings.debounce : debounceDelay) && (current_high1 - current_low1) < 10000)
{
relayControl(1, 2);
shortPress = true;
}
else if ((current_high1 - current_low1) >= 10000 && (current_high1 - current_low1) < 20000)
{
Settings.longPress = true;
SaveSettings();
ESP.restart();
}
else if ((current_high1 - current_low1) >= 20000 && (current_high1 - current_low1) < 60000)
{
Settings.reallyLongPress = true;
SaveSettings();
ESP.restart();
}
}
}
#if defined SONOFF_4CH || defined SONOFF_IFAN02
void relayToggle2() {
int reading = digitalRead(KEY_PIN2);
if (reading == LOW) {
current_low2 = millis();
state2 = LOW;
}
if (reading == HIGH && state2 == LOW)
{
current_high2 = millis();
state2 = HIGH;
if ((current_high2 - current_low2) > (Settings.debounce? Settings.debounce : debounceDelay) && (current_high2 - current_low2) < 10000)
{
relayControl(2, 2);
}
else if ((current_high2 - current_low2) >= 10000 && (current_high2 - current_low2) < 20000)
{
}
else if ((current_high2 - current_low2) >= 20000 && (current_high2 - current_low2) < 60000)
{
}
}
}
void relayToggle3() {
int reading = digitalRead(KEY_PIN3);
if (reading == LOW) {
current_low3 = millis();
state3 = LOW;
}
if (reading == HIGH && state3 == LOW)
{
current_high3 = millis();
state3 = HIGH;
if ((current_high3 - current_low3) > (Settings.debounce? Settings.debounce : debounceDelay) && (current_high3 - current_low3) < 10000)
{
relayControl(3, 2);
}
else if ((current_high3 - current_low3) >= 10000 && (current_high3 - current_low3) < 20000)
{
}
else if ((current_high3 - current_low3) >= 20000 && (current_high3 - current_low3) < 60000)
{
}
}
}
void relayToggle4() {
int reading = digitalRead(KEY_PIN4);
if (reading == LOW) {
current_low4 = millis();
state4 = LOW;
}
if (reading == HIGH && state4 == LOW)
{
current_high4 = millis();
state4 = HIGH;
if ((current_high4 - current_low4) > (Settings.debounce? Settings.debounce : debounceDelay) && (current_high4 - current_low4) < 10000)
{
relayControl(4, 2);
}
else if ((current_high4 - current_low4) >= 10000 && (current_high4 - current_low4) < 20000)
{
}
else if ((current_high4 - current_low4) >= 20000 && (current_high4 - current_low4) < 60000)
{
}
}
}
#endif
#if defined SONOFF || defined SONOFF_TH || defined SHELLY
void extRelayToggle() {
if (Settings.externalType > 0) {
int reading = digitalRead(EXT_PIN);
if (reading == LOW && state_ext == HIGH) {
current_low_ext = millis();
state_ext = LOW;
if ((current_low_ext - current_high_ext) > (Settings.debounce? Settings.debounce : debounceDelay)) {
relayControl(1, 2);
}
}
if (reading == HIGH && state_ext == LOW)
{
current_high_ext = millis();
state_ext = HIGH;
if ((current_high_ext - current_low_ext) > (Settings.debounce? Settings.debounce : debounceDelay))
{
if (Settings.externalType == 4) {
relayControl(1, 2);
}
}
}
} else {
//External switch has been disabled
}