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
secret.h
build/
# ---> C++
# Prerequisites

View File

@ -1,6 +1,7 @@
#include "serial.h"
#include "secret.h"
#include "tic.h"
#include "ota.h"
#include <Arduino.h>
@ -8,40 +9,32 @@
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ElegantOTA.h>
#include <PubSubClient.h>
// 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!");
// <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 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!");
}
// <Add your own code here>
}
// 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();

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