initiate MQTT interface
This commit is contained in:
parent
f2ddf22a5a
commit
43ff899cde
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
#specific files
|
#specific files
|
||||||
secret.h
|
secret.h
|
||||||
|
build/
|
||||||
|
|
||||||
# ---> C++
|
# ---> C++
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
|
@ -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,40 +9,32 @@
|
|||||||
#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() {
|
||||||
delay(10);
|
delay(10);
|
||||||
@ -158,16 +151,28 @@ 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) {
|
||||||
goToDeepSleep();
|
goToDeepSleep();
|
||||||
|
32
ota.cpp
Normal file
32
ota.cpp
Normal 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>
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user