I'm attempting to update my ESP32 from my server, but I'm facing some difficulties with the example code I've been using. The process seems to stall at a certain point, and when I check the serial output, I see the following:
Connecting...
Connected to WiFi
Downloading firmware...
HTTP_CODE_OK
http.getStreamPtr
Uploading firmware.
i run export compiled binary in arduino ide and that file i add to my server
#include <WiFi.h>
#include <HTTPClient.h>
#include <Update.h>
void OTA() {
const char* ssid = "test";
const char* password = "123456789";
const char* firmwareUrl = "https://example.com/FW/1.bin";
const unsigned long timeoutDuration = 30000; // Timeout duration in milliseconds (30 seconds)
Serial.println("Starting OTA update...");
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 10) {
delay(1000);
Serial.println("Connecting...");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi");
HTTPClient http;
http.begin(firmwareUrl);
Serial.println("Downloading firmware...");
unsigned long startTime = millis(); // Record start time
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
Serial.println("HTTP_CODE_OK");
WiFiClient* stream = http.getStreamPtr();
Serial.println("http.getStreamPtr");
if (Update.begin()) {
Serial.println("Uploading firmware...");
size_t written = 0;
unsigned long uploadStartTime = millis(); // Record upload start time
do {
written = Update.writeStream(*stream);
if (written > 0) {
Serial.print("Bytes written: ");
Serial.println(written);
}
} while (written > 0 && millis() - uploadStartTime < timeoutDuration); // Continue writing until timeout or all data is written
if (written == http.getSize()) {
Serial.println("Firmware upload successful");
} else {
Serial.println("Firmware upload failed");
}
if (Update.end()) {
Serial.println("OTA update successful");
ESP.restart();
} else {
Serial.println("OTA update failed");
}
} else {
Serial.println("Could not begin firmware update");
}
} else {
Serial.println("Failed to download firmware");
}
http.end();
} else {
Serial.println("Failed to connect to WiFi");
}
}
void setup() {
Serial.begin(9600);
OTA(); // Call OTA function to initiate the update process
}
void loop() {
Serial.println("V1 - test");
delay(1000);
}