mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +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,14 +17,14 @@ 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" : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkBrokerParams(MqttBroker broker) {
|
bool checkBrokerParams(MqttBroker broker) {
|
||||||
return !jsonReadStr(configSetupJson, getParamName("Server", broker)).isEmpty();
|
return !jsonReadStr(configSetupJson, getParamName("Server", broker)).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqttInit() {
|
void mqttInit() {
|
||||||
@@ -41,15 +42,25 @@ 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()) {
|
||||||
SerialPrint("I", "MQTT", "OK");
|
if (activeBroker == MQTT_RESERVE) {
|
||||||
setLedStatus(LED_OFF);
|
// при 20 cекундных интервалах проверки, каждые 100 сек
|
||||||
}
|
if (fallbackCounter++ > 5) {
|
||||||
else {
|
if (checkBrokerParams(MQTT_PRIMARY)) {
|
||||||
|
activeBroker = MQTT_PRIMARY;
|
||||||
|
fallbackCounter = 0;
|
||||||
|
mqttReconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SerialPrint("I", "MQTT", "OK");
|
||||||
|
setLedStatus(LED_OFF);
|
||||||
|
}
|
||||||
|
} 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) {
|
||||||
if (checkBrokerParams(MQTT_RESERVE)) {
|
if (checkBrokerParams(MQTT_RESERVE)) {
|
||||||
activeBroker = MQTT_RESERVE;
|
activeBroker = MQTT_RESERVE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
activeBroker = MQTT_PRIMARY;
|
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();
|
||||||
@@ -105,7 +115,7 @@ void mqttSubscribe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool readBrokerParams(MqttBroker broker) {
|
bool readBrokerParams(MqttBroker broker) {
|
||||||
if(!checkBrokerParams(broker)) {
|
if (!checkBrokerParams(broker)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
mqttServer = jsonReadStr(configSetupJson, getParamName("Server", broker));
|
mqttServer = jsonReadStr(configSetupJson, getParamName("Server", broker));
|
||||||
@@ -117,7 +127,7 @@ bool readBrokerParams(MqttBroker broker) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean mqttConnect() {
|
boolean mqttConnect() {
|
||||||
SerialPrint("I", "MQTT", String("use ") + (activeBroker == MQTT_PRIMARY? "primary": "reserve"));
|
SerialPrint("I", "MQTT", String("use ") + (activeBroker == MQTT_PRIMARY ? "primary" : "reserve"));
|
||||||
if (!checkBrokerParams(activeBroker)) {
|
if (!checkBrokerParams(activeBroker)) {
|
||||||
SerialPrint("E", "MQTT", "empty broker address");
|
SerialPrint("E", "MQTT", "empty broker address");
|
||||||
return false;
|
return false;
|
||||||
@@ -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,10 +175,9 @@ 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;
|
||||||
order += key;
|
order += key;
|
||||||
order += " ";
|
order += " ";
|
||||||
order += payloadStr;
|
order += payloadStr;
|
||||||
@@ -326,38 +334,38 @@ void publishState() {
|
|||||||
|
|
||||||
const String getStateStr() {
|
const String getStateStr() {
|
||||||
switch (mqtt.state()) {
|
switch (mqtt.state()) {
|
||||||
case -4:
|
case -4:
|
||||||
return F("no respond");
|
return F("no respond");
|
||||||
break;
|
break;
|
||||||
case -3:
|
case -3:
|
||||||
return F("connection was broken");
|
return F("connection was broken");
|
||||||
break;
|
break;
|
||||||
case -2:
|
case -2:
|
||||||
return F("connection failed");
|
return F("connection failed");
|
||||||
break;
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
return F("client disconnected");
|
return F("client disconnected");
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
return F("client connected");
|
return F("client connected");
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
return F("doesn't support the requested version");
|
return F("doesn't support the requested version");
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
return F("rejected the client identifier");
|
return F("rejected the client identifier");
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
return F("unable to accept the connection");
|
return F("unable to accept the connection");
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
return F("wrong username/password");
|
return F("wrong username/password");
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
return F("not authorized to connect");
|
return F("not authorized to connect");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return F("unspecified");
|
return F("unspecified");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user