/*
// JWSmythe autoupdater test 1.0
// (c) JWSmythe 220230920
// http basic pars from: https://RandomNerdTutorials.com/esp32-http-get-post-arduino/
// OTA over http from: https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPUpdate/examples/httpUpdate/httpUpdate.ino
//
// BTW, if it hangs at "Connecting...", reload the sim. It has problems
// reaching the Internet sometimes. That's a Wokwi problem, not an ESP* problem.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String version_url = "http://ota.hacker4hire.com/current_version.txt";
String firmware_url = "http://ota.hacker4hire.com/firmware";
String newbin_url = "";
unsigned long software_version = 2023092001; // yyyymmddhhmm
unsigned long update_version = 0;
String http_string = ""; // Holding variable.
unsigned long lastTime = 0;
unsigned long timerDelay = 60;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
/* *** BEGIN These are used by the updater below *** */
void update_started() {
Serial.println("CALLBACK: HTTP update process started");
}
void update_finished() {
Serial.println("CALLBACK: HTTP update process finished");
}
void update_progress(int cur, int total) {
Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...\n", cur, total);
}
void update_error(int err) {
Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err);
}
/* *** END These are used by the updater below *** */
void loop() {
if ((millis() - lastTime) > timerDelay) {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin(version_url.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http_string = http.getString();
update_version = http_string.toInt();
Serial.print("Current FW version: "); Serial.println(software_version);
Serial.print("Server FW version: "); Serial.println(update_version);
// ***** BEGIN UPDATER CODE *****
if (software_version != update_version){
newbin_url = firmware_url + "." + update_version + ".bin";
Serial.print("New firmware version available. Updating to "); Serial.print(newbin_url); Serial.print("\n");
httpUpdate.onStart(update_started);
httpUpdate.onEnd(update_finished);
httpUpdate.onProgress(update_progress);
httpUpdate.onError(update_error);
//t_httpUpdate_return ret = httpUpdate.update(client, "newbin_url");
t_httpUpdate_return ret = httpUpdate.update(http, newbin_url);
// Or:
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "/file.bin");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.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;
}
}
// ***** END UPDATER CODE *****
}else{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}else{
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}