#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