/
rfm69gw.ino
1661 lines (1531 loc) · 58.5 KB
/
rfm69gw.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
/* RFM69 library and code by Felix Rusu - felix@lowpowerlab.com
// Get libraries at: https://github.com/LowPowerLab/
// Make sure you adjust the settings in the configuration section below !!!
// **********************************************************************************
// Copyright Felix Rusu, LowPowerLab.com
// Library and code by Felix Rusu - felix@lowpowerlab.com
// **********************************************************************************
// License
// **********************************************************************************
// 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></http:>.
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
// **********************************************************************************/
// Required Modifications:
// 1. PubSubClient.h #define MQTT_MAX_PACKET_SIZE 256
// 2. add delay(1); in line 244 in RFM69.cpp to reset watchdog
// setup Gateway IP: 192.168.4.1
#define SERIAL_BAUD 57600
#include <ESP8266WiFi.h>
#include <RFM69.h> //https://www.github.com/lowpowerlab/rfm69
#include <pgmspace.h>
#include <PubSubClient.h>
uint32_t reachableNode[8];
char RadioConfig[128];
// Default values
const char PROGMEM ENCRYPTKEY[] = "sampleEncryptKey";
const char PROGMEM MDNS_NAME[] = "rfm69gw1";
const char PROGMEM MQTT_BROKER[] = "raspberrypi";
const char PROGMEM RFM69AP_NAME[] = "RFM69-AP";
//Passwort und Benutzer zum Softwareupdate ueber Browser
const char PROGMEM UPDATEUSER[] = "B";//sw
const char PROGMEM UPDATEPASSWORD[] = "B";//sw
const char PROGMEM MQTTPASSWORD[] = "B";
const char PROGMEM MQTTUSER[] = "B";
#define NETWORKID 1 //the same on all nodes that talk to each other
#define NODEID 1
//Match frequency to the hardware version of the radio
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
//#define FREQUENCY RF69_915MHZ
#define IS_RFM69HCW false // set to 'true' if you are using an RFM69HCW module
#define POWER_LEVEL 31
#define showKeysInWeb false
#define Led_user 0//sw
#define HiddenString "xxx"
WiFiClient espClient;
PubSubClient mqttClient(espClient);
// vvvvvvvvv Global Configuration vvvvvvvvvvv
#include <EEPROM.h>
struct _GLOBAL_CONFIG {
uint32_t checksum;
char rfmapname[32];
char mqttbroker[32];
char mqttclientname[32];
char mdnsname[32];
uint32_t ipaddress; // if 0, use DHCP
uint32_t ipnetmask;
uint32_t ipgateway;
uint32_t ipdns1;
uint32_t ipdns2;
char encryptkey[16+1];
uint8_t networkid;
uint8_t nodeid;
uint8_t powerlevel; // bits 0..4 power level, bit 7 RFM69HCW 1=true
uint8_t rfmfrequency;
char updateUser[20];
char updatePassword[20];
char mqttPassword[20];
char mqttUser[20];
};
#define GC_POWER_LEVEL (pGC->powerlevel & 0x1F)
#define GC_IS_RFM69HCW ((pGC->powerlevel & 0x80) != 0)
struct _GLOBAL_CONFIG *pGC;
// ^^^^^^^^^ Global Configuration ^^^^^^^^^^^
// vvvvvvvvv ESP8266 WiFi vvvvvvvvvvv
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
}
void wifi_setup(void) {
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing. Wipes out SSID/password.
//wifiManager.resetSettings();
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
uint32_t blocking = wifiManager.autoConnect(pGC->rfmapname);
while(!blocking) {
delay(20000);
wifiManager.autoConnect(pGC->rfmapname);
}
Serial.println("connected");
}
// ^^^^^^^^^ ESP8266 WiFi ^^^^^^^^^^^
// vvvvvvvvv Global Configuration vvvvvvvvvvv
uint32_t gc_checksum() {
uint8_t *p = (uint8_t *)pGC;
uint32_t checksum = 0;
p += sizeof(pGC->checksum);
for (size_t i = 0; i < (sizeof(*pGC) - 4); i++) {
checksum += *p++;
}
return checksum;
}
void eeprom_setup() {
EEPROM.begin(4096);
pGC = (struct _GLOBAL_CONFIG *)EEPROM.getDataPtr();
// if checksum bad init GC else use GC values
if (gc_checksum() != pGC->checksum) {
Serial.println("Factory reset");
memset(pGC, 0, sizeof(*pGC));
Serial.println("ENCRYPTKEY");
strcpy_P(pGC->encryptkey, ENCRYPTKEY);
Serial.println("RFM69AP_NAME");
strcpy_P(pGC->rfmapname, RFM69AP_NAME);
Serial.println("MQTT_BROKER");
strcpy_P(pGC->mqttbroker, MQTT_BROKER);
Serial.println("MDNS_NAME");
strcpy_P(pGC->mdnsname, MDNS_NAME);
Serial.println("mqttclientname");
//bei der folgenden Zeile haengt sich der Chip auf: Exception (28)
//strcpy(pGC->mqttclientname, WiFi.hostname().c_str());
Serial.println("NETWORKID");
pGC->networkid = NETWORKID;
Serial.println("NODEID");
pGC->nodeid = NODEID;
Serial.println("powerlevel");
pGC->powerlevel = ((IS_RFM69HCW)?0x80:0x00) | POWER_LEVEL;
Serial.println("rfmfrequency");
pGC->rfmfrequency = FREQUENCY;
Serial.println("UPDATEPW");
strncpy_P(pGC->updateUser, UPDATEUSER, 20);
strncpy_P(pGC->updatePassword, UPDATEPASSWORD, 20);
strncpy_P(pGC->mqttUser, MQTTUSER, 20);
strncpy_P(pGC->mqttPassword, MQTTPASSWORD, 20);
Serial.println("checksum");
pGC->checksum = gc_checksum();
Serial.println("EEPROM.commit");
EEPROM.commit();
}
}
// ^^^^^^^^^ Global Configuration ^^^^^^^^^^^
// vvvvvvvvv ESP8266 web sockets vvvvvvvvvvv
#include <ESP8266mDNS.h>
// URL: http://rfm69gw.local
MDNSResponder mdns;
void mdns_setup(void) {
if (pGC->mdnsname[0] == '\0') return;
if (mdns.begin(pGC->mdnsname, WiFi.localIP())) {
Serial.println("MDNS responder started");
mdns.addService("http", "tcp", 80);
mdns.addService("ws", "tcp", 81);
}
else {
Serial.println("MDNS.begin failed");
}
Serial.printf("Connect to http://%s.local or http://", pGC->mdnsname);
Serial.println(WiFi.localIP());
}
static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>RFM69 Gateway</title>
<style>
"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }"
</style>
<script>
var websock;
function start() {
var rfm69nodes = [];
websock = new WebSocket('ws://' + window.location.hostname + ':81/');
websock.onopen = function(evt) { console.log('websock open'); };
websock.onclose = function(evt) { console.log('websock close'); };
websock.onerror = function(evt) { console.log(evt); };
websock.onmessage = function(evt) {
//console.log(evt);
// evt.data holds the gateway radio status info in JSON format
var gwobj = JSON.parse(evt.data);
if (gwobj && gwobj.msgType) {
if (gwobj.msgType === "config") {
var eConfig = document.getElementById('rfm69Config');
eConfig.innerHTML = '<p>Frequency ' + gwobj.freq + ' MHz' +
', Network ID:' + gwobj.netid +
', RFM69HCW:' + gwobj.rfm69hcw +
', Power Level:' + gwobj.power;
}
else if (gwobj.msgType === "status") {
var eStatus = document.getElementById('rfm69Status');
rfm69nodes[gwobj.senderId] = gwobj;
var aTable = '<table>';
aTable = aTable.concat(
'<tr>' +
'<th>Node</th>' +
'<th>RSSI</th>' +
'<th>Packets</th>' +
'<th>Miss</th>' +
'<th>Dup</th>' +
'<th>Last</th>' +
'</tr>');
for (var i = 0; i <= 255; i++) {
if (rfm69nodes[i]) {
aTable = aTable.concat('<tr>' +
'<td>' + rfm69nodes[i].senderId + '</td>' +
'<td>' + rfm69nodes[i].rssi + '</td>' +
'<td>' + rfm69nodes[i].rxMsgCnt + '</td>' +
'<td>' + rfm69nodes[i].rxMsgMiss + '</td>' +
'<td>' + rfm69nodes[i].rxMsgDup + '</td>' +
'<td>' + rfm69nodes[i].message + '</td>' +
'</tr>');
}
}
aTable = aTable.concat('</table>');
eStatus.innerHTML = aTable;
}
else {
}
}
};
}
</script>
</head>
<body onload="javascript:start();">
<h2>RFM69 Gateway</h2>
<div id="rfm69Config"></div>
<div id="rfm69Status">Waiting for node data</div>
<div id="configureGateway">
<p><a href="/configGW"><button type="button">Configure Gateway</button></a>
</div>
<div id="configureNodes">
<p><a href="/configGWnode"><button type="button">Configure Nodes</button></a>
</div>
<div id="FirmwareUpdate">
<p><a href="/updater"><button type="button">Update Gateway Firmware</button></a>
</div>
</body>
</html>
)rawliteral";
static const char PROGMEM CONFIGUREGW_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>RFM69 Gateway Configuration</title>
<style>
"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }"
</style>
</head>
<body>
<h2>RFM69 Gateway Configuration</h2>
<a href="/configGWrfm69"><button type="button">RFM69/GW</button></a>
<p>
<a href="/configGWmqtt"><button type="button">MQTT</button></a>
<p>
<a href="/"><button type="button">Home</button></a>
</body>
</html>
)rawliteral";
static const char PROGMEM CONFIGUREGWRFM69_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>RFM69 Gateway Configuration</title>
<style>
"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }"
</style>
</head>
<body>
<h3>RFM69 Gateway Configuration</h3>
<form method='POST' action='/configGWrfm69' enctype='multipart/form-data'>
<label>RFM69 Network ID</label>
<input type='number' name='networkid' value="%d" min="1" max="255" size="3"><br>
<label>RFM69 Node ID</label>
<input type='number' name='nodeid' value="%d" min="1" max="255" size="3"><br>
<label>RFM69 Encryption Key</label>
<input type='text' name='encryptkeyweb' value="%s" size="16" maxlength="16"><br>
<label>RFM69 Power Level</label>
<input type='number' name='powerlevel' value="%d" min="0" max="31"size="2"><br>
<label>RFM69 Frequency</label>
<select name="rfmfrequency">
<option value="31" %s>315 MHz</option>
<option value="43" %s>433 MHz</option>
<option value="86" %s>868 MHz</option>
<option value="91" %s>915 MHz</option>
</select><br>
<label for=hcw>RFM69 HCW</label><br>
<input type='radio' name='rfm69hcw' id="hcw" value="1" %s> True<br>
<input type='radio' name='rfm69hcw' id="hcw" value="0" %s> False<br>
<label>RFM69 AP name</label>
<input type='text' name='rfmapname' value="%s" size="32" maxlength="32"><br>
<label>Updater password</label><br>
<label>old user</label>
<input type='text' name='olduser' value="%s" size="20" maxlength="20"><br>
<label>old password</label>
<input type='text' name='oldpassword' value="%s" size="20" maxlength="20"><br>
<label>new user</label>
<input type='text' name='newuser' size="20" maxlength="20"><br>
<label>new password</label>
<input type='text' name='newpassword' size="20" maxlength="20"><br>
<p><input type='submit' value='Save changes'>
</form>
<p><a href="/configGW"><button type="button">Cancel</button></a><a href="/configGWreset"><button type="button">Factory Reset</button></a>
</body>
</html>
)rawliteral";
static const char PROGMEM CONFIGUREGWMQTT_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>RFM69 Gateway MQTT Configuration</title>
<style>
"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }"
</style>
</head>
<body>
<h3>RFM69 Gateway MQTT Configuration</h3>
<form method='POST' action='/configGWmqtt' enctype='multipart/form-data'>
<label>MQTT broker</label>
<input type='text' name='mqttbroker' value="%s" size="32" maxlength="32"><br>
<label>MQTT client name</label>
<input type='text' name='mqttclientname' value="%s" size="32" maxlength="32"><br>
<label>MQTT client user</label>
<input type='text' name='mqttclientuser' value="%s" size="32" maxlength="20"><br>
<label>MQTT client password</label>
<input type='text' name='mqttclientpassword' value="%s" size="32" maxlength="20"><br>
<label>MDNS name</label>
<input type='text' name='mdnsname' value="%s" size="32" maxlength="32"><br>
<label>note: if you have seen %s and changed MQTT broker then set RFM69 encrypt key again!</label>
<p><input type='submit' value='Save changes'>
</form>
<p><a href="/configGW"><button type="button">Cancel</button></a>
</body>
</html>
)rawliteral";
static const char PROGMEM SELECTNODE[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0'>
<title>RFM69 Node Configuration</title>
<style>
'body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }'
</style>
</head>
<body>
<h3>RFM69 Node Configuration</h3>
<p><a href="/"><button type="button">Home</button></a>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<label>nodeId  </label>
<input type='number' name='nodeid' min='1' max='254' size='3'>
       
request Data from Node:
<input type='checkbox' name='reqfromNode' value='1'>
       
<input type='submit' value='set Node Id to edit'><br>
</form>
</body>
</html>
)rawliteral";
static const char PROGMEM CONFIGURENODE[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0'>
<title>RFM69 Node Configuration</title>
<style>
'body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }'
</style>
</head>
<body>
<h3>RFM69 Node Configuration</h3>
<p><a href="/"><button type="button">Home</button></a>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<label>nodeId  </label>
<input type='number' name='nodeid' min='1' max='254' size='3'>
       
request Data from Node:
<input type='checkbox' name='reqfromNode' value='1'>
       
<input type='submit' value='set Node Id to edit'><br>
</form>
DIO:
<table>
<tbody>
<tr>
<td>     </td>
<td> out     </td>
<td> high     </td>
<td> intRise     </td>
<td> intFall     </td>
<td> wdDefault     </td>
<td> wdReq     </td>
<td> readInput     </td>
<td> count     </td>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> P1   </td>
<td> <input type='checkbox' name='w_0' value='1'> </td>
<td> <input type='checkbox' name='w_0' value='2'> </td>
<td> <input type='checkbox' name='w_0' value='4'> </td>
<td> <input type='checkbox' name='w_0' value='8'> </td>
<td> <input type='checkbox' name='w_0' value='16'> </td>
<td> <input type='checkbox' name='w_0' value='32'> </td>
<td> <input type='checkbox' name='w_0' value='64'> </td>
<td> <input type='checkbox' name='w_0' value='128'> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> P0   </td>
<td> <input type='checkbox' name='w_1' value='1'> </td>
<td> <input type='checkbox' name='w_1' value='2'> </td>
<td> <input type='checkbox' name='w_1' value='4'> </td>
<td> <input type='checkbox' name='w_1' value='8'> </td>
<td> <input type='checkbox' name='w_1' value='16'> </td>
<td> <input type='checkbox' name='w_1' value='32'> </td>
<td> <input type='checkbox' name='w_1' value='64'> </td>
<td> <input type='checkbox' name='w_1' value='128'> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> P19   </td>
<td> <input type='checkbox' name='w_2' value='1'> </td>
<td> <input type='checkbox' name='w_2' value='2'> </td>
<td> <input type='checkbox' name='w_2' value='4'> </td>
<td> <input type='checkbox' name='w_2' value='8'> </td>
<td> <input type='checkbox' name='w_2' value='16'> </td>
<td> <input type='checkbox' name='w_2' value='32'> </td>
<td> <input type='checkbox' name='w_2' value='64'> </td>
<td> <input type='checkbox' name='w_2' value='128'> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> P18   </td>
<td> <input type='checkbox' name='w_3' value='1'> </td>
<td> <input type='checkbox' name='w_3' value='2'> </td>
<td> <input type='checkbox' name='w_3' value='4'> </td>
<td> <input type='checkbox' name='w_3' value='8'> </td>
<td> <input type='checkbox' name='w_3' value='16'> </td>
<td> <input type='checkbox' name='w_3' value='32'> </td>
<td> <input type='checkbox' name='w_3' value='64'> </td>
<td> <input type='checkbox' name='w_3' value='128'> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> P9   </td>
<td> <input type='checkbox' name='w_4' value='1'> </td>
<td> <input type='checkbox' name='w_4' value='2'> </td>
<td> <input type='checkbox' name='w_4' value='4'> </td>
<td> <input type='checkbox' name='w_4' value='8'> </td>
<td> <input type='checkbox' name='w_4' value='16'> </td>
<td> <input type='checkbox' name='w_4' value='32'> </td>
<td> <input type='checkbox' name='w_4' value='64'> </td>
<td> <input type='checkbox' name='w_4' value='128'> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> P16   </td>
<td> <input type='checkbox' name='w_5' value='1'> </td>
<td> <input type='checkbox' name='w_5' value='2'> </td>
<td> <input type='checkbox' name='w_5' value='4'> </td>
<td> <input type='checkbox' name='w_5' value='8'> </td>
<td> <input type='checkbox' name='w_5' value='16'> </td>
<td> <input type='checkbox' name='w_5' value='32'> </td>
<td> <input type='checkbox' name='w_5' value='64'> </td>
<td> <input type='checkbox' name='w_5' value='128'> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> P17   </td>
<td> <input type='checkbox' name='w_6' value='1'> </td>
<td> <input type='checkbox' name='w_6' value='2'> </td>
<td> <input type='checkbox' name='w_6' value='4'> </td>
<td> <input type='checkbox' name='w_6' value='8'> </td>
<td> <input type='checkbox' name='w_6' value='16'> </td>
<td> <input type='checkbox' name='w_6' value='32'> </td>
<td> <input type='checkbox' name='w_6' value='64'> </td>
<td> <input type='checkbox' name='w_6' value='128'> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
</tbody>
</table>
Analog:
<table>
<tbody>
<tr>
<td>     </td>
<td> plant     </td>
<td> ldr     </td>
<td> rain     </td>
<td> raw     </td>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> A2   </td>
<td> <input type="checkbox" name="w_10" value="1"> </td>
<td> <input type="checkbox" name="w_10" value="2"> </td>
<td> <input type="checkbox" name="w_10" value="4"> </td>
<td> <input type="checkbox" name="w_10" value="8"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> A3   </td>
<td> <input type="checkbox" name="w_11" value="1"> </td>
<td> <input type="checkbox" name="w_11" value="2"> </td>
<td> <input type="checkbox" name="w_11" value="4"> </td>
<td> <input type="checkbox" name="w_11" value="8"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> A4   </td>
<td> <input type="checkbox" name="w_12" value="1"> </td>
<td> <input type="checkbox" name="w_12" value="2"> </td>
<td> <input type="checkbox" name="w_12" value="4"> </td>
<td> <input type="checkbox" name="w_12" value="8"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> A5   </td>
<td> <input type="checkbox" name="w_13" value="1"> </td>
<td> <input type="checkbox" name="w_13" value="2"> </td>
<td> <input type="checkbox" name="w_13" value="4"> </td>
<td> <input type="checkbox" name="w_13" value="8"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
</tbody>
</table>
digitalSensors:
<table>
<tbody>
<tr>
<td>     </td>
<td> DS18b20     </td>
<td> HC05     </td>
<td> BME/P280     </td>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td>     </td>
<td> <input type="checkbox" name="w_15" value="1"> </td>
<td> <input type="checkbox" name="w_15" value="2"> </td>
<td> <input type="checkbox" name="w_15" value="4"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
</tbody>
</table>
digitalOut:
<table>
<tbody>
<tr>
<td>     </td>
<td> free     </td>
<td> free     </td>
<td> ssd1306_64x48     </td>
<td> ssd1306_128x64     </td>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td>     </td>
<td> <input type="checkbox" name="w_16" value="1"> </td>
<td> <input type="checkbox" name="w_16" value="2"> </td>
<td> <input type="checkbox" name="w_16" value="4"> </td>
<td> <input type="checkbox" name="w_16" value="8"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
</tbody>
</table>
nodeControll:
<table>
<tbody>
<tr>
<td>     </td>
<td> sensorPower     </td>
<td> pumpSensorV     </td>
<td> sensorPowerSleep     </td>
<td> debugLed     </td>
<td> displayAlwaysOn     </td>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td>     </td>
<td> <input type="checkbox" name="w_20" value="1"> </td>
<td> <input type="checkbox" name="w_20" value="2"> </td>
<td> <input type="checkbox" name="w_20" value="4"> </td>
<td> <input type="checkbox" name="w_20" value="8"> </td>
<td> <input type="checkbox" name="w_20" value="16"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> sleepTimeMulti     </td>
<td> <input type="text" name="w_21" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> sleepTime     </td>
<td> <input type="text" name="w_22" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> watchdogTimeout     </td>
<td> <input type="text" name="w_23" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> wathdogDelay     </td>
<td> <input type="text" name="w_24" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> contrast     </td>
<td> <input type="text" name="w_26" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> nodeId     </td>
<td> <input type="text" name="w_27" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> networkId     </td>
<td> <input type="text" name="w_28" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> gatewayId     </td>
<td> <input type="text" name="w_29" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
<tr>
<form method='POST' action='/configGWnode' enctype='multipart/form-data'>
<td> sensorDelay     </td>
<td> <input type="text" name="w_30" id="value"> </td>
<td> <input type='submit' value='save'></td>
</form>
</tr>
</tbody>
</table>
<p><a href="/"><button type="button">Home</button></a>
</body>
</html>
)rawliteral";
#include <WebSocketsServer.h> //https://github.com/Links2004/arduinoWebSockets
#include <Hash.h>
ESP8266WebServer webServer(80);
WebSocketsServer webSocket = WebSocketsServer(81);
void webSocketEvent(uint8_t num, int type, uint8_t * payload, size_t length)
{
Serial.printf("webSocketEvent(%d, %d, ...)\r\n", num, type);
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// Send the RFM69 radio configuration one time after connection
webSocket.sendTXT(num, RadioConfig, strlen(RadioConfig));
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\r\n", num, payload);
// send data to all connected clients
//webSocket.broadcastTXT(payload, length);
break;
case WStype_BIN:
Serial.printf("[%u] get binary length: %u\r\n", num, length);
//hexdump(payload, length);
// echo data back to browser
//webSocket.sendBIN(num, payload, length);
break;
default:
Serial.printf("Invalid WStype [%d]\r\n", type);
break;
}
}
void handleRoot()
{
Serial.print("Free heap="); Serial.println(ESP.getFreeHeap());
webServer.send_P(200, "text/html", INDEX_HTML);
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += webServer.uri();
message += "\nMethod: ";
message += (webServer.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += webServer.args();
message += "\n";
for (uint8_t i=0; i<webServer.args(); i++){
message += " " + webServer.argName(i) + ": " + webServer.arg(i) + "\n";
}
webServer.send(404, "text/plain", message);
}
void handleconfiguregw()
{
webServer.send_P(200, "text/html", CONFIGUREGW_HTML);
}
// Reset global config back to factory defaults
void handleconfiguregwreset()
{
pGC->checksum++;
EEPROM.commit();
ESP.reset();
delay(1000);
}
#define SELECTED_FREQ(f) ((pGC->rfmfrequency==f)?"selected":"")
void handleconfiguregwrfm69()
{
size_t formFinal_len = strlen_P(CONFIGUREGWRFM69_HTML) + sizeof(*pGC);
char *formFinal = (char *)malloc(formFinal_len);
if (formFinal == NULL) {
Serial.println("formFinal malloc failed");
return;
}
#if showKeysInWeb == true
snprintf_P(formFinal, formFinal_len, CONFIGUREGWRFM69_HTML,
pGC->networkid, pGC->nodeid, pGC->encryptkey, GC_POWER_LEVEL,
SELECTED_FREQ(RF69_315MHZ), SELECTED_FREQ(RF69_433MHZ),
SELECTED_FREQ(RF69_868MHZ), SELECTED_FREQ(RF69_915MHZ),
(GC_IS_RFM69HCW)?"checked":"", (GC_IS_RFM69HCW)?"":"checked",
pGC->rfmapname, pGC->updateUser, pGC->updatePassword
);
#else
snprintf_P(formFinal, formFinal_len, CONFIGUREGWRFM69_HTML,
pGC->networkid, pGC->nodeid, HiddenString, GC_POWER_LEVEL,
SELECTED_FREQ(RF69_315MHZ), SELECTED_FREQ(RF69_433MHZ),
SELECTED_FREQ(RF69_868MHZ), SELECTED_FREQ(RF69_915MHZ),
(GC_IS_RFM69HCW)?"checked":"", (GC_IS_RFM69HCW)?"":"checked",
pGC->rfmapname, HiddenString, HiddenString
);
#endif
webServer.send(200, "text/html", formFinal);
free(formFinal);
}
void handleconfiguregwrfm69Write()
{
bool commit_required = false;
bool oldPasswordSeen = false;
bool oldUserSeen = false;
String argi, argNamei;
for (uint8_t i=0; i<webServer.args(); i++) {
Serial.print(webServer.argName(i));
Serial.print('=');
Serial.println(webServer.arg(i));
argi = webServer.arg(i);
argNamei = webServer.argName(i);
if (argNamei == "networkid") {
uint8_t formnetworkid = argi.toInt();
if (formnetworkid != pGC->networkid) {
commit_required = true;
pGC->networkid = formnetworkid;
}
}
else if (argNamei == "nodeid") {
uint8_t formnodeid = argi.toInt();
if (formnodeid != pGC->nodeid) {
commit_required = true;
pGC->networkid = formnodeid;
}
}
else if (argNamei == "encryptkey") {
const char *enckey = argi.c_str();
if (strcmp(enckey, HiddenString) != 0){
if (strcmp(enckey, pGC->encryptkey) != 0) {
commit_required = true;
strcpy(pGC->encryptkey, enckey);
}
}
}
else if (argNamei == "rfmapname") {
const char *apname = argi.c_str();
if (strcmp(apname, pGC->rfmapname) != 0) {
commit_required = true;
strcpy(pGC->rfmapname, apname);
}
}
else if (argNamei == "powerlevel") {
uint8_t powlev = argi.toInt();
if (powlev != GC_POWER_LEVEL) {
commit_required = true;
pGC->powerlevel = (GC_IS_RFM69HCW << 7) | powlev;
}
}
else if (argNamei == "rfm69hcw") {
uint8_t hcw = argi.toInt();
if (hcw != GC_IS_RFM69HCW) {
commit_required = true;
pGC->powerlevel = (hcw << 7) | GC_POWER_LEVEL;
}
}
else if (argNamei == "rfmfrequency") {
uint8_t freq = argi.toInt();
if (freq != pGC->rfmfrequency) {
commit_required = true;
pGC->rfmfrequency = freq;
}
}
else if (argNamei == "olduser") {
const char *olduser = argi.c_str();
if (strcmp(olduser, HiddenString) != 0){
if (strcmp(olduser, pGC->updateUser) == 0) {
oldUserSeen = true;
}
}
}
else if (argNamei == "oldpassword") {
const char *oldpw = argi.c_str();
if (strcmp(oldpw, HiddenString) != 0){
if (strcmp(oldpw, pGC->updatePassword) == 0) {
oldPasswordSeen = true;
}
}
}
else if (argNamei == "newuser") {
const char *newusr = argi.c_str();
if ((strcmp(newusr, HiddenString) != 0) && oldPasswordSeen && oldUserSeen){
if (strcmp(newusr, pGC->updatePassword) != 0) {
commit_required = true;
strcpy(pGC->updateUser, newusr);
}
}
}
else if (argNamei == "newpassword") {
const char *newpw = argi.c_str();
if ((strcmp(newpw, HiddenString) != 0) && oldPasswordSeen && oldUserSeen){
if (strcmp(newpw, pGC->updatePassword) != 0) {
commit_required = true;
strcpy(pGC->updatePassword, newpw);
}
}
}
}
handleRoot();
if (commit_required) {
pGC->checksum = gc_checksum();
EEPROM.commit();
ESP.reset();
delay(1000);
}
}
void handleconfiguregwmqtt()
{
size_t formFinal_len = strlen_P(CONFIGUREGWMQTT_HTML) + sizeof(*pGC);
char *formFinal = (char *)malloc(formFinal_len);
if (formFinal == NULL) {}
#if showKeysInWeb == true
snprintf_P(formFinal, formFinal_len, CONFIGUREGWMQTT_HTML,
pGC->mqttbroker, pGC->mqttclientname, pGC->mqttUser, pGC->mqttPassword, pGC->mdnsname, HiddenString
);
#else
snprintf_P(formFinal, formFinal_len, CONFIGUREGWMQTT_HTML,
HiddenString, pGC->mqttclientname, HiddenString, HiddenString, pGC->mdnsname, HiddenString
);
#endif
webServer.send(200, "text/html", formFinal);
free(formFinal);
}
void handleconfiguregwmqttWrite()
{
bool commit_required = false;
String argi, argNamei;
for (uint8_t i=0; i<webServer.args(); i++) {
Serial.print(webServer.argName(i));
Serial.print('=');