I'm developing an react native based OTA app updater. The file is been searched by the fs and then uploaded to esp32 via http where esp32 is in AP mode to receive the file over webserver. During the update , the file bytes are successfully uploaded 4 times then gives an issue.
[Error Log image attachenter image description hereed here]2
For the rest of the file I am not getting what might be the issue . Am attaching the files below
#include <WiFi.h>
#include <WebServer.h>
#include <Update.h>
#include <FS.h>
#include <SPIFFS.h>
#include <ESPmDNS.h>
#include <Update.h>
const char* ssid = "KVAR";
const char* password = "kvar123456";
#define SSID "ESP32_AP"
#define PASSWORD "12345678"
#define SERVER_PORT 80
#define FIRMWARE_FILENAME "/firmware.bin"
const char* host = "esp32-webupdate";
WebServer server(SERVER_PORT);
void handleFileUpload() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Receiving file: %s\n", upload.filename.c_str());
// Open the file for writing
// For example:
// File file = SPIFFS.open("/uploads/" + upload.filename, "w");
} else if (upload.status == UPLOAD_FILE_WRITE) {
// Write the file chunk to the file
// For example:
// file.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
// Close the file
// For example:
// file.close();
Serial.printf("File received: %s, size: %u\n", upload.filename.c_str(), upload.totalSize);
// Update the firmware with the file
Update.begin(upload.totalSize);
int bytesWritten = Update.write(upload.buf, upload.currentSize);
if (bytesWritten == upload.currentSize) {
Serial.println("File written to flash");
} else {
Serial.println("Failed to write file to flash");
Update.abort();
}
if (Update.end(true)) {
Serial.println("Firmware updated successfully, rebooting...");
ESP.restart();
} else {
Serial.println("Firmware update failed");
}
server.send(200, "text/plain", "Firmware updated successfully");
}
}
void handlePostRequest() {
if (server.uri() == "/upload") {
// Handle file upload
handleFileUpload();
} else {
// Handle other POST requests
// ...
}
}
void handleRoot() {
String html = "<html><body><form method=\"post\" action=\"/upload\" enctype=\"multipart/form-data\"><input type=\"file\" name=\"firmware\"><input type=\"submit\" value=\"Update\"></form></body></html>";
server.send(200, "text/html", html);
}
void test2(){
String html = "<html><body><form method=\"post\" action=\"/upload\" enctype=\"multipart/form-data\"><input type=\"file\" name=\"firmware\"><input type=\"submit\" value=\"Update\"></form></body></html>";
server.send(200, "text/html", html);
}
void handlePost() {
String postData = server.arg("plain");
Serial.print("POST data received: ");
Serial.println(postData);
// Do something with the POST data here
server.send(200, "text/plain", "POST request received");
}
void server_init() {
MDNS.begin(host);
server.on("/ota", HTTP_POST, handlePost);
server.on("/test", HTTP_POST, handlePost);
//server.on("/wifi_settings", handle_wifi_settings);
//server.on("/download_data", handle_download_data);
/*handling uploading firmware file */
server.on(
"/update", HTTP_POST, []() {
Serial.println("server.on(/update)");
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
delay(1500);
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
Serial.println("!Update.begin(UPDATE_SIZE_UNKNOWN) \nERROR:");
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
Serial.printf("File received: %s, size: %u\n", upload.filename.c_str(), upload.totalSize);
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
//
Serial.println("Update.write(upload.buf, upload.currentSize) != upload.currentSize)... \nERROR:");
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.print("BEGIN SEVER");
}
void setup() {
Serial.begin(115200);
if (!SPIFFS.begin(true)) {
Serial.println("Error initializing SPIFFS");
return;
}
//WiFi.mode(WIFI_AP);
//WiFi.softAP(SSID, PASSWORD);
WiFi.mode(WIFI_STA);
const char* tempssid="KVAR";
const char* tempassword="kvar123456";
WiFi.begin(tempssid, tempassword);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
//Serial.println(WiFi.softAPIP());
server_init();
//server.on("/", handleRoot);
//server.on("/", test2);
server.on("/ota", HTTP_POST, handlePost);
server.on("/upload", HTTP_POST, handleFileUpload);
//server.begin();
}
void loop() {
server.handleClient();
}
how should i resolve the Wrong magic byte issue