forked from Roos-AID/ModbusRTU-ESP8266-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModbusRTU-ESP8266-webui.ino
299 lines (243 loc) · 7.74 KB
/
ModbusRTU-ESP8266-webui.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
/*
***************************************************************************
** Program : ModbusRTU-webui.ino
** Version 1.9.1
**
** Copyright (c) 2021 Rob Roos
** based on Framework ESP8266 from Willem Aandewiel and modifications
** from Robert van Breemen
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
/*
* How to install the ModbusRTU-webui on your nodeMCU
*
* Make sure you have all required library's installed:
* - AceTime v1.8.0 - https://github.com/bxparks/AceTime
* - TelnetStream - https://github.com/jandrassy/TelnetStream/commit/1294a9ee5cc9b1f7e51005091e351d60c8cddecf
* - ArduinoJson - https://arduinojson.org/
* - modbus-esp8266 -https://github.com/emelianov/modbus-esp8266
* - pubsubclient (MQTT) -https://github.com/knolleary/pubsubclient
* All the library's can be installed using the library manager.
*
* How to upload to your LittleFS?
* Just install the LittleFS upload plugin (https://github.com/earlephilhower/arduino-esp8266littlefs-plugin)
* and upload it to your LittleFS after first flashing the device.
*
* How to compile this firmware?
* - NodeMCU v1.0
* - Flashsize (4MB - FS:2MB - OTA ~1019KB)
* - CPU frequentcy: 160MHz
* - Normal defaults should work fine.
* First time: Make sure to flash sketch + wifi or flash ALL contents.
* Then load the LittleFS filesystem with the contents of the data folder
*
*/
#include "version.h"
#define _FW_VERSION _VERSION
#include "ModbusRTU-ESP8266-webui.h"
#define ON LOW
#define OFF HIGH
#define RELAYON HIGH
#define RELAYOFF LOW
DECLARE_TIMER_SEC(timerreadmodbus, settingModbusReadInterval, CATCH_UP_MISSED_TICKS);
//=====================================================================
void setup()
{
Serial.begin(115200, SERIAL_8N1);
while (!Serial) {} //Wait for OK
Serial.println(F("\r\n[ModbusRTU-webui firmware version]\r\n"));
Serial.printf("Booting....[%s]\r\n\r\n", String(_FW_VERSION).c_str());
// rebootCount = updateRebootCount();
//setup randomseed the right way
randomSeed(RANDOM_REG32); //This is 8266 HWRNG used to seed the Random PRNG: Read more: https://config9.com/arduino/getting-a-truly-random-number-in-arduino/
//setup the status LED
setLed(LED1, ON);
setLed(LED2, ON);
LittleFS.begin();
readSettings(true);
if (settingRelayAllwaysOnSwitch) setRelay(RELAYON); else
setRelay(RELAYOFF);
CHANGE_INTERVAL_SEC(timerreadmodbus, settingModbusReadInterval, CATCH_UP_MISSED_TICKS);
NodeId = getUniqueId() ;
Serial.println(F("Attempting to connect to WiFi network\r"));
setLed(LED1, ON);
startWiFi(CSTR(settingHostname), 240); // timeout 240 seconds
blinkLED(LED1, 3, 100);
setLed(LED1, OFF);
startTelnet(); //start the debug port 23
delayms(3000);
startMDNS(CSTR(settingHostname));
startLLMNR(CSTR(settingHostname));
startMQTT();
startNTP();
setupFSexplorer();
startWebserver();
setupPing();
// when dependend on time schedule, then ensure time is set
while (settingTimebasedSwitch && settingNTPenable && (NtpStatus != TIME_SYNC))
{
loopNTP(); // Make sure time is set
}
// log last reset reason after all above has started
lastReset = ESP.getResetReason();
DebugTf("Last reset reason: [%s]\r\n", CSTR(ESP.getResetReason()));
rebootCount = updateRebootCount();
// updateRebootLog(ESP.getResetReason());
updateRebootLog("Startup");
//============== Setup Modbus ======================================
setupModbus();
doInitModbusMap();
doInitDaytimemap();
printDaytimemap();
// printModbusmap() ;
// readModbusSetup();
readModbus();
// Double check if Wifi is still alive (seems sometimes problematic)
if (WiFi.status() != WL_CONNECTED)
{
//disconnected, try to reconnect then...
DebugTln("Wifi not Connected !!! Restart Wifi");
reconnectWiFiCount++;
restartWiFi(CSTR(settingHostname), 240);
MDNS.update();
//check telnet
startTelnet();
}
// Ping default gateway and restart Wifi if it fails.
if (pinger.Ping(WiFi.gatewayIP(), 1) == false)
{
DebugTf("Pinging default gateway with IP %s, FAILED\n", WiFi.gatewayIP().toString().c_str());
DebugTln("Error during last ping command. Restart Wifi");
restartWiFiCount++;
if (restartWiFiCount > 5)
{
updateRebootLog("In Setup IP Ping failed to often, restart ESP");
doRestart("In Setup IP Ping failed to often, restart ESP");
}
restartWiFi(CSTR(settingHostname), 240);
MDNS.update();
//check telnet
startTelnet();
}
Debugf("Reboot count = [%d]\r\n", rebootCount);
Debugln(F("Setup finished!"));
//Blink LED2 to signal setup done
setLed(LED1, OFF);
blinkLED(LED2, 3, 100);
setLed(LED2, OFF);
} // End setup
//=====================================================================
//=====================================================================
//===[ blink status led ]===
void setLed(uint8_t led, uint8_t status)
{
pinMode(led, OUTPUT);
digitalWrite(led, status);
}
void blinkLEDms(uint32_t delay)
{
//blink the statusled, when time passed
DECLARE_TIMER_MS(timerBlink, delay);
if (DUE(timerBlink))
{
blinkLEDnow();
}
}
void blinkLED(uint8_t led, int nr, uint32_t waittime_ms)
{
for (int i = nr; i > 0; i--)
{
blinkLEDnow(led);
delayms(waittime_ms);
blinkLEDnow(led);
delayms(waittime_ms);
}
}
void blinkLEDnow()
{
blinkLEDnow(LED1);
}
void blinkLEDnow(uint8_t led = LED1)
{
pinMode(led, OUTPUT);
digitalWrite(led, !digitalRead(led));
}
//===[ no-blocking delay with running background tasks in ms ]===
void delayms(unsigned long delay_ms)
{
DECLARE_TIMER_MS(timerDelayms, delay_ms);
while (DUE(timerDelayms))
doBackgroundTasks();
}
//=====================================================================
//===[ Do task every 1s ]===
void doTaskEvery1s(){
//== do tasks ==
upTimeSeconds++;
}
//===[ Do task every 5s ]===
void doTaskEvery5s(){
//== do tasks ==
}
//===[ Do task every 30s ]===
void doTaskEvery30s(){
//== do tasks ==
}
//===[ Do task every 60s ]===
void doTaskEvery60s(){
//== do tasks ==
checkactivateRelay(true);
//if no wifi, try reconnecting (once a minute)
if (WiFi.status() != WL_CONNECTED)
{
//disconnected, try to reconnect then...
reconnectWiFiCount++;
startWiFi(CSTR(settingHostname), 240);
MDNS.update();
//check telnet
startTelnet();
}
// Ping default gateway and restart Wifi if it fails.
if(pinger.Ping(WiFi.gatewayIP(),1) == false)
{
DebugTf("Pinging default gateway with IP %s, FAILED\n", WiFi.gatewayIP().toString().c_str());
DebugTln("Error during last ping command. Restart Wifi");
restartWiFiCount++ ;
if (restartWiFiCount > 5) {
updateRebootLog("In Run IP Ping failed to often, restart ESP");
doRestart("In Run IP Ping failed to often, restart ESP");
}
restartWiFi(CSTR(settingHostname), 240);
MDNS.update();
//check telnet
startTelnet();
}
}
// end doTaskEvery60s()
//===[ Do the background tasks ]===
void doBackgroundTasks()
{
if (WiFi.status() == WL_CONNECTED) {
//while connected handle everything that uses network stuff
handleDebug();
handleMQTT(); // MQTT transmissions
httpServer.handleClient();
loopNTP();
}
yield();
}
void loop()
{
DECLARE_TIMER_SEC(timer1s, 1, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer5s, 5, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer30s, 30, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer60s, 60, CATCH_UP_MISSED_TICKS);
if (DUE(timer1s)) doTaskEvery1s();
if (DUE(timer5s)) doTaskEvery5s();
if (DUE(timer30s)) doTaskEvery30s();
if (DUE(timer60s)) doTaskEvery60s();
if (DUE(timerreadmodbus)) readModbus();
doBackgroundTasks();
}