0

We're a bunch of noobs trying to develop an ESP32 program that could be updated OTA with ESPHttpUpdate. The code we're using is the basic:

 if((WiFi.status() == WL_CONNECTED)) {
    t_httpUpdate_return ret = ESPhttpUpdate.update("https://url.to/my.bin");

    switch(ret) {
        case HTTP_UPDATE_FAILED:
            Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
            break;

        case HTTP_UPDATE_NO_UPDATES:
            Serial.println("HTTP_UPDATE_NO_UPDATES");
            break;

        case HTTP_UPDATE_OK:
            Serial.println("HTTP_UPDATE_OK");
            break;
    }
}

In theory, it works. The program connects to WiFi, connects to the web server, downloads the new bin and flashes it successfully... Only in a loop. Once it downloads the new firmware, it flashes it, reboots the device, and starts all over again. I guess it's a simple question of getting it to recognize if the bin on the server is newer than the one on the device - but I can't seem to figure out how to do that :) Any pointers would be appreciated - Believe me I've googled a lot :)

Thank you in advance, Seb

2 Answers2

0

What you could do in it's firmware, is that it points to the next ''update'' link.

That file is only on the server when there's an update. When you update the firmware, just change the link to point to my2.bin etc.

So each firmware points to the next update (if available).

Example:
Current Firmware: my.bin
Points to: https://url.to/my1.bin

Current Firmware: my1.bin
Points to: https://url.to/my2.bin

etc

Blobtech
  • 11
  • 1
  • Seems like it would work, but since we'll be managing large numbers of these devices, it would probably be unpractical - we'd have to wait for all the devices to load the new firmware before removing it (to prevent the reboot cycle), so many would not be fully operational in the meantime. But thank you anyway! :) - Edit: nevermind, I thought about it one more second and I think it *would* work - once the device loads the new firmware, it's looking for a new, yet unexistent firmware so it would not reset. I'm gonna test it out :) – Sebastian Pelan May 19 '20 at 15:03
  • If the devices can send their firmware id to the server, then you can track how many are running on old firmware before removing it. Good luck! – Blobtech May 19 '20 at 15:08
0
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>

#define APSSID "<YOUR-SSID>"
#define APPSK  "<YOUR-PASSWD>"
 
ESP8266WiFiMulti WiFiMulti;
 
int FIRMWARE_VERSION = 1; // don't forget to Change this !!!. And save the .BIN to FirmwareV1.bin, FirmwareV2.bin etc...

void checkUpdate(int firmToCheck)
{
    WiFiClient client;
    ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); // Optional
    ESPhttpUpdate.rebootOnUpdate(false); // Manual reboot after Update
    
    String urlUPD = "http://192.168.1.100:8080/FirmwareV" + String(firmToCheck + 1) + ".bin";

    t_httpUpdate_return ret = ESPhttpUpdate.update(client, urlUPD);
    if (ret == HTTP_UPDATE_OK){
        Serial.println("HTTP_UPDATE_OK. Reboot");
        delay(1000); // Wait a second and restart
        ESP.restart();
    }   
}
void setup(){
 
    Serial.begin(115200);
    Serial.println();

    // Classic wifi connexion here. More or less complicated
    WiFi.mode(WIFI_STA);
    WiFiMulti.addAP(APSSID, APPSK);
    delay(5000);
 
    Serial.print("Firmware version : ");
    Serial.println(FIRMWARE_VERSION);

    checkUpdate(FIRMWARE_VERSION);
    // if not Server found, or not file found etc... move next
}

 
void loop() {
    // normal stuff here !!!
    //of course any time, we can do something like this : if (blablala) checkUpdate(FIRMWARE_VERSION);
}

@ miken32

Sorry.

This is the KISS version of "Updating a ESP8266 or ESP32 from a Web Server" and solves the problem of devices that constantly reboot after an update: the version number indicated in the code is the same as the file on the Server (The Version number is concatenated with the file name) and the code searches at each reboot the file of the "next version". If present, firmware update and reboot. If it is absent, the code continues normally. Ex: for "int FIRMWARE_VERSION = 1;" you must place on the server a file which will be called for example "Update_my_ESPV2.bin", concatenation of "Update_my_ESPV" + the next FIRMWARE_VERSION + ".bin" In this new file "Update_my_ESPV2.bin", int FIRMWARE_VERSION is set to 2. At the next reboot, this firmware will search the file "Update_my_ESPV3.bin" on the Server... etc etc....

I use this process without any trouble with a Standalone Http Server (Rebex Tiny WebServer). http://192.168.1.100:8080/ is the IP and Port of the Server. (For example). It can be anywhere. Local or remote network

On the web I've seen dozens of answers about updating via HTTP, all more complicated than that. Some solutions used a database.

This solution is much simpler , and much shorter code. Of couse, we can do better from a security point of view, but this is suitable for most uses.

Sorry for my English. I'm French ;)

ZJP

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Peter ZJP
  • 1
  • 1
  • Please provide some explanation as to what this code does and how it answers the question. Blocks of code without explanation are not helpful for future visitors. – miken32 Aug 23 '22 at 16:10