Skip to content

Commit 6fcd5a3

Browse files
author
Christopher Strider Cook
committed
remove rando return and add eeprom clear option
1 parent c32d452 commit 6fcd5a3

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

ESP-wifiboot.ino

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
MDNSResponder mdns;
77
WiFiServer server(80);
88

9-
// ssid is the access point name to use when booting into config mode
109
const char* ssid = "BUBBLES";
1110
String st;
1211

@@ -33,30 +32,30 @@ void setup() {
3332
epass += char(EEPROM.read(i));
3433
}
3534
Serial.print("PASS: ");
36-
Serial.println(epass);
35+
Serial.println(epass);
3736
if ( esid.length() > 1 ) {
38-
// test esid
37+
// test esid
3938
WiFi.begin(esid.c_str(), epass.c_str());
40-
if ( testWifi() == 20 ) {
39+
if ( testWifi() == 20 ) {
4140
launchWeb(0);
4241
return;
4342
}
4443
}
45-
setupAP();
44+
setupAP();
4645
}
4746

4847
int testWifi(void) {
4948
int c = 0;
50-
Serial.println("Waiting for Wifi to connect");
49+
Serial.println("Waiting for Wifi to connect");
5150
while ( c < 20 ) {
52-
if (WiFi.status() == WL_CONNECTED) { return(20); }
51+
if (WiFi.status() == WL_CONNECTED) { return(20); }
5352
delay(500);
54-
Serial.print(WiFi.status());
53+
Serial.print(WiFi.status());
5554
c++;
5655
}
5756
Serial.println("Connect timed out, opening AP");
5857
return(10);
59-
}
58+
}
6059

6160
void launchWeb(int webtype) {
6261
Serial.println("");
@@ -65,24 +64,23 @@ void launchWeb(int webtype) {
6564
Serial.println(WiFi.softAPIP());
6665
if (!mdns.begin("esp8266", WiFi.localIP())) {
6766
Serial.println("Error setting up MDNS responder!");
68-
while(1) {
67+
while(1) {
6968
delay(1000);
7069
}
7170
}
7271
Serial.println("mDNS responder started");
7372
// Start the server
7473
server.begin();
75-
Serial.println("Server started");
76-
return;
74+
Serial.println("Server started");
7775
int b = 20;
7876
int c = 0;
79-
while(b == 20) {
77+
while(b == 20) {
8078
b = mdns1(webtype);
8179
}
8280
}
8381

8482
void setupAP(void) {
85-
83+
8684
WiFi.mode(WIFI_STA);
8785
WiFi.disconnect();
8886
delay(100);
@@ -107,7 +105,7 @@ void setupAP(void) {
107105
delay(10);
108106
}
109107
}
110-
Serial.println("");
108+
Serial.println("");
111109
st = "<ul>";
112110
for (int i = 0; i < n; ++i)
113111
{
@@ -135,7 +133,7 @@ int mdns1(int webtype)
135133
{
136134
// Check for any mDNS queries and send responses
137135
mdns.update();
138-
136+
139137
// Check if a client has connected
140138
WiFiClient client = server.available();
141139
if (!client) {
@@ -148,10 +146,10 @@ int mdns1(int webtype)
148146
while(client.connected() && !client.available()){
149147
delay(1);
150148
}
151-
149+
152150
// Read the first line of HTTP request
153151
String req = client.readStringUntil('\r');
154-
152+
155153
// First line of HTTP request looks like "GET /path HTTP/1.1"
156154
// Retrieve the "/path" part by finding the spaces
157155
int addr_start = req.indexOf(' ');
@@ -164,7 +162,7 @@ int mdns1(int webtype)
164162
req = req.substring(addr_start + 1, addr_end);
165163
Serial.print("Request: ");
166164
Serial.println(req);
167-
client.flush();
165+
client.flush();
168166
String s;
169167
if ( webtype == 1 ) {
170168
if (req == "/")
@@ -183,29 +181,29 @@ int mdns1(int webtype)
183181
// /a?ssid=blahhhh&pass=poooo
184182
Serial.println("clearing eeprom");
185183
for (int i = 0; i < 96; ++i) { EEPROM.write(i, 0); }
186-
String qsid;
184+
String qsid;
187185
qsid = req.substring(8,req.indexOf('&'));
188186
Serial.println(qsid);
189187
Serial.println("");
190188
String qpass;
191189
qpass = req.substring(req.lastIndexOf('=')+1);
192190
Serial.println(qpass);
193191
Serial.println("");
194-
192+
195193
Serial.println("writing eeprom ssid:");
196194
for (int i = 0; i < qsid.length(); ++i)
197195
{
198196
EEPROM.write(i, qsid[i]);
199197
Serial.print("Wrote: ");
200-
Serial.println(qsid[i]);
198+
Serial.println(qsid[i]);
201199
}
202-
Serial.println("writing eeprom pass:");
200+
Serial.println("writing eeprom pass:");
203201
for (int i = 0; i < qpass.length(); ++i)
204202
{
205203
EEPROM.write(32+i, qpass[i]);
206204
Serial.print("Wrote: ");
207-
Serial.println(qpass[i]);
208-
}
205+
Serial.println(qpass[i]);
206+
}
209207
EEPROM.commit();
210208
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 ";
211209
s += "Found ";
@@ -217,7 +215,7 @@ int mdns1(int webtype)
217215
s = "HTTP/1.1 404 Not Found\r\n\r\n";
218216
Serial.println("Sending 404");
219217
}
220-
}
218+
}
221219
else
222220
{
223221
if (req == "/")
@@ -227,11 +225,20 @@ int mdns1(int webtype)
227225
s += "</html>\r\n\r\n";
228226
Serial.println("Sending 200");
229227
}
228+
else if ( req.startsWith("/cleareeprom") ) {
229+
s = "HTTP/1.1 200 OK\r\nContent-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+
}
230237
else
231238
{
232239
s = "HTTP/1.1 404 Not Found\r\n\r\n";
233240
Serial.println("Sending 404");
234-
}
241+
}
235242
}
236243
client.print(s);
237244
Serial.println("Done with client");

0 commit comments

Comments
 (0)