-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathRX_FSK.ino
3237 lines (2945 loc) · 104 KB
/
RX_FSK.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
#include "features.h"
#include "version.h"
#include "core.h"
#define TAG "RX_FSK"
#include "src/logger.h"
#include <dirent.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <ESPAsyncWebServer.h>
#include <LittleFS.h>
#include <SPI.h>
#include <Update.h>
#include <ESPmDNS.h>
#include <Ticker.h>
#include "esp_heap_caps.h"
//#include <rtc_wdt.h>
//#include "soc/timer_group_struct.h"
//#include "soc/timer_group_reg.h"
#include "src/SX1278FSK.h"
#include "src/Sonde.h"
#include "src/Display.h"
#include "src/Scanner.h"
#if FEATURE_RS92
#include "src/geteph.h"
#include "src/rs92gps.h"
#endif
#include "src/ShFreqImport.h"
#include "src/RS41.h"
#include "src/DFM.h"
#include "src/json.h"
#include "src/posinfo.h"
#include "src/pmu.h"
#include "src/user.h"
/* Data exchange connectors */
#if FEATURE_CHASEMAPPER
#include "src/conn-chasemapper.h"
#endif
#if FEATURE_MQTT
#include "src/conn-mqtt.h"
#endif
#if FEATURE_SDCARD
#include "src/conn-sdcard.h"
#endif
#if FEATURE_APRS
#include "src/conn-aprs.h"
#endif
#if FEATURE_SONDEHUB
#include "src/conn-sondehub.h"
#endif
#include "src/conn-system.h"
Conn *connectors[] = { &connSystem,
&connGPS,
#if FEATURE_APRS
&connAPRS,
#endif
#if FEATURE_SONDEHUB
&connSondehub,
#endif
#if FEATURE_CHASEMAPPER
&connChasemapper,
#endif
#if FEATURE_MQTT
&connMQTT,
#endif
#if FEATURE_SDCARD
&connSDCard,
#endif
NULL };
//#define ESP_MEM_DEBUG 1
//int e;
enum MainState { ST_DECODER, ST_SPECTRUM, ST_WIFISCAN, ST_UPDATE, ST_TOUCHCALIB };
static MainState mainState = ST_WIFISCAN; // ST_WIFISCAN;
const char *mainStateStr[5] = {"DECODER", "SPECTRUM", "WIFISCAN", "UPDATE", "TOUCHCALIB" };
AsyncWebServer server(80);
PMU *pmu = NULL;
SemaphoreHandle_t axpSemaphore;
extern uint8_t pmu_irq;
const char *updateHost = "rdzsonde.mooo.com";
int updatePort = 80;
const char *updatePrefixM = "/main/";
const char *updatePrefixD = "/dev2/";
const char *updatePrefix = updatePrefixM;
const char *updateFs = "update.fs.bin";
const char *updateIno = "update.ino.bin";
#define LOCALUDPPORT 9002
//Get real UTC time from NTP server
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0; //UTC
const int daylightOffset_sec = 0; //UTC
boolean connected = false;
WiFiUDP udp;
WiFiClient client;
/* Sonde.h: enum SondeType { STYPE_DFM,, STYPE_RS41, STYPE_RS92, STYPE_M10M20, STYPE_M10, STYPE_M20, STYPE_MP3H }; */
const char *sondeTypeStrSH[NSondeTypes] = { "DFM", "RS41", "RS92", "Mxx"/*never sent*/, "M10", "M20", "MRZ" };
// moved to connSondehub.cpp
//#if FEATURE_SONDEHUB
//#define SONDEHUB_STATION_UPDATE_TIME (60*60*1000) // 60 min
//#define SONDEHUB_MOBILE_STATION_UPDATE_TIME (30*1000) // 30 sec
//WiFiClient shclient; // Sondehub v2
//int shImportInterval = 0;
//char shImport = 0;
//unsigned long time_last_update = 0;
//#endif
// JSON over TCP for communicating with the rdzSonde (rdzwx-go) Android app
WiFiServer rdzserver(14570);
WiFiClient rdzclient;
// If a file "localupd.txt" exists, firmware can be updated from a custom IP address read from this file, stored in localUpdates.
// By default (localUpdates==NULL) this is disabled to prevent abuse
// Note: by enabling this, someone with access to the web interface can replace the firmware arbitrarily!
// Make sure that only trustworthy persons have access to the web interface...
char *localUpdates = NULL;
boolean forceReloadScreenConfig = false;
enum KeyPress { KP_NONE = 0, KP_SHORT, KP_DOUBLE, KP_MID, KP_LONG };
// "doublepress" is now also used to eliminate key glitch on TTGO T-Beam startup (SENSOR_VN/GPIO39)
struct Button {
uint8_t pin;
uint32_t numberKeyPresses;
KeyPress pressed;
unsigned long keydownTime;
int8_t doublepress;
bool isTouched;
};
Button button1 = {0, 0, KP_NONE, 0, -1, false};
Button button2 = {0, 0, KP_NONE, 0, -1, false};
static int lastDisplay = 1;
static int currentDisplay = 1;
// timestamp when spectrum display was activated
static unsigned long specTimer;
void enterMode(int mode);
void WiFiEvent(WiFiEvent_t event);
// Possibly we will need more fine grained permissions in the future...
// For now, disallow arbitrary firmware updates on standard installations
// development installations can add a file "localupd.txt" which enables updates from arbitrary locations
int checkAllowed(const char *filename) {
if(!localUpdates && (strstr(filename, "localupd.txt") != NULL)) return 0;
return 1;
}
// Read line from file, independent of line termination (LF or CR LF)
String readLine(Stream &stream) {
String s = stream.readStringUntil('\n');
int len = s.length();
if (len == 0) return s;
if (s.charAt(len - 1) == '\r') s.remove(len - 1);
return s;
}
// Read line from file, without using dynamic memory allocation (String class)
// returns length line.
int readLine(Stream &stream, char *buffer, int maxlen) {
int n = stream.readBytesUntil('\n', buffer, maxlen);
buffer[n] = 0;
if (n <= 0) return 0;
if (buffer[n - 1] == '\r') {
buffer[n - 1] = 0;
n--;
}
return n;
}
// Replaces placeholder with LED state value
String processor(const String& var) {
LOG_D(TAG, "%s\n", var.c_str());
if (var == "MAPCENTER") {
#if 0
double lat, lon;
if (gpsPos.valid) {
lat = gpsPos.lat;
lon = gpsPos.lon;
}
else {
lat = sonde.config.rxlat;
lon = sonde.config.rxlon;
}
//if ( !isnan(lat) && !isnan(lon) ) {
#endif
if ( posInfo.valid ) {
char p[40];
snprintf(p, 40, "%g,%g", posInfo.lat, posInfo.lon);
return String(p);
} else {
return String("48,13");
}
}
if (var == "VERSION_NAME") {
return String(version_name);
}
if (var == "VERSION_ID") {
return String(version_id);
}
if (var == "FULLNAMEID") {
char tmp[128];
snprintf(tmp, 128, "%s-%c%d", version_id, FS_MAJOR + 'A' - 1, FS_MINOR);
return String(tmp);
}
if (var == "AUTODETECT_INFO") {
char tmpstr[128];
const char *fpstr;
int i = 0;
while (fingerprintValue[i] != sonde.fingerprint && fingerprintValue[i] != -1) i++;
if (fingerprintValue[i] == -1) {
fpstr = "Unknown board";
} else {
fpstr = fingerprintText[i];
}
snprintf(tmpstr, 128, "Fingerprint %d (%s)", sonde.fingerprint, fpstr);
return String(tmpstr);
}
if (var == "EPHSTATE") {
#if FEATURE_RS92
return String(ephtxt[ephstate]);
#else
return String("Not supported");
#endif
}
if (var == "LOCAL_UPDATES") {
if(localUpdates) return String(localUpdates);
else return String();
}
if (var == "PREAUTH") {
char preauth[COOKIE_SIZE];
generateRandomCookie("preauth",preauth);
storeCookie(preauth, -1); // preauth value
return String(preauth);
}
return String();
}
const String sondeTypeSelect(int activeType) {
String sts = "";
for (int i = 0; i < NSondeTypes; i++) {
sts += "<option value=\"";
sts += sondeTypeLongStr[i];
sts += "\"";
if (activeType == i) {
sts += " selected";
}
sts += ">";
sts += sondeTypeLongStr[i];
sts += "</option>";
}
return sts;
}
//trying to work around
//"assertion "heap != NULL && "free() target pointer is outside heap areas"" failed:"
// which happens if request->send is called in createQRGForm!?!??
char message[10240 * 3 - 2048]; //needs to be large enough for all forms (not checked in code)
// QRG form is currently about 24kb with 100 entries
///////////////////////// Functions for Reading / Writing QRG list from/to qrg.txt
void setupChannelList() {
File file = LittleFS.open("/qrg.txt", "r");
if (!file) {
LOG_E(TAG, "There was an error opening the file '/qrg.txt' for reading");
return;
}
int i = 0;
char launchsite[17] = " ";
sonde.clearSonde();
LOG_I(TAG, "Reading qrg.txt:");
while (file.available()) {
String line = readLine(file);
String sitename;
if (line[0] == '#') continue;
char *space = strchr(line.c_str(), ' ');
if (!space) continue;
*space = 0;
float freq = atof(line.c_str());
SondeType type;
if (space[1] == '4') {
type = STYPE_RS41;
} else if (space[1] == 'R') {
type = STYPE_RS92;
}
else if (space[1] == 'D' || space[1] == '9' || space[1] == '6') {
type = STYPE_DFM;
}
else if (space[1] == 'M') {
type = STYPE_M10M20;
}
else if (space[1] == '2') {
type = STYPE_M10M20;
}
else if (space[1] == '3') {
type = STYPE_MP3H;
}
else continue;
int active = space[3] == '+' ? 1 : 0;
if (space[4] == ' ') {
memset(launchsite, ' ', 16);
strncpy(launchsite, space + 5, 16);
if (sonde.config.debug == 1) {
LOG_D(TAG, "Add %f - sondetype: %d (on/off: %d) - site #%d - name: %s\n ", freq, type, active, i, launchsite);
}
}
sonde.addSonde(freq, type, active, launchsite);
i++;
}
file.close();
}
const char *HTMLHEAD = "<!DOCTYPE html><html><head> <meta charset=\"UTF-8\"> <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">";
void HTMLBODY_OS(char *ptr, const char *which, const char *onsubmit) {
strcat(ptr, "<body><form class=\"wrapper\" action=\"");
strcat(ptr, which);
if(onsubmit) {
strcat(ptr, "\" onsubmit=\"");
strcat(ptr, onsubmit);
}
strcat(ptr, "\" method=\"post\"><div class=\"content\">");
}
void HTMLBODY(char *ptr, const char *which) { HTMLBODY_OS(ptr, which, NULL); }
void HTMLBODYEND(char *ptr) {
strcat(ptr, "</div></form></body></html>");
}
void HTMLSAVEBUTTON(char *ptr) {
strcat(ptr, "</div><div class=\"footer\"><input type=\"submit\" class=\"save\" value=\"Save changes\"/>"
"<span class=\"ttgoinfo\">rdzTTGOserver ");
strcat(ptr, version_id);
strcat(ptr, "</span>");
}
const char *handleLoginPost(AsyncWebServerRequest * request) {
LOG_D(TAG, "Handling login POST request");
AsyncWebParameter *userp = request->getParam("user", true, false);
AsyncWebParameter *authp = request->getParam("auth", true, false);
AsyncWebParameter *preauthp= request->getParam("preauth", true, false);
if (!userp || !authp || !preauthp) {
request->send(400, "text/plain", "Invalid Request");
return nullptr;
}
String username = userp->value();
String preauth = preauthp->value();
String auth = authp->value();
if (isValidUser(username.c_str(), preauth.c_str(), auth.c_str())) {
// Generate a new session cookie
char cookie[COOKIE_SIZE];
generateRandomCookie(username.c_str(), cookie);
if(upgradeCookie(preauth.c_str(), cookie, 1)==0) {
// Set cookie and redirect
AsyncWebServerResponse *response = request->beginResponse(302);
response->addHeader("Location","/index.html");
response->addHeader("Set-Cookie", "SESSION=" + String(cookie) + "; Path=/; SameSite=Strict");
request->send(response);
return nullptr;
}
}
request->send(401, "text/plain", "Invalid credentials or session expired");
return nullptr;
}
const char *createQRGForm() {
char *ptr = message;
strcpy(ptr, HTMLHEAD);
strcat(ptr, "<script src=\"rdz.js\"></script></head>");
HTMLBODY(ptr, "qrg.html");
//strcat(ptr, "<body><form class=\"wrapper\" action=\"qrg.html\" method=\"post\"><div class=\"content\"><table><tr><th>ID</th><th>Active</th><th>Freq</th><th>Launchsite</th><th>Mode</th></tr>");
strcat(ptr, "<script>\nvar qrgs = [];\n");
for (int i = 0; i < sonde.config.maxsonde; i++) {
SondeInfo *si = &sonde.sondeList[i];
sprintf(ptr + strlen(ptr), "qrgs.push([%d, \"%.3f\", \"%s\", \"%c\"]);\n", si->active, si->freq, si->launchsite, sondeTypeChar[si->type] );
}
strcat(ptr, "</script>\n");
strcat(ptr, "<div id=\"divTable\"></div>");
strcat(ptr, "<script> qrgTable() </script>\n");
//</div><div class=\"footer\"><input type=\"submit\" class=\"update\" value=\"Update\"/>");
HTMLSAVEBUTTON(ptr);
HTMLBODYEND(ptr);
LOG_D(TAG, "QRG form: size=%d bytes\n", strlen(message));
return message;
}
const char *handleQRGPost(AsyncWebServerRequest * request) {
char label[10];
// parameters: a_i, f_1, t_i (active/frequency/type)
File file = LittleFS.open("/qrg.txt", "w");
if (!file) {
LOG_E(TAG, "Error while opening '/qrg.txt' for writing");
return "Error while opening '/qrg.txt' for writing";
}
Serial.println("Handling post request");
#if 0
int params = request->params();
for (int i = 0; i < params; i++) {
String pname = request->getParam(i)->name();
Serial.println(pname.c_str());
}
#endif
for (int i = 1; i <= sonde.config.maxsonde; i++) {
snprintf(label, 10, "A%d", i);
AsyncWebParameter *active = request->getParam(label, true);
snprintf(label, 10, "F%d", i);
AsyncWebParameter *freq = request->getParam(label, true);
snprintf(label, 10, "S%d", i);
AsyncWebParameter *launchsite = request->getParam(label, true);
if (!freq) continue;
snprintf(label, 10, "T%d", i);
AsyncWebParameter *type = request->getParam(label, true);
if (!type) continue;
String fstring = freq->value();
String tstring = type->value();
String sstring = launchsite->value();
const char *fstr = fstring.c_str();
const char *tstr = tstring.c_str();
const char *sstr = sstring.c_str();
if (*tstr == '6' || *tstr == '9') tstr = "D";
LOG_D(TAG, "Processing a=%s, f=%s, t=%s, site=%s\n", active ? "YES" : "NO", fstr, tstr, sstr);
char typech = tstr[0];
file.printf("%3.3f %c %c %s\n", atof(fstr), typech, active ? '+' : '-', sstr);
}
file.close();
LOG_D(TAG, "Channel setup finished\n");
setupChannelList();
return "";
}
/////////////////// Functions for reading/writing Wifi networks from networks.txt
#define MAX_WIFI 10
int nNetworks;
struct {
String id;
String pw;
} networks[MAX_WIFI];
// used by improv wifi
int updateWiFi(String ssid, String pw) {
networks[1].id = ssid;
networks[1].pw = pw;
if(nNetworks<2) nNetworks = 2;
File file = LittleFS.open("/networks.txt", "w");
if(!file) return -1;
for(int i=0; i<nNetworks; i++) {
if(networks[i].id && networks[i].pw) {
file.printf("%s\n%s\n", networks[i].id, networks[i].pw);
}
}
file.close();
return 0;
}
// FIXME: For now, we don't uspport wifi networks that contain newline or null characters
// ... would require a more sophisicated file format (currently one line SSID; one line Password
void setupWifiList() {
File file = LittleFS.open("/networks.txt", "r");
if (!file) {
LOG_E(TAG, "There was an error opening the file '/networks.txt' for reading");
networks[0].id = "RDZsonde";
networks[0].pw = "RDZsonde";
return;
}
int i = 0;
while (file.available()) {
String line = readLine(file); //file.readStringUntil('\n');
if (!file.available()) break;
networks[i].id = line;
networks[i].pw = readLine(file); // file.readStringUntil('\n');
i++;
}
nNetworks = i;
LOG_I(TAG, "%d networks in networks.txt\n", i);
// Serial.print(i); Serial.println(" networks in networks.txt\n");
for (int j = 0; j < i; j++) {
LOG_I(TAG, "%s: %s\n", networks[j].id, networks[j].pw);
}
}
// copy string, replacing '"' with '"'
// max string length is 31 characters
const String quoteString(const char *s) {
char buf[6*32];
uint16_t i = 0, o = 0;
int len = strlen(s);
if(len>31) len=31;
while(i<len) {
if(s[i]=='"') { strcpy(buf+o, """); o+=6; }
else buf[o++] = s[i];
i++;
}
buf[o] = 0;
return String(buf);
}
const char *createWIFIForm() {
char *ptr = message;
char tmp[4];
strcpy(ptr, HTMLHEAD);
strcat(ptr, "<script src=\"rdz.js\"></script></head>");
HTMLBODY(ptr, "wifi.html");
strcat(ptr, "<table><tr><th>Nr</th><th>SSID</th><th>Password</th></tr>");
for (int i = 0; i < MAX_WIFI; i++) {
String pw = i < nNetworks ? quoteString( networks[i].pw.c_str() ) : "";
sprintf(tmp, "%d", i);
sprintf(ptr + strlen(ptr), "<tr><td>%s</td><td><input name=\"S%d\" type=\"text\" value=\"%s\"/></td>"
"<td><input name=\"P%d\" type=\"text\" value=\"%s\"/></td>",
i == 0 ? "<b>AP</b>" : tmp,
i + 1, i < nNetworks ? networks[i].id.c_str() : "",
i + 1, pw.c_str() );
}
strcat(ptr, "</table><script>footer()</script>");
//</div><div class=\"footer\"><input type=\"submit\" class=\"update\" value=\"Update\"/>");
HTMLSAVEBUTTON(ptr);
HTMLBODYEND(ptr);
LOG_D(TAG, "WIFI form: size=%d bytes\n", strlen(message));
return message;
}
const char *handleWIFIPost(AsyncWebServerRequest * request) {
char label[10];
// parameters: a_i, f_1, t_i (active/frequency/type)
#if 1
File f = LittleFS.open("/networks.txt", "w");
if (!f) {
LOG_E(TAG, "Error while opening '/networks.txt' for writing");
return "Error while opening '/networks.txt' for writing";
}
#endif
LOG_D(TAG, "Handling post request");
#if 0
int params = request->params();
for (int i = 0; i < params; i++) {
String param = request->getParam(i)->name();
Serial.println(param.c_str());
}
#endif
for (int i = 1; i <= MAX_WIFI; i++) {
snprintf(label, 10, "S%d", i);
AsyncWebParameter *ssid = request->getParam(label, true);
if (!ssid) continue;
snprintf(label, 10, "P%d", i);
AsyncWebParameter *pw = request->getParam(label, true);
if (!pw) continue;
String sstring = ssid->value();
String pstring = pw->value();
const char *sstr = sstring.c_str();
const char *pstr = pstring.c_str();
if (strlen(sstr) == 0) continue;
LOG_D(TAG, "Processing S=%s, P=%s\n", sstr, pstr);
f.printf("%s\n%s\n", sstr, pstr);
}
f.close();
setupWifiList();
return "";
}
// Show current status
void addSondeStatus(char *ptr, int i)
{
struct tm ts;
SondeInfo *s = &sonde.sondeList[i];
strcat(ptr, "<table class=\"stat\">");
sprintf(ptr + strlen(ptr), "<tr><td id=\"sfreq\">%3.3f MHz, Type: %s</td><tr><td>ID: %s", s->freq, sondeTypeLongStr[sonde.realType(s)],
s->d.validID ? s->d.id : "<?""?>");
if (s->d.validID && (TYPE_IS_DFM(s->type) || TYPE_IS_METEO(s->type) || s->type == STYPE_MP3H) ) {
sprintf(ptr + strlen(ptr), " (ser: %s)", s->d.ser);
}
sprintf(ptr + strlen(ptr), "</td></tr><tr><td>QTH: %.6f,%.6f h=%.0fm</td></tr>\n", s->d.lat, s->d.lon, s->d.alt);
const time_t t = s->d.time;
ts = *gmtime(&t);
sprintf(ptr + strlen(ptr), "<tr><td>Frame# %u, Sats=%d, %04d-%02d-%02d %02d:%02d:%02d</td></tr>",
s->d.frame, s->d.sats, ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
if (s->type == STYPE_RS41) {
sprintf(ptr + strlen(ptr), "<tr><td>Burst-KT=%d Launch-KT=%d Countdown=%d (vor %ds)</td></tr>\n",
s->d.burstKT, s->d.launchKT, s->d.countKT, ((uint16_t)s->d.frame - s->d.crefKT));
}
sprintf(ptr + strlen(ptr), "<tr><td><a target=\"_empty\" href=\"geo:%.6f,%.6f\">GEO-App</a> - ", s->d.lat, s->d.lon);
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://radiosondy.info/sonde_archive.php?sondenumber=%s\">radiosondy.info</a> - ", s->d.id);
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://tracker.sondehub.org/%s\">SondeHub Tracker</a> - ", s->d.ser);
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://www.openstreetmap.org/?mlat=%.6f&mlon=%.6f&zoom=14\">OSM</a> - ", s->d.lat, s->d.lon);
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://www.google.com/maps/search/?api=1&query=%.6f,%.6f\">Google</a></td></tr>", s->d.lat, s->d.lon);
strcat(ptr, "</table>\n");
}
const char *createStatusForm() {
char *ptr = message;
strcpy(ptr, HTMLHEAD);
strcat(ptr, "<meta http-equiv=\"refresh\" content=\"5\"></head>");
HTMLBODY(ptr, "status.html");
strcat(ptr, "<div class=\"content\">");
for (int i = 0; i < sonde.config.maxsonde; i++) {
int snum = (i + sonde.currentSonde) % sonde.config.maxsonde;
if (sonde.sondeList[snum].active) {
addSondeStatus(ptr, snum);
}
}
strcat(ptr, "</div><div class=\"footer\"><span></span>"
"<span class=\"ttgoinfo\">rdzTTGOserver ");
strcat(ptr, version_id);
strcat(ptr, "</span>");
HTMLBODYEND(ptr);
LOG_D(TAG, "Status form: size=%d bytes\n", strlen(message));
return message;
}
const char *createLiveJson() {
char *ptr = message;
SondeInfo *s = &sonde.sondeList[sonde.currentSonde];
strcpy(ptr, "{\"sonde\": {");
// use the same JSON format here as for MQTT and for the Android App
sonde2json( ptr + strlen(ptr), 1024, s );
#if 0
sprintf(ptr + strlen(ptr), "\"sonde\": {\"rssi\": %d, \"vframe\": %d, \"time\": %d,\"id\": \"%s\", \"freq\": %3.3f, \"type\": \"%s\"",
s->rssi, s->d.vframe, s->d.time, s->d.id, s->freq, sondeTypeStr[sonde.realType(s)]);
if ( !isnan(s->d.lat) && !isnan(s->d.lon) )
sprintf(ptr + strlen(ptr), ", \"lat\": %.6f, \"lon\": %.6f", s->d.lat, s->d.lon);
if ( !isnan(s->d.alt) )
sprintf(ptr + strlen(ptr), ", \"alt\": %.0f", s->d.alt);
if ( !isnan(s->d.dir) )
sprintf(ptr + strlen(ptr), ", \"dir\": %.0f", s->d.dir);
if ( !isnan(s->d.vs) )
sprintf(ptr + strlen(ptr), ", \"climb\": %.1f", s->d.vs);
if ( !isnan(s->d.hs) )
sprintf(ptr + strlen(ptr), ", \"speed\": %.1f", s->d.hs);
sprintf(ptr + strlen(ptr), ", \"launchsite\": \"%s\", \"res\": %d }", s->launchsite, s->rxStat[0]);
#endif
strcat(ptr, " }");
if (posInfo.valid) {
sprintf(ptr + strlen(ptr), ", \"gps\": {\"lat\": %g, \"lon\": %g, \"alt\": %d, \"sat\": %d, \"speed\": %g, \"dir\": %d, \"hdop\": %d }", posInfo.lat, posInfo.lon, posInfo.alt, posInfo.sat, posInfo.speed, posInfo.course, posInfo.hdop);
//}
}
strcat(ptr, "}");
return message;
}
///////////////////// Config form
void setupConfigData() {
File file = LittleFS.open("/config.txt", "r");
if (!file) {
LOG_E(TAG, "There was an error opening the file '/config.txt' for reading");
return;
}
while (file.available()) {
String line = readLine(file); //file.readStringUntil('\n');
sonde.setConfig(line.c_str());
}
sonde.checkConfig(); // eliminate invalid entries
}
struct st_configitems config_list[] = {
/* General config settings */
{"wifi", 0, &sonde.config.wifi},
{"debug", 0, &sonde.config.debug},
{"maxsonde", 0, &sonde.config.maxsonde},
{"periodic_reboot", 0, &sonde.config.periodic_reboot},
{"rxlat", -7, &sonde.config.rxlat},
{"rxlon", -7, &sonde.config.rxlon},
{"rxalt", -7, &sonde.config.rxalt},
{"b2mute", 0, &sonde.config.b2mute},
{"screenfile", 0, &sonde.config.screenfile},
{"display", -6, sonde.config.display},
{"dispsaver", 0, &sonde.config.dispsaver},
{"dispcontrast", 0, &sonde.config.dispcontrast},
/* Spectrum display settings */
{"spectrum", 0, &sonde.config.spectrum},
{"startfreq", 0, &sonde.config.startfreq},
{"channelbw", 0, &sonde.config.channelbw},
{"marker", 0, &sonde.config.marker},
{"noisefloor", 0, &sonde.config.noisefloor},
/* decoder settings */
{"freqofs", 0, &sonde.config.freqofs},
{"rs41.agcbw", 0, &sonde.config.rs41.agcbw},
{"rs41.rxbw", 0, &sonde.config.rs41.rxbw},
{"rs92.rxbw", 0, &sonde.config.rs92.rxbw},
{"rs92.alt2d", 0, &sonde.config.rs92.alt2d},
{"dfm.agcbw", 0, &sonde.config.dfm.agcbw},
{"dfm.rxbw", 0, &sonde.config.dfm.rxbw},
{"m10m20.agcbw", 0, &sonde.config.m10m20.agcbw},
{"m10m20.rxbw", 0, &sonde.config.m10m20.rxbw},
{"mp3h.agcbw", 0, &sonde.config.mp3h.agcbw},
{"mp3h.rxbw", 0, &sonde.config.mp3h.rxbw},
{"ephftp", 79, &sonde.config.ephftp},
/* APRS settings */
{"call", 9, sonde.config.call},
{"passcode", 0, &sonde.config.passcode},
/* KISS tnc settings */
{"kisstnc.active", 0, &sonde.config.kisstnc.active},
#if FEATURE_APRS
/* AXUDP settings */
{"axudp.active", -3, &sonde.config.udpfeed.active},
{"axudp.host", 63, sonde.config.udpfeed.host},
{"axudp.ratelimit", 0, &sonde.config.udpfeed.ratelimit},
/* APRS TCP settings */
{"tcp.active", -3, &sonde.config.tcpfeed.active},
{"tcp.timeout", 0, &sonde.config.tcpfeed.timeout},
{"tcp.host", 63, sonde.config.tcpfeed.host},
{"tcp.host2", 63, &sonde.config.tcpfeed.host2},
{"tcp.chase", 0, &sonde.config.chase},
{"tcp.comment", 30, sonde.config.comment},
{"tcp.objcall", 9, sonde.config.objcall},
{"tcp.beaconsym", 4, sonde.config.beaconsym},
{"tcp.highrate", 0, &sonde.config.tcpfeed.highrate},
#endif
#if FEATURE_CHASEMAPPER
/* Chasemapper settings */
{"cm.active", -3, &sonde.config.cm.active},
{"cm.host", 63, &sonde.config.cm.host},
{"cm.port", 0, &sonde.config.cm.port},
#endif
#if FEATURE_MQTT
/* MQTT */
{"mqtt.active", 0, &sonde.config.mqtt.active},
{"mqtt.id", 63, &sonde.config.mqtt.id},
{"mqtt.host", 63, &sonde.config.mqtt.host},
{"mqtt.port", 0, &sonde.config.mqtt.port},
{"mqtt.username", 63, &sonde.config.mqtt.username},
{"mqtt.password", 63, &sonde.config.mqtt.password},
{"mqtt.prefix", 63, &sonde.config.mqtt.prefix},
{"mqtt.report_interval", 0, &sonde.config.mqtt.report_interval},
#endif
#if FEATURE_SDCARD
/* SD-Card settings */
{"sd.cs", 0, &sonde.config.sd.cs},
{"sd.miso", 0, &sonde.config.sd.miso},
{"sd.mosi", 0, &sonde.config.sd.mosi},
{"sd.clk", 0, &sonde.config.sd.clk},
{"sd.sync", 0, &sonde.config.sd.sync},
{"sd.name", 0, &sonde.config.sd.name},
#endif
/* Hardware dependeing settings */
{"disptype", 0, &sonde.config.disptype},
{"norx_timeout", 0, &sonde.config.norx_timeout},
{"oled_sda", 0, &sonde.config.oled_sda},
{"oled_scl", 0, &sonde.config.oled_scl},
{"oled_rst", 0, &sonde.config.oled_rst},
{"tft_rs", 0, &sonde.config.tft_rs},
{"tft_cs", 0, &sonde.config.tft_cs},
{"tft_orient", 0, &sonde.config.tft_orient},
{"tft_spifreq", 0, &sonde.config.tft_spifreq},
{"button_pin", -4, &sonde.config.button_pin},
{"button2_pin", -4, &sonde.config.button2_pin},
{"button2_axp", 0, &sonde.config.button2_axp},
{"touch_thresh", 0, &sonde.config.touch_thresh},
{"power_pout", 0, &sonde.config.power_pout},
{"led_pout", 0, &sonde.config.led_pout},
{"gps_rxd", 0, &sonde.config.gps_rxd},
{"gps_txd", 0, &sonde.config.gps_txd},
{"batt_adc", 0, &sonde.config.batt_adc},
#if 1
{"sx1278_ss", 0, &sonde.config.sx1278_ss},
{"sx1278_miso", 0, &sonde.config.sx1278_miso},
{"sx1278_mosi", 0, &sonde.config.sx1278_mosi},
{"sx1278_sck", 0, &sonde.config.sx1278_sck},
#endif
{"mdnsname", 14, &sonde.config.mdnsname},
#if FEATURE_SONDEHUB
/* SondeHub settings */
{"sondehub.active", 0, &sonde.config.sondehub.active},
{"sondehub.chase", 0, &sonde.config.sondehub.chase},
{"sondehub.host", 63, &sonde.config.sondehub.host},
{"sondehub.callsign", 63, &sonde.config.sondehub.callsign},
{"sondehub.antenna", 63, &sonde.config.sondehub.antenna},
{"sondehub.email", 63, &sonde.config.sondehub.email},
{"sondehub.fiactive", 0, &sonde.config.sondehub.fiactive},
{"sondehub.fiinterval", 0, &sonde.config.sondehub.fiinterval},
{"sondehub.fimaxdist", 0, &sonde.config.sondehub.fimaxdist},
{"sondehub.fimaxage", -7, &sonde.config.sondehub.fimaxage},
#endif
};
const int N_CONFIG = (sizeof(config_list) / sizeof(struct st_configitems));
const char *createConfigForm() {
char *ptr = message;
strcpy(ptr, HTMLHEAD);
strcat(ptr, "<script src=\"rdz.js\"></script></head>");
HTMLBODY_OS(ptr, "config.html", "return checkForDuplicates()");
strcat(ptr, "<div id=\"cfgtab\"></div>");
strcat(ptr, "<script src=\"cfg.js\"></script>");
strcat(ptr, "<script>\n");
sprintf(ptr + strlen(ptr), "var scr=\"Using /screens%d.txt", Display::getScreenIndex(sonde.config.screenfile));
for (int i = 0; i < disp.nLayouts; i++) {
sprintf(ptr + strlen(ptr), "<br>%d=%s", i, disp.layouts[i].label);
}
strcat(ptr, "\";\n");
strcat(ptr, "var cf=new Map();\n");
for (int i = 0; i < N_CONFIG; i++) {
sprintf(ptr + strlen(ptr), "cf.set(\"%s\", \"", config_list[i].name);
switch (config_list[i].type) {
case -4:
case -3:
case -2:
case 0:
sprintf(ptr + strlen(ptr), "%d", *(int *)config_list[i].data);
LOG_D(TAG, "Config for %s is %d\n", config_list[i].name, *(int *)config_list[i].data);
break;
case -6: // list
{
int8_t *l = (int8_t *)config_list[i].data;
if (*l == -1) strcat(ptr, "0");
else {
sprintf(ptr + strlen(ptr), "%d", l[0]);
l++;
}
while (*l != -1) {
sprintf(ptr + strlen(ptr), ",%d", *l);
l++;
}
}
break;
case -7: // double
if (!isnan(*(double *)config_list[i].data))
sprintf(ptr + strlen(ptr), "%g", *(double *)config_list[i].data);
break;
default: // string
strcat(ptr, (char *)config_list[i].data);
}
strcat(ptr, "\");\n");
}
strcat(ptr, "configTable();\n </script>");
strcat(ptr, "<script>footer()</script>");
HTMLSAVEBUTTON(ptr);
HTMLBODYEND(ptr);
LOG_D(TAG, "Config form: size=%d bytes\n", strlen(message));
return message;
}
const char *handleConfigPost(AsyncWebServerRequest * request) {
// parameters: a_i, f_1, t_i (active/frequency/type)
LOG_D(TAG, "Handling config post request");
#if 1
File f = LittleFS.open("/config.txt", "w");
if (!f) {
LOG_E(TAG, "Error while opening '/config.txt' for writing");
return "Error while opening '/config.txt' for writing";
}
#endif
LOG_D(TAG, "File open for writing.");
int params = request->params();
#if 0
for (int i = 0; i < params; i++) {
String param = request->getParam(i)->name();
Serial.println(param.c_str());
}
#endif
for (int i = 0; i < params; i++) {
String strlabel = request->getParam(i)->name();
const char *label = strlabel.c_str();
if (label[strlen(label) - 1] == '#') continue;
AsyncWebParameter *value = request->getParam(label, true);
if (!value) continue;
String strvalue = value->value();
if ( strcmp(label, "button_pin") == 0 ||
strcmp(label, "button2_pin") == 0) {
AsyncWebParameter *touch = request->getParam(strlabel + "#", true);
if (touch) {
int i = atoi(strvalue.c_str());
if (i != -1 && i != 255) i += 128;
strvalue = String(i);
}
}
LOG_D(TAG, "Processing %s=%s\n", label, strvalue.c_str());
//int wlen = f.printf("%s=%s\n", config_list[idx].name, strvalue.c_str());
int wlen = f.printf("%s=%s\n", label, strvalue.c_str());
LOG_D(TAG, "Written bytes: %d\n", wlen);
}
LOG_D(TAG, "Flushing file\n");
f.flush();
LOG_D(TAG, "Closing file\n");
f.close();
LOG_D(TAG, "Re-reading file file\n");
setupConfigData();
if (!gpsPos.valid) fixedToPosInfo();
// TODO: Check if this is better done elsewhere?
// Use new config (whereever this is feasible without a reboot)
disp.setContrast();
return "";
}
const char *ctrlid[] = {"rx", "scan", "spec", "wifi", "rx2", "scan2", "spec2", "wifi2", "reboot"};
const char *ctrllabel[] = {"Receiver/next freq. (short keypress)", "Scanner (double keypress)", "Spectrum (medium keypress)", "WiFi (long keypress)",
"Button 2/next screen (short keypress)", "Button 2 (double keypress)", "Button 2 (medium keypress)", "Button 2 (long keypress)",
"Reboot"
};
const char *createControlForm() {
char *ptr = message;
strcpy(ptr, HTMLHEAD);
strcat(ptr, "</head>");
HTMLBODY(ptr, "control.html");
for (int i = 0; i < 9; i++) {
strcat(ptr, "<input class=\"ctlbtn\" type=\"submit\" name=\"");
strcat(ptr, ctrlid[i]);
strcat(ptr, "\" value=\"");
strcat(ptr, ctrllabel[i]);
strcat(ptr, "\"></input>");
if (i == 3 || i == 7 ) {
strcat(ptr, "<p></p>");
}
}
strcat(ptr, "</div><div class=\"footer\"><span></span>"
"<span class=\"ttgoinfo\">rdzTTGOserver ");
strcat(ptr, version_id);
strcat(ptr, "</span>");
HTMLBODYEND(ptr);
LOG_I(TAG, "Control form: size=%d bytes\n", strlen(message));
return message;
}
const char *handleControlPost(AsyncWebServerRequest * request) {
LOG_D(TAG, "Handling control post request");
int params = request->params();
for (int i = 0; i < params; i++) {
String param = request->getParam(i)->name();
Serial.println(param.c_str());
if (param.equals("rx")) {
Serial.println("equals rx");
button1.pressed = KP_SHORT;
}
else if (param.equals("scan")) {
Serial.println("equals scan");
button1.pressed = KP_DOUBLE;
}
else if (param.equals("spec")) {
Serial.println("equals spec");
button1.pressed = KP_MID;
}
else if (param.equals("wifi")) {
Serial.println("equals wifi");
button1.pressed = KP_LONG;
}
else if (param.equals("rx2")) {
Serial.println("equals rx2");
button2.pressed = KP_SHORT;
}
else if (param.equals("scan2")) {
Serial.println("equals scan2");
button2.pressed = KP_DOUBLE;
}
else if (param.equals("spec2")) {
Serial.println("equals spec2");
button2.pressed = KP_MID;
}
else if (param.equals("wifi2")) {
Serial.println("equals wifi2");
button2.pressed = KP_LONG;
}
else if (param.equals("reboot")) {
Serial.println("equals reboot");