#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* fimwareInfo = "https://raw.githubusercontent.comcotestatnt/esp-fs-webserver/main/examples/remoteOTA/version-esp32.json";
const char* actualVersion = "1.0.0";
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
delay(10);
static uint32_t checkTime;
if (millis() - checkTime > 30000) {
checkTime = millis();
WiFiClientSecure *client = new WiFiClientSecure;
HTTPClient http;
client->setInsecure();
Serial.print("[HTTP] begin...\n");
http.begin(*client, fimwareInfo); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// Parse the payload in order to get version and url
DynamicJsonDocument doc(512);
DeserializationError err = deserializeJson(doc, payload);
if (err) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(err.f_str());
}
String version = doc["version"].as<String>();
String firmware_url = doc["raw_url"].as<String>();
Serial.println(version);
Serial.println(firmware_url);
if(!version.equals(actualVersion)) {
Serial.println("DO UPDATE!!!");
}
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
}