-
Notifications
You must be signed in to change notification settings - Fork 32
/
SmartWebServer.ino
314 lines (258 loc) · 9.08 KB
/
SmartWebServer.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
/*
* Title OnStep Smart Web Server
* by Howard Dutton
*
* Copyright (C) 2016 to 2024 Howard Dutton
*
* 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/>.
*
* Revision History, see GitHub
*
* Author: Howard Dutton
* http://www.stellarjourney.com
*
* Description
*
* Web and IP Servers for OnStep and OnStepX
*
*/
#define Product "Smart Web Server"
#define FirmwareVersionMajor "2"
#define FirmwareVersionMinor "08"
#define FirmwareVersionPatch "l"
// Use Config.h to configure the SWS to your requirements
#include "src/Common.h"
NVS nv;
#include "src/lib/tasks/OnTask.h"
#include "src/libApp/cmd/Cmd.h"
#include "src/libApp/bleGamepad/BleGamepad.h"
#include "src/libApp/encoders/Encoders.h"
#include "src/pages/Pages.h"
#include "src/libApp/status/Status.h"
#include "src/lib/ethernet/cmdServer/CmdServer.h"
#include "src/lib/ethernet/webServer/WebServer.h"
#include "src/lib/wifi/cmdServer/CmdServer.h"
#include "src/lib/wifi/webServer/WebServer.h"
#if DEBUG == PROFILER
extern void profiler();
#endif
#if COMMAND_SERVER == PERSISTENT || COMMAND_SERVER == BOTH
CmdServer persistentCmdSvr1(9996, 10L*1000L, true);
#if OPERATIONAL_MODE != ETHERNET_W5100
CmdServer persistentCmdSvr2(9997, 10L*1000L, true);
#endif
CmdServer persistentCmdSvr3(9998, 10L*1000L, true);
#endif
#if COMMAND_SERVER == STANDARD || COMMAND_SERVER == BOTH
CmdServer cmdSvr(9999, 1L*1000L);
#endif
void systemServices() {
nv.poll();
}
void pollWebSvr() {
www.handleClient();
}
void pollCmdSvr() {
#if COMMAND_SERVER == PERSISTENT || COMMAND_SERVER == BOTH
persistentCmdSvr1.handleClient(); Y;
#if OPERATIONAL_MODE != ETHERNET_W5100
persistentCmdSvr2.handleClient(); Y;
#endif
persistentCmdSvr3.handleClient(); Y;
#endif
#if COMMAND_SERVER == STANDARD || COMMAND_SERVER == BOTH
cmdSvr.handleClient(); Y;
#endif
}
void setup(void) {
#if OPERATIONAL_MODE == WIFI
WiFi.disconnect();
WiFi.softAPdisconnect(true);
#endif
strcpy(firmwareVersion.str, FirmwareVersionMajor "." FirmwareVersionMinor FirmwareVersionPatch);
// start debug serial port
if (DEBUG == ON || DEBUG == VERBOSE || DEBUG == PROFILER) SERIAL_DEBUG.begin(SERIAL_DEBUG_BAUD);
delay(2000);
#ifdef PIN_INIT
PIN_INIT();
#endif
VF("MSG: SmartWebServer "); VL(firmwareVersion.str);
VF("MSG: MCU = "); VF(MCU_STR); V(", "); VF("Pinmap = "); VLF(PINMAP_STR);
delay(2000);
// call gamepad BLE initialization
#if (BLE_GAMEPAD == ON && ESP32)
VLF("MSG: Init BLE");
bleInit();
#endif
// call hardware specific initialization
VLF("MSG: Init HAL");
HAL_INIT();
HAL_NV_INIT();
#if LED_STATUS != OFF
pinMode(LED_STATUS_PIN, OUTPUT);
#endif
// attempt to connect to OnStep
int serialSwap = OFF;
serialSwap = SERIAL_SWAP;
if (serialSwap == AUTO) serialSwap = AUTO_OFF;
long serial_baud = SERIAL_BAUD;
onStep.serialBegin(SERIAL_BAUD_DEFAULT, serialSwap);
uint8_t tb = 1;
Again:
VLF("MSG: Clearing serial channel");
onStep.clearSerialChannel();
// look for OnStep
VLF("MSG: Attempting to contact OnStep");
SERIAL_ONSTEP.print(":GVP#"); delay(100);
String s = SERIAL_ONSTEP.readString();
if (s == "On-Step#") {
// if there is more than one baud rate specified
if (SERIAL_BAUD != SERIAL_BAUD_DEFAULT) {
// get fastest baud rate, Mega2560 returns '4' for 19200 baud recommended
SERIAL_ONSTEP.print(":GB#"); delay(100);
if (SERIAL_ONSTEP.available() != 1) { onStep.serialRecvFlush(); goto Again; }
if (SERIAL_ONSTEP.read() == '4' && serial_baud > 19200) serial_baud = 19200;
// set faster baud rate
SERIAL_ONSTEP.print(onStep.highSpeedCommsStr(serial_baud)); delay(100);
if (SERIAL_ONSTEP.available() != 1) { onStep.serialRecvFlush(); goto Again; }
if (SERIAL_ONSTEP.read() != '1') goto Again;
}
// we're all set, just change the baud rate to match OnStep
onStep.serialBegin(serial_baud, serialSwap);
#if DEBUG == REMOTE
debugRemoteConnected = true;
#endif
VLF("MSG: OnStep Connection established");
} else {
if (DEBUG == ON || DEBUG == VERBOSE) { VF("MSG: No valid reply found ("); V(s); VL(")"); }
#if LED_STATUS == ON
digitalWrite(LED_STATUS_PIN, LED_STATUS_OFF_STATE);
#endif
// got nothing back, toggle baud rate and/or swap ports
onStep.serialRecvFlush();
tb++;
if (tb == 16) { tb = 1; if (serialSwap == AUTO_OFF) serialSwap = AUTO_ON; else if (serialSwap == AUTO_ON) serialSwap = AUTO_OFF; }
if (tb == 1) onStep.serialBegin(SERIAL_BAUD_DEFAULT, serialSwap);
if (tb == 6) onStep.serialBegin(serial_baud, serialSwap);
if (tb == 11) { if (SERIAL_BAUD_DEFAULT == 9600) { onStep.serialBegin(19200, serialSwap); } else tb = 15; }
goto Again;
}
onStep.clearSerialChannel();
// System services
// add task for system services, runs at 10ms intervals so commiting 1KB of NV takes about 10 seconds
VF("MSG: Setup, starting system services");
VF(" task (rate 10ms priority 7)... ");
if (tasks.add(10, 0, true, 7, systemServices, "SysSvcs")) { VL("success"); } else { VL("FAILED!"); }
// get NV ready
if (!nv.isKeyValid(INIT_NV_KEY)) {
VF("MSG: NV, invalid key wipe "); V(nv.size); VLF(" bytes");
if (nv.verify()) { VLF("MSG: NV, ready for reset to defaults"); }
} else { VLF("MSG: NV, correct key found"); }
// get the command and web timeouts
if (!nv.hasValidKey()) {
nv.write(NV_TIMEOUT_CMD, (int16_t)cmdTimeout);
nv.write(NV_TIMEOUT_WEB, (int16_t)webTimeout);
}
cmdTimeout = nv.readUI(NV_TIMEOUT_CMD);
webTimeout = nv.readUI(NV_TIMEOUT_WEB);
// bring network servers up
#if OPERATIONAL_MODE == WIFI
VLF("MSG: Init WiFi");
wifiManager.init();
#else
VLF("MSG: Init Ethernet");
ethernetManager.init();
#endif
// ready encoders
VLF("MSG: Initialize Encoders");
encoders.init();
// init is done, write the NV key if necessary
if (!nv.hasValidKey()) {
nv.writeKey((uint32_t)INIT_NV_KEY);
nv.wait();
if (!nv.isKeyValid(INIT_NV_KEY)) { DLF("ERR: NV, failed to read back key!"); } else { VLF("MSG: NV, reset complete"); }
}
VLF("MSG: Set webpage handlers");
www.on("/index.htm", handleRoot);
www.on("/index-ajax-get.txt", indexAjaxGet);
www.on("/index.txt", indexAjax);
www.on("/mount.htm", handleMount);
www.on("/mount-ajax-get.txt", mountAjaxGet);
www.on("/mount-ajax.txt", mountAjax);
www.on("/libraryHelp.htm", handleLibraryHelp);
www.on("/rotator.htm", handleRotator);
www.on("/rotator-ajax-get.txt", rotatorAjaxGet);
www.on("/rotator-ajax.txt", rotatorAjax);
www.on("/focuser.htm", handleFocuser);
www.on("/focuser-ajax-get.txt", focuserAjaxGet);
www.on("/focuser-ajax.txt", focuserAjax);
www.on("/auxiliary.htm", handleAux);
www.on("/auxiliary-ajax-get.txt", auxAjaxGet);
www.on("/auxiliary-ajax.txt", auxAjax);
#if ENCODERS == ON
www.on("/enc.htm", handleEncoders);
www.on("/enc-ajax-get.txt", encAjaxGet);
www.on("/enc-ajax.txt", encAjax);
#endif
www.on("/net.htm", handleNetwork);
www.on("/", handleRoot);
www.onNotFound(handleNotFound);
#if COMMAND_SERVER == PERSISTENT || COMMAND_SERVER == BOTH
#if OPERATIONAL_MODE != ETHERNET_W5100
VLF("MSG: Starting port 9996 cmd server");
persistentCmdSvr3.begin();
VLF("MSG: Starting port 9997 cmd server");
persistentCmdSvr2.begin();
#endif
VLF("MSG: Starting port 9998 cmd server");
persistentCmdSvr1.begin();
#endif
#if COMMAND_SERVER == STANDARD || COMMAND_SERVER == BOTH
VLF("MSG: Starting port 9999 cmd server");
cmdSvr.begin();
#endif
VLF("MSG: Starting port 80 web server");
www.begin();
// allow time for the background servers to come up
delay(2000);
// clear the serial channel one last time
onStep.clearSerialChannel();
if (status.onStepFound) {
status.update();
delay(100);
}
state.init();
VF("MSG: Setup, starting cmd channel polling");
VF(" task (rate 5ms priority 2)... ");
if (tasks.add(5, 0, true, 2, pollCmdSvr, "cmdPoll")) { VL("success"); } else { VL("FAILED!"); }
VF("MSG: Setup, starting web server polling");
VF(" task (rate 20ms priority 3)... ");
if (tasks.add(20, 0, true, 3, pollWebSvr, "webPoll")) { VL("success"); } else { VL("FAILED!"); }
// start task manager debug events
#if DEBUG == PROFILER
tasks.add(142, 0, true, 7, profiler, "Profilr");
#endif
#if (BLE_GAMEPAD == ON && ESP32)
bleSetup();
#endif
VLF("MSG: SmartWebServer ready");
}
void loop(void) {
#if (BLE_GAMEPAD == ON && ESP32)
bleTimers(); Y;
bleConnTest(); Y;
#endif
tasks.yield();
}