From e60d5eb62fbfa139d45b596266427cec18770801 Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Sat, 19 Dec 2020 20:12:31 +0300 Subject: [PATCH 1/2] mqtt reserve #2 --- platformio.ini | 24 +++++----- src/MqttClient.cpp | 108 ++++++++++++++++++++++++--------------------- 2 files changed, 72 insertions(+), 60 deletions(-) diff --git a/platformio.ini b/platformio.ini index 20165fdf..3d8bb9ec 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 -;============================================================================================================================================= diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 62aab647..f76ff552 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -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,14 +17,14 @@ 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": ""); + return String("mqtt") + param + (broker == MQTT_RESERVE ? "2" : ""); } bool checkBrokerParams(MqttBroker broker) { - return !jsonReadStr(configSetupJson, getParamName("Server", broker)).isEmpty(); + return !jsonReadStr(configSetupJson, getParamName("Server", broker)).isEmpty(); } void mqttInit() { @@ -41,15 +42,25 @@ void mqttInit() { if (WiFi.status() == WL_CONNECTED) { SerialPrint("I", "WIFI", "OK"); if (mqtt.connected()) { - SerialPrint("I", "MQTT", "OK"); - setLedStatus(LED_OFF); - } - else { + 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 { SerialPrint("E", "MQTT", "lost connection"); if (reconnectionCounter++ > 5) { if (activeBroker == MQTT_PRIMARY) { if (checkBrokerParams(MQTT_RESERVE)) { - activeBroker = MQTT_RESERVE; + activeBroker = MQTT_RESERVE; } } else { 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(); @@ -105,7 +115,7 @@ void mqttSubscribe() { } bool readBrokerParams(MqttBroker broker) { - if(!checkBrokerParams(broker)) { + if (!checkBrokerParams(broker)) { return false; } mqttServer = jsonReadStr(configSetupJson, getParamName("Server", broker)); @@ -117,7 +127,7 @@ bool readBrokerParams(MqttBroker broker) { } 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)) { SerialPrint("E", "MQTT", "empty broker address"); return false; @@ -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,10 +175,9 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) { } else if (topicStr.indexOf("control") != -1) { - String key = selectFromMarkerToMarker(topicStr, "/", 3); - String order; + String order; order += key; order += " "; order += payloadStr; @@ -326,38 +334,38 @@ void publishState() { const String getStateStr() { switch (mqtt.state()) { - case -4: - return F("no respond"); - break; - case -3: - return F("connection was broken"); - break; - case -2: - return F("connection failed"); - break; - case -1: - return F("client disconnected"); - break; - case 0: - return F("client connected"); - break; - case 1: - return F("doesn't support the requested version"); - break; - case 2: - return F("rejected the client identifier"); - break; - case 3: - return F("unable to accept the connection"); - break; - case 4: - return F("wrong username/password"); - break; - case 5: - return F("not authorized to connect"); - break; - default: - return F("unspecified"); - break; + case -4: + return F("no respond"); + break; + case -3: + return F("connection was broken"); + break; + case -2: + return F("connection failed"); + break; + case -1: + return F("client disconnected"); + break; + case 0: + return F("client connected"); + break; + case 1: + return F("doesn't support the requested version"); + break; + case 2: + return F("rejected the client identifier"); + break; + case 3: + return F("unable to accept the connection"); + break; + case 4: + return F("wrong username/password"); + break; + case 5: + return F("not authorized to connect"); + break; + default: + return F("unspecified"); + break; } } From 2aa3b56e85e614a2a4d09d3d38206be7fefabb04 Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Sun, 20 Dec 2020 01:24:08 +0300 Subject: [PATCH 2/2] #SPIFFS --- include/Consts.h | 2 +- include/ESP8266.h | 1 - include/FSEditor.h | 8 +++----- include/FileSystem.h | 2 ++ include/Utils/FileHelper.h | 10 ++-------- include/Utils/FileUtils.h | 9 +-------- src/FSEditor.cpp | 11 ++--------- src/ItemsList.cpp | 9 +++++---- src/MqttClient.cpp | 4 ++-- src/UpgradeFirm.cpp | 8 +++++--- src/Utils/FileUtils.cpp | 37 +++++++++++++++++++------------------ src/WebServer.cpp | 14 +++++++------- src/items/vLogging.cpp | 5 ++++- 13 files changed, 53 insertions(+), 67 deletions(-) diff --git a/include/Consts.h b/include/Consts.h index 1a37c29b..1805957a 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -4,7 +4,7 @@ #define FIRMWARE_VERSION 273 #define FLASH_SIZE_1MB true //===========FileSystem============================================================================================================================================== -#define USE_LITTLEFS true +#define USE_LITTLEFS false //================================================================================================================================================================== #define NUM_BUTTONS 6 #define LED_PIN LED_BUILTIN diff --git a/include/ESP8266.h b/include/ESP8266.h index 6296fc81..76abdc10 100644 --- a/include/ESP8266.h +++ b/include/ESP8266.h @@ -5,7 +5,6 @@ #include #include "ESPAsyncTCP.h" #include "ESPAsyncWebServer.h" -#include #include #include #include diff --git a/include/FSEditor.h b/include/FSEditor.h index c037d008..4fcdef02 100644 --- a/include/FSEditor.h +++ b/include/FSEditor.h @@ -1,9 +1,7 @@ #pragma once + #include -//#include -#ifdef ESP8266 -#include -#endif +#include "FileSystem.h" class FSEditor : public AsyncWebHandler { private: @@ -20,7 +18,7 @@ class FSEditor : public AsyncWebHandler { #ifdef ESP32 FSEditor(const fs::FS& fs, const String& username = String(), const String& password = String()); #else - FSEditor(const String& username = String(), const String& password = String(), const fs::FS& fs = LittleFS); + FSEditor(const String& username = String(), const String& password = String(), const fs::FS& fs = FileFS); #endif virtual bool canHandle(AsyncWebServerRequest* request) override final; virtual void handleRequest(AsyncWebServerRequest* request) override final; diff --git a/include/FileSystem.h b/include/FileSystem.h index 813569f7..48706d84 100644 --- a/include/FileSystem.h +++ b/include/FileSystem.h @@ -6,6 +6,8 @@ #define FILE_WRITE "w" #define FILE_APPEND "a" +#include + #if USE_LITTLEFS #include extern FS LittleFS; diff --git a/include/Utils/FileHelper.h b/include/Utils/FileHelper.h index d2691706..b5ba764c 100644 --- a/include/Utils/FileHelper.h +++ b/include/Utils/FileHelper.h @@ -1,13 +1,7 @@ #pragma once #include -//#include "FS.h" -#ifdef ESP32 -#include "LITTLEFS.h" -#define LittleFS LITTLEFS -#endif -#ifdef ESP8266 -#include -#endif + +#include "FileSystem.h" class FileHelper { public: diff --git a/include/Utils/FileUtils.h b/include/Utils/FileUtils.h index cd51532f..3c33557e 100644 --- a/include/Utils/FileUtils.h +++ b/include/Utils/FileUtils.h @@ -1,14 +1,7 @@ #pragma once #include #include "Consts.h" -//#include "FS.h" -#ifdef ESP32 -#include "LITTLEFS.h" -#define LittleFS LITTLEFS -#endif -#ifdef ESP8266 -#include -#endif +#include "FileSystem.h" /* * Инициализация ФС diff --git a/src/FSEditor.cpp b/src/FSEditor.cpp index 5f33d075..98be53cd 100644 --- a/src/FSEditor.cpp +++ b/src/FSEditor.cpp @@ -1,17 +1,10 @@ #include "FSEditor.h" -#ifdef ESP32 -#include "LITTLEFS.h" -#define LittleFS LITTLEFS -#endif -#ifdef ESP8266 -#include -#endif +#include "FileSystem.h" #define FS_MAXLENGTH_FILEPATH 32 const char *excludeListFile = "/.exclude.files"; - typedef struct ExcludeListS { char *item; ExcludeListS *next; @@ -239,7 +232,7 @@ void FSEditor::handleRequest(AsyncWebServerRequest *request) { if (request->header("If-Modified-Since").equals(buildTime)) { request->send(304); } else { - AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/edit.htm", "text/html"); + AsyncWebServerResponse *response = request->beginResponse(FileFS, "/edit.htm", "text/html"); // response->addHeader("Content-Encoding", "gzip"); response->addHeader("Last-Modified", buildTime); request->send(response); diff --git a/src/ItemsList.cpp b/src/ItemsList.cpp index aa83a65d..a2f99028 100644 --- a/src/ItemsList.cpp +++ b/src/ItemsList.cpp @@ -1,5 +1,6 @@ #include "ItemsList.h" +#include "FileSystem.h" #include "Class/NotAsync.h" #include "Init.h" #include "Utils/StringUtils.h" @@ -25,7 +26,7 @@ void itemsListInit() { void addItem2(String param) { int num = selectToMarker(param, "-").toInt(); - File configFile = LittleFS.open("/items/items.txt", "r"); + File configFile = FileFS.open("/items/items.txt", "r"); if (!configFile) { return; } @@ -62,7 +63,7 @@ void addItem2(String param) { void addPreset2(int num) { - File configFile = LittleFS.open("/presets/presets.c.txt", "r"); + File configFile = FileFS.open("/presets/presets.c.txt", "r"); if (!configFile) { return; } @@ -85,7 +86,7 @@ void addPreset2(int num) { configFile.close(); addFile(DEVICE_CONFIG_FILE, config); - File scenFile = LittleFS.open("/presets/presets.s.txt", "r"); + File scenFile = FileFS.open("/presets/presets.s.txt", "r"); if (!scenFile) { return; } @@ -160,7 +161,7 @@ uint8_t getFreePinAnalog() { } void delChoosingItems() { - File configFile = LittleFS.open("/" + String(DEVICE_CONFIG_FILE), "r"); + File configFile = FileFS.open("/" + String(DEVICE_CONFIG_FILE), "r"); if (!configFile) { return; } diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index f76ff552..610957f1 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -1,6 +1,5 @@ #include "MqttClient.h" #include "BufferExecute.h" -#include #include "items/vLogging.h" #include "Class/NotAsync.h" #include "Global.h" @@ -8,7 +7,8 @@ enum MqttBroker { MQTT_PRIMARY, MQTT_RESERVE }; -MqttBroker activeBroker = MQTT_PRIMARY; + +MqttBroker activeBroker = MQTT_PRIMARY; String mqttPrefix; String mqttRootDevice; diff --git a/src/UpgradeFirm.cpp b/src/UpgradeFirm.cpp index 21e835eb..b3f57051 100644 --- a/src/UpgradeFirm.cpp +++ b/src/UpgradeFirm.cpp @@ -1,5 +1,7 @@ #include "Upgrade.h" +#include "FileSystem.h" + #include "Class/NotAsync.h" #ifdef ESP8266 #include "ESP8266.h" @@ -92,16 +94,16 @@ void upgrade_firmware(int type) { bool upgradeFS() { WiFiClient wifiClient; bool ret = false; - Serial.println("Start upgrade LittleFS, please wait..."); + Serial.printf("Start upgrade %s, please wait...", FS_NAME); #ifdef ESP8266 ESPhttpUpdate.rebootOnUpdate(false); - t_httpUpdate_return retFS = ESPhttpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp8266/littlefs/littlefs.bin")); + t_httpUpdate_return retFS = ESPhttpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp8266/") + FS_NAME + "/"+ FS_NAME+ ".bin"); #else httpUpdate.rebootOnUpdate(false); HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp32/littlefs/spiffs.bin")); #endif if (retFS == HTTP_UPDATE_OK) { //если FS обновилась успешно - SerialPrint("I", "Update", "LittleFS upgrade done!"); + SerialPrint("I", "Update", "FS upgrade done!"); ret = true; } return ret; diff --git a/src/Utils/FileUtils.cpp b/src/Utils/FileUtils.cpp index 31927eed..dd8b8a0e 100644 --- a/src/Utils/FileUtils.cpp +++ b/src/Utils/FileUtils.cpp @@ -1,15 +1,16 @@ +#include "FileSystem.h" + #include "Utils/FileUtils.h" #include "Utils\SerialPrint.h" #include "Utils/StringUtils.h" - const String filepath(const String& filename) { return filename.startsWith("/") ? filename : "/" + filename; } bool fileSystemInit() { - if (!LittleFS.begin()) { + if (!FileFS.begin()) { SerialPrint("E", F("FS"), F("FS Init ERROR, may be FS was not flashed")); return false; } @@ -19,8 +20,8 @@ bool fileSystemInit() { void removeFile(const String& filename) { String path = filepath(filename); - if (LittleFS.exists(path)) { - if (!LittleFS.remove(path)) { + if (FileFS.exists(path)) { + if (!FileFS.remove(path)) { SerialPrint("I","Files","remove " + path); } } else { @@ -30,7 +31,7 @@ void removeFile(const String& filename) { File seekFile(const String& filename, size_t position) { String path = filepath(filename); - auto file = LittleFS.open(path, "r"); + auto file = FileFS.open(path, "r"); if (!file) { SerialPrint("[E]","Files","open " + path); } @@ -42,7 +43,7 @@ File seekFile(const String& filename, size_t position) { const String readFileString(const String& filename, const String& to_find) { String path = filepath(filename); String res = "failed"; - auto file = LittleFS.open(path, "r"); + auto file = FileFS.open(path, "r"); if (!file) { return "failed"; } @@ -55,7 +56,7 @@ const String readFileString(const String& filename, const String& to_find) { const String addFileLn(const String& filename, const String& str) { String path = filepath(filename); - auto file = LittleFS.open(path, "a"); + auto file = FileFS.open(path, "a"); if (!file) { return "failed"; } @@ -66,7 +67,7 @@ const String addFileLn(const String& filename, const String& str) { const String addFile(const String& filename, const String& str) { String path = filepath(filename); - auto file = LittleFS.open(path, "a"); + auto file = FileFS.open(path, "a"); if (!file) { return "failed"; } @@ -79,19 +80,19 @@ bool copyFile(const String& src, const String& dst, bool overwrite) { String srcPath = filepath(src); String dstPath = filepath(dst); SerialPrint("I","Files","copy " + srcPath + " to " + dstPath); - if (!LittleFS.exists(srcPath)) { + if (!FileFS.exists(srcPath)) { SerialPrint("[E]","Files","not exist: " + srcPath); return false; } - if (LittleFS.exists(dstPath)) { + if (FileFS.exists(dstPath)) { if (!overwrite) { SerialPrint("[E]","Files","already exist: " + dstPath); return false; } - LittleFS.remove(dstPath); + FileFS.remove(dstPath); } - auto srcFile = LittleFS.open(srcPath, "r"); - auto dstFile = LittleFS.open(dstPath, "w"); + auto srcFile = FileFS.open(srcPath, "r"); + auto dstFile = FileFS.open(dstPath, "w"); uint8_t buf[512]; while (srcFile.available()) { @@ -105,7 +106,7 @@ bool copyFile(const String& src, const String& dst, bool overwrite) { const String writeFile(const String& filename, const String& str) { String path = filepath(filename); - auto file = LittleFS.open(path, "w"); + auto file = FileFS.open(path, "w"); if (!file) { return "failed"; } @@ -116,7 +117,7 @@ const String writeFile(const String& filename, const String& str) { const String readFile(const String& filename, size_t max_size) { String path = filepath(filename); - auto file = LittleFS.open(path, "r"); + auto file = FileFS.open(path, "r"); if (!file) { return "failed"; } @@ -132,7 +133,7 @@ const String readFile(const String& filename, size_t max_size) { const String getFileSize(const String filename) { String filepath(filename); - auto file = LittleFS.open(filepath, "r"); + auto file = FileFS.open(filepath, "r"); if (!file) { return "failed"; } @@ -145,13 +146,13 @@ const String getFSSizeInfo() { String res; #ifdef ESP8266 FSInfo info; - if (LittleFS.info(info)) { + if (FileFS.info(info)) { res = prettyBytes(info.usedBytes) + " of " + prettyBytes(info.totalBytes); } else { res = "error"; } #else - res = prettyBytes(LittleFS.usedBytes()) + " of " + prettyBytes(LittleFS.totalBytes()); + res = prettyBytes(FileFS.usedBytes()) + " of " + prettyBytes(FileFS.totalBytes()); #endif return res; } diff --git a/src/WebServer.cpp b/src/WebServer.cpp index c955e9a2..b6db2e19 100644 --- a/src/WebServer.cpp +++ b/src/WebServer.cpp @@ -16,18 +16,18 @@ void init() { String login = jsonReadStr(configSetupJson, "weblogin"); String pass = jsonReadStr(configSetupJson, "webpass"); #ifdef ESP32 - server.addHandler(new FSEditor(LittleFS, login, pass)); + server.addHandler(new FSEditor(FileFS, login, pass)); #else server.addHandler(new FSEditor(login, pass)); #endif - server.serveStatic("/css/", LittleFS, "/css/").setCacheControl("max-age=600"); - server.serveStatic("/js/", LittleFS, "/js/").setCacheControl("max-age=600"); - server.serveStatic("/favicon.ico", LittleFS, "/favicon.ico").setCacheControl("max-age=600"); - server.serveStatic("/icon.jpeg", LittleFS, "/icon.jpeg").setCacheControl("max-age=600"); - server.serveStatic("/edit", LittleFS, "/edit").setCacheControl("max-age=600"); + server.serveStatic("/css/", FileFS, "/css/").setCacheControl("max-age=600"); + server.serveStatic("/js/", FileFS, "/js/").setCacheControl("max-age=600"); + server.serveStatic("/favicon.ico", FileFS, "/favicon.ico").setCacheControl("max-age=600"); + server.serveStatic("/icon.jpeg", FileFS, "/icon.jpeg").setCacheControl("max-age=600"); + server.serveStatic("/edit", FileFS, "/edit").setCacheControl("max-age=600"); - server.serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(login.c_str(), pass.c_str()); + server.serveStatic("/", FileFS, "/").setDefaultFile("index.htm").setAuthentication(login.c_str(), pass.c_str()); server.onNotFound([](AsyncWebServerRequest *request) { SerialPrint("[E]","WebServer","not found:\n" + getRequestInfo(request)); diff --git a/src/items/vLogging.cpp b/src/items/vLogging.cpp index b4cbc6d2..66f71094 100644 --- a/src/items/vLogging.cpp +++ b/src/items/vLogging.cpp @@ -2,6 +2,8 @@ #include +#include "FileSystem.h" + #include "Class/LineParsing.h" #include "Global.h" #include "BufferExecute.h" @@ -146,8 +148,9 @@ void sendLogData(String file, String topic) { } void cleanLogAndData() { + #ifdef ESP8266 - auto dir = LittleFS.openDir("logs"); + auto dir = FileFS.openDir("logs"); while (dir.next()) { String fname = dir.fileName(); SerialPrint("I", "System", fname);