forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESPEasy.ino
991 lines (856 loc) · 29.4 KB
/
ESPEasy.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
#define ESP_CORE 210
/****************************************************************************************************************************\
* Arduino project "ESP Easy" © Copyright www.esp8266.nu
*
* 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 received a copy of the GNU General Public License along with this program in file 'License.txt'.
*
* IDE download : https://www.arduino.cc/en/Main/Software
* ESP8266 Package : https://github.com/esp8266/Arduino
*
* Source Code : https://sourceforge.net/projects/espeasy/
* Support : http://www.esp8266.nu
* Discussion : http://www.esp8266.nu/forum/
*
* Additional information about licensing can be found at : http://www.gnu.org/licenses
\*************************************************************************************************************************/
// This file incorporates work covered by the following copyright and permission notice:
/****************************************************************************************************************************\
* Arduino project "Nodo" © Copyright 2010..2015 Paul Tonkes
*
* 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 received a copy of the GNU General Public License along with this program in file 'License.txt'.
*
* Voor toelichting op de licentievoorwaarden zie : http://www.gnu.org/licenses
* Uitgebreide documentatie is te vinden op : http://www.nodo-domotica.nl
* Compiler voor deze programmacode te downloaden op : http://arduino.cc
\*************************************************************************************************************************/
// Simple Arduino sketch for ESP module, supporting:
// =================================================================================
// Simple switch inputs and direct GPIO output control to drive relais, mosfets, etc
// Analog input (ESP-7/12 only)
// Pulse counters
// Dallas OneWire DS18b20 temperature sensors
// DHT11/22 humidity sensors
// BMP085 I2C Barometric Pressure sensor
// PCF8591 4 port Analog to Digital converter (I2C)
// RFID Wiegand-26 reader
// MCP23017 I2C IO Expanders
// BH1750 I2C Luminosity sensor
// Arduino Pro Mini with IO extender sketch, connected through I2C
// LCD I2C display 4x20 chars
// HC-SR04 Ultrasonic distance sensor
// SI7021 I2C temperature/humidity sensors
// TSL2561 I2C Luminosity sensor
// TSOP4838 IR receiver
// PN532 RFID reader
// Sharp GP2Y10 dust sensor
// PCF8574 I2C IO Expanders
// PCA9685 I2C 16 channel PWM driver
// OLED I2C display with SSD1306 driver
// MLX90614 I2C IR temperature sensor
// ADS1115 I2C ADC
// INA219 I2C voltage/current sensor
// BME280 I2C temp/hum/baro sensor
// MSP5611 I2C temp/baro sensor
// Experimental/Preliminary:
// =========================
// Ser2Net server
// Local Level Control to GPIO
// ********************************************************************************
// User specific configuration
// ********************************************************************************
// Set default configuration settings if you want (not mandatory)
// You can always change these during runtime and save to eeprom
// After loading firmware, issue a 'reset' command to load the defaults.
#define DEFAULT_NAME "newdevice" // Enter your device friendly name
#define DEFAULT_SSID "ssid" // Enter your network SSID
#define DEFAULT_KEY "wpakey" // Enter your network WPA key
#define DEFAULT_SERVER "192.168.0.8" // Enter your Domoticz Server IP address
#define DEFAULT_PORT 8080 // Enter your Domoticz Server port value
#define DEFAULT_DELAY 60 // Enter your Send delay in seconds
#define DEFAULT_AP_KEY "configesp" // Enter network WPA key for AP (config) mode
#define DEFAULT_USE_STATIC_IP false // true or false enabled or disabled set static IP
#define DEFAULT_IP "192.168.0.50" // Enter your IP address
#define DEFAULT_DNS "192.168.0.1" // Enter your DNS
#define DEFAULT_GW "192.168.0.1" // Enter your gateway
#define DEFAULT_SUBNET "255.255.255.0" // Enter your subnet
#define DEFAULT_MQTT_TEMPLATE false // true or false enabled or disabled set mqqt sub and pub
#define DEFAULT_MQTT_PUB "sensors/espeasy/%sysname%/%tskname%/%valname%" // Enter your pub
#define DEFAULT_MQTT_SUB "sensors/espeasy/%sysname%/#" // Enter your sub
#define DEFAULT_PROTOCOL 1 // Protocol used for controller communications
// 1 = Domoticz HTTP
// 2 = Domoticz MQTT
// 3 = Nodo Telnet
// 4 = ThingSpeak
// 5 = OpenHAB MQTT
// 6 = PiDome MQTT
// 7 = EmonCMS
#define UNIT 0
#define FEATURE_TIME true
#define FEATURE_SSDP true
// Enable FEATURE_ADC_VCC to measure supply voltage using the analog pin
// Please note that the TOUT pin has to be disconnected in this mode
// Use the "System Info" device to read the VCC value
#define FEATURE_ADC_VCC false
// ********************************************************************************
// DO NOT CHANGE ANYTHING BELOW THIS LINE
// ********************************************************************************
#define ESP_PROJECT_PID 2015050101L
#define ESP_EASY
#define VERSION 9
#define BUILD 108
#define FEATURE_SPIFFS false
#define CPLUGIN_PROTOCOL_ADD 1
#define CPLUGIN_PROTOCOL_TEMPLATE 2
#define CPLUGIN_PROTOCOL_SEND 3
#define CPLUGIN_PROTOCOL_RECV 4
#define CPLUGIN_GET_DEVICENAME 5
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_INFO 2
#define LOG_LEVEL_DEBUG 3
#define LOG_LEVEL_DEBUG_MORE 4
#define CMD_REBOOT 89
#define CMD_WIFI_DISCONNECT 135
#define DEVICES_MAX 64
#define TASKS_MAX 12
#define VARS_PER_TASK 4
#define PLUGIN_MAX 64
#define PLUGIN_CONFIGVAR_MAX 8
#define PLUGIN_CONFIGFLOATVAR_MAX 4
#define PLUGIN_CONFIGLONGVAR_MAX 4
#define PLUGIN_EXTRACONFIGVAR_MAX 16
#define CPLUGIN_MAX 16
#define UNIT_MAX 32
#define RULES_TIMER_MAX 8
#define SYSTEM_TIMER_MAX 8
#define SYSTEM_CMD_TIMER_MAX 2
#define PINSTATE_TABLE_MAX 32
#define RULES_MAX_SIZE 2048
#define RULES_MAX_NESTING_LEVEL 3
#define PIN_MODE_UNDEFINED 0
#define PIN_MODE_INPUT 1
#define PIN_MODE_OUTPUT 2
#define PIN_MODE_PWM 3
#define PIN_MODE_SERVO 4
#define SEARCH_PIN_STATE true
#define NO_SEARCH_PIN_STATE false
#define DEVICE_TYPE_SINGLE 1 // connected through 1 datapin
#define DEVICE_TYPE_I2C 2 // connected through I2C
#define DEVICE_TYPE_ANALOG 3 // tout pin
#define DEVICE_TYPE_DUAL 4 // connected through 2 datapins
#define SENSOR_TYPE_SINGLE 1
#define SENSOR_TYPE_TEMP_HUM 2
#define SENSOR_TYPE_TEMP_BARO 3
#define SENSOR_TYPE_TEMP_HUM_BARO 4
#define SENSOR_TYPE_SWITCH 10
#define SENSOR_TYPE_DIMMER 11
#define SENSOR_TYPE_LONG 20
#define PLUGIN_INIT_ALL 1
#define PLUGIN_INIT 2
#define PLUGIN_READ 3
#define PLUGIN_ONCE_A_SECOND 4
#define PLUGIN_TEN_PER_SECOND 5
#define PLUGIN_DEVICE_ADD 6
#define PLUGIN_EVENTLIST_ADD 7
#define PLUGIN_WEBFORM_SAVE 8
#define PLUGIN_WEBFORM_LOAD 9
#define PLUGIN_WEBFORM_SHOW_VALUES 10
#define PLUGIN_GET_DEVICENAME 11
#define PLUGIN_GET_DEVICEVALUENAMES 12
#define PLUGIN_WRITE 13
#define PLUGIN_EVENT_OUT 14
#define PLUGIN_WEBFORM_SHOW_CONFIG 15
#define PLUGIN_SERIAL_IN 16
#define PLUGIN_UDP_IN 17
#define PLUGIN_CLOCK_IN 18
#define PLUGIN_TIMER_IN 19
#define VALUE_SOURCE_SYSTEM 1
#define VALUE_SOURCE_SERIAL 2
#define VALUE_SOURCE_HTTP 3
#define VALUE_SOURCE_MQTT 4
#define VALUE_SOURCE_UDP 5
#define BOOT_CAUSE_MANUAL_REBOOT 0
#define BOOT_CAUSE_COLD_BOOT 1
#define BOOT_CAUSE_EXT_WD 10
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#if FEATURE_SPIFFS
#include <FS.h>
#endif
#include <ESP8266HTTPUpdateServer.h>
ESP8266HTTPUpdateServer httpUpdater(true);
#if ESP_CORE >= 210
#include <base64.h>
#endif
#if FEATURE_ADC_VCC
ADC_MODE(ADC_VCC);
#endif
#define LWIP_OPEN_SRC
#include "lwip/opt.h"
#include "lwip/udp.h"
#include "lwip/igmp.h"
#include "include/UdpContext.h"
extern "C" {
#include "user_interface.h"
}
// Setup DNS, only used if the ESP has no valid WiFi config
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 4, 1);
DNSServer dnsServer;
Servo myservo1;
Servo myservo2;
// MQTT client
PubSubClient MQTTclient("");
// WebServer
ESP8266WebServer WebServer(80);
// syslog stuff
WiFiUDP portUDP;
#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;
struct SecurityStruct
{
char WifiSSID[32];
char WifiKey[64];
char WifiAPKey[64];
char ControllerUser[26];
char ControllerPassword[64];
char Password[26];
} SecuritySettings;
struct SettingsStruct
{
unsigned long PID;
int Version;
byte Unit;
byte Controller_IP[4];
unsigned int ControllerPort;
byte IP_Octet;
char NTPHost[64];
unsigned long Delay;
int8_t Pin_i2c_sda;
int8_t Pin_i2c_scl;
byte Syslog_IP[4];
unsigned int UDPPort;
byte Protocol;
byte IP[4];
byte Gateway[4];
byte Subnet[4];
char Name[26];
byte SyslogLevel;
byte SerialLogLevel;
byte WebLogLevel;
unsigned long BaudRate;
unsigned long MessageDelay;
byte TaskDeviceNumber[TASKS_MAX];
unsigned int TaskDeviceID[TASKS_MAX];
int8_t TaskDevicePin1[TASKS_MAX];
int8_t TaskDevicePin2[TASKS_MAX];
byte TaskDevicePort[TASKS_MAX];
boolean TaskDevicePin1PullUp[TASKS_MAX];
int16_t TaskDevicePluginConfig[TASKS_MAX][PLUGIN_CONFIGVAR_MAX];
boolean TaskDevicePin1Inversed[TASKS_MAX];
byte deepSleep;
char MQTTpublish[81];
char MQTTsubscribe[81];
boolean CustomCSS;
float TaskDevicePluginConfigFloat[TASKS_MAX][PLUGIN_CONFIGFLOATVAR_MAX];
long TaskDevicePluginConfigLong[TASKS_MAX][PLUGIN_CONFIGLONGVAR_MAX];
boolean TaskDeviceSendData[TASKS_MAX];
int16_t Build;
byte DNS[4];
int8_t TimeZone;
char ControllerHostName[64];
boolean UseNTP;
boolean DST;
byte WDI2CAddress;
boolean TaskDeviceGlobalSync[TASKS_MAX];
int8_t TaskDevicePin3[TASKS_MAX];
byte TaskDeviceDataFeed[TASKS_MAX];
int8_t PinBootStates[17];
byte UseDNS;
boolean UseRules;
int8_t Pin_status_led;
boolean UseSerial;
unsigned long TaskDeviceTimer[TASKS_MAX];
boolean UseSSDP;
unsigned long WireClockStretchLimit;
boolean GlobalSync;
unsigned long ConnectionFailuresThreshold;
} Settings;
struct ExtraTaskSettingsStruct
{
byte TaskIndex;
char TaskDeviceName[41];
char TaskDeviceFormula[VARS_PER_TASK][41];
char TaskDeviceValueNames[VARS_PER_TASK][41];
long TaskDevicePluginConfigLong[PLUGIN_EXTRACONFIGVAR_MAX];
byte TaskDeviceValueDecimals[VARS_PER_TASK];
} ExtraTaskSettings;
struct EventStruct
{
byte Source;
byte TaskIndex;
byte BaseVarIndex;
int idx;
byte sensorType;
int Par1;
int Par2;
int Par3;
byte OriginTaskIndex;
String String1;
String String2;
byte *Data;
};
struct LogStruct
{
unsigned long timeStamp;
String Message;
} Logging[10];
int logcount = -1;
struct DeviceStruct
{
byte Number;
byte Type;
byte VType;
byte Ports;
boolean PullUpOption;
boolean InverseLogicOption;
boolean FormulaOption;
byte ValueCount;
boolean Custom;
boolean SendDataOption;
boolean GlobalSyncOption;
boolean TimerOption;
boolean TimerOptional;
} Device[DEVICES_MAX + 1]; // 1 more because first device is empty device
struct ProtocolStruct
{
byte Number;
boolean usesMQTT;
boolean usesAccount;
boolean usesPassword;
int defaultPort;
} Protocol[CPLUGIN_MAX];
struct NodeStruct
{
byte ip[4];
byte age;
} Nodes[UNIT_MAX];
struct systemTimerStruct
{
unsigned long timer;
byte plugin;
byte Par1;
byte Par2;
byte Par3;
} systemTimers[SYSTEM_TIMER_MAX];
struct systemCMDTimerStruct
{
unsigned long timer;
String action;
} systemCMDTimers[SYSTEM_CMD_TIMER_MAX];
struct pinStatesStruct
{
byte plugin;
byte index;
byte mode;
uint16_t value;
} pinStates[PINSTATE_TABLE_MAX];
int deviceCount = -1;
int protocolCount = -1;
boolean printToWeb = false;
String printWebString = "";
boolean printToWebJSON = false;
float UserVar[VARS_PER_TASK * TASKS_MAX];
unsigned long RulesTimer[RULES_TIMER_MAX];
unsigned long timerSensor[TASKS_MAX];
unsigned long timer;
unsigned long timer100ms;
unsigned long timer1s;
unsigned long timerwd;
unsigned long lastSend;
unsigned int NC_Count = 0;
unsigned int C_Count = 0;
boolean AP_Mode = false;
byte cmd_within_mainloop = 0;
unsigned long connectionFailures;
unsigned long wdcounter = 0;
#if FEATURE_ADC_VCC
float vcc = -1.0;
#endif
boolean WebLoggedIn = false;
int WebLoggedInTimer = 300;
boolean (*Plugin_ptr[PLUGIN_MAX])(byte, struct EventStruct*, String&);
byte Plugin_id[PLUGIN_MAX];
boolean (*CPlugin_ptr[PLUGIN_MAX])(byte, struct EventStruct*, String&);
byte CPlugin_id[PLUGIN_MAX];
String dummyString = "";
boolean systemOK = false;
byte lastBootCause = 0;
boolean wifiSetup = false;
boolean wifiSetupConnect = false;
unsigned long start = 0;
unsigned long elapsed = 0;
unsigned long loopCounter = 0;
unsigned long loopCounterLast = 0;
unsigned long loopCounterMax = 1;
String eventBuffer = "";
/*********************************************************************************************\
* SETUP
\*********************************************************************************************/
void setup()
{
Serial.begin(115200);
if (SpiffsSectors() == 0)
{
Serial.println(F("\nNo SPIFFS area..\nSystem Halted\nPlease reflash with SPIFFS"));
while (true)
delay(1);
}
#if FEATURE_SPIFFS
fileSystemCheck();
#endif
emergencyReset();
LoadSettings();
if (strcasecmp(SecuritySettings.WifiSSID, "ssid") == 0)
wifiSetup = true;
ExtraTaskSettings.TaskIndex = 255; // make sure this is an unused nr to prevent cache load on boot
// if different version, eeprom settings structure has changed. Full Reset needed
// on a fresh ESP module eeprom values are set to 255. Version results into -1 (signed int)
if (Settings.Version == VERSION && Settings.PID == ESP_PROJECT_PID)
{
systemOK = true;
}
else
{
// Direct Serial is allowed here, since this is only an emergency task.
Serial.print(F("\nPID:"));
Serial.println(Settings.PID);
Serial.print(F("Version:"));
Serial.println(Settings.Version);
Serial.println(F("INIT : Incorrect PID or version!"));
delay(1000);
ResetFactory();
}
if (systemOK)
{
if (Settings.UseSerial)
Serial.begin(Settings.BaudRate);
if (Settings.Build != BUILD)
BuildFixes();
String log = F("\nINIT : Booting Build nr:");
log += BUILD;
addLog(LOG_LEVEL_INFO, log);
if (Settings.UseSerial && Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE)
Serial.setDebugOutput(true);
WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters
WifiAPconfig();
WifiConnect(3);
hardwareInit();
PluginInit();
CPluginInit();
WebServerInit();
// setup UDP
if (Settings.UDPPort != 0)
portUDP.begin(Settings.UDPPort);
// Setup MQTT Client
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
if (Protocol[ProtocolIndex].usesMQTT)
MQTTConnect();
sendSysInfoUDP(3);
log = F("INIT : Boot OK");
addLog(LOG_LEVEL_INFO, log);
if (Settings.deepSleep)
{
log = F("INIT : Deep sleep enabled");
addLog(LOG_LEVEL_INFO, log);
}
byte bootMode = 0;
if (readFromRTC(&bootMode))
{
if (bootMode == 1)
log = F("INIT : Reboot from deepsleep");
else
log = F("INIT : Normal boot");
}
else
{
// cold boot situation
if (lastBootCause == 0) // only set this if not set earlier during boot stage.
lastBootCause = BOOT_CAUSE_COLD_BOOT;
log = F("INIT : Cold Boot");
}
addLog(LOG_LEVEL_INFO, log);
saveToRTC(0);
if (Settings.UseRules)
{
String event = F("System#Boot");
rulesProcessing(event);
}
// Setup timers
if (bootMode == 0)
{
for (byte x = 0; x < TASKS_MAX; x++)
if (Settings.TaskDeviceTimer[x] !=0)
timerSensor[x] = millis() + 30000 + (x * Settings.MessageDelay);
timer = millis() + 30000; // startup delay 30 sec
}
else
{
for (byte x = 0; x < TASKS_MAX; x++)
timerSensor[x] = millis() + 0;
timer = millis() + 0; // no startup from deepsleep wake up
}
timer100ms = millis() + 100; // timer for periodic actions 10 x per/sec
timer1s = millis() + 1000; // timer for periodic actions once per/sec
timerwd = millis() + 30000; // timer for watchdog once per 30 sec
#if FEATURE_TIME
if (Settings.UseNTP)
initTime();
#endif
#if FEATURE_ADC_VCC
vcc = ESP.getVcc() / 1000.0;
#endif
// Start DNS, only used if the ESP has no valid WiFi config
// It will reply with it's own address on all DNS requests
// (captive portal concept)
if (wifiSetup)
dnsServer.start(DNS_PORT, "*", apIP);
}
else
{
Serial.println(F("Entered Rescue mode!"));
}
}
/*********************************************************************************************\
* MAIN LOOP
\*********************************************************************************************/
void loop()
{
loopCounter++;
if (wifiSetupConnect)
{
// try to connect for setup wizard
WifiConnect(1);
wifiSetupConnect = false;
}
if (Settings.UseSerial)
if (Serial.available())
if (!PluginCall(PLUGIN_SERIAL_IN, 0, dummyString))
serial();
if (systemOK)
{
if (millis() > timer100ms)
run10TimesPerSecond();
if (millis() > timer1s)
runOncePerSecond();
if (millis() > timerwd)
runEach30Seconds();
backgroundtasks();
}
else
delay(1);
}
/*********************************************************************************************\
* Tasks that run 10 times per second
\*********************************************************************************************/
void run10TimesPerSecond()
{
start = micros();
timer100ms = millis() + 100;
PluginCall(PLUGIN_TEN_PER_SECOND, 0, dummyString);
checkUDP();
if (Settings.UseRules && eventBuffer.length() > 0)
{
rulesProcessing(eventBuffer);
eventBuffer = "";
}
elapsed = micros() - start;
}
/*********************************************************************************************\
* Tasks each second
\*********************************************************************************************/
void runOncePerSecond()
{
timer1s = millis() + 1000;
checkSensors();
if (Settings.ConnectionFailuresThreshold)
if (connectionFailures > Settings.ConnectionFailuresThreshold)
delayedReboot(60);
if (cmd_within_mainloop != 0)
{
switch (cmd_within_mainloop)
{
case CMD_WIFI_DISCONNECT:
{
WifiDisconnect();
break;
}
case CMD_REBOOT:
{
ESP.reset();
break;
}
}
cmd_within_mainloop = 0;
}
#if FEATURE_TIME
// clock events
if (Settings.UseNTP)
checkTime();
#endif
unsigned long timer = micros();
PluginCall(PLUGIN_ONCE_A_SECOND, 0, dummyString);
checkSystemTimers();
if (Settings.UseRules)
rulesTimers();
timer = micros() - timer;
if (SecuritySettings.Password[0] != 0)
{
if (WebLoggedIn)
WebLoggedInTimer++;
if (WebLoggedInTimer > 300)
WebLoggedIn = false;
}
// I2C Watchdog feed
if (Settings.WDI2CAddress != 0)
{
Wire.beginTransmission(Settings.WDI2CAddress);
Wire.write(0xA5);
Wire.endTransmission();
}
if (Settings.SerialLogLevel == 5)
{
Serial.print(F("10 ps:"));
Serial.print(elapsed);
Serial.print(F(" uS 1 ps:"));
Serial.println(timer);
}
}
/*********************************************************************************************\
* Tasks each 30 seconds
\*********************************************************************************************/
void runEach30Seconds()
{
wdcounter++;
timerwd = millis() + 30000;
char str[60];
str[0] = 0;
sprintf_P(str, PSTR("Uptime %u ConnectFailures %u FreeMem %u"), wdcounter / 2, connectionFailures, FreeMem());
String log = F("WD : ");
log += str;
addLog(LOG_LEVEL_INFO, log);
sendSysInfoUDP(1);
refreshNodeList();
MQTTCheck();
#if FEATURE_SSDP
if (Settings.UseSSDP)
SSDP_update();
#endif
#if FEATURE_ADC_VCC
vcc = ESP.getVcc() / 1000.0;
#endif
loopCounterLast = loopCounter;
loopCounter = 0;
if (loopCounterLast > loopCounterMax)
loopCounterMax = loopCounterLast;
WifiCheck();
}
/*********************************************************************************************\
* Check sensor timers
\*********************************************************************************************/
void checkSensors()
{
// Check sensors and send data to controller when sensor timer has elapsed
// If deepsleep, use the single timer
if (Settings.deepSleep)
{
if (millis() > timer)
{
timer = millis() + Settings.Delay * 1000;
SensorSend();
saveToRTC(1);
String log = F("Enter deep sleep...");
addLog(LOG_LEVEL_INFO, log);
ESP.deepSleep(Settings.Delay * 1000000, WAKE_RF_DEFAULT); // Sleep for set delay
}
}
else // use individual timers for tasks
{
for (byte x = 0; x < TASKS_MAX; x++)
{
if ((Settings.TaskDeviceTimer[x] != 0) && (millis() > timerSensor[x]))
{
timerSensor[x] = millis() + Settings.TaskDeviceTimer[x] * 1000;
if (timerSensor[x] == 0) // small fix if result is 0, else timer will be stopped...
timerSensor[x] = 1;
SensorSendTask(x);
}
}
}
}
/*********************************************************************************************\
* send all sensordata
\*********************************************************************************************/
void SensorSend()
{
for (byte x = 0; x < TASKS_MAX; x++)
{
SensorSendTask(x);
}
}
/*********************************************************************************************\
* send specific sensor task data
\*********************************************************************************************/
void SensorSendTask(byte TaskIndex)
{
if (Settings.TaskDeviceID[TaskIndex] != 0)
{
byte varIndex = TaskIndex * VARS_PER_TASK;
boolean success = false;
byte DeviceIndex = getDeviceIndex(Settings.TaskDeviceNumber[TaskIndex]);
LoadTaskSettings(TaskIndex);
struct EventStruct TempEvent;
TempEvent.TaskIndex = TaskIndex;
TempEvent.BaseVarIndex = varIndex;
TempEvent.idx = Settings.TaskDeviceID[TaskIndex];
TempEvent.sensorType = Device[DeviceIndex].VType;
float preValue[VARS_PER_TASK]; // store values before change, in case we need it in the formula
for (byte varNr = 0; varNr < VARS_PER_TASK; varNr++)
preValue[varNr] = UserVar[varIndex + varNr];
if(Settings.TaskDeviceDataFeed[TaskIndex] == 0) // only read local connected sensorsfeeds
success = PluginCall(PLUGIN_READ, &TempEvent, dummyString);
else
success = true;
if (success)
{
for (byte varNr = 0; varNr < VARS_PER_TASK; varNr++)
{
if (ExtraTaskSettings.TaskDeviceFormula[varNr][0] != 0)
{
String spreValue = String(preValue[varNr]);
String formula = ExtraTaskSettings.TaskDeviceFormula[varNr];
float value = UserVar[varIndex + varNr];
float result = 0;
String svalue = String(value);
formula.replace("%pvalue%", spreValue);
formula.replace("%value%", svalue);
byte error = Calculate(formula.c_str(), &result);
if (error == 0)
UserVar[varIndex + varNr] = result;
}
}
sendData(&TempEvent);
}
}
}
/*********************************************************************************************\
* set global system timer
\*********************************************************************************************/
boolean setSystemTimer(unsigned long timer, byte plugin, byte Par1, byte Par2, byte Par3)
{
// plugin number and par1 form a unique key that can be used to restart a timer
// first check if a timer is not already running for this request
boolean reUse = false;
for (byte x = 0; x < SYSTEM_TIMER_MAX; x++)
if (systemTimers[x].timer != 0)
{
if ((systemTimers[x].plugin == plugin) && (systemTimers[x].Par1 == Par1))
{
systemTimers[x].timer = millis() + timer;
reUse = true;
break;
}
}
if (!reUse)
{
// find a new free timer slot...
for (byte x = 0; x < SYSTEM_TIMER_MAX; x++)
if (systemTimers[x].timer == 0)
{
systemTimers[x].timer = millis() + timer;
systemTimers[x].plugin = plugin;
systemTimers[x].Par1 = Par1;
systemTimers[x].Par2 = Par2;
systemTimers[x].Par3 = Par3;
break;
}
}
}
/*********************************************************************************************\
* set global system command timer
\*********************************************************************************************/
boolean setSystemCMDTimer(unsigned long timer, String& action)
{
for (byte x = 0; x < SYSTEM_CMD_TIMER_MAX; x++)
if (systemCMDTimers[x].timer == 0)
{
systemCMDTimers[x].timer = millis() + timer;
systemCMDTimers[x].action = action;
break;
}
}
/*********************************************************************************************\
* check global system timers
\*********************************************************************************************/
boolean checkSystemTimers()
{
for (byte x = 0; x < SYSTEM_TIMER_MAX; x++)
if (systemTimers[x].timer != 0)
{
if (timeOut(systemTimers[x].timer))
{
struct EventStruct TempEvent;
TempEvent.Par1 = systemTimers[x].Par1;
TempEvent.Par2 = systemTimers[x].Par2;
TempEvent.Par3 = systemTimers[x].Par3;
for (byte y = 0; y < PLUGIN_MAX; y++)
if (Plugin_id[y] == systemTimers[x].plugin)
Plugin_ptr[y](PLUGIN_TIMER_IN, &TempEvent, dummyString);
systemTimers[x].timer = 0;
}
}
for (byte x = 0; x < SYSTEM_CMD_TIMER_MAX; x++)
if (systemCMDTimers[x].timer != 0)
if (timeOut(systemCMDTimers[x].timer))
{
struct EventStruct TempEvent;
parseCommandString(&TempEvent, systemCMDTimers[x].action);
if (!PluginCall(PLUGIN_WRITE, &TempEvent, systemCMDTimers[x].action))
ExecuteCommand(VALUE_SOURCE_SYSTEM, systemCMDTimers[x].action.c_str());
systemCMDTimers[x].timer = 0;
systemCMDTimers[x].action = "";
}
}
/*********************************************************************************************\
* run background tasks
\*********************************************************************************************/
void backgroundtasks()
{
// process DNS, only used if the ESP has no valid WiFi config
if (wifiSetup)
dnsServer.processNextRequest();
WebServer.handleClient();
MQTTclient.loop();
statusLED(false);
yield();
}