OTA with ESP32 as AP
Posted: Wed Jul 31, 2019 8:48 pm
Hi there, I am having the following use case scenario:
The ESP32 is placed within a case that cannot be easily opened. Therefore, in case I need to update its firmware, OTA is a must. The second point, the ESP32 will be working without a Wifi network in reach. I thought it would be possible to setup the OTA code to set the ESP32 as AP so it can provide its own wifi net and I could just upload my bin file without needing nothing else than a laptop and the ESP32..
I managed to configure the ESP32 as AP, I can access its webpage but when I try to upload the file, I got always the message that the page hasnt been found ("serverIndex/Update Not found").
I have used the code shown here: https://randomnerdtutorials.com/esp32- ... ogramming/
and replaced the first part where the wifi is started to be set as AP.
Is there any hint you could provide me? may be a working example?
Thanks in advance!
The ESP32 is placed within a case that cannot be easily opened. Therefore, in case I need to update its firmware, OTA is a must. The second point, the ESP32 will be working without a Wifi network in reach. I thought it would be possible to setup the OTA code to set the ESP32 as AP so it can provide its own wifi net and I could just upload my bin file without needing nothing else than a laptop and the ESP32..
I managed to configure the ESP32 as AP, I can access its webpage but when I try to upload the file, I got always the message that the page hasnt been found ("serverIndex/Update Not found").
I have used the code shown here: https://randomnerdtutorials.com/esp32- ... ogramming/
and replaced the first part where the wifi is started to be set as AP.
- /* OTAWebUpdater.ino Example from ArduinoOTA Library
- * Rui Santos
- * Complete Project Details https://randomnerdtutorials.com
- */
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <ESPmDNS.h>
- #include <Update.h>
- const char* host = "esp32";
- const char* ssid = "REPLACE_WITH_YOUR_SSID";
- const char* password = "REPLACE_WITH_YOUR_PASSWORD";
- WebServer server(80);
- /*
- * Login page
- */
- const char* loginIndex =
- "<form name='loginForm'>"
- "<table width='20%' bgcolor='A09F9F' align='center'>"
- "<tr>"
- "<td colspan=2>"
- "<center><font size=4><b>ESP32 Login Page</b></font></center>"
- "<br>"
- "</td>"
- "<br>"
- "<br>"
- "</tr>"
- "<td>Username:</td>"
- "<td><input type='text' size=25 name='userid'><br></td>"
- "</tr>"
- "<br>"
- "<br>"
- "<tr>"
- "<td>Password:</td>"
- "<td><input type='Password' size=25 name='pwd'><br></td>"
- "<br>"
- "<br>"
- "</tr>"
- "<tr>"
- "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
- "</tr>"
- "</table>"
- "</form>"
- "<script>"
- "function check(form)"
- "{"
- "if(form.userid.value=='admin' && form.pwd.value=='admin')"
- "{"
- "window.open('/serverIndex')"
- "}"
- "else"
- "{"
- " alert('Error Password or Username')/*displays error message*/"
- "}"
- "}"
- "</script>";
- /*
- * Server Index Page
- */
- const char* serverIndex =
- "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
- "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
- "<input type='file' name='update'>"
- "<input type='submit' value='Update'>"
- "</form>"
- "<div id='prg'>progress: 0%</div>"
- "<script>"
- "$('form').submit(function(e){"
- "e.preventDefault();"
- "var form = $('#upload_form')[0];"
- "var data = new FormData(form);"
- " $.ajax({"
- "url: '/update',"
- "type: 'POST',"
- "data: data,"
- "contentType: false,"
- "processData:false,"
- "xhr: function() {"
- "var xhr = new window.XMLHttpRequest();"
- "xhr.upload.addEventListener('progress', function(evt) {"
- "if (evt.lengthComputable) {"
- "var per = evt.loaded / evt.total;"
- "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
- "}"
- "}, false);"
- "return xhr;"
- "},"
- "success:function(d, s) {"
- "console.log('success!')"
- "},"
- "error: function (a, b, c) {"
- "}"
- "});"
- "});"
- "</script>";
- /*
- * setup function
- */
- void setup(void) {
- Serial.begin(115200);
- // Connect to WiFi network
- //WiFi.begin(ssid, password);
- WiFi.softAP(ssid, password);
- Serial.println("");
- IPAddress IP = WiFi.softAPIP();
- Serial.print("AP IP address: ");
- Serial.println(IP);
- /*use mdns for host name resolution*/
- if (!MDNS.begin(host)) { //http://esp32.local
- Serial.println("Error setting up MDNS responder!");
- while (1) {
- delay(1000);
- }
- }
- Serial.println("mDNS responder started");
- /*return index page which is stored in serverIndex */
- server.on("/", HTTP_GET, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/html", loginIndex);
- });
- server.on("/serverIndex", HTTP_GET, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/html", serverIndex);
- });
- /*handling uploading firmware file */
- server.on("/update", HTTP_POST, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
- ESP.restart();
- }, []() {
- HTTPUpload& upload = server.upload();
- if (upload.status == UPLOAD_FILE_START) {
- Serial.printf("Update: %s\n", upload.filename.c_str());
- if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
- Update.printError(Serial);
- }
- } else if (upload.status == UPLOAD_FILE_WRITE) {
- /* flashing firmware to ESP*/
- 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);
- }
- }
- });
- server.begin();
- }
- void loop(void) {
- server.handleClient();
- delay(1);
- }
Is there any hint you could provide me? may be a working example?
Thanks in advance!