From f2ddf22a5a0badfa5cd8d36189329bdd0f1d6bbc Mon Sep 17 00:00:00 2001 From: Nagoydede Date: Sun, 13 Apr 2025 23:38:36 +0200 Subject: [PATCH] Add OTA through ElegantOTA lite. OTA is accessible via IP_ADRESS/update --- .theia/launch.json | 8 ++++++ linky_tic.ino | 68 ++++++++++++++++++++++++++-------------------- secret-generic.h | 10 ++++++- 3 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 .theia/launch.json diff --git a/.theia/launch.json b/.theia/launch.json new file mode 100644 index 0000000..9a49ac9 --- /dev/null +++ b/.theia/launch.json @@ -0,0 +1,8 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + "version": "0.2.0", + "configurations": [ + + ] +} diff --git a/linky_tic.ino b/linky_tic.ino index e01497e..3018aac 100644 --- a/linky_tic.ino +++ b/linky_tic.ino @@ -2,21 +2,45 @@ #include "secret.h" #include "tic.h" + #include #include #include #include +#include // Activer le Wi-Fi #define WIFI_ENABLE // Initialiser le serveur web si le Wi-Fi est activé -#ifdef WIFI_ENABLE ESP8266WebServer server(HTTP_PORT); -#endif -// Durée de sommeil en microsecondes (par exemple, 5 secondes) -const int sleepDuration = 1 * 1000000; +unsigned long ota_progress_millis = 0; + +void onOTAStart() { + // Log when OTA has started + Serial.println("OTA update started!"); + // +} + +void onOTAProgress(size_t current, size_t final) { + // Log every 1 second + if (millis() - ota_progress_millis > 1000) { + ota_progress_millis = millis(); + Serial.printf("OTA Progress Current: %u bytes, Final: %u bytes\n", current, final); + } +} + +void onOTAEnd(bool success) { + // Log when OTA has finished + if (success) { + Serial.println("OTA update finished successfully!"); + } else { + Serial.println("There was an error during OTA update!"); + } + // +} + // Fonction pour configurer et connecter au réseau Wi-Fi void setup_wifi() { @@ -114,25 +138,6 @@ void setup_serial() { #endif } -// Fonction pour mettre l'ESP8266 en mode deep sleep -void goToDeepSleep() { - DebugPort.println("Going to deep sleep..."); - ESP.deepSleep(sleepDuration); -} - -void goToLightSleep() { - DebugPort.println("Going to light sleep..."); - wifi_set_opmode(NULL_MODE); // Turn off Wi-Fi - wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // Set light sleep mode - wifi_fpm_open(); - //gpio_pin_wakeup_enable(GPIO_ID_PIN(D1), GPIO_PIN_INTR_LOLEVEL); // Enable wakeup on GPIO - //wifi_fpm_do_wakeup_gpio(true); - //wifi_fpm_set_wakeup_gpio_mode(WAKEUP_GPIO_MODE_KEEP_HIGH); - wifi_fpm_set_wakeup_cb(loop); - wifi_fpm_do_sleep(sleepDuration); // Sleep - delay(20); // Short delay to allow the ESP8266 to enter sleep mode -} - // Fonction d'initialisation principale void setup() { @@ -143,6 +148,12 @@ void setup() { // Configurer les routes du serveur restServerRouting(); + ElegantOTA.begin(&server); + // ElegantOTA callbacks + ElegantOTA.onStart(onOTAStart); + ElegantOTA.onProgress(onOTAProgress); + ElegantOTA.onEnd(onOTAEnd); + // Démarrer le serveur HTTP DebugPort.println("Start HTTP server"); server.begin(); @@ -152,14 +163,13 @@ void setup() { // Boucle principale void loop() { - + ElegantOTA.loop(); server.handleClient(); readTicPort(); - delay(1000); - // Si aucune requête n'est en cours, mettre l'ESP8266 en mode deep sleep /* - if (server.client().available() == 0) { - goToLightSleep(); - }*/ + // Si aucune requête n'est en cours, mettre l'ESP8266 en mode deep sleep + if (server.client().available() == 0) { + goToDeepSleep(); + }*/ } \ No newline at end of file diff --git a/secret-generic.h b/secret-generic.h index ec7d542..51066c2 100644 --- a/secret-generic.h +++ b/secret-generic.h @@ -15,4 +15,12 @@ #endif const char *ssid = STASSID; -const char *passPhrase = STAPSK; \ No newline at end of file +const char *passPhrase = STAPSK; + +// MQTT Broker settings +const char *mqtt_broker = "broker.emqx.io"; // EMQX broker endpoint +const char *mqtt_topic = "emqx/esp8266/led"; // MQTT topic +const char *mqtt_username = "emqx"; // MQTT username for authentication +const char *mqtt_password = "public"; // MQTT password for authentication +const int mqtt_port = 1883; // MQTT port (TCP) +const char *mqtt_clientId = "tic_client-" \ No newline at end of file