-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp_web_conf.ino
414 lines (382 loc) · 12.7 KB
/
esp_web_conf.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
/*
* esp_web_conf
*
* This is a web service to configure ESP8266 connect WiFi network with DHCP.
*
* ### Setup WiFi
* Then follow this instructions to connect WiFi network.
*
* 1. Connect the module's access point SSID 'esp-????' directly.
* - module ID 'esp-????': '????' is determined by MAC address.
* 2. Open 'http://192.168.4.1/wifi_conf' and set the SSID and Password of your WiFi network.
* 3. Reboot the module to connect the WiFi network and get DHCP address.
* 4. The module can be accessed by name 'esp-????.local' using mDNS(Bonjour).
* Or, you can find the assigned IP address in 'http://192.168.4.1/' via direct connecting 'esp-????' as AP.
*
* ### Change Module ID
* When you want to change the Module name, open '/module_id' and change the module ID.
* If you submit blank ID, the default ID 'esp-????' will be used.
*
* ### Update This Sketch
* When you want to update this sketch via Web, open '/update' and upload a compiled sketch file named '*.bin' by a FORM in the page.
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Koji Yokokawa <[email protected]>
*
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#define WIFI_CONF_FORMAT {0, 0, 0, 1}
const uint8_t wifi_conf_format[] = WIFI_CONF_FORMAT;
#define NAME_PREF "esp-"
String network_html;
#define WIFI_CONF_START 0
struct WiFiConfStruct {
uint8_t format[4];
char sta_ssid[32];
char sta_pwd[64];
char module_id[32];
} WiFiConf = {
WIFI_CONF_FORMAT,
"ssid",
"password",
""
};
void printWiFiConf(void) {
Serial.print("SSID: ");
Serial.println(WiFiConf.sta_ssid);
Serial.print("Password: ");
Serial.println(WiFiConf.sta_pwd);
Serial.print("Module ID: ");
Serial.println(WiFiConf.module_id);
}
bool loadWiFiConf() {
Serial.println();
Serial.println("loading WiFiConf");
if (EEPROM.read(WIFI_CONF_START + 0) == wifi_conf_format[0] &&
EEPROM.read(WIFI_CONF_START + 1) == wifi_conf_format[1] &&
EEPROM.read(WIFI_CONF_START + 2) == wifi_conf_format[2] &&
EEPROM.read(WIFI_CONF_START + 3) == wifi_conf_format[3])
{
for (unsigned int t = 0; t < sizeof(WiFiConf); t++) {
*((char*)&WiFiConf + t) = EEPROM.read(WIFI_CONF_START + t);
}
printWiFiConf();
return true;
} else {
Serial.println("WiFiConf was not saved on EEPROM.");
return false;
}
}
void saveWiFiConf(void) {
Serial.println("writing WiFiConf");
for (unsigned int t = 0; t < sizeof(WiFiConf); t++) {
EEPROM.write(WIFI_CONF_START + t, *((char*)&WiFiConf + t));
}
EEPROM.commit();
printWiFiConf();
}
void setDefaultModuleId(char* dst) {
uint8_t macAddr[WL_MAC_ADDR_LENGTH];
WiFi.macAddress(macAddr);
sprintf(dst, "%s%02x%02x", NAME_PREF, macAddr[WL_MAC_ADDR_LENGTH - 2], macAddr[WL_MAC_ADDR_LENGTH - 1]);
}
void resetModuleId(void) {
uint8_t macAddr[WL_MAC_ADDR_LENGTH];
WiFi.macAddress(macAddr);
setDefaultModuleId(WiFiConf.module_id);
Serial.print("Reset Module ID to default: ");
Serial.println(WiFiConf.module_id);
}
ESP8266WebServer server(80);
void setup() {
Serial.begin(9600);
EEPROM.begin(512);
delay(10);
Serial.println();
Serial.println();
Serial.println("Startup");
// read eeprom for ssid and pass
if (!loadWiFiConf()) {
// EEPROM was not initialized.
resetModuleId();
saveWiFiConf();
}
// scan Access Points
scanWiFi();
// start WiFi
WiFi.mode(WIFI_AP_STA);
WiFi.begin(WiFiConf.sta_ssid, WiFiConf.sta_pwd);
waitConnected();
if (WiFi.status() == WL_CONNECTED) {
WiFi.softAP(WiFiConf.module_id);
} else {
WiFi.mode(WIFI_AP);
WiFi.softAP(WiFiConf.module_id);
}
printIP();
// setup Web Interface
setupWeb();
setupWiFiConf();
setupWebUpdate();
// start Web Server
server.begin();
Serial.println();
Serial.println("Server started");
// start mDNS responder
if (!MDNS.begin(WiFiConf.module_id)) {
Serial.println();
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println();
Serial.println("mDNS responder started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void scanWiFi(void) {
int founds = WiFi.scanNetworks();
Serial.println();
Serial.println("scan done");
if (founds == 0) {
Serial.println("no networks found");
} else {
Serial.print(founds);
Serial.println(" networks found");
for (int i = 0; i < founds; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
}
}
Serial.println();
network_html = "<ol>";
for (int i = 0; i < founds; ++i)
{
// Print SSID and RSSI for each network found
network_html += "<li>";
network_html += WiFi.SSID(i);
network_html += " (";
network_html += WiFi.RSSI(i);
network_html += ")";
network_html += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
network_html += "</li>";
}
network_html += "</ol>";
}
int waitConnected(void) {
int wait = 0;
Serial.println();
Serial.println("Waiting for WiFi to connect");
while ( wait < 20 ) {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
return (1);
}
delay(500);
Serial.print(WiFi.status());
wait++;
}
Serial.println("");
Serial.println("Connect timed out");
return (0);
}
void printIP(void) {
Serial.println();
Serial.print("Name: ");
Serial.print(WiFiConf.module_id);
Serial.println(".local");
Serial.print("LAN: ");
Serial.println(WiFi.localIP());
Serial.print("AP: ");
Serial.println(WiFi.softAPIP());
}
void setupWiFiConf(void) {
server.on("/wifi_conf", []() {
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
String content = "<!DOCTYPE HTML>\r\n<html><head><title>";
content += WiFiConf.module_id;
content += ".local - Configuration";
content += "</title></head><body>";
content += "<h1>Configuration of ESP8266</h1>";
content += "<p>LAN: ";
content += WiFiConf.sta_ssid;
content += "</br>IP: ";
content += ipStr;
content += " ( ";
content += WiFiConf.module_id;
content += ".local";
content += " )</p>";
content += "<p>";
content += "</p><form method='get' action='set_wifi_conf'><label for='ssid'>SSID: </label><input name='ssid'id='ssid' maxlength=32 value=''>";
content += "<label for='pwd'>PASS: </label> <input type='password' name='pwd' id='pwd' />";
content += "<input type='submit' onclick='return confirm(\"Are you sure you want to change the WiFi settings?\");'></form>";
content += network_html;
content += "</body></html>";
server.send(200, "text/html", content);
});
server.on("/set_wifi_conf", []() {
String new_ssid = server.arg("ssid");
String new_pwd = server.arg("pwd");
String content = "<!DOCTYPE HTML>\r\n<html><head><title>";
content += WiFiConf.module_id;
content += ".local - set WiFi";
content += "</title></head><body>";
content += "<h1>Set WiFi of ESP8266</h1>";
if (new_ssid.length() > 0) {
new_ssid.toCharArray(WiFiConf.sta_ssid, sizeof(WiFiConf.sta_ssid));
new_pwd.toCharArray(WiFiConf.sta_pwd, sizeof(WiFiConf.sta_pwd));
String deviceURL = "http://";
deviceURL += WiFiConf.module_id;
deviceURL += ".local/";
saveWiFiConf();
content += "<p>saved '";
content += WiFiConf.sta_ssid;
content += "'... Reset to boot into new WiFi</p>";
content += "<p>You can access this device at <a href='";
content += deviceURL;
content += "'>";
content += deviceURL;
content += "</a>.<p>";
content += "<body></html>";
} else {
content += "<p>Empty SSID is not acceptable. </p>";
content += "<body></html>";
Serial.println("Rejected empty SSID.");
}
server.send(200, "text/html", content);
});
server.on("/module_id", []() {
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
char defaultId[sizeof(WiFiConf.module_id)];
setDefaultModuleId(defaultId);
String content = "<!DOCTYPE HTML>\r\n<html><head><title>";
content += WiFiConf.module_id;
content += ".local - Module ID";
content += "</title></head><body>";
content += "<h1>Module ID of ESP8266</h1>";
content += "<p>Module ID: ";
content += WiFiConf.module_id;
content += "</br>IP: ";
content += ipStr;
content += "</p>";
content += "<p>";
content += "<form method='get' action='set_module_id'><label for='module_id'>New Module ID: </label><input name='module_id' id='module_id' maxlength=32 value='";
content += WiFiConf.module_id;
content += "'><input type='submit' onclick='return confirm(\"Are you sure you want to change the Module ID?\");'></form>";
content += " Empty will reset to default ID '";
content += defaultId;
content += "'</p>";
content += "</body></html>";
server.send(200, "text/html", content);
});
server.on("/set_module_id", []() {
String new_id = server.arg("module_id");
String content = "<!DOCTYPE HTML>\r\n<html><head>";
content += "<title>";
content += WiFiConf.module_id;
content += ".local - set WiFi";
content += "</title>";
content += "</head><body>";
if (new_id.length() > 0) {
new_id.toCharArray(WiFiConf.module_id, sizeof(WiFiConf.module_id));
} else {
resetModuleId();
}
saveWiFiConf();
content += "<h1>Set WiFi of ESP8266</h1>";
content += "<p>Set Module ID to '";
content += WiFiConf.module_id;
content += "' ... Restart to applay it. </p>";
content += "</body></html>";
server.send(200, "text/html", content);
});
}
const char* sketchUploadForm = "<form method='POST' action='/upload_sketch' enctype='multipart/form-data'><input type='file' name='sketch'><input type='submit' value='Upload' onclick='return confirm(\"Are you sure you want to update the Sketch?\");'></form>";
void setupWebUpdate(void) {
server.on("/update", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/html", sketchUploadForm);
});
server.onFileUpload([]() {
if (server.uri() != "/upload_sketch") return;
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.setDebugOutput(true);
WiFiUDP::stopAll();
Serial.printf("Sketch: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
});
server.on("/upload_sketch", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
});
}
void setupWeb(void) {
server.on("/", []() {
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
String content = "<!DOCTYPE HTML>\r\n<html><head><title>";
content += WiFiConf.module_id;
content += ".local";
content += "</title></head><body>";
content += "<h1>Hello from ESP8266</h1>";
content += "<p>LAN: ";
content += WiFiConf.sta_ssid;
content += "</br>IP: ";
content += ipStr;
content += " ( ";
content += WiFiConf.module_id;
content += ".local";
content += " )</p>";
content += "<p>";
content += "</p>";
content += "<ul>";
content += "<li><a href='/wifi_conf'>Setup WiFi</a>";
content += "<li><a href='/module_id'>Setup Module ID</a>";
content += "<li><a href='/update'>Update Sketch</a>";
content += "</ul>";
content += "</body></html>";
server.send(200, "text/html", content);
});
}
void loop() {
// handle web server
server.handleClient();
// put your main code here, to run repeatedly:
}