mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 03:49:13 +03:00
mqtt reserve #2
This commit is contained in:
@@ -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]
|
[platformio]
|
||||||
default_envs = esp8266_01_1m
|
default_envs = esp8266
|
||||||
;=============================================================================================================================================
|
|
||||||
[common_env_data]
|
[common_env_data]
|
||||||
lib_deps_external =
|
lib_deps_external =
|
||||||
bblanchon/ArduinoJson @5.*
|
bblanchon/ArduinoJson @5.*
|
||||||
@@ -15,7 +24,7 @@ lib_deps_internal =
|
|||||||
ESP Async WebServer
|
ESP Async WebServer
|
||||||
GyverFilters
|
GyverFilters
|
||||||
OneWire
|
OneWire
|
||||||
;=============================================================================================================================================
|
|
||||||
[env:esp32]
|
[env:esp32]
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
@@ -32,15 +41,11 @@ upload_speed = 921600
|
|||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
extra_scripts = ./tools/littlefsbuilder.py
|
extra_scripts = ./tools/littlefsbuilder.py
|
||||||
;=============================================================================================================================================
|
|
||||||
[env:esp8266_01_1m]
|
[env:esp8266_01_1m]
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board = nodemcuv2
|
board = nodemcuv2
|
||||||
;board = esp01_1m
|
|
||||||
;board = esp12e
|
|
||||||
board_build.ldscript = eagle.flash.1m512.ld
|
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
|
platform = https://github.com/platformio/platform-espressif8266.git
|
||||||
lib_deps =
|
lib_deps =
|
||||||
${common_env_data.lib_deps_external}
|
${common_env_data.lib_deps_external}
|
||||||
@@ -53,7 +58,7 @@ monitor_filters = esp8266_exception_decoder
|
|||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
;=============================================================================================================================================
|
|
||||||
[env:esp8266]
|
[env:esp8266]
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board = nodemcuv2
|
board = nodemcuv2
|
||||||
@@ -70,4 +75,3 @@ monitor_filters = esp8266_exception_decoder
|
|||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
;=============================================================================================================================================
|
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "Init.h"
|
#include "Init.h"
|
||||||
|
|
||||||
enum MqttBroker {MQTT_PRIMARY, MQTT_RESERVE};
|
enum MqttBroker { MQTT_PRIMARY,
|
||||||
|
MQTT_RESERVE };
|
||||||
MqttBroker activeBroker = MQTT_PRIMARY;
|
MqttBroker activeBroker = MQTT_PRIMARY;
|
||||||
|
|
||||||
String mqttPrefix;
|
String mqttPrefix;
|
||||||
@@ -16,7 +17,7 @@ String mqttServer;
|
|||||||
String mqttUser;
|
String mqttUser;
|
||||||
uint16_t mqttPort{0};
|
uint16_t mqttPort{0};
|
||||||
uint16_t reconnectionCounter{0};
|
uint16_t reconnectionCounter{0};
|
||||||
bool primaryExist = false;
|
uint16_t fallbackCounter{0};
|
||||||
|
|
||||||
const String getParamName(const char* param, MqttBroker broker) {
|
const String getParamName(const char* param, MqttBroker broker) {
|
||||||
return String("mqtt") + param + (broker == MQTT_RESERVE ? "2" : "");
|
return String("mqtt") + param + (broker == MQTT_RESERVE ? "2" : "");
|
||||||
@@ -41,10 +42,20 @@ void mqttInit() {
|
|||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
SerialPrint("I", "WIFI", "OK");
|
SerialPrint("I", "WIFI", "OK");
|
||||||
if (mqtt.connected()) {
|
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");
|
SerialPrint("I", "MQTT", "OK");
|
||||||
setLedStatus(LED_OFF);
|
setLedStatus(LED_OFF);
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
SerialPrint("E", "MQTT", "lost connection");
|
SerialPrint("E", "MQTT", "lost connection");
|
||||||
if (reconnectionCounter++ > 5) {
|
if (reconnectionCounter++ > 5) {
|
||||||
if (activeBroker == MQTT_PRIMARY) {
|
if (activeBroker == MQTT_PRIMARY) {
|
||||||
@@ -58,8 +69,7 @@ void mqttInit() {
|
|||||||
}
|
}
|
||||||
mqttConnect();
|
mqttConnect();
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
SerialPrint("E", "WIFI", "Lost WiFi connection");
|
SerialPrint("E", "WIFI", "Lost WiFi connection");
|
||||||
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
||||||
startAPMode();
|
startAPMode();
|
||||||
@@ -137,8 +147,7 @@ boolean mqttConnect() {
|
|||||||
setLedStatus(LED_OFF);
|
setLedStatus(LED_OFF);
|
||||||
mqttSubscribe();
|
mqttSubscribe();
|
||||||
res = true;
|
res = true;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
SerialPrint("E", "MQTT", "could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
|
SerialPrint("E", "MQTT", "could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
|
||||||
setLedStatus(LED_FAST);
|
setLedStatus(LED_FAST);
|
||||||
}
|
}
|
||||||
@@ -166,7 +175,6 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if (topicStr.indexOf("control") != -1) {
|
else if (topicStr.indexOf("control") != -1) {
|
||||||
|
|
||||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||||
|
|
||||||
String order;
|
String order;
|
||||||
|
|||||||
Reference in New Issue
Block a user