-
Notifications
You must be signed in to change notification settings - Fork 0
/
_wwwServer.ino
957 lines (795 loc) · 32.1 KB
/
_wwwServer.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
// 1-channel LoRa Gateway for ESP8266
// Copyright (c) 2016, 2017 Maarten Westenberg version for ESP8266
// Version 4.0.8
// Date: 2017-08-05
//
// based on work done by many people and making use of several libraries.
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the MIT License
// which accompanies this distribution, and is available at
// https://opensource.org/licenses/mit-license.php
//
// Author: Maarten Westenberg (mw12554@hotmail.com)
//
// This file contains the webserver code for the ESP Single Channel Gateway.
// Note:
// The ESP Webserver works with Strings to display html content.
// Care must be taken that not all data is output to the webserver in one string
// as this will use a LOT of memory and possibly kill the heap (cause system
// crash or other unreliable behaviour.
// Instead, output of the various tables on the webpage should be displayed in
// chucks so that strings are limited in size.
// Be aware that using no strings but only sendContent() calls has its own
// disadvantage that these calls take a lot of time and cause the page to be
// displayed like an old typewriter.
// So, the trick is to make chucks that are sent to the website by using
// a response String but not make those Strings too big.
// ----------------------------------------------------------------------------
// PRINT IP
// Output the 4-byte IP address for easy printing.
// As this function is also used by _otaServer.ino do not put in #define
// ----------------------------------------------------------------------------
static void printIP(IPAddress ipa, const char sep, String& response)
{
response+=(IPAddress)ipa[0]; response+=sep;
response+=(IPAddress)ipa[1]; response+=sep;
response+=(IPAddress)ipa[2]; response+=sep;
response+=(IPAddress)ipa[3];
}
#if A_SERVER==1
// ================================================================================
// WEBSERVER DECLARATIONS
// None at the moment
// ================================================================================
// WEBSERVER FUNCTIONS
// ----------------------------------------------------------------------------
// Print a HEXadecimal string from a 4-byte char string
//
// ----------------------------------------------------------------------------
static void printHEX(char * hexa, const char sep, String& response)
{
char m;
m = hexa[0]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
m = hexa[1]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
m = hexa[2]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
m = hexa[3]; if (m<016) response+='0'; response += String(m, HEX); response+=sep;
}
// ----------------------------------------------------------------------------
// stringTime
// Only when RTC is present we print real time values
// t contains number of milli seconds since system started that the event happened.
// So a value of 100 wold mean that the event took place 1 minute and 40 seconds ago
// ----------------------------------------------------------------------------
static void stringTime(unsigned long t, String& response) {
if (t==0) { response += "--"; return; }
// now() gives seconds since 1970
time_t eventTime = now() - ((millis()-t)/1000);
byte _hour = hour(eventTime);
byte _minute = minute(eventTime);
byte _second = second(eventTime);
switch(weekday(eventTime)) {
case 1: response += "Sunday "; break;
case 2: response += "Monday "; break;
case 3: response += "Tuesday "; break;
case 4: response += "Wednesday "; break;
case 5: response += "Thursday "; break;
case 6: response += "Friday "; break;
case 7: response += "Saturday "; break;
}
response += String() + day(eventTime) + "-";
response += String() + month(eventTime) + "-";
response += String() + year(eventTime) + " ";
if (_hour < 10) response += "0";
response += String() + _hour + ":";
if (_minute < 10) response += "0";
response += String() + _minute + ":";
if (_second < 10) response += "0";
response += String() + _second;
}
// ----------------------------------------------------------------------------
// SET ESP8266 WEB SERVER VARIABLES
//
// This funtion implements the WiFI Webserver (very simple one). The purpose
// of this server is to receive simple admin commands, and execute these
// results are sent back to the web client.
// Commands: DEBUG, ADDRESS, IP, CONFIG, GETTIME, SETTIME
// The webpage is completely built response and then printed on screen.
// ----------------------------------------------------------------------------
static void setVariables(const char *cmd, const char *arg) {
// DEBUG settings; These can be used as a single argument
if (strcmp(cmd, "DEBUG")==0) { // Set debug level 0-2
if (atoi(arg) == 1) {
debug = (debug+1)%4;
}
else if (atoi(arg) == -1) {
debug = (debug+3)%4;
}
writeGwayCfg(CONFIGFILE); // Save configuration to file
}
if (strcmp(cmd, "CAD")==0) { // Set -cad on=1 or off=0
_cad=(bool)atoi(arg);
writeGwayCfg(CONFIGFILE); // Save configuration to file
}
if (strcmp(cmd, "HOP")==0) { // Set -hop on=1 or off=0
_hop=(bool)atoi(arg);
if (! _hop) { ifreq=0; freq=freqs[0]; rxLoraModem(); }
writeGwayCfg(CONFIGFILE); // Save configuration to file
}
if (strcmp(cmd, "DELAY")==0) { // Set delay usecs
txDelay+=atoi(arg)*1000;
}
// SF; Handle Spreading Factor Settings
if (strcmp(cmd, "SF")==0) {
uint8_t sfi = sf;
if (atoi(arg) == 1) {
if (sf==SF12) sf=SF7; else sf= (sf_t)((int)sf+1);
}
else if (atoi(arg) == -1) {
if (sf==SF7) sf=SF12; else sf= (sf_t)((int)sf-1);
}
rxLoraModem(); // Reset the radion with the new spreading factor
writeGwayCfg(CONFIGFILE); // Save configuration to file
}
// FREQ; Handle Frequency Settings
if (strcmp(cmd, "FREQ")==0) {
uint8_t nf = sizeof(freqs)/sizeof(int); // Number of elements in array
// Compute frequency index
if (atoi(arg) == 1) {
if (ifreq==(nf-1)) ifreq=0; else ifreq++;
}
else if (atoi(arg) == -1) {
Serial.println("down");
if (ifreq==0) ifreq=(nf-1); else ifreq--;
}
freq = freqs[ifreq];
rxLoraModem(); // Reset the radion with the new frequency
writeGwayCfg(CONFIGFILE); // Save configuration to file
}
//if (strcmp(cmd, "GETTIME")==0) { Serial.println(F("gettime tbd")); } // Get the local time
//if (strcmp(cmd, "SETTIME")==0) { Serial.println(F("settime tbd")); } // Set the local time
if (strcmp(cmd, "HELP")==0) { Serial.println(F("Display Help Topics")); }
#if GATEWAYNODE==1
if (strcmp(cmd, "NODE")==0) { // Set node on=1 or off=0
gwayConfig.node =(bool)atoi(arg);
writeGwayCfg(CONFIGFILE); // Save configuration to file
}
if (strcmp(cmd, "FCNT")==0) {
frameCount=0;
rxLoraModem(); // Reset the radion with the new frequency
writeGwayCfg(CONFIGFILE);
}
#endif
#if WIFIMANAGER==1
if (strcmp(cmd, "NEWSSID")==0) {
WiFiManager wifiManager;
strcpy(wpa[0].login,"");
strcpy(wpa[0].passw,"");
WiFi.disconnect();
wifiManager.autoConnect(AP_NAME, AP_PASSWD );
}
#endif
#if A_OTAA==1
if (strcmp(cmd, "UPDATE")==0) {
if (atoi(arg) == 1) {
updateOtaa();
}
}
#endif
#if A_REFRESH==1
if (strcmp(cmd, "REFR")==0) { // Set refresh on=1 or off=0
gwayConfig.refresh =(bool)atoi(arg);
writeGwayCfg(CONFIGFILE); // Save configuration to file
}
#endif
}
// ----------------------------------------------------------------------------
// OPEN WEB PAGE
// This is the init function for opening the webpage
//
// ----------------------------------------------------------------------------
static void openWebPage()
{
++gwayConfig.views; // increment number of views
#if A_REFRESH==1
//server.client().stop(); // Experimental, stop webserver in case something is still running!
#endif
String response="";
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
// init webserver, fill the webpage
// NOTE: The page is renewed every _WWW_INTERVAL seconds, please adjust in ESP-sc-gway.h
//
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", "");
#if A_REFRESH==1
if (gwayConfig.refresh) {
response += String() + "<!DOCTYPE HTML><HTML><HEAD><meta http-equiv='refresh' content='"+_WWW_INTERVAL+";http://";
printIP((IPAddress)WiFi.localIP(),'.',response);
response += "'><TITLE>ESP8266 1ch Gateway</TITLE>";
}
else {
response += String() + "<!DOCTYPE HTML><HTML><HEAD><TITLE>ESP8266 1ch Gateway</TITLE>";
}
#else
response += String() + "<!DOCTYPE HTML><HTML><HEAD><TITLE>ESP8266 1ch Gateway</TITLE>";
#endif
response += "<META HTTP-EQUIV='CONTENT-TYPE' CONTENT='text/html; charset=UTF-8'>";
response += "<META NAME='AUTHOR' CONTENT='M. Westenberg (mw1554@hotmail.com)'>";
response += "<style>.thead {background-color:green; color:white;} ";
response += ".cell {border: 1px solid black;}";
response += ".config_table {max_width:100%; min-width:400px; width:95%; border:1px solid black; border-collapse:collapse;}";
response += "</style></HEAD><BODY>";
response +="<h1>ESP Gateway Config</h1>";
response +="Version: "; response+=VERSION;
response +="<br>ESP alive since ";
stringTime(1, response);
response +=", Uptime: ";
uint32_t secs = millis()/1000;
uint16_t days = secs / 86400; // Determine number of days
uint8_t _hour = hour(secs);
uint8_t _minute = minute(secs);
uint8_t _second = second(secs);
response += String() + days + "-";
if (_hour < 10) response += "0";
response += String() + _hour + ":";
if (_minute < 10) response += "0";
response += String() + _minute + ":";
if (_second < 10) response += "0";
response += String() + _second;
response +="<br>Current time ";
stringTime(millis(), response);
response +="<br>";
server.sendContent(response);
}
// ----------------------------------------------------------------------------
// CONFIG DATA
//
//
// ----------------------------------------------------------------------------
static void configData()
{
String response="";
String bg="";
response +="<h2>Gateway Settings</h2>";
response +="<table class=\"config_table\">";
response +="<tr>";
response +="<th class=\"thead\">Setting</th>";
response +="<th style=\"background-color: green; color: white; width:120px;\">Value</th>";
response +="<th colspan=\"2\" style=\"background-color: green; color: white; width:100px;\">Set</th>";
response +="</tr>";
#if GATEWAYMGT==1
bg = " background-color: ";
bg += ( (_cad == 1) ? "LightGreen" : "orange" );
response +="<tr><td class=\"cell\">CAD</td>";
response +="<td style=\"border: 1px solid black;"; response += bg; response += "\">";
response += ( (_cad == 1) ? "ON" : "OFF" );
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"CAD=1\"><button>ON</button></a></td>";
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"CAD=0\"><button>OFF</button></a></td>";
response +="</tr>";
bg = " background-color: ";
bg += ( (_hop == 1) ? "LightGreen" : "orange" );
response +="<tr><td class=\"cell\">HOP</td>";
response +="<td style=\"border: 1px solid black;"; response += bg; response += "\">";
response += ( (_hop == 1) ? "ON" : "OFF" );
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"HOP=1\"><button>ON</button></a></td>";
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"HOP=0\"><button>OFF</button></a></td>";
response +="</tr>";
response +="<tr><td class=\"cell\">SF Setting</td><td class=\"cell\">";
if (_cad == 1) {
response += "AUTO</td>";
}
else {
response += sf;
response +="<td class=\"cell\"><a href=\"SF=-1\"><button>-</button></a></td>";
response +="<td class=\"cell\"><a href=\"SF=1\"><button>+</button></a></td>";
}
response +="</tr>";
// Channel
response +="<tr><td class=\"cell\">Channel</td>";
response +="<td class=\"cell\">";
if (_hop == 1) {
response += "AUTO</td>";
}
else {
response += String() + ifreq;
response +="</td>";
response +="<td class=\"cell\"><a href=\"FREQ=-1\"><button>-</button></a></td>";
response +="<td class=\"cell\"><a href=\"FREQ=1\"><button>+</button></a></td>";
}
response +="</tr>";
#endif
// Debugging options
response +="<tr><td class=\"cell\">";
response +="Debug level</td><td class=\"cell\">";
response +=debug;
response +="</td>";
response +="<td class=\"cell\"><a href=\"DEBUG=-1\"><button>-</button></a></td>";
response +="<td class=\"cell\"><a href=\"DEBUG=1\"><button>+</button></a></td>";
response +="</tr>";
#if GATEWAYNODE==1
response +="<tr><td class=\"cell\">Framecounter Internal Sensor</td>";
response +="<td class=\"cell\">";
response +=frameCount;
response +="</td><td colspan=\"2\" style=\"border: 1px solid black;\">";
response +="<button><a href=\"/FCNT\">RESET</a></button></td>";
response +="</tr>";
bg = " background-color: ";
bg += ( (gwayConfig.node == 1) ? "LightGreen" : "orange" );
response +="<tr><td class=\"cell\">Gateway Node</td>";
response +="<td class=\"cell\" style=\"border: 1px solid black;" + bg + "\">";
response += ( (gwayConfig.node == true) ? "ON" : "OFF" );
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"NODE=1\"><button>ON</button></a></td>";
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"NODE=0\"><button>OFF</button></a></td>";
response +="</tr>";
#endif
#if A_REFRESH==1
bg = " background-color: ";
bg += ( (gwayConfig.refresh == 1) ? "LightGreen" : "orange" );
response +="<tr><td class=\"cell\">WWW Refresh</td>";
response +="<td class=\"cell\" style=\"border: 1px solid black;" + bg + "\">";
response += ( (gwayConfig.refresh == 1) ? "ON" : "OFF" );
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"REFR=1\"><button>ON</button></a></td>";
response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"REFR=0\"><button>OFF</button></a></td>";
response +="</tr>";
#endif
#if WIFIMANAGER==1
response +="<tr><td>";
response +="Click <a href=\"/NEWSSID\">here</a> to reset accesspoint<br>";
response +="</td><td></td></tr>";
#endif
// Reset all statistics
#if STATISTICS >= 1
response +="<tr><td class=\"cell\">Statistics</td>";
response +=String() + "<td class=\"cell\">"+statc.resets+"</td>";
response +="<td colspan=\"2\" class=\"cell\"><a href=\"/RESET\"><button>RESET</button></a></td></tr>";
response +="<tr><td class=\"cell\">Boots and Resets</td>";
response +=String() + "<td class=\"cell\">"+gwayConfig.boots+"</td>";
response +="<td colspan=\"2\" class=\"cell\"><a href=\"/BOOT\"><button>RESET</button></a></td></tr>";
#endif
response +="</table>";
// Update Firmware all statistics
response +="<tr><td class=\"cell\">Update Firmware</td>";
response +="<td class=\"cell\"></td><td colspan=\"2\" class=\"cell\"><a href=\"/UPDATE=1\"><button>RESET</button></a></td></tr>";
response +="</table>";
server.sendContent(response);
}
// ----------------------------------------------------------------------------
// INTERRUPT DATA
// Display interrupt data, but only for debug >= 2
//
// ----------------------------------------------------------------------------
static void interruptData()
{
if (debug >= 2) {
String response="";
response +="<h2>System State and Interrupt</h2>";
response +="<table class=\"config_table\">";
response +="<tr>";
response +="<th class=\"thead\">Parameter</th>";
response +="<th class=\"thead\">Value</th>";
response +="<th colspan=\"2\" class=\"thead\">Set</th>";
response +="</tr>";
response +="<tr><td class=\"cell\">_state</td>";
response +="<td class=\"cell\">";
switch (_state) { // See loraModem.h
case 0: response +="INIT"; break;
case 1: response +="SCAN"; break;
case 2: response +="CAD"; break;
case 3: response +="RX"; break;
case 4: response +="RXDONE"; break;
case 5: response +="TX"; break;
default: response +="unknown"; break;
}
response +="</td></tr>";
response +="<tr><td class=\"cell\">flags (8 bits)</td>";
response +="<td class=\"cell\">0x";
if (flags <16) response += "0";
response+=String(flags,HEX); response+="</td></tr>";
response +="<tr><td class=\"cell\">mask (8 bits)</td>";
response +="<td class=\"cell\">0x";
if (mask <16) response += "0";
response+=String(mask,HEX); response+="</td></tr>";
response +="<tr><td class=\"cell\">REENTRANT</td>";
response +="<td class=\"cell\">";
response += (REENTRANT==1 ? "SAFE" : (REENTRANT==2 ? "ON" : "OFF" ));
response+="</td></tr>";
response +="<tr><td class=\"cell\">Re-entrant cntr</td>";
response +="<td class=\"cell\">";
response += String() + gwayConfig.reents;
response+="</td></tr>";
response +="<tr><td class=\"cell\">ntp call cntr</td>";
response +="<td class=\"cell\">";
response += String() + gwayConfig.ntps;
response+="</td></tr>";
response +="<tr><td class=\"cell\">ntpErr cntr</td>";
response +="<td class=\"cell\">";
response += String() + gwayConfig.ntpErr;
response+="</td></tr>";
response +="<tr><td class=\"cell\">Timing Correction (uSec)</td><td class=\"cell\">";
response += txDelay;
response +="</td>";
response +="<td class=\"cell\"><a href=\"DELAY=-1\"><button>-</button></a></td>";
response +="<td class=\"cell\"><a href=\"DELAY=1\"><button>+</button></a></td>";
response +="</tr>";
response +="</table>";
server.sendContent(response);
}// if debug>=2
}
// ----------------------------------------------------------------------------
// STATISTICS DATA
//
// ----------------------------------------------------------------------------
static void statisticsData()
{
String response="";
response +="<h2>Package Statistics</h2>";
response +="<table class=\"config_table\">";
response +="<tr>";
response +="<th class=\"thead\">Counter</th>";
response +="<th class=\"thead\">Value</th>";
response +="</tr>";
response +="<tr><td class=\"cell\">Packages Uplink Total</td><td class=\"cell\">";
response +=cp_nb_rx_rcv; response+="</tr>";
response +="<tr><td class=\"cell\">Packages Uplink OK </td><td class=\"cell\">";
response +=cp_nb_rx_ok; response+="</tr>";
response +="<tr><td class=\"cell\">Packages Downlink</td><td class=\"cell\">";
response +=cp_up_pkt_fwd; response+="</tr>";
#if STATISTICS >= 2
response +="<tr><td class=\"cell\">SF7 rcvd</td>"; response +="<td class=\"cell\">"; response +=statc.sf7; response +="</td></tr>";
response +="<tr><td class=\"cell\">SF8 rcvd</td>"; response +="<td class=\"cell\">"; response +=statc.sf8; response +="</td></tr>";
response +="<tr><td class=\"cell\">SF9 rcvd</td>"; response +="<td class=\"cell\">"; response +=statc.sf9; response +="</td></tr>";
response +="<tr><td class=\"cell\">SF10 rcvd</td>"; response +="<td class=\"cell\">"; response +=statc.sf10; response +="</td></tr>";
response +="<tr><td class=\"cell\">SF11 rcvd</td>"; response +="<td class=\"cell\">"; response +=statc.sf11; response +="</td></tr>";
response +="<tr><td class=\"cell\">SF12 rcvd</td>"; response +="<td class=\"cell\">"; response +=statc.sf12; response +="</td></tr>";
#endif
response +="</table>";
server.sendContent(response);
}
// ----------------------------------------------------------------------------
// SENSORDATA
// If enabled, display the sensorHistory on the current webserver Page.
// Parameters:
// - <none>
// Returns:
// - <none>
// ----------------------------------------------------------------------------
static void sensorData()
{
#if STATISTICS >= 1
String response="";
response += "<h2>Message History</h2>";
response += "<table class=\"config_table\">";
response += "<tr>";
response += "<th class=\"thead\">Time</th>";
response += "<th class=\"thead\">Node</th>";
response += "<th class=\"thead\" colspan=\"2\">Channel</th>";
response += "<th class=\"thead\" style=\"width: 50px;\">SF</th>";
response += "<th class=\"thead\" style=\"width: 50px;\">RSSI</th>";
response += "<th class=\"thead\" style=\"width: 50px;\">pRSSI</th>";
response += "</tr>";
server.sendContent(response);
for (int i=0; i<MAX_STAT; i++) {
if (statr[i].sf == 0) break;
response = "";
response += String() + "<tr><td class=\"cell\">";
stringTime(statr[i].tmst, response);
response += "</td>";
response += String() + "<td class=\"cell\">";
printHEX((char *)(& (statr[i].node)),' ',response);
response += "</td>";
response += String() + "<td class=\"cell\">" + statr[i].ch + "</td>";
response += String() + "<td class=\"cell\">" + freqs[statr[i].ch] + "</td>";
response += String() + "<td class=\"cell\">" + statr[i].sf + "</td>";
response += String() + "<td class=\"cell\">" + statr[i].rssi + "</td>";
response += String() + "<td class=\"cell\">" + statr[i].prssi + "</td></tr>";
server.sendContent(response);
}
server.sendContent("</table>");
#endif
}
// ----------------------------------------------------------------------------
// SYSTEMDATA
//
// ----------------------------------------------------------------------------
static void systemData()
{
String response="";
response +="<h2>System Status</h2>";
response +="<table class=\"config_table\">";
response +="<tr>";
response +="<th class=\"thead\">Parameter</th>";
response +="<th class=\"thead\">Value</th>";
response +="</tr>";
response +="<tr><td style=\"border: 1px solid black; width:120px;\">Gateway ID</td>";
response +="<td class=\"cell\">";
if (MAC_array[0]< 0x10) response +='0'; response +=String(MAC_array[0],HEX); // The MAC array is always returned in lowercase
if (MAC_array[1]< 0x10) response +='0'; response +=String(MAC_array[1],HEX);
if (MAC_array[2]< 0x10) response +='0'; response +=String(MAC_array[2],HEX);
response +="FFFF";
if (MAC_array[3]< 0x10) response +='0'; response +=String(MAC_array[3],HEX);
if (MAC_array[4]< 0x10) response +='0'; response +=String(MAC_array[4],HEX);
if (MAC_array[5]< 0x10) response +='0'; response +=String(MAC_array[5],HEX);
response+="</tr>";
response +="<tr><td class=\"cell\">Free heap</td><td class=\"cell\">"; response+=ESP.getFreeHeap(); response+="</tr>";
response +="<tr><td class=\"cell\">ESP Chip ID</td><td class=\"cell\">"; response+=ESP.getChipId(); response+="</tr>";
#if STATISTICS>=1
response +="<tr><td class=\"cell\">WiFi Setups</td><td class=\"cell\">"; response+=gwayConfig.wifis; response+="</tr>";
response +="<tr><td class=\"cell\">WWW Views</td><td class=\"cell\">"; response+=gwayConfig.views; response+="</tr>";
#endif
response +="</table>";
server.sendContent(response);
}
// ----------------------------------------------------------------------------
// WIFIDATA
// Display the most important Wifi parameters gathered
//
// ----------------------------------------------------------------------------
static void wifiData()
{
String response="";
response +="<h2>WiFi Config</h2>";
response +="<table class=\"config_table\">";
response +="<tr><th class=\"thead\">Parameter</th><th class=\"thead\">Value</th></tr>";
response +="<tr><td class=\"cell\">WiFi host</td><td class=\"cell\">";
response +=wifi_station_get_hostname(); response+="</tr>";
response +="<tr><td class=\"cell\">WiFi SSID</td><td class=\"cell\">";
response +=WiFi.SSID(); response+="</tr>";
response +="<tr><td class=\"cell\">IP Address</td><td class=\"cell\">";
printIP((IPAddress)WiFi.localIP(),'.',response);
response +="</tr>";
response +="<tr><td class=\"cell\">IP Gateway</td><td class=\"cell\">";
printIP((IPAddress)WiFi.gatewayIP(),'.',response);
response +="</tr>";
response +="<tr><td class=\"cell\">NTP Server</td><td class=\"cell\">"; response+=NTP_TIMESERVER; response+="</tr>";
response +="<tr><td class=\"cell\">LoRa Router</td><td class=\"cell\">"; response+=_TTNSERVER; response+="</tr>";
response +="<tr><td class=\"cell\">LoRa Router IP</td><td class=\"cell\">";
printIP((IPAddress)ttnServer,'.',response);
response +="</tr>";
#ifdef _THINGSERVER
response +="<tr><td class=\"cell\">LoRa Router 2</td><td class=\"cell\">"; response+=_THINGSERVER;
response += String() + ":" + _THINGPORT + "</tr>";
response +="<tr><td class=\"cell\">LoRa Router 2 IP</td><td class=\"cell\">";
printIP((IPAddress)thingServer,'.',response);
response +="</tr>";
#endif
response +="</table>";
server.sendContent(response);
}
// ----------------------------------------------------------------------------
// SEND WEB PAGE()
// Call the webserver and send the standard content and the content that is
// passed by the parameter.
//
// NOTE: This is the only place where yield() or delay() calls are used.
//
// ----------------------------------------------------------------------------
void sendWebPage(const char *cmd, const char *arg)
{
openWebPage(); yield();
setVariables(cmd,arg); yield();
statisticsData(); yield(); // Node statistics
sensorData(); yield(); // Display the sensor history, message statistics
systemData(); yield(); // System statistics such as heap etc.
wifiData(); yield(); // WiFI specific parameters
configData(); yield(); // Display web configuration
interruptData(); yield(); // Display interrupts only when debug >= 2
// Close the client connection to server
server.sendContent(String() + "<br><br />Click <a href=\"/HELP\">here</a> to explain Help and REST options<br>");
server.sendContent(String() + "</BODY></HTML>");
server.sendContent(""); yield();
server.client().stop();
}
// ----------------------------------------------------------------------------
// Used by timeout functions
// This function only displays the standard homepage
// Note: This function is not actively used, as the page is renewed
// by using a HTML meta setting of 60 seconds
// ----------------------------------------------------------------------------
static void renewWebPage()
{
//Serial.println(F("Renew Web Page"));
//sendWebPage("","");
//return;
}
// ----------------------------------------------------------------------------
// SetupWWW function called by main setup() program to setup webserver
// It does actually not much more than installing the callback handlers
// for messages sent to the webserver
//
// ----------------------------------------------------------------------------
void setupWWW()
{
server.begin(); // Start the webserver
// -----------------
// BUTTONS, define what should happen with the buttons we press on the homepage
server.on("/", []() {
sendWebPage("",""); // Send the webPage string
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/HELP", []() {
sendWebPage("HELP",""); // Send the webPage string
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Reset the statistics
server.on("/RESET", []() {
Serial.println(F("RESET"));
cp_nb_rx_rcv = 0;
cp_nb_rx_ok = 0;
cp_up_pkt_fwd = 0;
#if STATISTICS >= 1
for (int i=0; i<MAX_STAT; i++) { statr[i].sf = 0; }
#if STATISTICS >= 2
statc.sf7 = 0;
statc.sf8 = 0;
statc.sf9 = 0;
statc.sf10= 0;
statc.sf11= 0;
statc.sf12= 0;
statc.resets= 0;
writeGwayCfg(CONFIGFILE);
#endif
#endif
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Reset the boot counter
server.on("/BOOT", []() {
Serial.println(F("BOOT"));
#if STATISTICS >= 2
gwayConfig.boots = 0;
gwayConfig.wifis = 0;
gwayConfig.views = 0;
gwayConfig.ntpErr = 0; // NTP errors
gwayConfig.ntps = 0; // Number of NTP calls
#endif
gwayConfig.reents = 0; // Re-entrance
writeGwayCfg(CONFIGFILE);
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/NEWSSID", []() {
sendWebPage("NEWSSID",""); // Send the webPage string
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Set debug parameter
server.on("/DEBUG=-1", []() { // Set debug level 0-2
debug = (debug+3)%4;
writeGwayCfg(CONFIGFILE); // Save configuration to file
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/DEBUG=1", []() {
debug = (debug+1)%4;
writeGwayCfg(CONFIGFILE); // Save configuration to file
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Set delay in microseconds
server.on("/DELAY=1", []() {
txDelay+=1000;
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/DELAY=-1", []() {
txDelay-=1000;
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Spreading Factor setting
server.on("/SF=1", []() {
if (sf==SF12) sf=SF7; else sf= (sf_t)((int)sf+1);
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/SF=-1", []() {
if (sf==SF7) sf=SF12; else sf= (sf_t)((int)sf-1);
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Frequency of the GateWay node
server.on("/FREQ=1", []() {
uint8_t nf = sizeof(freqs)/sizeof(int); // Number of elements in array
if (ifreq==(nf-1)) ifreq=0; else ifreq++;
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/FREQ=-1", []() {
uint8_t nf = sizeof(freqs)/sizeof(int); // Number of elements in array
if (ifreq==0) ifreq=(nf-1); else ifreq--;
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Set CAD function off/on
server.on("/CAD=1", []() {
_cad=(bool)1;
writeGwayCfg(CONFIGFILE); // Save configuration to file
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/CAD=0", []() {
_cad=(bool)0;
writeGwayCfg(CONFIGFILE); // Save configuration to file
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// GatewayNode
server.on("/NODE=1", []() {
#if GATEWAYNODE==1
gwayConfig.node =(bool)1;
writeGwayCfg(CONFIGFILE); // Save configuration to file
#endif
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/NODE=0", []() {
#if GATEWAYNODE==1
gwayConfig.node =(bool)0;
writeGwayCfg(CONFIGFILE); // Save configuration to file
#endif
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Framecounter of the Gateway node
server.on("/FCNT", []() {
#if GATEWAYNODE==1
frameCount=0;
rxLoraModem(); // Reset the radion with the new frequency
writeGwayCfg(CONFIGFILE);
#endif
//sendWebPage("",""); // Send the webPage string
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// WWW Page refresh function
server.on("/REFR=1", []() { // WWW page auto refresh ON
#if A_REFRESH==1
gwayConfig.refresh =1;
writeGwayCfg(CONFIGFILE); // Save configuration to file
#endif
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/REFR=0", []() { // WWW page auto refresh OFF
#if A_REFRESH==1
gwayConfig.refresh =0;
writeGwayCfg(CONFIGFILE); // Save configuration to file
#endif
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Switch off/on the HOP functions
server.on("/HOP=1", []() {
_hop=(bool)1;
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
server.on("/HOP=0", []() {
_hop=(bool)0;
if (! _hop) { ifreq=0; freq=freqs[0]; rxLoraModem(); }
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// Update the sketch. Not yet implemented
server.on("/UPDATE=1", []() {
#if A_OTAA==1
updateOtaa();
#endif
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
});
// -----------
// This section from version 4.0.7 defines what PART of the
// webpage is shown based on the buttons pressed by the user
// Maybe not all information wshould be put on the screen since it
// may take too much time to serve all information before a next
// package interrupt arrives at tyhe gateway
Serial.print(F("WWW Server started on port "));
Serial.println(A_SERVERPORT);
return;
}
#endif