0

I have many ESP32 devices distributed to my client but not easy to update the code manually. The best is to let let the Arduino to download(by instruction) the .bin file through http and update bythemself which is using OTA. Can any one give me code axample of

  1. Downloading the .bin file through http (NOT ESP AS WEB SERVER please)
  2. Extract the .bin file and update the code

Thank you

1
  • the HttpUpdate library Commented Jan 11, 2024 at 5:16

2 Answers 2

1

Here are some links to tutorials that answer yours need.

Some Renzo Mischianti's tutorials.

Another example could be the AutoConnect library for ESP8266/ESP32 In the section "OTA updates" -> "OTA using Update Server" https://hieromon.github.io/AutoConnect/otaupdate.html

And finally there are the Espressif Arduino-esp32 update library examples: https://github.com/espressif/arduino-esp32/tree/master/libraries/Update/examples

Sign up to request clarification or add additional context in comments.

Comments

0

I have found the best answer here

#include <WiFi.h>
#include <HTTPClient.h>
#include <Update.h>

const char* ssid = "CGSB_2.4G";
const char* password = "xxx@";
const char* otaServer = "domain.com";
const char* otaFilePath = "/rnd/OTAWebUpdaterExampleCodeB.ino.bin";
const int BUTTON_PIN = 2;

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");

  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  Serial.println("Waiting 5 seconds before starting OTA update...");
  delay(5000); // Wait for 5 seconds

  performOTAUpdate();

  // Your main loop code here
}

void performOTAUpdate() {
  Serial.println("Starting OTA update...");

  // Construct the full URL for the firmware binary
  String otaURL = String(otaServer) + otaFilePath;

  // Begin HTTP client
  HTTPClient http;
  http.begin(otaURL);

  // Start the update process
  if (Update.begin(UPDATE_SIZE_UNKNOWN)) {
    Serial.println("Downloading...");

    // Start the download
    int httpCode = http.GET();
    if (httpCode == HTTP_CODE_OK) {
      WiFiClient& stream = http.getStream();
      uint8_t buffer[1024];
      int bytesRead;

      // Write the stream to the Update library in chunks
      while ((bytesRead = stream.readBytes(buffer, sizeof(buffer))) > 0) {
        if (Update.write(buffer, bytesRead) != bytesRead) {
          Serial.println("Error during OTA update. Please try again.");
          Update.end(false); // false parameter indicates a failed update
          return;
        }
      }

      // End the update process
      if (Update.end(true)) {
        Serial.println("OTA update complete. Rebooting...");
        ESP.restart();
      } else {
        Serial.println("Error during OTA update. Please try again.");
        Update.end(false); // false parameter indicates a failed update
      }
    } else {
      Serial.println("Failed to download firmware.");
      Update.end(false); // false parameter indicates a failed update
    }
  } else {
    Serial.println("Failed to start OTA update.");
  }

  // End HTTP client
  http.end();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.