forked from arendst/Tasmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport.ino
2172 lines (1963 loc) · 60.2 KB
/
support.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
/*
support.ino - support for Sonoff-Tasmota
Copyright (C) 2018 Theo Arends
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/>.
*/
IPAddress syslog_host_addr; // Syslog host IP address
uint32_t syslog_host_hash = 0; // Syslog host name hash
/*********************************************************************************************\
* Watchdog extension (https://github.com/esp8266/Arduino/issues/1532)
\*********************************************************************************************/
Ticker tickerOSWatch;
#define OSWATCH_RESET_TIME 120
static unsigned long oswatch_last_loop_time;
byte oswatch_blocked_loop = 0;
#ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves exception
//void OsWatchTicker() ICACHE_RAM_ATTR;
#endif // USE_WS2812_DMA
#ifdef USE_KNX
bool knx_started = false;
#endif // USE_KNX
void OsWatchTicker()
{
unsigned long t = millis();
unsigned long last_run = abs(t - oswatch_last_loop_time);
#ifdef DEBUG_THEO
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_OSWATCH " FreeRam %d, rssi %d, last_run %d"), ESP.getFreeHeap(), WifiGetRssiAsQuality(WiFi.RSSI()), last_run);
AddLog(LOG_LEVEL_DEBUG);
#endif // DEBUG_THEO
if (last_run >= (OSWATCH_RESET_TIME * 1000)) {
// AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_OSWATCH " " D_BLOCKED_LOOP ". " D_RESTARTING)); // Save iram space
RtcSettings.oswatch_blocked_loop = 1;
RtcSettingsSave();
// ESP.restart(); // normal reboot
ESP.reset(); // hard reset
}
}
void OsWatchInit()
{
oswatch_blocked_loop = RtcSettings.oswatch_blocked_loop;
RtcSettings.oswatch_blocked_loop = 0;
oswatch_last_loop_time = millis();
tickerOSWatch.attach_ms(((OSWATCH_RESET_TIME / 3) * 1000), OsWatchTicker);
}
void OsWatchLoop()
{
oswatch_last_loop_time = millis();
// while(1) delay(1000); // this will trigger the os watch
}
String GetResetReason()
{
char buff[32];
if (oswatch_blocked_loop) {
strncpy_P(buff, PSTR(D_JSON_BLOCKED_LOOP), sizeof(buff));
return String(buff);
} else {
return ESP.getResetReason();
}
}
/*********************************************************************************************\
* Miscellaneous
\*********************************************************************************************/
#ifdef ARDUINO_ESP8266_RELEASE_2_3_0
// Functions not available in 2.3.0
// http://clc-wiki.net/wiki/C_standard_library:string.h:memchr
void* memchr(const void* ptr, int value, size_t num)
{
unsigned char *p = (unsigned char*)ptr;
while (num--) {
if (*p != (unsigned char)value) {
p++;
} else {
return p;
}
}
return 0;
}
// http://clc-wiki.net/wiki/C_standard_library:string.h:strspn
// Get span until any character in string
size_t strcspn(const char *str1, const char *str2)
{
size_t ret = 0;
while (*str1) {
if (strchr(str2, *str1)) { // Slow
return ret;
} else {
str1++;
ret++;
}
}
return ret;
}
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
// Get span until single character in string
size_t strchrspn(const char *str1, int character)
{
size_t ret = 0;
char *start = (char*)str1;
char *end = strchr(str1, character);
if (end) ret = end - start;
return ret;
}
// Function to return a substring defined by a delimiter at an index
char* subStr(char* dest, char* str, const char *delim, int index)
{
char *act;
char *sub;
char *ptr;
int i;
// Since strtok consumes the first arg, make a copy
strncpy(dest, str, strlen(str));
for (i = 1, act = dest; i <= index; i++, act = NULL) {
sub = strtok_r(act, delim, &ptr);
if (sub == NULL) break;
}
sub = Trim(sub);
return sub;
}
double CharToDouble(char *str)
{
// simple ascii to double, because atof or strtod are too large
char strbuf[24];
strcpy(strbuf, str);
char *pt;
double left = atoi(strbuf);
double right = 0;
short len = 0;
pt = strtok (strbuf, ".");
if (pt) {
pt = strtok (NULL, ".");
if (pt) {
right = atoi(pt);
len = strlen(pt);
double fac = 1;
while (len) {
fac /= 10.0;
len--;
}
// pow is also very large
//double fac=pow(10,-len);
right *= fac;
}
}
double result = left + right;
if (left < 0) { result = left - right; }
return result;
}
char* dtostrfd(double number, unsigned char prec, char *s)
{
return dtostrf(number, 1, prec, s);
}
char* Unescape(char* buffer, uint16_t* size)
{
uint8_t* read = (uint8_t*)buffer;
uint8_t* write = (uint8_t*)buffer;
uint16_t start_size = *size;
uint16_t end_size = *size;
uint8_t che = 0;
while (start_size > 0) {
uint8_t ch = *read++;
start_size--;
if (ch != '\\') {
*write++ = ch;
} else {
if (start_size > 0) {
uint8_t chi = *read++;
start_size--;
end_size--;
switch (chi) {
case '\\': che = '\\'; break; // 5C Backslash
case 'a': che = '\a'; break; // 07 Bell (Alert)
case 'b': che = '\b'; break; // 08 Backspace
case 'e': che = '\e'; break; // 1B Escape
case 'f': che = '\f'; break; // 0C Formfeed
case 'n': che = '\n'; break; // 0A Linefeed (Newline)
case 'r': che = '\r'; break; // 0D Carriage return
case 's': che = ' '; break; // 20 Space
case 't': che = '\t'; break; // 09 Horizontal tab
case 'v': che = '\v'; break; // 0B Vertical tab
// case '?': che = '\?'; break; // 3F Question mark
default : {
che = chi;
*write++ = ch;
end_size++;
}
}
*write++ = che;
}
}
}
*size = end_size;
return buffer;
}
char* UpperCase(char* dest, const char* source)
{
char* write = dest;
const char* read = source;
char ch = '.';
while (ch != '\0') {
ch = *read++;
*write++ = toupper(ch);
}
return dest;
}
char* UpperCase_P(char* dest, const char* source)
{
char* write = dest;
const char* read = source;
char ch = '.';
while (ch != '\0') {
ch = pgm_read_byte(read++);
*write++ = toupper(ch);
}
return dest;
}
char* LTrim(char* p)
{
while ((*p != '\0') && (isblank(*p))) {
p++; // Trim leading spaces
}
return p;
}
char* RTrim(char* p)
{
char* q = p + strlen(p) -1;
while ((q >= p) && (isblank(*q))) {
q--; // Trim trailing spaces
}
q++;
*q = '\0';
return p;
}
char* Trim(char* p)
{
if (*p == '\0') { return p; }
while (isspace(*p)) { p++; } // Trim leading spaces
if (*p == '\0') { return p; }
char* q = p + strlen(p) -1;
while (isspace(*q) && q >= p) { q--; } // Trim trailing spaces
q++;
*q = '\0';
return p;
}
char* NoAlNumToUnderscore(char* dest, const char* source)
{
char* write = dest;
const char* read = source;
char ch = '.';
while (ch != '\0') {
ch = *read++;
*write++ = (isalnum(ch) || ('\0' == ch)) ? ch : '_';
}
return dest;
}
boolean ParseIp(uint32_t* addr, const char* str)
{
uint8_t *part = (uint8_t*)addr;
byte i;
*addr = 0;
for (i = 0; i < 4; i++) {
part[i] = strtoul(str, NULL, 10); // Convert byte
str = strchr(str, '.');
if (str == NULL || *str == '\0') {
break; // No more separators, exit
}
str++; // Point to next character after separator
}
return (3 == i);
}
void MakeValidMqtt(byte option, char* str)
{
// option 0 = replace by underscore
// option 1 = delete character
uint16_t i = 0;
while (str[i] > 0) {
// if ((str[i] == '/') || (str[i] == '+') || (str[i] == '#') || (str[i] == ' ')) {
if ((str[i] == '+') || (str[i] == '#') || (str[i] == ' ')) {
if (option) {
uint16_t j = i;
while (str[j] > 0) {
str[j] = str[j +1];
j++;
}
i--;
} else {
str[i] = '_';
}
}
i++;
}
}
// Function to parse & check if version_str is newer than our currently installed version.
bool NewerVersion(char* version_str)
{
uint32_t version = 0;
uint8_t i = 0;
char *str_ptr;
char* version_dup = strdup(version_str); // Duplicate the version_str as strtok_r will modify it.
if (!version_dup) {
return false; // Bail if we can't duplicate. Assume bad.
}
// Loop through the version string, splitting on '.' seperators.
for (char *str = strtok_r(version_dup, ".", &str_ptr); str && i < sizeof(VERSION); str = strtok_r(NULL, ".", &str_ptr), i++) {
int field = atoi(str);
// The fields in a version string can only range from 0-255.
if ((field < 0) || (field > 255)) {
free(version_dup);
return false;
}
// Shuffle the accumulated bytes across, and add the new byte.
version = (version << 8) + field;
// Check alpha delimiter after 1.2.3 only
if ((2 == i) && isalpha(str[strlen(str)-1])) {
field = str[strlen(str)-1] & 0x1f;
version = (version << 8) + field;
i++;
}
}
free(version_dup); // We no longer need this.
// A version string should have 2-4 fields. e.g. 1.2, 1.2.3, or 1.2.3a (= 1.2.3.1).
// If not, then don't consider it a valid version string.
if ((i < 2) || (i > sizeof(VERSION))) {
return false;
}
// Keep shifting the parsed version until we hit the maximum number of tokens.
// VERSION stores the major number of the version in the most significant byte of the uint32_t.
while (i < sizeof(VERSION)) {
version <<= 8;
i++;
}
// Now we should have a fully constructed version number in uint32_t form.
return (version > VERSION);
}
char* GetPowerDevice(char* dest, uint8_t idx, size_t size, uint8_t option)
{
char sidx[8];
strncpy_P(dest, S_RSLT_POWER, size); // POWER
if ((devices_present + option) > 1) {
snprintf_P(sidx, sizeof(sidx), PSTR("%d"), idx); // x
strncat(dest, sidx, size); // POWERx
}
return dest;
}
char* GetPowerDevice(char* dest, uint8_t idx, size_t size)
{
return GetPowerDevice(dest, idx, size, 0);
}
float ConvertTemp(float c)
{
float result = c;
if (!isnan(c) && Settings.flag.temperature_conversion) {
result = c * 1.8 + 32; // Fahrenheit
}
return result;
}
char TempUnit()
{
return (Settings.flag.temperature_conversion) ? 'F' : 'C';
}
void SetGlobalValues(float temperature, float humidity)
{
global_update = uptime;
global_temperature = temperature;
global_humidity = humidity;
}
void ResetGlobalValues()
{
if ((uptime - global_update) > GLOBAL_VALUES_VALID) { // Reset after 5 minutes
global_update = 0;
global_temperature = 0;
global_humidity = 0;
}
}
double FastPrecisePow(double a, double b)
{
// https://martin.ankerl.com/2012/01/25/optimized-approximative-pow-in-c-and-cpp/
// calculate approximation with fraction of the exponent
int e = (int)b;
union {
double d;
int x[2];
} u = { a };
u.x[1] = (int)((b - e) * (u.x[1] - 1072632447) + 1072632447);
u.x[0] = 0;
// exponentiation by squaring with the exponent's integer part
// double r = u.d makes everything much slower, not sure why
double r = 1.0;
while (e) {
if (e & 1) {
r *= a;
}
a *= a;
e >>= 1;
}
return r * u.d;
}
char* GetTextIndexed(char* destination, size_t destination_size, uint16_t index, const char* haystack)
{
// Returns empty string if not found
// Returns text of found
char* write = destination;
const char* read = haystack;
index++;
while (index--) {
size_t size = destination_size -1;
write = destination;
char ch = '.';
while ((ch != '\0') && (ch != '|')) {
ch = pgm_read_byte(read++);
if (size && (ch != '|')) {
*write++ = ch;
size--;
}
}
if (0 == ch) {
if (index) {
write = destination;
}
break;
}
}
*write = '\0';
return destination;
}
int GetCommandCode(char* destination, size_t destination_size, const char* needle, const char* haystack)
{
// Returns -1 of not found
// Returns index and command if found
int result = -1;
const char* read = haystack;
char* write = destination;
while (true) {
result++;
size_t size = destination_size -1;
write = destination;
char ch = '.';
while ((ch != '\0') && (ch != '|')) {
ch = pgm_read_byte(read++);
if (size && (ch != '|')) {
*write++ = ch;
size--;
}
}
*write = '\0';
if (!strcasecmp(needle, destination)) {
break;
}
if (0 == ch) {
result = -1;
break;
}
}
return result;
}
int GetStateNumber(char *state_text)
{
char command[CMDSZ];
int state_number = -1;
if ((GetCommandCode(command, sizeof(command), state_text, kOptionOff) >= 0) || !strcasecmp(state_text, Settings.state_text[0])) {
state_number = 0;
}
else if ((GetCommandCode(command, sizeof(command), state_text, kOptionOn) >= 0) || !strcasecmp(state_text, Settings.state_text[1])) {
state_number = 1;
}
else if ((GetCommandCode(command, sizeof(command), state_text, kOptionToggle) >= 0) || !strcasecmp(state_text, Settings.state_text[2])) {
state_number = 2;
}
else if (GetCommandCode(command, sizeof(command), state_text, kOptionBlink) >= 0) {
state_number = 3;
}
else if (GetCommandCode(command, sizeof(command), state_text, kOptionBlinkOff) >= 0) {
state_number = 4;
}
return state_number;
}
boolean GetUsedInModule(byte val, uint8_t *arr)
{
int offset = 0;
if (!val) { return false; } // None
#ifndef USE_I2C
if (GPIO_I2C_SCL == val) { return true; }
if (GPIO_I2C_SDA == val) { return true; }
#endif
#ifndef USE_SR04
if (GPIO_SR04_TRIG == val) { return true; }
if (GPIO_SR04_ECHO == val) { return true; }
#endif
#ifndef USE_WS2812
if (GPIO_WS2812 == val) { return true; }
#endif
#ifndef USE_IR_REMOTE
if (GPIO_IRSEND == val) { return true; }
#ifndef USE_IR_RECEIVE
if (GPIO_IRRECV == val) { return true; }
#endif
#endif
#ifndef USE_MHZ19
if (GPIO_MHZ_TXD == val) { return true; }
if (GPIO_MHZ_RXD == val) { return true; }
#endif
#ifndef USE_PZEM004T
if (GPIO_PZEM_TX == val) { return true; }
if (GPIO_PZEM_RX == val) { return true; }
#endif
#ifndef USE_SENSEAIR
if (GPIO_SAIR_TX == val) { return true; }
if (GPIO_SAIR_RX == val) { return true; }
#endif
#ifndef USE_SPI
if (GPIO_SPI_CS == val) { return true; }
if (GPIO_SPI_DC == val) { return true; }
#endif
#ifndef USE_DISPLAY
if (GPIO_BACKLIGHT == val) { return true; }
#endif
#ifndef USE_PMS5003
if (GPIO_PMS5003 == val) { return true; }
#endif
#ifndef USE_NOVA_SDS
if (GPIO_SDS0X1 == val) { return true; }
#endif
#ifndef USE_SERIAL_BRIDGE
if (GPIO_SBR_TX == val) { return true; }
if (GPIO_SBR_RX == val) { return true; }
#endif
#ifndef USE_SR04
if (GPIO_SR04_TRIG == val) { return true; }
if (GPIO_SR04_ECHO == val) { return true; }
#endif
#ifndef USE_SDM120
if (GPIO_SDM120_TX == val) { return true; }
if (GPIO_SDM120_RX == val) { return true; }
#endif
#ifndef USE_SDM630
if (GPIO_SDM630_TX == val) { return true; }
if (GPIO_SDM630_RX == val) { return true; }
#endif
#ifndef USE_TM1638
if (GPIO_TM16CLK == val) { return true; }
if (GPIO_TM16DIO == val) { return true; }
if (GPIO_TM16STB == val) { return true; }
#endif
if ((val >= GPIO_REL1) && (val < GPIO_REL1 + MAX_RELAYS)) {
offset = (GPIO_REL1_INV - GPIO_REL1);
}
if ((val >= GPIO_REL1_INV) && (val < GPIO_REL1_INV + MAX_RELAYS)) {
offset = -(GPIO_REL1_INV - GPIO_REL1);
}
if ((val >= GPIO_LED1) && (val < GPIO_LED1 + MAX_LEDS)) {
offset = (GPIO_LED1_INV - GPIO_LED1);
}
if ((val >= GPIO_LED1_INV) && (val < GPIO_LED1_INV + MAX_LEDS)) {
offset = -(GPIO_LED1_INV - GPIO_LED1);
}
if ((val >= GPIO_PWM1) && (val < GPIO_PWM1 + MAX_PWMS)) {
offset = (GPIO_PWM1_INV - GPIO_PWM1);
}
if ((val >= GPIO_PWM1_INV) && (val < GPIO_PWM1_INV + MAX_PWMS)) {
offset = -(GPIO_PWM1_INV - GPIO_PWM1);
}
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
if (arr[i] == val) { return true; }
if (arr[i] == val + offset) { return true; }
}
return false;
}
void SetSerialBaudrate(int baudrate)
{
Settings.baudrate = baudrate / 1200;
if (Serial.baudRate() != baudrate) {
if (seriallog_level) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_SET_BAUDRATE_TO " %d"), baudrate);
AddLog(LOG_LEVEL_INFO);
}
delay(100);
Serial.flush();
Serial.begin(baudrate, serial_config);
delay(10);
Serial.println();
}
}
void ClaimSerial()
{
serial_local = 1;
AddLog_P(LOG_LEVEL_INFO, PSTR("SNS: Hardware Serial"));
SetSeriallog(LOG_LEVEL_NONE);
baudrate = Serial.baudRate();
Settings.baudrate = baudrate / 1200;
}
uint32_t GetHash(const char *buffer, size_t size)
{
uint32_t hash = 0;
for (uint16_t i = 0; i <= size; i++) {
hash += (uint8_t)*buffer++ * (i +1);
}
return hash;
}
void ShowSource(int source)
{
if ((source > 0) && (source < SRC_MAX)) {
char stemp1[20];
snprintf_P(log_data, sizeof(log_data), PSTR("SRC: %s"), GetTextIndexed(stemp1, sizeof(stemp1), source, kCommandSource));
AddLog(LOG_LEVEL_DEBUG);
}
}
/*********************************************************************************************\
* Fill feature list
\*********************************************************************************************/
void GetFeatures()
{
feature_drv1 = 0x00000000; // xdrv_01_mqtt.ino, xdrv_01_light.ino, xdrv_04_snfbridge.ino
// feature_drv1 |= 0x00000001;
// feature_drv1 |= 0x00000002;
#ifdef USE_I2C
feature_drv1 |= 0x00000004; // sonoff.ino
#endif
#ifdef USE_SPI
feature_drv1 |= 0x00000008; // sonoff.ino
#endif
#ifdef USE_DISCOVERY
feature_drv1 |= 0x00000010; // sonoff.ino
#endif
#ifdef USE_ARDUINO_OTA
feature_drv1 |= 0x00000020; // sonoff.ino
#endif
#ifdef USE_MQTT_TLS
feature_drv1 |= 0x00000040; // sonoff.ino
#endif
#ifdef USE_WEBSERVER
feature_drv1 |= 0x00000080; // xdrv_02_webserver.ino
#endif
#ifdef WEBSERVER_ADVERTISE
feature_drv1 |= 0x00000100; // xdrv_02_webserver.ino
#endif
#ifdef USE_EMULATION
feature_drv1 |= 0x00000200; // xplg_wemohue.ino
#endif
#if (MQTT_LIBRARY_TYPE == MQTT_PUBSUBCLIENT)
feature_drv1 |= 0x00000400; // xdrv_01_mqtt.ino
#endif
#if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT)
feature_drv1 |= 0x00000800; // xdrv_01_mqtt.ino
#endif
#if (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO)
feature_drv1 |= 0x00001000; // xdrv_01_mqtt.ino
#endif
#ifdef MQTT_HOST_DISCOVERY
feature_drv1 |= 0x00002000; // xdrv_01_mqtt.ino
#endif
#ifdef USE_ARILUX_RF
feature_drv1 |= 0x00004000; // xdrv_04_light.ino
#endif
#ifdef USE_WS2812
feature_drv1 |= 0x00008000; // xdrv_04_light.ino
#endif
#ifdef USE_WS2812_DMA
feature_drv1 |= 0x00010000; // xdrv_04_light.ino
#endif
#ifdef USE_IR_REMOTE
feature_drv1 |= 0x00020000; // xdrv_05_irremote.ino
#endif
#ifdef USE_IR_HVAC
feature_drv1 |= 0x00040000; // xdrv_05_irremote.ino
#endif
#ifdef USE_IR_RECEIVE
feature_drv1 |= 0x00080000; // xdrv_05_irremote.ino
#endif
#ifdef USE_DOMOTICZ
feature_drv1 |= 0x00100000; // xdrv_07_domoticz.ino
#endif
#ifdef USE_DISPLAY
feature_drv1 |= 0x00200000; // xdrv_98_display.ino
#endif
#ifdef USE_HOME_ASSISTANT
feature_drv1 |= 0x00400000; // xdrv_12_home_assistant.ino
#endif
#ifdef USE_SERIAL_BRIDGE
feature_drv1 |= 0x00800000; // xdrv_08_serial_bridge.ino
#endif
#ifdef USE_TIMERS
feature_drv1 |= 0x01000000; // xdrv_09_timers.ino
#endif
#ifdef USE_SUNRISE
feature_drv1 |= 0x02000000; // xdrv_09_timers.ino
#endif
#ifdef USE_TIMERS_WEB
feature_drv1 |= 0x04000000; // xdrv_09_timers.ino
#endif
#ifdef USE_RULES
feature_drv1 |= 0x08000000; // xdrv_10_rules.ino
#endif
#ifdef USE_KNX
feature_drv1 |= 0x10000000; // xdrv_11_knx.ino
#endif
#ifdef USE_WPS
feature_drv1 |= 0x20000000; // support.ino
#endif
#ifdef USE_SMARTCONFIG
feature_drv1 |= 0x40000000; // support.ino
#endif
/*********************************************************************************************/
feature_drv2 = 0x00000000;
#ifdef USE_CONFIG_OVERRIDE
feature_drv2 |= 0x00000001; // user_config(_override).h
#endif
#ifdef BE_MINIMAL
feature_drv2 |= 0x00000002; // user_config(_override).h
#endif
#ifdef USE_ALL_SENSORS
feature_drv2 |= 0x00000004; // user_config(_override).h
#endif
#ifdef USE_CLASSIC
feature_drv2 |= 0x00000008; // user_config(_override).h
#endif
#ifdef USE_KNX_NO_EMULATION
feature_drv2 |= 0x00000010; // user_config(_override).h
#endif
#ifdef VTABLES_IN_FLASH
feature_drv2 |= 0x04000000; // platformio.ini
#endif
#ifdef PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH
feature_drv2 |= 0x08000000; // platformio.ini
#endif
#ifdef PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
feature_drv2 |= 0x10000000; // platformio.ini
#endif
#ifdef PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
feature_drv2 |= 0x20000000; // platformio.ini
#endif
#ifdef DEBUG_THEO
feature_drv2 |= 0x40000000; // xdrv_99_debug.ino
#endif
#ifdef USE_DEBUG_DRIVER
feature_drv2 |= 0x80000000; // xdrv_99_debug.ino
#endif
/*********************************************************************************************/
feature_sns1 = 0x00000000; // xsns_01_counter.ino, xsns_04_snfsc.ino
// feature_sns1 |= 0x00000001;
#ifdef USE_ADC_VCC
feature_sns1 |= 0x00000002; // support.ino (ADC)
#endif
#ifdef USE_ENERGY_SENSOR
feature_sns1 |= 0x00000004; // xdrv_03_energy.ino
#endif
#ifdef USE_PZEM004T
feature_sns1 |= 0x00000008; // xdrv_03_energy.ino
#endif
#ifdef USE_DS18B20
feature_sns1 |= 0x00000010; // xsns_05_ds18b20.ino
#endif
#ifdef USE_DS18x20_LEGACY
feature_sns1 |= 0x00000020; // xsns_05_ds18x20_legacy.ino
#endif
#ifdef USE_DS18x20
feature_sns1 |= 0x00000040; // xsns_05_ds18x20.ino
#endif
#ifdef USE_DHT
feature_sns1 |= 0x00000080; // xsns_06_dht.ino
#endif
#ifdef USE_SHT
feature_sns1 |= 0x00000100; // xsns_07_sht1x.ino
#endif
#ifdef USE_HTU
feature_sns1 |= 0x00000200; // xsns_08_htu21.ino
#endif
#ifdef USE_BMP
feature_sns1 |= 0x00000400; // xsns_09_bmp.ino
#endif
#ifdef USE_BME680
feature_sns1 |= 0x00000800; // xsns_09_bmp.ino - BME680
#endif
#ifdef USE_BH1750
feature_sns1 |= 0x00001000; // xsns_10_bh1750.ino
#endif
#ifdef USE_VEML6070
feature_sns1 |= 0x00002000; // xsns_11_veml6070.ino
#endif
#ifdef USE_ADS1115_I2CDEV
feature_sns1 |= 0x00004000; // xsns_12_ads1115_i2cdev.ino
#endif
#ifdef USE_ADS1115
feature_sns1 |= 0x00008000; // xsns_12_ads1115.ino
#endif
#ifdef USE_INA219
feature_sns1 |= 0x00010000; // xsns_13_ina219.ino
#endif
#ifdef USE_SHT3X
feature_sns1 |= 0x00020000; // xsns_14_sht3x.ino
#endif
#ifdef USE_MHZ19
feature_sns1 |= 0x00040000; // xsns_15_mhz19.ino
#endif
#ifdef USE_TSL2561
feature_sns1 |= 0x00080000; // xsns_16_tsl2561.ino
#endif
#ifdef USE_SENSEAIR
feature_sns1 |= 0x00100000; // xsns_17_senseair.ino
#endif
#ifdef USE_PMS5003
feature_sns1 |= 0x00200000; // xsns_18_pms5003.ino
#endif
#ifdef USE_MGS
feature_sns1 |= 0x00400000; // xsns_19_mgs.ino
#endif
#ifdef USE_NOVA_SDS
feature_sns1 |= 0x00800000; // xsns_20_novasds.ino
#endif
#ifdef USE_SGP30
feature_sns1 |= 0x01000000; // xsns_21_sgp30.ino
#endif
#ifdef USE_SR04
feature_sns1 |= 0x02000000; // xsns_22_sr04.ino
#endif
#ifdef USE_SDM120
feature_sns1 |= 0x04000000; // xsns_23_sdm120.ino
#endif
#ifdef USE_SI1145
feature_sns1 |= 0x08000000; // xsns_24_si1145.ino
#endif
#ifdef USE_SDM630
feature_sns1 |= 0x10000000; // xsns_25_sdm630.ino
#endif
#ifdef USE_LM75AD
feature_sns1 |= 0x20000000; // xsns_26_lm75ad.ino
#endif
#ifdef USE_APDS9960
feature_sns1 |= 0x40000000; // xsns_27_apds9960.ino
#endif
#ifdef USE_TM1638
feature_sns1 |= 0x80000000; // xsns_28_tm1638.ino
#endif
/*********************************************************************************************/
feature_sns2 = 0x00000000;
#ifdef USE_MCP230xx
feature_sns2 |= 0x00000001; // xsns_29_mcp230xx.ino
#endif
#ifdef USE_MPR121
feature_sns2 |= 0x00000002; // xsns_30_mpr121.ino
#endif
#ifdef USE_CCS811
feature_sns2 |= 0x00000004; // xsns_31_ccs811.ino
#endif
#ifdef USE_MPU6050
feature_sns2 |= 0x00000008; // xsns_32_mpu6050.ino
#endif
}
/*********************************************************************************************\
* Wifi
\*********************************************************************************************/
#define WIFI_CONFIG_SEC 180 // seconds before restart
#define WIFI_CHECK_SEC 20 // seconds
#define WIFI_RETRY_OFFSET_SEC 20 // seconds
uint8_t wifi_counter;
uint8_t wifi_retry_init;
uint8_t wifi_retry;
uint8_t wifi_status;
uint8_t wps_result;
uint8_t wifi_config_type = 0;
uint8_t wifi_config_counter = 0;
int WifiGetRssiAsQuality(int rssi)
{
int quality = 0;
if (rssi <= -100) {
quality = 0;
} else if (rssi >= -50) {
quality = 100;
} else {
quality = 2 * (rssi + 100);
}
return quality;
}
boolean WifiConfigCounter()
{
if (wifi_config_counter) {
wifi_config_counter = WIFI_CONFIG_SEC;
}
return (wifi_config_counter);
}
extern "C" {
#include "user_interface.h"
}
void WifiWpsStatusCallback(wps_cb_status status);
void WifiWpsStatusCallback(wps_cb_status status)
{
/* from user_interface.h:
enum wps_cb_status {
WPS_CB_ST_SUCCESS = 0,
WPS_CB_ST_FAILED,
WPS_CB_ST_TIMEOUT,
WPS_CB_ST_WEP, // WPS failed because that WEP is not supported
WPS_CB_ST_SCAN_ERR, // can not find the target WPS AP
};
*/
wps_result = status;
if (WPS_CB_ST_SUCCESS == wps_result) {
wifi_wps_disable();
} else {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_WIFI D_WPS_FAILED_WITH_STATUS " %d"), wps_result);
AddLog(LOG_LEVEL_DEBUG);
wifi_config_counter = 2;
}
}