From 43ff899cde72fcb33ac6988b360c046b632c3a73 Mon Sep 17 00:00:00 2001 From: Nagoydede Date: Mon, 14 Apr 2025 22:28:49 +0200 Subject: [PATCH] initiate MQTT interface --- .gitignore | 1 + linky_tic.ino | 57 ++++++++++++++++++++++++++++----------------------- ota.cpp | 32 +++++++++++++++++++++++++++++ ota.h | 12 +++++++++++ 4 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 ota.cpp create mode 100644 ota.h diff --git a/.gitignore b/.gitignore index 63d1e7b..ca85c4e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ #specific files secret.h +build/ # ---> C++ # Prerequisites diff --git a/linky_tic.ino b/linky_tic.ino index 3018aac..d8362f2 100644 --- a/linky_tic.ino +++ b/linky_tic.ino @@ -1,6 +1,7 @@ #include "serial.h" #include "secret.h" #include "tic.h" +#include "ota.h" #include @@ -8,40 +9,32 @@ #include #include #include +#include // Activer le Wi-Fi #define WIFI_ENABLE // Initialiser le serveur web si le Wi-Fi est activé ESP8266WebServer server(HTTP_PORT); +WiFiClient espClient; +PubSubClient mqttclient(espClient); -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 mqttConnect() { + // Loop until we're reconnected + while (!mqttclient.connected()) { + // Create a random client ID + String clientId = MQTT_CLIENTID; + clientId += String(random(0xffff), HEX); + // Attempt to connect + if (mqttclient.connect(clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) { + // Once connected, publish an announcement... + mqttclient.publish(MQTT_TOPIC, "MQTT TIC interface online"); + } else { + delay(1000); + } } } -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() { delay(10); @@ -149,7 +142,7 @@ void setup() { restServerRouting(); ElegantOTA.begin(&server); - // ElegantOTA callbacks + // ElegantOTA callbacks ElegantOTA.onStart(onOTAStart); ElegantOTA.onProgress(onOTAProgress); ElegantOTA.onEnd(onOTAEnd); @@ -158,16 +151,28 @@ void setup() { DebugPort.println("Start HTTP server"); server.begin(); DebugPort.println("HTTP server started"); + + //Démarrer le serveur MQTT + mqttclient.setServer(MQTT_SERVER, MQTT_PORT); + #endif } + // Boucle principale void loop() { ElegantOTA.loop(); server.handleClient(); readTicPort(); -/* +//Interface MQTT + if (!mqttclient.connected()) { + mqttConnect(); + } + mqttclient.loop(); + + + /* // Si aucune requête n'est en cours, mettre l'ESP8266 en mode deep sleep if (server.client().available() == 0) { goToDeepSleep(); diff --git a/ota.cpp b/ota.cpp new file mode 100644 index 0000000..344516b --- /dev/null +++ b/ota.cpp @@ -0,0 +1,32 @@ +#include "ota.h" +#include "serial.h" + +#include +#include + +unsigned long ota_progress_millis = 0; + +void onOTAStart() { + // Log when OTA has started + DebugPort.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(); + DebugPort.printf("OTA Progress Current: %u bytes, Final: %u bytes\n", current, final); + } +} + +void onOTAEnd(bool success) { + // Log when OTA has finished + if (success) { + DebugPort.println("OTA update finished successfully!"); + } else { + DebugPort.println("There was an error during OTA update!"); + } + // +} + diff --git a/ota.h b/ota.h new file mode 100644 index 0000000..69178e3 --- /dev/null +++ b/ota.h @@ -0,0 +1,12 @@ +#ifndef OTA +#define OTA + +#include + + + +void onOTAStart(); +void onOTAProgress(size_t current, size_t final); +void onOTAEnd(bool success); + +#endif \ No newline at end of file