From 0d6d1a7f6c031e8a288f0c3ba5eb80efefb264f1 Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Fri, 18 Dec 2020 04:39:44 +0300 Subject: [PATCH 1/3] FSInfo --- include/Consts.h | 2 +- include/FileSystem.h | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 include/FileSystem.h diff --git a/include/Consts.h b/include/Consts.h index 0bce4c82..8905e172 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -16,7 +16,7 @@ #endif //===========FileSystem============================================================================================================================================== -#define littlefs_on +#define USE_LITTLEFS true //================================================================================================================================================================== #define NUM_BUTTONS 6 #define LED_PIN 2 diff --git a/include/FileSystem.h b/include/FileSystem.h new file mode 100644 index 00000000..09fe0307 --- /dev/null +++ b/include/FileSystem.h @@ -0,0 +1,36 @@ +#pragma once + +#include "Consts.h" + +#define FILE_READ "r" +#define FILE_WRITE "w" +#define FILE_APPEND "a" + +#if USE_LITTLEFS +#include +extern FS LittleFS; +using littlefs_impl::LittleFSConfig; +extern FS *filesystem; +#define FileFS LittleFS +#define FS_NAME "LittleFS" +#else +extern FS *filesystem; +#define FileFS SPIFFS +#define FS_NAME "SPIFFS" +#endif + +/* +* Информация о ФС + size_t totalBytes; // всего + size_t usedBytes; // использовано + size_t maxOpenFiles; // лимит на открые файлы + size_t maxPathLength; // лимит на полное пути + имя файла + + FSInfo buf; + getInfo(buf); + size_t freeBytes = buf.totalBytes - buf.usedBytes; + float freePer = buf.usedBytes / buf.totalBytes * 100; +*/ +bool getInfo(FSInfo& info) { + return FileFS.info(info); +} \ No newline at end of file From 5f9d9ec48eaee1d8ada09094a8ced4ef80f8a60c Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Fri, 18 Dec 2020 04:44:23 +0300 Subject: [PATCH 2/3] Use system defined led --- include/Consts.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/Consts.h b/include/Consts.h index 8905e172..04c9d6fc 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -19,7 +19,7 @@ #define USE_LITTLEFS true //================================================================================================================================================================== #define NUM_BUTTONS 6 -#define LED_PIN 2 +#define LED_PIN LED_BUILTIN //===========MQTT================================================================================================================================================= #define MQTT_RECONNECT_INTERVAL 20000 //==========Telemetry============================================================================================================================================= From eddb2eb289408de789cda5733465160571c2113e Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Fri, 18 Dec 2020 13:43:13 +0300 Subject: [PATCH 3/3] Mqtt Reserve --- src/MqttClient.cpp | 59 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 951d16c4..062d8d36 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -6,8 +6,25 @@ #include "Global.h" #include "Init.h" +enum MqttBroker {MQTT_PRIMARY, MQTT_RESERVE}; +MqttBroker activeBroker = MQTT_PRIMARY; + String mqttPrefix; String mqttRootDevice; +String mqttPass; +String mqttServer; +String mqttUser; +uint16_t mqttPort{0}; +uint16_t reconnectionCounter{0}; +bool primaryExist = false; + +const String getParamName(const char* param, MqttBroker broker) { + return String("mqtt") + param + (broker == MQTT_RESERVE? "2": ""); +} + +bool checkBrokerParams(MqttBroker broker) { + return !jsonReadStr(configSetupJson, getParamName("Server", broker)).isEmpty(); +} void mqttInit() { myNotAsyncActions->add( @@ -29,6 +46,16 @@ void mqttInit() { } else { SerialPrint("E", "MQTT", "lost connection"); + if (reconnectionCounter++ > 5) { + if (activeBroker == MQTT_PRIMARY) { + if (checkBrokerParams(MQTT_RESERVE)) { + activeBroker = MQTT_RESERVE; + } + } else { + activeBroker = MQTT_PRIMARY; + } + reconnectionCounter = 0; + } mqttConnect(); } } @@ -77,25 +104,35 @@ void mqttSubscribe() { } } -boolean mqttConnect() { - SerialPrint("I", "MQTT", "start connection"); - String addr = jsonReadStr(configSetupJson, "mqttServer"); - if (!addr) { - SerialPrint("E", "MQTT", "no broker address"); +bool readBrokerParams(MqttBroker broker) { + if(!checkBrokerParams(broker)) { return false; } - int port = jsonReadInt(configSetupJson, "mqttPort"); - String user = jsonReadStr(configSetupJson, "mqttUser"); - String pass = jsonReadStr(configSetupJson, "mqttPass"); + mqttServer = jsonReadStr(configSetupJson, getParamName("Server", broker)); + mqttPort = jsonReadInt(configSetupJson, getParamName("Port", broker)); + mqttUser = jsonReadStr(configSetupJson, getParamName("User", broker)); + mqttPass = jsonReadStr(configSetupJson, getParamName("Pass", broker)); + + return true; +} + +boolean mqttConnect() { + SerialPrint("I", "MQTT", String("use ") + (activeBroker == MQTT_PRIMARY? "primary": "reserve")); + if (!checkBrokerParams(activeBroker)) { + SerialPrint("E", "MQTT", "empty broker address"); + return false; + } + readBrokerParams(activeBroker); + SerialPrint("I", "MQTT", "start connection"); mqttPrefix = jsonReadStr(configSetupJson, "mqttPrefix"); mqttRootDevice = mqttPrefix + "/" + chipId; - SerialPrint("I", "MQTT", "broker " + addr + ":" + String(port, DEC)); + SerialPrint("I", "MQTT", "broker " + mqttServer + ":" + String(mqttPort, DEC)); SerialPrint("I", "MQTT", "topic " + mqttRootDevice); setLedStatus(LED_FAST); - mqtt.setServer(addr.c_str(), port); + mqtt.setServer(mqttServer.c_str(), mqttPort); bool res = false; if (!mqtt.connected()) { - if (mqtt.connect(chipId.c_str(), user.c_str(), pass.c_str())) { + if (mqtt.connect(chipId.c_str(), mqttUser.c_str(), mqttPass.c_str())) { SerialPrint("I", "MQTT", "connected"); setLedStatus(LED_OFF); mqttSubscribe();