-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebserver.ino
372 lines (340 loc) · 12.1 KB
/
webserver.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
/************************************************************************************
* All webserver related stuff
************************************************************************************/
#include <ESPAsyncWebServer.h>
// webserver on standard port 80. But can be any port.
AsyncWebServer server(80);
void notFound(AsyncWebServerRequest *request);
/************************************************************************************
* This is the code for the HTML page as a RAW literal. You can change this if you want
* Basically it is a template that contains variables which will be replaced by actual
* values when sent to the client.
*
* The variables in the templates look like %VARIABLE% and will be replaced with actual
* values in the function "processor"
************************************************************************************/
const char index_html[] PROGMEM = R"rawliteral(
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta charset="utf-8">
<style type="text/css">
.myTable { background-color:#FFFFE0;border-collapse:collapse; }
.myTable th { background-color:#3F4DE4;color:white;width:50%% }
.myTable {font-family:verdana;}
.myTable td, .myTable th { padding:5px;border:1px solid #000; }
.myBody{font-family:verdana;}
</style>
<title>TBTracker-RX</title>
<meta http-equiv="refresh" content="20">
</head>
<body class="myBody" >
<table class="myTable" border="0" width="100%%" height="50">
<tbody>
<tr>
<th align="center">%TBTRACKERRX%</th>
</tr>
</tbody>
</table>
<table class="myTable" border="0" width="100%%" height="50">
<tbody>
<tr>
<td>Current frequency:</td><td><B>%FREQUENCY%</B></td>
<td><form action="/get"><input type="text" name="frequency"><input type="submit" value="change"></form></td>
</tr>
<tr>
<td>LoRa Mode:</td><td>%LORAMODE%</td>
<td>
<form action="/get4">
<select id="modes" name="modes">
<option value="-1"> </option>
<option value="0">Mode 0</option>
<option value="1">Mode 1</option>
<option value="2">Mode 2</option>
<option value="3">Mode 3</option>
<option value="5">Mode 5</option>
<option value="99">LoRa APRS (experimental)</option>
</select>
<input type="submit" value="change">
</form>
</td>
</tr>
<tr>
<td>Your callsign:</td><td>%CALLSIGN%</td><td> </td>
</tr>
<tr>
<td>Your location:</td><td><a href="https:www.google.com/maps/place/%LOCATION%" target="_blank">Google Maps</a></td>
<td><form action="/get6"><input type="submit" value="(re)upload your position"></form></td>
</tr>
<tr>
<td>Upload to <a href="https:amateur.sondehub.org/" target="_blank">Sondehub:</a></td><td>%SONDEHUB%</td>
<td><form action="/get2"><input type="submit" value="toggle"></form></td>
</tr>
<tr>
<td>Test mode:</a></td><td>%DEVFLAG%</td>
<td><form action="/get3"><input type="submit" value="toggle"></form></td>
</tr>
<tr>
<td>OLED Mode:</td><td>%OLEDMODE%</td>
<td>
<form action="/get5">
<select id="oledmodes" name="oledmodes">
<option value="0"> </option>
<option value="0">Default mode</option>
<option value="1">Chase mode</option>
<option value="2">See it All mode</option>
</select>
<input type="submit" value="change">
</form>
</td>
</tr>
</tbody>
</table>
<table class="myTable" border="0" width="100%%" height="50">
<tbody>
<tr>
<th align="center">LATEST PACKET %TIMESINCE%</th>
</tr>
</tbody>
</table>
<table class="myTable" border="0" width="100%%" height="50">
<tbody>
<tr>
<td>Payload ID:</td><td><B>%PAYLOADID%</B> (SNR: %SNR% dB, RSSI: %RSSI% dBm, FREQ.ERR.: %FERROR% Hz)</td>
</tr>
<tr>
<td>Telemetry:</td><td style="white-space:nowrap">%TELEMETRY%</td>
</tr>
<tr>
<td>Location:</td><td><a href="https:www.google.com/maps/place/%PAYLOADLOCATION%" target="_blank">Google Maps</a></td>
</tr>
<tr>
<td>Altitude (m):</td><td><B>%ALTITUDE%</B></td>
</tr>
<tr>
<td>Distance (km):</td><td>%DISTANCE%</td>
</tr>
<tr>
<td>Bearing (degr.):</td><td>%BEARING% - travel <B>%COMPASS%</B> to chase</td>
</tr>
<tr>
<td>Upload result:</td><td>%UPLOADRESULT%</td>
</tr>
</tbody>
</table>
<table class="myTable" border="0" width="100%%" height="50">
<tbody>
<tr>
<th align="center">LAST PACKETS</th>
</tr>
</tbody>
</table>
<table class="myTable" border="0" width="100%%" height="50">
<tbody>
%LOGHISTORY%
</tbody>
</table>
<p>
(Page will autoload every 20 seconds. If not, press the button)
</p>
<input type="button" value="Refresh Page" onClick="location.href=location.href">
</body>
</html>)rawliteral";
/************************************************************************************
* The function that replaces the variables from the template with real values
************************************************************************************/
String processor(const String& var)
{
if (var == "TBTRACKERRX")
return "TBTRACKER-RX " + String(TBTRACKER_VERSION);
else if (var == "FREQUENCY")
return String(LoRaSettings.Frequency,3);
else if (var == "DEVFLAG")
if (devflag) return String("Yes");
else return("No");
else if (var == "CALLSIGN")
return String(CALLSIGN);
else if (var == "TELEMETRY")
return Telemetry.raw;
else if (var == "PAYLOADID")
return Telemetry.payload_callsign;
else if (var == "PAYLOADLOCATION")
return String(Telemetry.lat,5) + "," + String(Telemetry.lon,5);
else if (var == "ALTITUDE")
return String(Telemetry.alt,0);
else if (var == "DISTANCE")
return String(Telemetry.distance,2);
else if (var == "BEARING")
return String(Telemetry.bearing,0);
else if (var == "SNR")
return String(Telemetry.snr);
else if (var == "LOGHISTORY")
return getLogs();
else if (var == "RSSI")
return String(Telemetry.rssi);
else if (var == "FERROR")
return String(Telemetry.frequency_error);
else if (var == "LORAMODE")
return String(LoRaSettings.LoRaMode);
else if (var == "SCAN1")
return String(TBScanner.scanFreq[1],3);
else if (var == "SCAN2")
return String(TBScanner.scanFreq[2],3);
else if (var == "SCAN3")
return String(TBScanner.scanFreq[3],3);
else if (var == "SCAN4")
return String(TBScanner.scanFreq[4],3);
#if defined(USE_SSD1306)
else if (var == "OLEDMODE")
{
switch (oledMode)
{
case OLED_CHASE:
return String("Chase");
break;
case OLED_GOD:
return String("See it all");
break;
default:
return String("Default");
break;
}
}
#endif
else if (var == "TIMESINCE")
{
if (Telemetry.atmillis > 0)
return getDuration(Telemetry.atmillis,false);
else
return "";
}
else if (var == "UPLOADRESULT")
{
if (Telemetry.uploadSondehub)
return Telemetry.uploadResult;
}
else if (var == "COMPASS")
return String(Telemetry.compass);
else if (var == "LOCATION")
{
#if defined(USE_GPS)
processGPSData();
return String(Telemetry.uploader_position[0],5) + "," + String(Telemetry.uploader_position[1],5);
#else
return String(UPL_LAT) + "," + String(UPL_LON);
#endif
}
else if (var == "SONDEHUB")
if (Telemetry.uploadSondehub)
return String("Yes");
else
return String("No");
return String();
}
/************************************************************************************
* HTML template for when the user requests a non existing page
************************************************************************************/
void notFound(AsyncWebServerRequest *request)
{
request->send(404, "text/plain", "Not found");
}
/************************************************************************************
* Setting up the webserver with logic when a specific page is requested.
************************************************************************************/
void setupWebserver()
{
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{
request->send_P(200, "text/html", index_html, processor);
});
server.on("/get2", HTTP_GET, [](AsyncWebServerRequest *request)
{
if (Telemetry.uploadSondehub)
Telemetry.uploadSondehub = false;
else
Telemetry.uploadSondehub = true;
request->send(200, "text/html", "Upload to Sondehub was changed.<br><a href=\"/\">Return to Home Page</a>");
});
server.on("/get6", HTTP_GET, [](AsyncWebServerRequest *request)
{
postStationToServer();
request->send(200, "text/html", "Your position was uploaded to Sondehub.<br><a href=\"/\">Return to Home Page</a>");
});
server.on("/get3", HTTP_GET, [](AsyncWebServerRequest *request)
{
if (devflag)
devflag = false;
else
devflag = true;
request->send(200, "text/html", "Development flag was changed.<br><a href=\"/\">Return to Home Page</a>");
});
server.on("/getscan", HTTP_GET, [](AsyncWebServerRequest *request)
{
changeFrequency(request->getParam("scan1")->value(),1);
changeFrequency(request->getParam("scan2")->value(),2);
changeFrequency(request->getParam("scan3")->value(),3);
changeFrequency(request->getParam("scan4")->value(),4);
request->send(200, "text/html", "Scanning frequencies were changed.<br><a href=\"/\">Return to Home Page</a>");
});
server.on("/get4", HTTP_GET, [](AsyncWebServerRequest *request)
{
String lMode;
if (request->hasParam("modes"))
{
lMode = request->getParam("modes")->value();
if (changeLoRaMode(lMode.toInt()))
{
request->send(200, "text/html", "LoRa mode was changed.<br><a href=\"/\">Return to Home Page</a>");
}
else
{
request->send(200, "text/html", "LoRa mode was NOT changed.<br><a href=\"/\">Return to Home Page</a>");
}
}
request->send(200, "text/html", "LoRa mode was changed.<br><a href=\"/\">Return to Home Page</a>");
});
#if defined(USE_SSD1306)
server.on("/get5", HTTP_GET, [](AsyncWebServerRequest *request)
{
String oledMode;
if (request->hasParam("oledmodes"))
{
oledMode = request->getParam("oledmodes")->value();
if (changeOLEDMode(oledMode.toInt()))
{
request->send(200, "text/html", "OLED mode was changed.<br><a href=\"/\">Return to Home Page</a>");
}
else
{
request->send(200, "text/html", "OLED mode was NOT changed.<br><a href=\"/\">Return to Home Page</a>");
}
}
request->send(200, "text/html", "OLED mode was changed.<br><a href=\"/\">Return to Home Page</a>");
});
#endif
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request)
{
String inputMessage;
String inputParam;
// GET input1 value on <ESP_IP>/get?input1=<inputMessage>
if (request->hasParam("frequency"))
{
inputMessage = request->getParam("frequency")->value();
inputParam = "frequency";
}
Serial.print("Frequency changed to: ");
Serial.println(inputMessage);
// Try to change the frequency
if ( changeFrequency(inputMessage,0) )
{
request->send(200, "text/html", "Frequency changed to " + inputMessage + "<br><a href=\"/\">Return to Home Page</a>");
}
else
{
request->send(200, "text/html", "ERROR changing frequency to " + inputMessage + "<br><a href=\"/\">Return to Home Page</a>");
}
});
server.onNotFound(notFound);
server.begin();
}