-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEByte-LoRa-Manager-esp8266-Gateway.ino
1029 lines (780 loc) · 33.9 KB
/
EByte-LoRa-Manager-esp8266-Gateway.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
/*
* LoRa EByte E32
* EByte LoRa E32 Manager for esp8266.
*
* Created by Renzo Mischianti <[email protected]>
* License: CC BY-NC-ND 3.0
*
* https://www.mischianti.org
*
* E32 ----- WeMos D1 mini
* M0 ----- D7 (or GND)
* M1 ----- D6 (or 3.3v)
* TX ----- D3 (PullUP)
* RX ----- D4 (PullUP)
* AUX ----- D5 (PullUP)
* VCC ----- 3.3v/5v
* GND ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
#include <ESP8266WiFi.h> // Include the Wi-Fi library
#include <WebSocketsServer.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include <ESP8266mDNS.h> // Include the mDNS library
#include "LittleFS.h"
#ifndef SERVER_MODE
#include <DNSServer.h>
DNSServer dnsServer;
const byte DNS_PORT = 53;
#endif
// Uncomment to enable printing out nice debug messages.
#define EBYTE_MANAGER_DEBUG
// Define where debug output will be printed.
#define DEBUG_PRINTER Serial
// Setup debug printing macros.
#ifdef EBYTE_MANAGER_DEBUG
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif
#define RESET_PIN D0
#define HTTP_REST_PORT 8080
ESP8266WebServer httpRestServer(HTTP_REST_PORT);
#define HTTP_PORT 80
ESP8266WebServer httpServer(HTTP_PORT);
#define WS_PORT 8081
#define WS_UPDATE_TIME 10000
WebSocketsServer webSocket = WebSocketsServer(WS_PORT);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
// ---------- esp8266 pins --------------
//LoRa_E22 e32ttl(RX, TX, AUX, M0, M1); // Arduino RX <-- e22 TX, Arduino TX --> e22 RX
LoRa_E32 e32ttl(D3, D4, D5, D7, D6); // Arduino RX <-- e22 TX, Arduino TX --> e22 RX AUX M0 M1
//LoRa_E22 e22ttl(D2, D3); // Config without connect AUX and M0 M1
//LoRa_E32 e32ttl(&Serial, D3, D0, D8); // RX, TX
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // Arduino RX <-- e22 TX, Arduino TX --> e22 RX
//LoRa_E22 e22ttl(&mySerial, D5, D7, D6); // AUX M0 M1
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E22 e22ttl(4, 5, 3, 7, 6); // Arduino RX <-- e22 TX, Arduino TX --> e22 RX AUX M0 M1
//LoRa_E22 e32ttl(4, 5); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(4, 5); // Arduino RX <-- e22 TX, Arduino TX --> e22 RX
//LoRa_E22 e22ttl(&mySerial, 3, 7, 6); // AUX M0 M1
// -------------------------------------
// ---------- esp32 pins --------------
//LoRa_E22 e32ttl(&Serial2, 18, 21, 19); // RX AUX M0 M1
//LoRa_E22 e32ttl(&Serial2, 22, 4, 18, 21, 19, UART_BPS_RATE_9600); // esp32 RX <-- e22 TX, esp32 TX --> e22 RX AUX M0 M1
// -------------------------------------
void sendCrossOriginHeader();
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
void sendTransparentMessage();
void sendFixedMessage();
void sendBroadcastMessage();
void resetModule();
void resetMicrocontroller();
void serverRouting();
void realtimeDataCallbak();
void sendWSMessageOfMessageReceived(bool readSingleMessage);
#define SERVER_MODE
const char* ssid = "<YOUR-SSID>"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "<YOUR-PASSWD>"; // The password of the Wi-Fi network
// Config for SoftAccessPoint
const char* hostname = "e32dev01";
#ifndef SERVER_MODE
IPAddress local_IP(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
#endif
bool readSingleMessage = true;
bool isConnectedWebSocket = false;
bool isConnectedWebSocketAck = false;
void setup() {
DEBUG_PRINTER.begin(9600);
delay(500);
#ifdef SERVER_MODE
WiFi.begin(ssid, password); // Connect to the network
DEBUG_PRINT(F("Connecting to "));
DEBUG_PRINT(ssid); DEBUG_PRINTLN(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
DEBUG_PRINT(++i); DEBUG_PRINT(' ');
}
DEBUG_PRINTLN('\n');
DEBUG_PRINTLN("Connection established!");
DEBUG_PRINT("IP address:\t");
DEBUG_PRINTLN(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
#else
DEBUG_PRINT("Setting soft-AP configuration ... ");
DEBUG_PRINTLN(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
DEBUG_PRINT("Setting soft-AP ... ");
bool sap = WiFi.softAP(hostname);
DEBUG_PRINTLN(sap ? "Ready" : "Failed!");
if(sap == true)
{
DEBUG_PRINT("Name: ");
DEBUG_PRINT(hostname);
DEBUG_PRINTLN(F(" Ready"));
DEBUG_PRINT("Soft-AP IP address = ");
DEBUG_PRINTLN(WiFi.softAPIP());
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
}
else
{
DEBUG_PRINTLN(F("Failed!"));
}
#endif
if (!MDNS.begin(hostname)) { // Start the mDNS responder for esp8266.local
DEBUG_PRINTLN(F("Error setting up mDNS responder!"));
}
DEBUG_PRINT(hostname);
DEBUG_PRINTLN(F(" --> mDNS responder started"));
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
// Startup all pins and UART
e32ttl.begin();
DEBUG_PRINTLN(F("Inizializing FS..."));
if (LittleFS.begin()){
DEBUG_PRINTLN(F("done."));
}else{
DEBUG_PRINTLN(F("fail."));
}
restServerRouting();
httpRestServer.begin();
DEBUG_PRINTLN(F("REST Server Started"));
serverRouting();
httpServer.begin();
DEBUG_PRINTLN(F("Web Server Started"));
webSocket.begin();
webSocket.onEvent(webSocketEvent);
DEBUG_PRINTLN(F("WS Server Started"));
#ifdef RESET_PIN
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, HIGH);
#endif
}
unsigned long lastRead = millis();
void loop() {
if (isConnectedWebSocket){
if (isConnectedWebSocketAck && e32ttl.available()>1){
sendWSMessageOfMessageReceived(readSingleMessage);
}
if (lastRead+WS_UPDATE_TIME<millis()){
realtimeDataCallbak();
lastRead = millis();
}
}
httpRestServer.handleClient();
httpServer.handleClient();
webSocket.loop();
#ifndef SERVER_MODE
dnsServer.processNextRequest();
#endif
}
void restServerRouting() {
// httpRestServer.header("Access-Control-Allow-Headers: Authorization, Content-Type");
//
httpRestServer.on(F("/"), HTTP_GET, []() {
DEBUG_PRINTLN(F("CALL /"));
httpRestServer.send(200, F("text/html"),
F("Welcome to the Inverter Centraline REST Web Server"));
});
httpRestServer.on(F("/configuration"), HTTP_GET, getConfiguration);
httpRestServer.on(F("/configuration"), HTTP_POST, postConfiguration);
httpRestServer.on(F("/configuration"), HTTP_OPTIONS, sendCrossOriginHeader);
httpRestServer.on(F("/reset"), HTTP_GET, resetMicrocontroller);
httpRestServer.on(F("/resetModule"), HTTP_GET, resetModule);
httpRestServer.on(F("/moduleInfo"), HTTP_GET, getModuleInfo);
httpRestServer.on(F("/transparentMessage"), HTTP_POST, sendTransparentMessage);
httpRestServer.on(F("/transparentMessage"), HTTP_OPTIONS, sendCrossOriginHeader);
httpRestServer.on(F("/fixedMessage"), HTTP_POST, sendFixedMessage);
httpRestServer.on(F("/fixedMessage"), HTTP_OPTIONS, sendCrossOriginHeader);
httpRestServer.on(F("/broadcastMessage"), HTTP_POST, sendBroadcastMessage);
httpRestServer.on(F("/broadcastMessage"), HTTP_OPTIONS, sendCrossOriginHeader);
}
void setCrossOrigin(){
httpRestServer.sendHeader(F("Access-Control-Allow-Origin"), F("*"));
httpRestServer.sendHeader(F("Access-Control-Max-Age"), F("600"));
httpRestServer.sendHeader(F("Access-Control-Allow-Methods"), F("PUT,POST,GET,OPTIONS"));
httpRestServer.sendHeader(F("Access-Control-Allow-Headers"), F("*"));
};
void sendCrossOriginHeader(){
DEBUG_PRINTLN(F("sendCORSHeader"));
httpRestServer.sendHeader(F("access-control-allow-credentials"), F("false"));
setCrossOrigin();
httpRestServer.send(204);
}
void getConfigurationJSON(Configuration configuration){
DynamicJsonDocument doc(2048);
JsonObject rootObj = doc.to<JsonObject>();
JsonObject config = rootObj.createNestedObject("configuration");
config[F("ADDH")] = configuration.ADDH;
config[F("ADDL")] = configuration.ADDL;
config[F("CHAN")] = configuration.CHAN;
JsonObject option = config.createNestedObject("OPTION");
option[F("fec")] = configuration.OPTION.fec;
option[F("fixedTransmission")] = configuration.OPTION.fixedTransmission;
option[F("ioDriveMode")] = configuration.OPTION.ioDriveMode;
option[F("transmissionPower")] = configuration.OPTION.transmissionPower;
option[F("wirelessWakeupTime")] = configuration.OPTION.wirelessWakeupTime;
JsonObject speed = config.createNestedObject("SPED");
speed[F("airDataRate")] = configuration.SPED.airDataRate;
speed[F("uartBaudRate")] = configuration.SPED.uartBaudRate;
speed[F("uartParity")] = configuration.SPED.uartParity;
DEBUG_PRINT(F("Stream file..."));
String buf;
serializeJson(rootObj, buf);
httpRestServer.send(200, F("application/json"), buf);
DEBUG_PRINTLN(F("done."));
}
void getConfiguration() {
DEBUG_PRINTLN(F("------------------------------ getConfiguration ------------------------------"));
setCrossOrigin();
ResponseStructContainer c;
c = e32ttl.getConfiguration();
// It's important get configuration pointer before all other operation
Configuration configuration = *(Configuration*) c.data;
DEBUG_PRINTLN(c.status.getResponseDescription());
DEBUG_PRINTLN(c.status.code);
printParameters(configuration);
getConfigurationJSON(configuration);
c.close();
}
void getModuleInfo() {
DEBUG_PRINTLN(F("------------------------------ getModuleInfo ------------------------------"));
setCrossOrigin();
ResponseStructContainer c;
c = e32ttl.getModuleInformation();
// It's important get configuration pointer before all other operation
ModuleInformation moduleInformation = *(ModuleInformation*) c.data;
DEBUG_PRINTLN(c.status.getResponseDescription());
DEBUG_PRINTLN(c.status.code);
printModuleInformation(moduleInformation);
DynamicJsonDocument doc(2048);
JsonObject rootObj = doc.to<JsonObject>();
JsonObject config = rootObj.createNestedObject("moduleInfo");
config[F("frequency")] = String(moduleInformation.frequency, HEX);
config[F("version")] = String(moduleInformation.version, HEX);
config[F("features")] = String(moduleInformation.features, HEX);
DEBUG_PRINT(F("Stream file..."));
String buf;
serializeJson(rootObj, buf);
httpRestServer.send(200, F("application/json"), buf);
DEBUG_PRINTLN(F("done."));
c.close();
}
void resetMicrocontroller() {
#ifdef RESET_PIN
digitalWrite(RESET_PIN, LOW);
#endif
}
void resetModule() {
DEBUG_PRINTLN(F("------------------------------ resetModule ------------------------------"));
setCrossOrigin();
ResponseStatus rs;
rs = e32ttl.resetModule();
// It's important get configuration pointer before all other operation
DEBUG_PRINTLN(rs.getResponseDescription());
DEBUG_PRINTLN(rs.code);
DEBUG_PRINT(F("==> reset END"));
DynamicJsonDocument doc(512);
if (rs.code != SUCCESS) {
DEBUG_PRINTLN(F("fail."));
httpRestServer.send(400, F("text/html"), String(rs.getResponseDescription()));
}else{
JsonObject rootObj = doc.to<JsonObject>();
JsonObject status = rootObj.createNestedObject("status");
status[F("code")] = String(rs.code);
status[F("error")] = rs.code!=1;
status[F("description")] = rs.getResponseDescription();
DEBUG_PRINT(F("Stream file..."));
String buf;
serializeJson(rootObj, buf);
if (rs.code!=1){
httpRestServer.send(500, F("application/json"), buf);
}else{
httpRestServer.send(200, F("application/json"), buf);
}
DEBUG_PRINTLN(F("done."));
}
}
void postConfiguration() {
DEBUG_PRINTLN(F("postConfigFile"));
setCrossOrigin();
String postBody = httpRestServer.arg("plain");
DEBUG_PRINTLN(postBody);
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, postBody);
if (error) {
// if the file didn't open, print an error:
DEBUG_PRINT(F("Error parsing JSON "));
DEBUG_PRINTLN(error.c_str());
String msg = error.c_str();
httpRestServer.send(400, F("text/html"), "Error in parsin json body! <br>"+msg);
}else{
JsonObject postObj = doc.as<JsonObject>();
DEBUG_PRINT(F("HTTP Method: "));
DEBUG_PRINTLN(httpRestServer.method());
if (httpRestServer.method() == HTTP_POST) {
if (postObj.containsKey("ADDH") && postObj.containsKey("ADDL") && postObj.containsKey("CHAN") && postObj.containsKey("OPTION")&& postObj.containsKey("SPED") ) {
DEBUG_PRINT(F("==> Set config file..."));
// JsonObject config = postObj[F("configuration")];
DEBUG_PRINT("ADDH");
DEBUG_PRINTLN((byte)postObj[F("ADDH")]);
Configuration configuration;
configuration.ADDH = (byte)postObj[F("ADDH")];
configuration.ADDL = (byte)postObj[F("ADDL")];
configuration.CHAN = (byte)postObj[F("CHAN")];
JsonObject option = postObj[F("OPTION")];
DEBUG_PRINT("fec");
DEBUG_PRINTLN( (FORWARD_ERROR_CORRECTION_SWITCH)(byte)option[F("fec")] );
// byte optionByte = 0b00000000;
// bitWrite(bitRead((byte)option[F("fec")], 0))
configuration.OPTION.fec = (FORWARD_ERROR_CORRECTION_SWITCH)(byte)option[F("fec")];
configuration.OPTION.fixedTransmission = (FIDEX_TRANSMISSION)(byte)option[F("fixedTransmission")];
configuration.OPTION.ioDriveMode = (IO_DRIVE_MODE)(byte)option[F("ioDriveMode")];
configuration.OPTION.transmissionPower = (TRANSMISSION_POWER)(byte)option[F("transmissionPower")];
configuration.OPTION.wirelessWakeupTime = (WIRELESS_WAKE_UP_TIME)(byte)option[F("wirelessWakeupTime")];
JsonObject speed = postObj[F("SPED")];
configuration.SPED.airDataRate = (AIR_DATA_RATE)(byte)speed[F("airDataRate")];
configuration.SPED.uartBaudRate = (UART_BPS_TYPE)(byte)speed[F("uartBaudRate")];
configuration.SPED.uartParity = (UART_PARITY)(byte)speed[F("uartParity")];
// configuration.ADDH = atoi(config[F("ADDH")]);
// configuration.ADDL = atoi(config[F("ADDL")]);
// configuration.CHAN = atoi(config[F("CHAN")]);
//
// JsonObject option = config[F("OPTION")];
//
// configuration.OPTION.fec = atoi(option[F("fec")]);
// configuration.OPTION.fixedTransmission = atoi(option[F("fixedTransmission")]);
// configuration.OPTION.ioDriveMode = atoi(option[F("ioDriveMode")]);
// configuration.OPTION.transmissionPower = atoi(option[F("transmissionPower")]);
// configuration.OPTION.wirelessWakeupTime = atoi(option[F("wirelessWakeupTime")]);
//
// JsonObject speed = config[F("SPED")];
//
// configuration.SPED.airDataRate = atoi(speed[F("airDataRate")]);
// configuration.SPED.uartBaudRate = atoi(speed[F("uartBaudRate")]);
// configuration.SPED.uartParity = atoi(speed[F("uartParity")]);
printParameters(configuration);
ResponseStatus rs = e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
DEBUG_PRINTLN(rs.getResponseDescription());
DEBUG_PRINTLN(rs.code);
printParameters(configuration);
DEBUG_PRINT(F("==> Set config file END"));
if (rs.code != SUCCESS) {
DEBUG_PRINTLN(F("fail."));
httpRestServer.send(400, F("text/html"), String(rs.getResponseDescription()));
}else{
// DEBUG_PRINT(F("Stream file..."));
// String buf;
// serializeJson(rootObj, buf);
// httpRestServer.send(200, F("application/json"), buf);
// DEBUG_PRINTLN(F("done."));
ResponseStructContainer c;
c = e32ttl.getConfiguration();
// It's important get configuration pointer before all other operation
Configuration configurationRetrieve = *(Configuration*) c.data;
DEBUG_PRINTLN(c.status.getResponseDescription());
DEBUG_PRINTLN(c.status.code);
printParameters(configuration);
if (
configuration.ADDH==configurationRetrieve.ADDH &&
configuration.ADDL==configurationRetrieve.ADDL &&
configuration.CHAN==configurationRetrieve.CHAN ){
getConfigurationJSON(configurationRetrieve);
delay(1000);
resetMicrocontroller();
}else{
httpRestServer.send(500, F("text/html"), F("Problem on store data on device, try to reset!"));
}
c.close();
}
}
else {
DEBUG_PRINTLN("Configuration not present")
httpRestServer.send(204, F("text/html"), F("No data found, or incorrect!"));
}
}
}
}
void sendTransparentMessage() {
DEBUG_PRINTLN(F("sendTransparentMessage"));
setCrossOrigin();
String postBody = httpRestServer.arg("plain");
DEBUG_PRINTLN(postBody);
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, postBody);
if (error) {
// if the file didn't open, print an error:
DEBUG_PRINT(F("Error parsing JSON "));
DEBUG_PRINTLN(error.c_str());
String msg = error.c_str();
httpRestServer.send(400, F("text/html"), "Error in parsin json body! <br>"+msg);
}else{
JsonObject postObj = doc.as<JsonObject>();
DEBUG_PRINT(F("HTTP Method: "));
DEBUG_PRINTLN(httpRestServer.method());
if (httpRestServer.method() == HTTP_POST) {
if (postObj.containsKey("message") ) {
DEBUG_PRINT(F("==> readMessage..."));
// JsonObject config = postObj[F("configuration")];
String message = postObj[F("message")];
ResponseStatus rs = e32ttl.sendMessage(message+'\0');
DEBUG_PRINTLN(rs.getResponseDescription());
DEBUG_PRINTLN(rs.code);
DEBUG_PRINT(F("==> Send message END"));
if (rs.code != SUCCESS) {
DEBUG_PRINTLN(F("fail."));
httpRestServer.send(400, F("text/html"), String(rs.getResponseDescription()));
}else{
JsonObject rootObj = doc.to<JsonObject>();
JsonObject status = rootObj.createNestedObject("status");
status[F("code")] = String(rs.code);
status[F("error")] = rs.code!=1;
status[F("description")] = rs.getResponseDescription();
DEBUG_PRINT(F("Stream file..."));
String buf;
serializeJson(rootObj, buf);
if (rs.code!=1){
httpRestServer.send(500, F("application/json"), buf);
}else{
httpRestServer.send(200, F("application/json"), buf);
}
DEBUG_PRINTLN(F("done."));
}
}
else {
DEBUG_PRINTLN("Configuration not present")
httpRestServer.send(204, F("text/html"), F("No data found, or incorrect!"));
}
}
}
}
void sendFixedMessage() {
DEBUG_PRINTLN(F("sendFixedMessage"));
setCrossOrigin();
String postBody = httpRestServer.arg("plain");
DEBUG_PRINTLN(postBody);
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, postBody);
if (error) {
// if the file didn't open, print an error:
DEBUG_PRINT(F("Error parsing JSON "));
DEBUG_PRINTLN(error.c_str());
String msg = error.c_str();
httpRestServer.send(400, F("text/html"), "Error in parsin json body! <br>"+msg);
}else{
JsonObject postObj = doc.as<JsonObject>();
DEBUG_PRINT(F("HTTP Method: "));
DEBUG_PRINTLN(httpRestServer.method());
if (httpRestServer.method() == HTTP_POST) {
if (postObj.containsKey("message") && postObj.containsKey("CHAN") && postObj.containsKey("ADDL") && postObj.containsKey("ADDH")) {
DEBUG_PRINT(F("==> readMessage..."));
// JsonObject config = postObj[F("configuration")];
String message = postObj[F("message")];
byte CHAN = (byte)postObj[F("CHAN")];
byte ADDL = (byte)postObj[F("ADDL")];
byte ADDH = (byte)postObj[F("ADDH")];
ResponseStatus rs = e32ttl.sendFixedMessage(ADDH, ADDL, CHAN, message+'\0');
DEBUG_PRINTLN(rs.getResponseDescription());
DEBUG_PRINTLN(rs.code);
DEBUG_PRINT(F("==> Send message END"));
if (rs.code != SUCCESS) {
DEBUG_PRINTLN(F("fail."));
httpRestServer.send(400, F("text/html"), String(rs.getResponseDescription()));
}else{
JsonObject rootObj = doc.to<JsonObject>();
JsonObject status = rootObj.createNestedObject("status");
status[F("code")] = String(rs.code);
status[F("error")] = rs.code!=1;
status[F("description")] = rs.getResponseDescription();
DEBUG_PRINT(F("Stream file..."));
String buf;
serializeJson(rootObj, buf);
if (rs.code!=1){
httpRestServer.send(500, F("application/json"), buf);
}else{
httpRestServer.send(200, F("application/json"), buf);
}
DEBUG_PRINTLN(F("done."));
}
}
else {
DEBUG_PRINTLN("Configuration not present")
httpRestServer.send(204, F("text/html"), F("No data found, or incorrect!"));
}
}
}
}
void sendBroadcastMessage() {
DEBUG_PRINTLN(F("sendBroadcastMessage"));
setCrossOrigin();
String postBody = httpRestServer.arg("plain");
DEBUG_PRINTLN(postBody);
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, postBody);
if (error) {
// if the file didn't open, print an error:
DEBUG_PRINT(F("Error parsing JSON "));
DEBUG_PRINTLN(error.c_str());
String msg = error.c_str();
httpRestServer.send(400, F("text/html"), "Error in parsin json body! <br>"+msg);
}else{
JsonObject postObj = doc.as<JsonObject>();
DEBUG_PRINT(F("HTTP Method: "));
DEBUG_PRINTLN(httpRestServer.method());
if (httpRestServer.method() == HTTP_POST) {
if (postObj.containsKey("message") && postObj.containsKey("CHAN")) {
DEBUG_PRINT(F("==> readMessage..."));
// JsonObject config = postObj[F("configuration")];
String message = postObj[F("message")];
byte CHAN = (byte)postObj[F("CHAN")];
ResponseStatus rs = e32ttl.sendBroadcastFixedMessage(CHAN, message+'\0');
DEBUG_PRINTLN(rs.getResponseDescription());
DEBUG_PRINTLN(rs.code);
DEBUG_PRINT(F("==> Send message END"));
if (rs.code != SUCCESS) {
DEBUG_PRINTLN(F("fail."));
httpRestServer.send(400, F("text/html"), String(rs.getResponseDescription()));
}else{
JsonObject rootObj = doc.to<JsonObject>();
JsonObject status = rootObj.createNestedObject("status");
status[F("code")] = String(rs.code);
status[F("error")] = rs.code!=1;
status[F("description")] = rs.getResponseDescription();
DEBUG_PRINT(F("Stream file..."));
String buf;
serializeJson(rootObj, buf);
if (rs.code!=1){
httpRestServer.send(500, F("application/json"), buf);
}else{
httpRestServer.send(200, F("application/json"), buf);
}
DEBUG_PRINTLN(F("done."));
}
}
else {
DEBUG_PRINTLN("Configuration not present")
httpRestServer.send(204, F("text/html"), F("No data found, or incorrect!"));
}
}
}
}
void printParameters(struct Configuration configuration) {
DEBUG_PRINTLN("----------------------------------------");
DEBUG_PRINT(F("HEAD BIN: ")); DEBUG_PRINT(configuration.HEAD, BIN);DEBUG_PRINT(" ");DEBUG_PRINT(configuration.HEAD, DEC);DEBUG_PRINT(" ");DEBUG_PRINTLN(configuration.HEAD, HEX);
DEBUG_PRINTLN(F(" "));
DEBUG_PRINT(F("AddH BIN: ")); DEBUG_PRINTLN(configuration.ADDH, DEC);
DEBUG_PRINT(F("AddL BIN: ")); DEBUG_PRINTLN(configuration.ADDL, DEC);
DEBUG_PRINT(F("Chan BIN: ")); DEBUG_PRINT(configuration.CHAN, DEC); DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.getChannelDescription());
DEBUG_PRINTLN(F(" "));
DEBUG_PRINT(F("SpeedParityBit BIN : ")); DEBUG_PRINT(configuration.SPED.uartParity, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.SPED.getUARTParityDescription());
DEBUG_PRINT(F("SpeedUARTDataRate BIN : ")); DEBUG_PRINT(configuration.SPED.uartBaudRate, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.SPED.getUARTBaudRate());
DEBUG_PRINT(F("SpeedAirDataRate BIN : ")); DEBUG_PRINT(configuration.SPED.airDataRate, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.SPED.getAirDataRate());
DEBUG_PRINT(F("OptionTrans BIN : ")); DEBUG_PRINT(configuration.OPTION.fixedTransmission, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.OPTION.getFixedTransmissionDescription());
DEBUG_PRINT(F("OptionPullup BIN : ")); DEBUG_PRINT(configuration.OPTION.ioDriveMode, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.OPTION.getIODroveModeDescription());
DEBUG_PRINT(F("OptionWakeup BIN : ")); DEBUG_PRINT(configuration.OPTION.wirelessWakeupTime, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.OPTION.getWirelessWakeUPTimeDescription());
DEBUG_PRINT(F("OptionFEC BIN : ")); DEBUG_PRINT(configuration.OPTION.fec, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.OPTION.getFECDescription());
DEBUG_PRINT(F("OptionPower BIN : ")); DEBUG_PRINT(configuration.OPTION.transmissionPower, BIN);DEBUG_PRINT(" -> "); DEBUG_PRINTLN(configuration.OPTION.getTransmissionPowerDescription());
DEBUG_PRINTLN("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
DEBUG_PRINTLN("----------------------------------------");
DEBUG_PRINT(F("HEAD BIN: ")); DEBUG_PRINT(moduleInformation.HEAD, BIN);DEBUG_PRINT(" ");DEBUG_PRINT(moduleInformation.HEAD, DEC);DEBUG_PRINT(" ");DEBUG_PRINTLN(moduleInformation.HEAD, HEX);
DEBUG_PRINT(F("Frequency.: ")); DEBUG_PRINTLN(moduleInformation.frequency, HEX);
DEBUG_PRINT(F("Version : ")); DEBUG_PRINTLN(moduleInformation.version, HEX);
DEBUG_PRINT(F("Features : ")); DEBUG_PRINTLN(moduleInformation.features, HEX);
DEBUG_PRINTLN("----------------------------------------");
}
void sendWSMessageOfMessageReceived(bool readSingleMessage){
DynamicJsonDocument docws(512);
JsonObject objws = docws.to<JsonObject>();
ResponseContainer rs;
if (readSingleMessage){
DEBUG_PRINTLN("READ MESSAGE UNTIL!");
rs = e32ttl.receiveMessageUntil();
}else{
DEBUG_PRINTLN("READ MESSAGE!");
rs = e32ttl.receiveMessage();
}
objws[F("type")] = "message";
objws[F("code")] = rs.status.code;
objws[F("description")] = rs.status.getResponseDescription();
DEBUG_PRINTLN(rs.status.getResponseDescription());
if (rs.status.code==1){
String message = rs.data;
objws[F("message")] = message;
DEBUG_PRINTLN(message);
objws[F("error")] = false;
}else{
objws[F("error")] = true;
}
String buf;
serializeJson(objws, buf);
webSocket.broadcastTXT(buf);
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
DynamicJsonDocument docwsT(512);
DeserializationError error;
switch(type) {
case WStype_DISCONNECTED:
webSocket.sendTXT(num, "{\"connection\": false}");
DEBUG_PRINT(F(" Disconnected "));
DEBUG_PRINTLN(num, DEC);
isConnectedWebSocket = false;
isConnectedWebSocketAck = false;
// DEBUG_PRINTF_AI(F("[%u] Disconnected!\n"), num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
// DEBUG_PRINTF_AI(F("[%u] Connected from %d.%d.%d.%d url: %s\n"), num, ip[0], ip[1], ip[2], ip[3], payload);
DEBUG_PRINT(num);
DEBUG_PRINT(F("Connected from: "));
DEBUG_PRINT(ip.toString());
DEBUG_PRINT(F(" "));
DEBUG_PRINTLN((char*)payload);
String msg = String("{\"type\":\"connection\", \"connection\": true, \"simpleMessage\": ")+String(readSingleMessage?"true":"false")+String("}");
// send message to client
webSocket.sendTXT(num, msg.c_str());
isConnectedWebSocket = true;
}
break;
case WStype_TEXT:
DEBUG_PRINT("NUM -> ");DEBUG_PRINT(num);
DEBUG_PRINT("payload -> ");DEBUG_PRINTLN((char*)payload);
error = deserializeJson(docwsT, (char*)payload);
if (error) {
// if the file didn't open, print an error:
DEBUG_PRINT(F("Error parsing JSON "));
webSocket.broadcastTXT("Error on WS");
}else{
JsonObject postObjws = docwsT.as<JsonObject>();
bool startReceiveDevMsg = postObjws[F("startReceiveDevMsg")];
if (startReceiveDevMsg==true){
// readSingleMessage = postObjws[F("singleMessage")];
isConnectedWebSocketAck = true;
DEBUG_PRINT(F("Start listening messages SM -> "));
DEBUG_PRINTLN(isConnectedWebSocketAck);
webSocket.broadcastTXT("{\"type\": \"device_msg\", \"receiving\": true}");
}else if (startReceiveDevMsg==false){
isConnectedWebSocketAck = false;
DEBUG_PRINT(F("Start listening messages SM -> "));
DEBUG_PRINTLN(isConnectedWebSocketAck);
webSocket.broadcastTXT("{\"type\": \"device_msg\", \"receiving\": false}");
}
bool singleMessage = postObjws[F("singleMessage")];
DEBUG_PRINT(F("Single message -> "));
DEBUG_PRINTLN(singleMessage);
if (singleMessage){
readSingleMessage = singleMessage;
}
}
// DEBUG_PRINTF_AI(F("[%u] get Text: %s\n"), num, payload);
// send message to client
// webSocket.sendTXT(num, "message here");
// send data to all connected clients
// webSocket.broadcastTXT("message here");
break;
case WStype_BIN:
//// DEBUG_PRINTF_AI(F("[%u] get binary length: %u\n"), num, length);
// hexdump(payload, length);
//
// // send message to client
// // webSocket.sendBIN(num, payload, length);
// break;
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
case WStype_PING:
case WStype_PONG:
// DEBUG_PRINTF_AI(F("[%u] get binary length: %u\n"), num, length);
DEBUG_PRINT(F("WS : "))
DEBUG_PRINT(type)
DEBUG_PRINT(F(" - "))
DEBUG_PRINTLN((char*)payload);
// send message to client
// webSocket.sendBIN(num, payload, length);
break;
}
}
String getContentType(String filename){
if(filename.endsWith(F(".htm"))) return F("text/html");
else if(filename.endsWith(F(".html"))) return F("text/html");
else if(filename.endsWith(F(".css"))) return F("text/css");
else if(filename.endsWith(F(".js"))) return F("application/javascript");
else if(filename.endsWith(F(".json"))) return F("application/json");
else if(filename.endsWith(F(".png"))) return F("image/png");
else if(filename.endsWith(F(".gif"))) return F("image/gif");
else if(filename.endsWith(F(".jpg"))) return F("image/jpeg");
else if(filename.endsWith(F(".ico"))) return F("image/x-icon");
else if(filename.endsWith(F(".xml"))) return F("text/xml");
else if(filename.endsWith(F(".pdf"))) return F("application/x-pdf");
else if(filename.endsWith(F(".zip"))) return F("application/x-zip");
else if(filename.endsWith(F(".gz"))) return F("application/x-gzip");
return F("text/plain");
}
bool handleFileRead(String path){ // send the right file to the client (if it exists)
DEBUG_PRINT(F("handleFileRead: "));
DEBUG_PRINTLN(path);
// Capite portal
if (
path.endsWith("/generate_204") || //Android captive portal. Maybe not needed. Might be handled by notFound handler.
path.endsWith("/fwlink") //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
){
path = "/";
}
if(path.endsWith("/")) path += F("index.html"); // If a folder is requested, send the index file
String contentType = getContentType(path); // Get the MIME type
String pathWithGz = path + F(".gz");
DEBUG_PRINT(F("Path exist : "));
DEBUG_PRINTLN(path);
if(LittleFS.exists(pathWithGz) || LittleFS.exists(path)){ // If the file exists, either as a compressed archive, or normal
if(LittleFS.exists(pathWithGz)) // If there's a compressed version available
path += F(".gz"); // Use the compressed version
fs::File file = LittleFS.open(path, "r"); // Open the file
size_t sent = httpServer.streamFile(file, contentType); // Send it to the client
file.close(); // Close the file again
DEBUG_PRINTLN(String(F("\tSent file: ")) + path + String(F(" of size ")) + sent);
return true;
}
DEBUG_PRINTLN(String(F("\tFile Not Found: ")) + path);
return false; // If the file doesn't exist, return false
}
void serverRouting() {
httpServer.onNotFound([]() { // If the client requests any URI
DEBUG_PRINTLN(F("On not found"));