mqtt reserve #2

This commit is contained in:
Yuri Trikoz
2020-12-19 20:12:31 +03:00
parent 6573b2c145
commit e60d5eb62f
2 changed files with 72 additions and 60 deletions

View File

@@ -1,7 +1,16 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[platformio]
default_envs = esp8266_01_1m
;=============================================================================================================================================
default_envs = esp8266
[common_env_data]
lib_deps_external =
bblanchon/ArduinoJson @5.*
@@ -15,7 +24,7 @@ lib_deps_internal =
ESP Async WebServer
GyverFilters
OneWire
;=============================================================================================================================================
[env:esp32]
framework = arduino
board = esp32dev
@@ -32,15 +41,11 @@ upload_speed = 921600
monitor_speed = 115200
board_build.filesystem = littlefs
extra_scripts = ./tools/littlefsbuilder.py
;=============================================================================================================================================
[env:esp8266_01_1m]
framework = arduino
board = nodemcuv2
;board = esp01_1m
;board = esp12e
board_build.ldscript = eagle.flash.1m512.ld
;board_build.filesystem.spiffs_blocksize = 4096
board_build.spiffs_blocksize = 4096
platform = https://github.com/platformio/platform-espressif8266.git
lib_deps =
${common_env_data.lib_deps_external}
@@ -53,7 +58,7 @@ monitor_filters = esp8266_exception_decoder
upload_speed = 921600
monitor_speed = 115200
board_build.filesystem = littlefs
;=============================================================================================================================================
[env:esp8266]
framework = arduino
board = nodemcuv2
@@ -70,4 +75,3 @@ monitor_filters = esp8266_exception_decoder
upload_speed = 921600
monitor_speed = 115200
board_build.filesystem = littlefs
;=============================================================================================================================================

View File

@@ -6,7 +6,8 @@
#include "Global.h"
#include "Init.h"
enum MqttBroker {MQTT_PRIMARY, MQTT_RESERVE};
enum MqttBroker { MQTT_PRIMARY,
MQTT_RESERVE };
MqttBroker activeBroker = MQTT_PRIMARY;
String mqttPrefix;
@@ -16,7 +17,7 @@ String mqttServer;
String mqttUser;
uint16_t mqttPort{0};
uint16_t reconnectionCounter{0};
bool primaryExist = false;
uint16_t fallbackCounter{0};
const String getParamName(const char* param, MqttBroker broker) {
return String("mqtt") + param + (broker == MQTT_RESERVE ? "2" : "");
@@ -41,10 +42,20 @@ void mqttInit() {
if (WiFi.status() == WL_CONNECTED) {
SerialPrint("I", "WIFI", "OK");
if (mqtt.connected()) {
if (activeBroker == MQTT_RESERVE) {
// при 20 cекундных интервалах проверки, каждые 100 сек
if (fallbackCounter++ > 5) {
if (checkBrokerParams(MQTT_PRIMARY)) {
activeBroker = MQTT_PRIMARY;
fallbackCounter = 0;
mqttReconnect();
}
}
} else {
SerialPrint("I", "MQTT", "OK");
setLedStatus(LED_OFF);
}
else {
} else {
SerialPrint("E", "MQTT", "lost connection");
if (reconnectionCounter++ > 5) {
if (activeBroker == MQTT_PRIMARY) {
@@ -58,8 +69,7 @@ void mqttInit() {
}
mqttConnect();
}
}
else {
} else {
SerialPrint("E", "WIFI", "Lost WiFi connection");
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
startAPMode();
@@ -137,8 +147,7 @@ boolean mqttConnect() {
setLedStatus(LED_OFF);
mqttSubscribe();
res = true;
}
else {
} else {
SerialPrint("E", "MQTT", "could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
setLedStatus(LED_FAST);
}
@@ -166,7 +175,6 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
}
else if (topicStr.indexOf("control") != -1) {
String key = selectFromMarkerToMarker(topicStr, "/", 3);
String order;