Problem with Updating ESP32 Firmware through External Web Server

hi i tried the OTA programming with making a web server and connecting to it using my phone and it worked now I'm trying to do the OTA programming for my esp32 using a external web server. when i run the code the esp32 connects to WiFi but it can't download the firmware and gives an error when i use the link with https it shows 303 error when i change it to http it shows 302 error and when i delete the http from the link it shows this error "failed to parse protocol HTTPClient.cpp:251"
here is my code what is the problem?

#include <WiFi.h>

#include <HTTPClient.h>

#include <Update.h>

void updateFirmware(uint8_t *data, size_t len);

// location of firmware file on external web server

// change to your actual .bin location

#define HOST "www.drive.google.com/uc?export=download&id=1CGPNlpRMR9VKpC6HS3vWJtr4JCS6gB6Q"

HTTPClient client;

// Your WiFi credentials

const char* ssid = "abdc";

const char* password = "8765";

// Global variables

int totalLength;       //total size of firmware

int currentLength = 0; //current size of written firmware

void setup() {

  Serial.begin(9600);

  // Start WiFi connection

  WiFi.mode(WIFI_MODE_STA);        

  WiFi.begin(ssid, password);

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

      delay(500);

      Serial.print(".");

  }

  Serial.println("");

  Serial.println("WiFi connected");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());

  // Connect to external web server

  client.begin(HOST);

  // Get file, just to check if each reachable

  int resp = client.GET();

  Serial.print("Response: ");

  Serial.println(resp);

  // If file is reachable, start downloading

  if(resp == 200){

      // get length of document (is -1 when Server sends no Content-Length header)

      totalLength = client.getSize();

      // transfer to local variable

      int len = totalLength;

      // this is required to start firmware update process

      Update.begin(UPDATE_SIZE_UNKNOWN);

      Serial.printf("FW Size: %u\n",totalLength);

      // create buffer for read

      uint8_t buff[128] = { 0 };

      // get tcp stream

      WiFiClient * stream = client.getStreamPtr();

      // read all data from server

      Serial.println("Updating firmware...");

      while(client.connected() && (len > 0 || len == -1)) {

           // get available data size

           size_t size = stream->available();

           if(size) {

              // read up to 128 byte

              int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

              // pass to function

              updateFirmware(buff, c);

              if(len > 0) {

                 len -= c;

              }

           }

           delay(1);

      }

  }else{

    Serial.println("Cannot download firmware file. Only HTTP response 200: OK is supported. Double check firmware location #defined in HOST.");

  }

  client.end();

 

}

void loop() {}

// Function to update firmware incrementally

// Buffer is declared to be 128 so chunks of 128 bytes

// from firmware is written to device until server closes

void updateFirmware(uint8_t *data, size_t len){

  Update.write(data, len);

  currentLength += len;

  // Print dots while waiting for update to finish

  Serial.print('.');

  // if current length of written firmware is not equal to total firmware size, repeat

  if(currentLength != totalLength) return;

  Update.end(true);

  Serial.printf("\nUpdate Success, Total Size: %u\nRebooting...\n", currentLength);

  // Restart ESP32 to see changes

  ESP.restart();

}

That the OTA client doesn't expect to be redirected. Services as Google Drive aren't simple web servers but web applications that expect a browser to be used for access. Put your firmware to a simple webserver and it probably works.

do you know any free simple web server that provides direct link ?

Apache and Nginx are two free web servers that can server any content. You can install them on your PC as they are available for all common operating systems.

1 Like

i used another server and the problem was solved and i did OTA update i used this site https://uupload.ir/

Did you change the Arduino's code?

i only changed the define host part.

1 Like

What ESP32 did you use it?

i used esp32WROOM

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.