0% found this document useful (0 votes)
25 views2 pages

Ota

This document defines code for an ESP32 board to serve as a WiFi access point and host a web server for over-the-air firmware updates. It includes code to connect to a local WiFi network, start an mDNS web server on port 80, define an index page for firmware updates, and handle POST requests to upload and flash new firmware over WiFi.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Ota

This document defines code for an ESP32 board to serve as a WiFi access point and host a web server for over-the-air firmware updates. It includes code to connect to a local WiFi network, start an mDNS web server on port 80, define an index page for firmware updates, and handle POST requests to upload and flash new firmware over WiFi.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <DNSServer.

h>

#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <WiFiUdp.h>
#include "ArduinoOTA.h"
//https://github.com/esp8266/Arduino/tree/master/libraries/ArduinoOTA
WebServer server(80);
const char* host = "esp32";

/*
* 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
*/

const char* ssid_sta = "Joy 3_1579";


const char* password_sta = "nhungdaica";
const char* ssid = "Scubic-ota";
const char* password = "scubic1234";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid_sta, password_sta);

while (WiFi.waitForConnectResult() != WL_CONNECTED) {


Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}

/*return index page which is stored in serverIndex */

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();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
server.handleClient();

//any custom code or logic goes here


}
////C:\Users\WINDOWS\AppData\Local\Temp\.arduinoIDE-unsaved2023118-4344-
nxp55d.4dwwf\sketch_feb18a\build\esp32.esp32.esp32

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy