@@ -28,7 +28,7 @@ enum action_type : byte {
28
28
ETH_SOFT, // Ethernet software reset
29
29
SERIAL_SOFT, // Serial software reset
30
30
SCAN, // Initialize RS485 scan
31
- RST_STATS, // Reset Modbus Statistics
31
+ RST_STATS, // Reset Modbus Statistics
32
32
WEB // Restart webserver
33
33
};
34
34
enum action_type action;
@@ -43,7 +43,7 @@ enum page : byte {
43
43
PAGE_IP,
44
44
PAGE_TCP,
45
45
PAGE_RTU,
46
- PAGE_WAIT // page with "Reloading. Please wait..." message. Must be the last element within this enum!!
46
+ PAGE_WAIT, // page with "Reloading. Please wait..." message.
47
47
};
48
48
49
49
// Keys for POST parameters, used in web forms and processed by processPost() function.
@@ -79,9 +79,36 @@ enum post_key : byte {
79
79
POST_FRAMEDELAY, // frame delay
80
80
POST_TIMEOUT, // response timeout
81
81
POST_ATTEMPTS, // number of request attempts
82
- POST_ACTION // actions on Tools page
82
+ POST_ACTION, // actions on Tools page
83
83
};
84
84
85
+ // Keys for JSON elements, used in: 1) JSON documents, 2) ID of span tags, 3) Javascript.
86
+ enum JSON_type : byte {
87
+ JSON_SECS, // Runtime seconds
88
+ JSON_MINS, // Runtime minutes
89
+ JSON_HOURS, // Runtime hours
90
+ JSON_DAYS, // Runtime days
91
+ JSON_ERROR, // Modbus status from array errorCount[STAT_ERROR_0B_QUEUE]
92
+ JSON_ERROR_1,
93
+ JSON_ERROR_2,
94
+ JSON_ERROR_3,
95
+ JSON_ERROR_TCP,
96
+ JSON_ERROR_RTU,
97
+ JSON_ERROR_TIMEOUT,
98
+ JSON_QUEUE_DATA,
99
+ JSON_QUEUE_REQUESTS,
100
+ JSON_MASTERS, // list of Modbus TCP/UDP masters separated by <br>
101
+ JSON_SLAVES, // list of Modbus RTU slaves separated by <br>
102
+ #ifdef ENABLE_EXTRA_DIAG
103
+ JSON_RS485_TX,
104
+ JSON_RS485_RX,
105
+ JSON_ETH_TX,
106
+ JSON_ETH_RX,
107
+ #endif /* ENABLE_EXTRA_DIAG */
108
+ JSON_LAST, // Must be the very last element in this array
109
+ };
110
+
111
+
85
112
void recvWeb () {
86
113
EthernetClient client = webServer.available ();
87
114
if (client) {
@@ -139,6 +166,7 @@ void recvWeb() {
139
166
else if ((uri[0 ] == ' /' ) && !strcmp (uri + 2 , " .htm" )) {
140
167
reqPage = (byte)(uri[1 ] - 48 ); // Convert single ASCII char to byte
141
168
}
169
+
142
170
// Actions that require "please wait" page
143
171
if (action == WEB || action == REBOOT || action == ETH_SOFT || action == FACTORY || action == MAC) {
144
172
reqPage = PAGE_WAIT;
@@ -147,6 +175,8 @@ void recvWeb() {
147
175
// Send page
148
176
if ((reqPage > 0 ) && (reqPage <= PAGE_WAIT))
149
177
sendPage (client, reqPage);
178
+ else if (!strcmp (uri, " /data.json" ))
179
+ sendJson (client);
150
180
else if (!strcmp (uri, " /favicon.ico" )) // a favicon
151
181
send204 (client); // if you don't have a favicon, send 204
152
182
else // if the page is unknown, HTTP response code 404
@@ -156,22 +186,21 @@ void recvWeb() {
156
186
if (reqPage == PAGE_WAIT) {
157
187
for (byte n = 0 ; n < maxSockNum; n++) {
158
188
// in case of webserver restart, stop only clients from old webserver (clients with port different from current settings)
159
- // EthernetClient clientTemp = EthernetClient(n);
160
- unsigned int port = W5100. readSnPORT (n );
189
+ EthernetClient client = EthernetClient (n);
190
+ unsigned int port = client. localPort ( );
161
191
// for WEB, stop only clients from old webserver (those that do not match modbus ports or current web port); for other actions stop all clients
162
192
if (action != WEB || (port && port != localConfig.webPort && port != localConfig.udpPort && port != localConfig.tcpPort )) {
163
- W5100.execCmdSn (n, Sock_CLOSE); // close it forcefully
164
- // clientTemp.flush();
165
- // clientTemp.stop();
193
+ client.flush ();
194
+ client.stop ();
166
195
}
167
196
}
168
197
switch (action) {
169
198
case WEB:
170
199
webServer = EthernetServer (localConfig.webPort );
171
200
break ;
172
201
case REBOOT:
173
- Serial .flush ();
174
- Serial .end ();
202
+ mySerial .flush ();
203
+ mySerial .end ();
175
204
resetFunc ();
176
205
break ;
177
206
default : // ETH_SOFT, FACTORY, MAC
@@ -218,7 +247,7 @@ void processPost(char postParameter[]) {
218
247
#endif /* ENABLE_DHCP */
219
248
case POST_IP ... POST_IP_3:
220
249
{
221
- action = ETH_SOFT; // this ETH_SOFT is triggered when the user changes anything on the "IP Settings" page.
250
+ action = ETH_SOFT; // this ETH_SOFT is triggered when the user changes anything on the "IP Settings" page.
222
251
// No need to trigger ETH_SOFT for other cases (POST_SUBNET, POST_GATEWAY etc.)
223
252
localConfig.ip [paramKeyByte - POST_IP] = (byte)paramValueUlong;
224
253
}
@@ -236,10 +265,10 @@ void processPost(char postParameter[]) {
236
265
case POST_TCP:
237
266
{
238
267
for (byte i = 0 ; i < maxSockNum; i++) {
239
- EthernetClient clientTemp = EthernetClient (i);
240
- if (clientTemp. status () != SnSR::UDP && clientTemp .localPort () == localConfig.tcpPort ) {
241
- clientTemp .flush ();
242
- clientTemp .stop ();
268
+ EthernetClient client = EthernetClient (i);
269
+ if (client .localPort () == localConfig.tcpPort ) {
270
+ client .flush ();
271
+ client .stop ();
243
272
}
244
273
}
245
274
localConfig.tcpPort = (unsigned int )paramValueUlong;
@@ -266,7 +295,7 @@ void processPost(char postParameter[]) {
266
295
break ;
267
296
case POST_BAUD:
268
297
{
269
- action = SERIAL_SOFT; // this SERIAL_SOFT is triggered when the user changes anything on the "RS485 Settings" page.
298
+ action = SERIAL_SOFT; // this SERIAL_SOFT is triggered when the user changes anything on the "RS485 Settings" page.
270
299
// No need to trigger ETH_SOFT for other cases (POST_DATA, POST_PARITY etc.)
271
300
localConfig.baud = paramValueUlong;
272
301
byte minFrameDelay = (byte)(frameDelay () / 1000UL ) + 1 ;
@@ -313,6 +342,7 @@ void processPost(char postParameter[]) {
313
342
memcpy (tempMac, localConfig.macEnd , 3 ); // keep current MAC
314
343
localConfig = DEFAULT_CONFIG;
315
344
memcpy (localConfig.macEnd , tempMac, 3 );
345
+ startSerial ();
316
346
break ;
317
347
}
318
348
case MAC:
@@ -330,10 +360,11 @@ void processPost(char postParameter[]) {
330
360
}
331
361
// new parameter values received, save them to EEPROM
332
362
EEPROM.put (CONFIG_START + 1 , localConfig); // it is safe to call, only changed values are updated
333
- if (action == SERIAL_SOFT) { // can do it without "please wait" page
334
- Serial.flush ();
335
- Serial.end ();
336
- startSerial (); // TODO clear queue?
363
+ #ifdef ENABLE_DHCP
364
+ EEPROM.put (CONFIG_START + 1 + sizeof (localConfig), extraConfig);
365
+ #endif /* ENABLE_DHCP */
366
+ if (action == SERIAL_SOFT) { // can do it without "please wait" page
367
+ startSerial ();
337
368
action = NONE;
338
369
}
339
370
}
0 commit comments