6
6
MDNSResponder mdns;
7
7
WiFiServer server (80 );
8
8
9
- // ssid is the access point name to use when booting into config mode
10
9
const char * ssid = " BUBBLES" ;
11
10
String st;
12
11
@@ -33,30 +32,30 @@ void setup() {
33
32
epass += char (EEPROM.read (i));
34
33
}
35
34
Serial.print (" PASS: " );
36
- Serial.println (epass);
35
+ Serial.println (epass);
37
36
if ( esid.length () > 1 ) {
38
- // test esid
37
+ // test esid
39
38
WiFi.begin (esid.c_str (), epass.c_str ());
40
- if ( testWifi () == 20 ) {
39
+ if ( testWifi () == 20 ) {
41
40
launchWeb (0 );
42
41
return ;
43
42
}
44
43
}
45
- setupAP ();
44
+ setupAP ();
46
45
}
47
46
48
47
int testWifi (void ) {
49
48
int c = 0 ;
50
- Serial.println (" Waiting for Wifi to connect" );
49
+ Serial.println (" Waiting for Wifi to connect" );
51
50
while ( c < 20 ) {
52
- if (WiFi.status () == WL_CONNECTED) { return (20 ); }
51
+ if (WiFi.status () == WL_CONNECTED) { return (20 ); }
53
52
delay (500 );
54
- Serial.print (WiFi.status ());
53
+ Serial.print (WiFi.status ());
55
54
c++;
56
55
}
57
56
Serial.println (" Connect timed out, opening AP" );
58
57
return (10 );
59
- }
58
+ }
60
59
61
60
void launchWeb (int webtype) {
62
61
Serial.println (" " );
@@ -65,24 +64,23 @@ void launchWeb(int webtype) {
65
64
Serial.println (WiFi.softAPIP ());
66
65
if (!mdns.begin (" esp8266" , WiFi.localIP ())) {
67
66
Serial.println (" Error setting up MDNS responder!" );
68
- while (1 ) {
67
+ while (1 ) {
69
68
delay (1000 );
70
69
}
71
70
}
72
71
Serial.println (" mDNS responder started" );
73
72
// Start the server
74
73
server.begin ();
75
- Serial.println (" Server started" );
76
- return ;
74
+ Serial.println (" Server started" );
77
75
int b = 20 ;
78
76
int c = 0 ;
79
- while (b == 20 ) {
77
+ while (b == 20 ) {
80
78
b = mdns1 (webtype);
81
79
}
82
80
}
83
81
84
82
void setupAP (void ) {
85
-
83
+
86
84
WiFi.mode (WIFI_STA);
87
85
WiFi.disconnect ();
88
86
delay (100 );
@@ -107,7 +105,7 @@ void setupAP(void) {
107
105
delay (10 );
108
106
}
109
107
}
110
- Serial.println (" " );
108
+ Serial.println (" " );
111
109
st = " <ul>" ;
112
110
for (int i = 0 ; i < n; ++i)
113
111
{
@@ -135,7 +133,7 @@ int mdns1(int webtype)
135
133
{
136
134
// Check for any mDNS queries and send responses
137
135
mdns.update ();
138
-
136
+
139
137
// Check if a client has connected
140
138
WiFiClient client = server.available ();
141
139
if (!client) {
@@ -148,10 +146,10 @@ int mdns1(int webtype)
148
146
while (client.connected () && !client.available ()){
149
147
delay (1 );
150
148
}
151
-
149
+
152
150
// Read the first line of HTTP request
153
151
String req = client.readStringUntil (' \r ' );
154
-
152
+
155
153
// First line of HTTP request looks like "GET /path HTTP/1.1"
156
154
// Retrieve the "/path" part by finding the spaces
157
155
int addr_start = req.indexOf (' ' );
@@ -164,7 +162,7 @@ int mdns1(int webtype)
164
162
req = req.substring (addr_start + 1 , addr_end);
165
163
Serial.print (" Request: " );
166
164
Serial.println (req);
167
- client.flush ();
165
+ client.flush ();
168
166
String s;
169
167
if ( webtype == 1 ) {
170
168
if (req == " /" )
@@ -183,29 +181,29 @@ int mdns1(int webtype)
183
181
// /a?ssid=blahhhh&pass=poooo
184
182
Serial.println (" clearing eeprom" );
185
183
for (int i = 0 ; i < 96 ; ++i) { EEPROM.write (i, 0 ); }
186
- String qsid;
184
+ String qsid;
187
185
qsid = req.substring (8 ,req.indexOf (' &' ));
188
186
Serial.println (qsid);
189
187
Serial.println (" " );
190
188
String qpass;
191
189
qpass = req.substring (req.lastIndexOf (' =' )+1 );
192
190
Serial.println (qpass);
193
191
Serial.println (" " );
194
-
192
+
195
193
Serial.println (" writing eeprom ssid:" );
196
194
for (int i = 0 ; i < qsid.length (); ++i)
197
195
{
198
196
EEPROM.write (i, qsid[i]);
199
197
Serial.print (" Wrote: " );
200
- Serial.println (qsid[i]);
198
+ Serial.println (qsid[i]);
201
199
}
202
- Serial.println (" writing eeprom pass:" );
200
+ Serial.println (" writing eeprom pass:" );
203
201
for (int i = 0 ; i < qpass.length (); ++i)
204
202
{
205
203
EEPROM.write (32 +i, qpass[i]);
206
204
Serial.print (" Wrote: " );
207
- Serial.println (qpass[i]);
208
- }
205
+ Serial.println (qpass[i]);
206
+ }
209
207
EEPROM.commit ();
210
208
s = " HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n <!DOCTYPE HTML>\r\n <html>Hello from ESP8266 " ;
211
209
s += " Found " ;
@@ -217,7 +215,7 @@ int mdns1(int webtype)
217
215
s = " HTTP/1.1 404 Not Found\r\n\r\n " ;
218
216
Serial.println (" Sending 404" );
219
217
}
220
- }
218
+ }
221
219
else
222
220
{
223
221
if (req == " /" )
@@ -227,11 +225,20 @@ int mdns1(int webtype)
227
225
s += " </html>\r\n\r\n " ;
228
226
Serial.println (" Sending 200" );
229
227
}
228
+ else if ( req.startsWith (" /cleareeprom" ) ) {
229
+ s = " HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n <!DOCTYPE HTML>\r\n <html>Hello from ESP8266" ;
230
+ s += " <p>Clearing the EEPROM<p>" ;
231
+ s += " </html>\r\n\r\n " ;
232
+ Serial.println (" Sending 200" );
233
+ Serial.println (" clearing eeprom" );
234
+ for (int i = 0 ; i < 96 ; ++i) { EEPROM.write (i, 0 ); }
235
+ EEPROM.commit ();
236
+ }
230
237
else
231
238
{
232
239
s = " HTTP/1.1 404 Not Found\r\n\r\n " ;
233
240
Serial.println (" Sending 404" );
234
- }
241
+ }
235
242
}
236
243
client.print (s);
237
244
Serial.println (" Done with client" );
0 commit comments