This repository has been archived by the owner on Mar 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
esp8266-webconf-mDNS-OTA.ino
426 lines (343 loc) · 8.75 KB
/
esp8266-webconf-mDNS-OTA.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
/**
* @file esp8266-webconf-mDNS-OTA.ino
*
* @author Pascal Gollor (http://www.pgollor.de/cms/)
* @data 2015-08-17
*
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>
/**
* @brief mDNS and OTA Constants
* @{
*/
#define HOSTNAME "ESP8266-" ///< Hostename
#define APORT 8266 ///< OTA Port
/// @}
/**
* @brief Default WiFi connection information.
* @{
*/
const char* ap_default_ssid = "esp8266"; ///< Default SSID.
const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
/// @}
/// Uncomment the next line for verbose output over UART.
#define SERIAL_VERBOSE
/// HTML answer on restart request.
#define RESTART_HTML_ANSWER "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"15; URL=http://" HOSTNAME ".local/\"></head><body>Restarting in 15 seconds.<br/><img src=\"/loading.gif\"></body></html>"
/// Webserver handle on port 80.
ESP8266WebServer g_server(80);
/// global WiFi SSID.
String g_ssid = "";
/// global WiFi PSK.
String g_pass = "";
/// Restart will be triggert on this time
unsigned long g_restartTime = 0;
/**
* @brief Read WiFi connection information from file system.
* @param ssid String pointer for storing SSID.
* @param pass String pointer for storing PSK.
* @return True or False.
*
* The config file have to containt the WiFi SSID in the first line
* and the WiFi PSK in the second line.
* Line seperator can be \r\n (CR LF) \r or \n.
*/
bool loadConfig(String *ssid, String *pass)
{
// open file for reading.
File configFile = SPIFFS.open("/cl_conf.txt", "r");
if (!configFile)
{
Serial.println("Failed to open cl_conf.txt.");
return false;
}
// Read content from config file.
String content = configFile.readString();
configFile.close();
content.trim();
// Check if ther is a second line available.
int8_t pos = content.indexOf("\r\n");
uint8_t le = 2;
// check for linux and mac line ending.
if (pos == -1)
{
le = 1;
pos = content.indexOf("\n");
if (pos == -1)
{
pos = content.indexOf("\r");
}
}
// If there is no second line: Some information is missing.
if (pos == -1)
{
Serial.println("Infvalid content.");
Serial.println(content);
return false;
}
// Store SSID and PSK into string vars.
*ssid = content.substring(0, pos);
*pass = content.substring(pos + le);
ssid->trim();
pass->trim();
#ifdef SERIAL_VERBOSE
Serial.println("----- file content -----");
Serial.println(content);
Serial.println("----- file content -----");
Serial.println("ssid: " + *ssid);
Serial.println("psk: " + *pass);
#endif
return true;
} // loadConfig
/**
* @brief Save WiFi SSID and PSK to configuration file.
* @param ssid SSID as string pointer.
* @param pass PSK as string pointer,
* @return True or False.
*/
bool saveConfig(String *ssid, String *pass)
{
// Open config file for writing.
File configFile = SPIFFS.open("/cl_conf.txt", "w");
if (!configFile)
{
Serial.println("Failed to open cl_conf.txt for writing");
return false;
}
// Save SSID and PSK.
configFile.println(*ssid);
configFile.println(*pass);
configFile.close();
return true;
} // saveConfig
/**
* @brief Handle http root request.
*/
void handleRoot()
{
String indexHTML;
char buff[10];
uint16_t s = millis() / 1000;
uint16_t m = s / 60;
uint8_t h = m / 60;
File indexFile = SPIFFS.open("/index.html", "r");
if (indexFile)
{
indexHTML = indexFile.readString();
indexFile.close();
snprintf(buff, 10, "%02d:%02d:%02d", h, m % 60, s % 60);
// replace placeholder
indexHTML.replace("[esp8266]", String(ESP.getChipId(), HEX));
indexHTML.replace("[rssi]", String(WiFi.RSSI()));
indexHTML.replace("[ssid]", g_ssid);
indexHTML.replace("[pass]", g_pass);
indexHTML.replace("[uptime]", buff);
//Serial.println(g_indexHTML);
}
else
{
indexHTML = "<!DOCTYPE html><html><head><title>File not found</title></head><body><h1>File not found.</h1></body></html>";
}
g_server.send (200, "text/html", indexHTML);
} // handleRoot
/**
* @brief Handle set request from http server.
*
* URI: /set?ssid=[WiFi SSID],pass=[WiFi Pass]
*/
void handleSet()
{
String response = "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"2; URL=http://";
response += HOSTNAME;
response += ".local\"></head><body>";
// Some debug output
Serial.print("uri: ");
Serial.println(g_server.uri());
Serial.print("method: ");
Serial.println(g_server.method());
Serial.print("args: ");
Serial.println(g_server.args());
// Check arguments
if (g_server.args() < 2)
{
g_server.send (200, "text/plain", "Arguments fail.");
return;
}
String ssid = "";
String pass = "";
// read ssid and psk
for (uint8_t i = 0; i < g_server.args(); i++)
{
if (g_server.argName(i) == "ssid")
{
ssid = g_server.arg(i);
}
else if (g_server.argName(i) == "pass")
{
pass = g_server.arg(i);
}
}
// check ssid and psk
if (ssid != "" && pass != "")
{
// save ssid and psk to file
if (saveConfig(&ssid, &pass))
{
// store SSID and PSK into global variables.
g_ssid = ssid;
g_pass = pass;
response += "Successfull.";
}
else
{
response += "<h1>Fail save to config file.</h1>";
}
}
else
{
response += "<h1>Wrong arguments.</h1>";
}
response += "</body></html>";
g_server.send (200, "text/html", response);
} // handleSet
/**
* @brief Load loading.gif from filesytem and draw.
*/
void drawLoading()
{
File gif = SPIFFS.open("/loading.gif", "r");
if (gif)
{
g_server.send(200, "image/gif", gif.readString());
gif.close();
}
else
{
g_server.send(404, "text/plain", "Gif not found.");
}
} // drawLoading
/**
* @brief Arduino setup function.
*/
void setup()
{
g_ssid = "";
g_pass = "";
Serial.begin(115200);
delay(100);
Serial.println("\r\n");
Serial.print("Chip ID: 0x");
Serial.println(ESP.getChipId(), HEX);
// Set Hostname.
String hostname(HOSTNAME);
hostname += String(ESP.getChipId(), HEX);
WiFi.hostname(hostname);
// Print hostname.
Serial.println("Hostname: " + hostname);
//Serial.println(WiFi.hostname());
// Initialize file system.
if (!SPIFFS.begin())
{
Serial.println("Failed to mount file system");
return;
}
// Load wifi connection information.
if (! loadConfig(&g_ssid, &g_pass))
{
g_ssid = "";
g_pass = "";
Serial.println("No WiFi connection information available.");
}
// Check WiFi connection
// ... check mode
if (WiFi.getMode() != WIFI_STA)
{
WiFi.mode(WIFI_STA);
delay(10);
}
// ... Compare file config with sdk config.
if (WiFi.SSID() != g_ssid || WiFi.psk() != g_pass)
{
Serial.println("WiFi config changed.");
// ... Try to connect to WiFi station.
WiFi.begin(g_ssid.c_str(), g_pass.c_str());
// ... Pritn new SSID
Serial.print("new SSID: ");
Serial.println(WiFi.SSID());
// ... Uncomment this for debugging output.
//WiFi.printDiag(Serial);
}
else
{
// ... Begin with sdk config.
WiFi.begin();
}
Serial.println("Wait for WiFi connection.");
// ... Give ESP 10 seconds to connect to station.
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
{
Serial.write('.');
//Serial.print(WiFi.status());
delay(500);
}
Serial.println();
// Check connection
if(WiFi.status() == WL_CONNECTED)
{
// ... print IP Address
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("Can not connect to WiFi station. Go into AP mode.");
// Go into AP mode.
WiFi.mode(WIFI_AP);
delay(10);
WiFi.softAP(ap_default_ssid, ap_default_psk);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
}
// Set port
ArduinoOTA.setPort(APORT);
// set hostname
ArduinoOTA.setHostname(hostname.c_str());
// start OTA Server
ArduinoOTA.begin();
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
// Initialize web server.
// ... Add requests.
g_server.on("/", handleRoot);
g_server.on("/set", HTTP_GET, handleSet);
g_server.on("/restart", []() {
g_server.send(200, "text/html", RESTART_HTML_ANSWER);
g_restartTime = millis() + 100;
} );
g_server.serveStatic("/loading.gif", SPIFFS, "/loading.gif");
// ... Start server.
g_server.begin();
}
/**
* @brief Arduino loop function.
*/
void loop()
{
// Handle OTA server.
ArduinoOTA.handle();
yield();
// Handle Webserver requests.
g_server.handleClient();
// trigger restart?
if (g_restartTime > 0 && millis() >= g_restartTime)
{
g_restartTime = 0;
ESP.restart();
}
}