initiate MQTT interface
This commit is contained in:
parent
f2ddf22a5a
commit
43ff899cde
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
#specific files
|
||||
secret.h
|
||||
build/
|
||||
|
||||
# ---> C++
|
||||
# Prerequisites
|
||||
|
@ -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 onOTAEnd(bool success) {
|
||||
// Log when OTA has finished
|
||||
if (success) {
|
||||
Serial.println("OTA update finished successfully!");
|
||||
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 {
|
||||
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
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
@ -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
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