|
23 | 23 | #include <Update.h> |
24 | 24 |
|
25 | 25 | const char* host = "esp32"; |
26 | | -const char* ssid = "xxxxxx"; // your WiFi SSID here |
27 | | -const char* password = "xxxxxx"; // your WiFi password here |
| 26 | +const char* ssid = "xxxxxxx"; // your WiFi SSID here |
| 27 | +const char* password = "xxxxxxxxx"; // your WiFi password here |
28 | 28 | WebServer server(80); |
29 | 29 |
|
30 | 30 | /* |
@@ -176,61 +176,69 @@ void setup() |
176 | 176 | WiFi.begin(ssid, password); |
177 | 177 | Serial.println(""); |
178 | 178 |
|
179 | | - // Wait for connection |
180 | | - while (WiFi.status() != WL_CONNECTED) { |
| 179 | + // Wait for connection for 20 seconds, then move on |
| 180 | + unsigned long startTime = millis(); // Get the current time |
| 181 | + while (!(WiFi.status() == WL_CONNECTED) && ((millis() - startTime) < 2000)) { |
181 | 182 | delay(500); |
182 | 183 | Serial.print("."); |
183 | 184 | } |
184 | | - Serial.println(""); |
185 | | - Serial.print("Connected to "); |
186 | | - Serial.println(ssid); |
187 | | - Serial.print("IP address: "); |
188 | | - Serial.println(WiFi.localIP()); |
189 | 185 |
|
190 | | - /*use mdns for host name resolution*/ |
191 | | - if (!MDNS.begin(host)) { //http://esp32.local |
192 | | - Serial.println("Error setting up MDNS responder!"); |
193 | | - while (1) { |
194 | | - delay(1000); |
195 | | - } |
196 | | - } |
197 | | - Serial.println("mDNS responder started"); |
198 | | - /*return index page which is stored in serverIndex */ |
199 | | - server.on("/", HTTP_GET, []() { |
200 | | - server.sendHeader("Connection", "close"); |
201 | | - server.send(200, "text/html", loginIndex); |
202 | | - }); |
203 | | - server.on("/serverIndex", HTTP_GET, []() { |
204 | | - server.sendHeader("Connection", "close"); |
205 | | - server.send(200, "text/html", serverIndex); |
206 | | - }); |
207 | | - /*handling uploading firmware file */ |
208 | | - server.on("/update", HTTP_POST, []() { |
209 | | - server.sendHeader("Connection", "close"); |
210 | | - server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); |
211 | | - ESP.restart(); |
212 | | - }, []() { |
213 | | - HTTPUpload& upload = server.upload(); |
214 | | - if (upload.status == UPLOAD_FILE_START) { |
215 | | - Serial.printf("Update: %s\n", upload.filename.c_str()); |
216 | | - if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size |
217 | | - Update.printError(Serial); |
218 | | - } |
219 | | - } else if (upload.status == UPLOAD_FILE_WRITE) { |
220 | | - /* flashing firmware to ESP*/ |
221 | | - if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { |
222 | | - Update.printError(Serial); |
223 | | - } |
224 | | - } else if (upload.status == UPLOAD_FILE_END) { |
225 | | - if (Update.end(true)) { //true to set the size to the current progress |
226 | | - Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); |
227 | | - } else { |
228 | | - Update.printError(Serial); |
| 186 | + if (WiFi.status() == WL_CONNECTED) { |
| 187 | + |
| 188 | + Serial.println(""); |
| 189 | + Serial.print("Connected to "); |
| 190 | + Serial.println(ssid); |
| 191 | + Serial.print("IP address: "); |
| 192 | + Serial.println(WiFi.localIP()); |
| 193 | + |
| 194 | + /*use mdns for host name resolution*/ |
| 195 | + if (!MDNS.begin(host)) { //http://esp32.local |
| 196 | + Serial.println("Error setting up MDNS responder!"); |
| 197 | + while (1) { |
| 198 | + delay(1000); |
229 | 199 | } |
230 | 200 | } |
231 | | - }); |
232 | | - server.begin(); |
233 | | - |
| 201 | + Serial.println("mDNS responder started"); |
| 202 | + /*return index page which is stored in serverIndex */ |
| 203 | + server.on("/", HTTP_GET, []() { |
| 204 | + server.sendHeader("Connection", "close"); |
| 205 | + server.send(200, "text/html", loginIndex); |
| 206 | + }); |
| 207 | + server.on("/serverIndex", HTTP_GET, []() { |
| 208 | + server.sendHeader("Connection", "close"); |
| 209 | + server.send(200, "text/html", serverIndex); |
| 210 | + }); |
| 211 | + /*handling uploading firmware file */ |
| 212 | + server.on("/update", HTTP_POST, []() { |
| 213 | + server.sendHeader("Connection", "close"); |
| 214 | + server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); |
| 215 | + ESP.restart(); |
| 216 | + }, []() { |
| 217 | + HTTPUpload& upload = server.upload(); |
| 218 | + if (upload.status == UPLOAD_FILE_START) { |
| 219 | + Serial.printf("Update: %s\n", upload.filename.c_str()); |
| 220 | + if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size |
| 221 | + Update.printError(Serial); |
| 222 | + } |
| 223 | + } else if (upload.status == UPLOAD_FILE_WRITE) { |
| 224 | + /* flashing firmware to ESP*/ |
| 225 | + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { |
| 226 | + Update.printError(Serial); |
| 227 | + } |
| 228 | + } else if (upload.status == UPLOAD_FILE_END) { |
| 229 | + if (Update.end(true)) { //true to set the size to the current progress |
| 230 | + Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); |
| 231 | + } else { |
| 232 | + Update.printError(Serial); |
| 233 | + } |
| 234 | + } |
| 235 | + }); |
| 236 | + server.begin(); |
| 237 | + } |
| 238 | + else { |
| 239 | + Serial.println(""); |
| 240 | + Serial.println("WiFi connection timed out, you may need to update SSID/password. Moving on now."); |
| 241 | + } |
234 | 242 | } |
235 | 243 |
|
236 | 244 | void loop() |
|
0 commit comments