使用 React-native 应用程序的 Esp32 OTA 更新不起作用

问题描述 投票:0回答:1

我正在开发一个基于 React Native 的 OTA 应用程序更新程序。 文件由 fs 搜索,然后通过 http 上传到 esp32,其中 esp32 处于 AP 模式,通过网络服务器接收文件。更新过程中,文件字节成功上传4次然后出现问题。

[错误日志图片附在此处enter image description 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();
}

我应该如何解决错误的魔法字节问题

react-native esp32 arduino-esp8266 esp8266wifi
1个回答
0
投票

在Update.begin函数中尝试使用参数

Update.begin(UPDATE_SIZE_UNKNOWN, U_SPIFFS);
© www.soinside.com 2019 - 2024. All rights reserved.