Add OTA through ElegantOTA lite. OTA is accessible via IP_ADRESS/update

This commit is contained in:
nago 2025-04-13 23:38:36 +02:00
parent 1180e340fb
commit f2ddf22a5a
3 changed files with 56 additions and 30 deletions

8
.theia/launch.json Normal file
View File

@ -0,0 +1,8 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
"version": "0.2.0",
"configurations": [
]
}

View File

@ -2,21 +2,45 @@
#include "secret.h"
#include "tic.h"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ElegantOTA.h>
// 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!");
// <Add your own code here>
}
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!");
}
// <Add your own code here>
}
// 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();
}*/
}

View File

@ -15,4 +15,12 @@
#endif
const char *ssid = STASSID;
const char *passPhrase = STAPSK;
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-"