-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOnStepESPServer.ino
More file actions
375 lines (314 loc) · 14.3 KB
/
OnStepESPServer.ino
File metadata and controls
375 lines (314 loc) · 14.3 KB
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
/*
* Title OnStepESPServer
* by Howard Dutton
*
* Copyright (C) 2017 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
* hjd1964@gmail.com
*
* Description
*
* ESP8266-01 OnStep control
*
*/
#define Product "OnEsp"
#define Version "1.0a 05 02 17"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include "Config.h"
#include <ESP8266WebServer.h>
#include <ESP8266WiFiAP.h>
#include <EEPROM.h>
#define Default_Password "password"
char masterPassword[40]=Default_Password;
bool accessPointEnabled=true;
bool stationEnabled=false;
bool stationDhcpEnabled=true;
char wifi_sta_ssid[40]="";
char wifi_sta_pwd[40]="";
IPAddress wifi_sta_ip = IPAddress(192,168,0,1);
IPAddress wifi_sta_gw = IPAddress(192,168,0,1);
IPAddress wifi_sta_sn = IPAddress(255,255,255,0);
char wifi_ap_ssid[40]="ONSTEP";
char wifi_ap_pwd[40]="password";
byte wifi_ap_ch=7;
IPAddress wifi_ap_ip = IPAddress(192,168,0,1);
IPAddress wifi_ap_gw = IPAddress(192,168,0,1);
IPAddress wifi_ap_sn = IPAddress(255,255,255,0);
// base timeouts
int WebTimeout=TIMEOUT_WEB; // default 15
int CmdTimeout=TIMEOUT_CMD; // default 30
ESP8266WebServer server(80);
WiFiServer cmdSvr(9999);
WiFiClient cmdSvrClient;
unsigned long clientTime = 0;
char writeBuffer[40]="";
int writeBufferPos=0;
enum Errors {ERR_NONE, ERR_MOTOR_FAULT, ERR_ALT, ERR_LIMIT_SENSE, ERR_DEC, ERR_AZM, ERR_UNDER_POLE, ERR_MERIDIAN, ERR_SYNC};
Errors lastError = ERR_NONE;
#define PierSideNone 0
#define PierSideEast 1
#define PierSideWest 2
#define PierSideBest 3
#define PierSideFlipWE1 10
#define PierSideFlipWE2 11
#define PierSideFlipWE3 12
#define PierSideFlipEW1 20
#define PierSideFlipEW2 21
#define PierSideFlipEW3 22
byte pierSide = PierSideNone;
int AlignMaxNumStars = -1;
const char* html_head1 = "<!DOCTYPE HTML>\r\n<html>\r\n<head>\r\n";
const char* html_headerPec = "<meta http-equiv=\"refresh\" content=\"5; URL=/pec.htm\">\r\n";
const char* html_headerIdx = "<meta http-equiv=\"refresh\" content=\"5; URL=/index.htm\">\r\n";
const char* html_head2 = "</head>\r\n<body bgcolor=\"#26262A\">\r\n";
const char* html_main_css1 = "<STYLE>";
const char* html_main_css2 = ".a { background-color: #111111; } .t { padding: 10px 10px 20px 10px; border: 5px solid #551111;";
const char* html_main_css3 = " margin: 25px 25px 0px 25px; color: #999999; background-color: #111111; } input { width:4em; font-weight: bold; background-color: #A01010; padding: 2px 2px; }";
const char* html_main_css4 = ".b { padding: 10px; border-left: 5px solid #551111; border-right: 5px solid #551111; border-bottom: 5px solid #551111; margin: 0px 25px 25px 25px; color: #999999;";
const char* html_main_css5 = "background-color: #111111; } select { width:4em; font-weight: bold; background-color: #A01010; padding: 2px 2px; } .c { color: #A01010; font-weight: bold; }";
const char* html_main_css6 = "h1 { text-align: right; } a:hover, a:active { background-color: red; } .g { color: #105010; font-weight: bold; }";
const char* html_main_css7 = "a:link, a:visited { background-color: #332222; color: #a07070; border:1px solid red; padding: 5px 10px;";
const char* html_main_css8 = " margin: none; text-align: center; text-decoration: none; display: inline-block; }";
const char* html_main_css9 = "button { background-color: #A01010; font-weight: bold; border-radius: 5px; font-size: 12px; margin: 2px; padding: 4px 8px; }</STYLE>";
const char* html_links1in = "<a href=\"/index.htm\" style=\"background-color: #552222;\">Status</a><a href=\"/control.htm\">Control</a>";
const char* html_links2in = "<a href=\"/guide.htm\">Guide</a><a href=\"/pec.htm\">PEC</a><a href=\"/settings.htm\">Settings</a>";
const char* html_links3in = "<a href=\"/wifi.htm\">WiFi</a><a href=\"/config.htm\">Config.h</a><br />";
const char* html_links1ct = "<a href=\"/index.htm\">Status</a><a href=\"/control.htm\" style=\"background-color: #552222;\">Control</a>";
const char* html_links2ct = "<a href=\"/guide.htm\">Guide</a><a href=\"/pec.htm\">PEC</a><a href=\"/settings.htm\">Settings</a>";
const char* html_links3ct = "<a href=\"/wifi.htm\">WiFi</a><a href=\"/config.htm\">Config.h</a><br />";
const char* html_links1gu = "<a href=\"/index.htm\">Status</a><a href=\"/control.htm\">Control</a>";
const char* html_links2gu = "<a href=\"/guide.htm\" style=\"background-color: #552222;\">Guide</a><a href=\"/pec.htm\">PEC</a><a href=\"/settings.htm\">Settings</a>";
const char* html_links3gu = "<a href=\"/wifi.htm\">WiFi</a><a href=\"/config.htm\">Config.h</a><br />";
const char* html_links1pe = "<a href=\"/index.htm\">Status</a><a href=\"/control.htm\">Control</a>";
const char* html_links2pe = "<a href=\"/guide.htm\">Guide</a><a href=\"/pec.htm\" style=\"background-color: #552222;\">PEC</a><a href=\"/settings.htm\">Settings</a>";
const char* html_links3pe = "<a href=\"/wifi.htm\">WiFi</a><a href=\"/config.htm\">Config.h</a><br />";
const char* html_links1se = "<a href=\"/index.htm\">Status</a><a href=\"/control.htm\">Control</a>";
const char* html_links2se = "<a href=\"/guide.htm\">Guide</a><a href=\"/pec.htm\">PEC</a><a href=\"/settings.htm\" style=\"background-color: #552222;\">Settings</a>";
const char* html_links3se = "<a href=\"/wifi.htm\">WiFi</a><a href=\"/config.htm\">Config.h</a><br />";
const char* html_links1es = "<a href=\"/index.htm\">Status</a><a href=\"/control.htm\">Control</a>";
const char* html_links2es = "<a href=\"/guide.htm\">Guide</a><a href=\"/pec.htm\">PEC</a><a href=\"/settings.htm\">Settings</a>";
const char* html_links3es = "<a href=\"/wifi.htm\" style=\"background-color: #552222;\">WiFi</a><a href=\"/config.htm\">Config.h</a><br />";
const char* html_links1co = "<a href=\"/index.htm\">Status</a><a href=\"/control.htm\">Control</a>";
const char* html_links2co = "<a href=\"/guide.htm\">Guide</a><a href=\"/pec.htm\">PEC</a><a href=\"/settings.htm\">Settings</a>";
const char* html_links3co = "<a href=\"/wifi.htm\">WiFi</a><a href=\"/config.htm\" style=\"background-color: #552222;\">Config.h</a><br />";
#define LED_PIN 2
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void){
delay(3000);
pinMode(LED_PIN,OUTPUT);
EEPROM.begin(1024);
Restart:
// EEPROM Init
if ((EEPROM_readInt(0)!=8266) || (EEPROM_readInt(2)!=0)) {
EEPROM_writeInt(0,8266);
EEPROM_writeInt(2,0);
EEPROM_writeInt(4,(int)accessPointEnabled);
EEPROM_writeInt(6,(int)stationEnabled);
EEPROM_writeInt(8,(int)stationDhcpEnabled);
EEPROM_writeInt(10,(int)WebTimeout);
EEPROM_writeInt(12,(int)CmdTimeout);
EEPROM_writeString(100,wifi_sta_ssid);
EEPROM_writeString(150,wifi_sta_pwd);
EEPROM_writeString(200,masterPassword);
EEPROM.write(20,wifi_sta_ip[0]); EEPROM.write(21,wifi_sta_ip[1]); EEPROM.write(22,wifi_sta_ip[2]); EEPROM.write(23,wifi_sta_ip[3]);
EEPROM.write(24,wifi_sta_gw[0]); EEPROM.write(25,wifi_sta_gw[1]); EEPROM.write(26,wifi_sta_gw[2]); EEPROM.write(27,wifi_sta_gw[3]);
EEPROM.write(28,wifi_sta_sn[0]); EEPROM.write(29,wifi_sta_sn[1]); EEPROM.write(30,wifi_sta_sn[2]); EEPROM.write(31,wifi_sta_sn[3]);
EEPROM_writeString(500,wifi_ap_ssid);
EEPROM_writeString(550,wifi_ap_pwd);
EEPROM_writeInt(50,(int)wifi_ap_ch);
EEPROM.write(60,wifi_ap_ip[0]); EEPROM.write(61,wifi_ap_ip[1]); EEPROM.write(62,wifi_ap_ip[2]); EEPROM.write(63,wifi_ap_ip[3]);
EEPROM.write(70,wifi_ap_gw[0]); EEPROM.write(71,wifi_ap_gw[1]); EEPROM.write(72,wifi_ap_gw[2]); EEPROM.write(73,wifi_ap_gw[3]);
EEPROM.write(80,wifi_ap_sn[0]); EEPROM.write(81,wifi_ap_sn[1]); EEPROM.write(82,wifi_ap_sn[2]); EEPROM.write(83,wifi_ap_sn[3]);
EEPROM.commit();
} else {
accessPointEnabled=EEPROM_readInt(4);
stationEnabled=EEPROM_readInt(6);
stationDhcpEnabled=EEPROM_readInt(8);
WebTimeout=EEPROM_readInt(10);
CmdTimeout=EEPROM_readInt(12);
EEPROM_readString(100,wifi_sta_ssid);
EEPROM_readString(150,wifi_sta_pwd);
EEPROM_readString(200,masterPassword);
wifi_sta_ip[0]=EEPROM.read(20); wifi_sta_ip[1]=EEPROM.read(21); wifi_sta_ip[2]=EEPROM.read(22); wifi_sta_ip[3]=EEPROM.read(23);
wifi_sta_gw[0]=EEPROM.read(24); wifi_sta_gw[1]=EEPROM.read(25); wifi_sta_gw[2]=EEPROM.read(26); wifi_sta_gw[3]=EEPROM.read(27);
wifi_sta_sn[0]=EEPROM.read(28); wifi_sta_sn[1]=EEPROM.read(29); wifi_sta_sn[2]=EEPROM.read(30); wifi_sta_sn[3]=EEPROM.read(31);
EEPROM_readString(500,wifi_ap_ssid);
EEPROM_readString(550,wifi_ap_pwd);
wifi_ap_ch=EEPROM_readInt(50);
wifi_ap_ip[0]=EEPROM.read(60); wifi_ap_ip[1]=EEPROM.read(61); wifi_ap_ip[2]=EEPROM.read(62); wifi_ap_ip[3]=EEPROM.read(63);
wifi_ap_gw[0]=EEPROM.read(70); wifi_ap_gw[1]=EEPROM.read(71); wifi_ap_gw[2]=EEPROM.read(72); wifi_ap_gw[3]=EEPROM.read(73);
wifi_ap_sn[0]=EEPROM.read(80); wifi_ap_sn[1]=EEPROM.read(81); wifi_ap_sn[2]=EEPROM.read(82); wifi_ap_sn[3]=EEPROM.read(83);
}
#ifndef DEBUG_ON
Serial.begin(SERIAL_BAUD_DEFAULT);
#ifdef SERIAL_SWAP_ON
Serial.swap();
#endif
Again:
digitalWrite(LED_PIN,LOW);
char c=0;
// clear the buffers and any noise on the serial lines
for (int i=0; i<3; i++) {
Serial.print(":#");
digitalWrite(LED_PIN,HIGH);
delay(500);
while (Serial.available()>0) { c=Serial.read(); }
digitalWrite(LED_PIN,LOW);
}
// safety net
if ((c=='R') || (!accessPointEnabled && !stationEnabled)) {
// reset EEPROM values, triggers an init
EEPROM_writeInt(0,0); EEPROM_writeInt(2,0);
EEPROM.commit();
Serial.println();
Serial.println("Cycle power for reset to defaults.");
Serial.println();
}
// switch OnStep Serial1 up to ? baud
Serial.print(SERIAL_BAUD);
delay(50);
int count=0; c=0;
while (Serial.available()>0) { count++; c=Serial.read(); }
if ((c=='1') && (count==1)) {
if (!strcmp(SERIAL_BAUD,":SB0#")) Serial.begin(115200); else
if (!strcmp(SERIAL_BAUD,":SB1#")) Serial.begin(57600); else
if (!strcmp(SERIAL_BAUD,":SB2#")) Serial.begin(38400); else
if (!strcmp(SERIAL_BAUD,":SB3#")) Serial.begin(28800); else
if (!strcmp(SERIAL_BAUD,":SB4#")) Serial.begin(19200); else Serial.begin(9600);
#ifdef SERIAL_SWAP_ON
Serial.swap();
#endif
} else {
digitalWrite(LED_PIN,HIGH);
delay(5000);
goto Again;
}
#else
Serial.begin(115200);
delay(10000);
Serial.println(accessPointEnabled);
Serial.println(stationEnabled);
Serial.println(stationDhcpEnabled);
Serial.println(WebTimeout);
Serial.println(CmdTimeout);
Serial.println(wifi_sta_ssid);
Serial.println(wifi_sta_pwd);
Serial.println(wifi_sta_ip.toString());
Serial.println(wifi_sta_gw.toString());
Serial.println(wifi_sta_sn.toString());
Serial.println(wifi_ap_ssid);
Serial.println(wifi_ap_pwd);
Serial.println(wifi_ap_ch);
Serial.println(wifi_ap_ip.toString());
Serial.println(wifi_ap_gw.toString());
Serial.println(wifi_ap_sn.toString());
#endif
if ((stationEnabled) && (!stationDhcpEnabled)) WiFi.config(wifi_sta_ip, wifi_sta_gw, wifi_sta_sn);
if (accessPointEnabled) WiFi.softAPConfig(wifi_ap_ip, wifi_ap_gw, wifi_ap_sn);
if (accessPointEnabled && !stationEnabled) {
WiFi.softAP(wifi_ap_ssid, wifi_ap_pwd, wifi_ap_ch);
WiFi.mode(WIFI_AP);
} else
if (!accessPointEnabled && stationEnabled) {
WiFi.softAPdisconnect(true);
WiFi.begin(wifi_sta_ssid, wifi_sta_pwd);
WiFi.mode(WIFI_STA);
} else
if (accessPointEnabled && stationEnabled) {
WiFi.softAP(wifi_ap_ssid, wifi_ap_pwd, wifi_ap_ch);
WiFi.begin(wifi_sta_ssid, wifi_sta_pwd);
WiFi.mode(WIFI_AP_STA);
}
// clear the buffers and any noise on the serial lines
for (int i=0; i<3; i++) {
Serial.print(":#");
delay(50);
while (Serial.available()>0) { c=Serial.read(); }
}
// Wait for connection
if (stationEnabled) {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
server.on("/", handleRoot);
server.on("/index.htm", handleRoot);
server.on("/settings.htm", handleSettings);
server.on("/control.htm", handleControl);
server.on("/guide.htm", handleGuide);
server.on("/guide.txt", handleGuideAjax);
server.on("/pec.htm", handlePec);
server.on("/wifi.htm", handleWifi);
server.on("/config.htm", handleConfig);
server.onNotFound(handleNotFound);
cmdSvr.begin();
cmdSvr.setNoDelay(true);
server.begin();
#ifdef DEBUG_ON
Serial.println("HTTP server started");
#endif
}
void loop(void){
server.handleClient();
// disconnect client
if (cmdSvrClient && (!cmdSvrClient.connected())) cmdSvrClient.stop();
if (cmdSvrClient && ((long)(clientTime-millis())<0)) cmdSvrClient.stop();
// new client
if (!cmdSvrClient && (cmdSvr.hasClient())) {
// find free/disconnected spot
cmdSvrClient = cmdSvr.available();
clientTime=millis()+2000UL;
}
// check clients for data, if found get the command, send cmd and pickup the response, then return the response
while (cmdSvrClient && cmdSvrClient.connected() && (cmdSvrClient.available()>0)) {
// get the data
byte b=cmdSvrClient.read();
writeBuffer[writeBufferPos]=b; writeBufferPos++; if (writeBufferPos>39) writeBufferPos=39; writeBuffer[writeBufferPos]=0;
// send cmd and pickup the response
if (b=='#') {
char readBuffer[40]="";
readLX200Bytes(writeBuffer,readBuffer,CmdTimeout); writeBuffer[0]=0; writeBufferPos=0;
// return the response, if we have one
if (strlen(readBuffer)>0) {
if (cmdSvrClient && cmdSvrClient.connected()) {
cmdSvrClient.print(readBuffer);
delay(2);
}
}
} else server.handleClient();
}
}