initiate MQTT interface

This commit is contained in:
nago 2025-04-14 22:28:49 +02:00
parent f2ddf22a5a
commit 43ff899cde
4 changed files with 76 additions and 26 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
#specific files #specific files
secret.h secret.h
build/
# ---> C++ # ---> C++
# Prerequisites # Prerequisites

View File

@ -1,6 +1,7 @@
#include "serial.h" #include "serial.h"
#include "secret.h" #include "secret.h"
#include "tic.h" #include "tic.h"
#include "ota.h"
#include <Arduino.h> #include <Arduino.h>
@ -8,39 +9,31 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <ElegantOTA.h> #include <ElegantOTA.h>
#include <PubSubClient.h>
// Activer le Wi-Fi // Activer le Wi-Fi
#define WIFI_ENABLE #define WIFI_ENABLE
// Initialiser le serveur web si le Wi-Fi est activé // Initialiser le serveur web si le Wi-Fi est activé
ESP8266WebServer server(HTTP_PORT); ESP8266WebServer server(HTTP_PORT);
WiFiClient espClient;
PubSubClient mqttclient(espClient);
unsigned long ota_progress_millis = 0; void mqttConnect() {
// Loop until we're reconnected
void onOTAStart() { while (!mqttclient.connected()) {
// Log when OTA has started // Create a random client ID
Serial.println("OTA update started!"); String clientId = MQTT_CLIENTID;
// <Add your own code here> clientId += String(random(0xffff), HEX);
} // Attempt to connect
if (mqttclient.connect(clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) {
void onOTAProgress(size_t current, size_t final) { // Once connected, publish an announcement...
// Log every 1 second mqttclient.publish(MQTT_TOPIC, "MQTT TIC interface online");
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 { } else {
Serial.println("There was an error during OTA update!"); delay(1000);
}
} }
// <Add your own code here>
} }
// Fonction pour configurer et connecter au réseau Wi-Fi // Fonction pour configurer et connecter au réseau Wi-Fi
void setup_wifi() { void setup_wifi() {
@ -158,15 +151,27 @@ void setup() {
DebugPort.println("Start HTTP server"); DebugPort.println("Start HTTP server");
server.begin(); server.begin();
DebugPort.println("HTTP server started"); DebugPort.println("HTTP server started");
//Démarrer le serveur MQTT
mqttclient.setServer(MQTT_SERVER, MQTT_PORT);
#endif #endif
} }
// Boucle principale // Boucle principale
void loop() { void loop() {
ElegantOTA.loop(); ElegantOTA.loop();
server.handleClient(); server.handleClient();
readTicPort(); readTicPort();
//Interface MQTT
if (!mqttclient.connected()) {
mqttConnect();
}
mqttclient.loop();
/* /*
// Si aucune requête n'est en cours, mettre l'ESP8266 en mode deep sleep // Si aucune requête n'est en cours, mettre l'ESP8266 en mode deep sleep
if (server.client().available() == 0) { if (server.client().available() == 0) {

32
ota.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "ota.h"
#include "serial.h"
#include <Arduino.h>
#include <stddef.h>
unsigned long ota_progress_millis = 0;
void onOTAStart() {
// Log when OTA has started
DebugPort.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();
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!");
}
// <Add your own code here>
}

12
ota.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef OTA
#define OTA
#include <stddef.h>
void onOTAStart();
void onOTAProgress(size_t current, size_t final);
void onOTAEnd(bool success);
#endif