Merge pull request #69 from IoTManagerProject/alpha

mqtt reserve #2
This commit is contained in:
Dmitry Borisenko
2020-12-20 13:04:06 +01:00
committed by GitHub
14 changed files with 125 additions and 127 deletions

View File

@@ -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

View File

@@ -5,7 +5,6 @@
#include <ESP8266httpUpdate.h>
#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include <LittleFS.h>
#include <SPIFFSEditor.h>
#include <Servo.h>
#include <WiFiUdp.h>

View File

@@ -1,9 +1,7 @@
#pragma once
#include <ESPAsyncWebServer.h>
//#include <FS.h>
#ifdef ESP8266
#include <LittleFS.h>
#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;

View File

@@ -6,6 +6,8 @@
#define FILE_WRITE "w"
#define FILE_APPEND "a"
#include <FS.h>
#if USE_LITTLEFS
#include <LittleFS.h>
extern FS LittleFS;

View File

@@ -1,13 +1,7 @@
#pragma once
#include <Arduino.h>
//#include "FS.h"
#ifdef ESP32
#include "LITTLEFS.h"
#define LittleFS LITTLEFS
#endif
#ifdef ESP8266
#include <LittleFS.h>
#endif
#include "FileSystem.h"
class FileHelper {
public:

View File

@@ -1,14 +1,7 @@
#pragma once
#include <Arduino.h>
#include "Consts.h"
//#include "FS.h"
#ifdef ESP32
#include "LITTLEFS.h"
#define LittleFS LITTLEFS
#endif
#ifdef ESP8266
#include <LittleFS.h>
#endif
#include "FileSystem.h"
/*
* Инициализация ФС

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

@@ -1,17 +1,10 @@
#include "FSEditor.h"
#ifdef ESP32
#include "LITTLEFS.h"
#define LittleFS LITTLEFS
#endif
#ifdef ESP8266
#include <LittleFS.h>
#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);

View File

@@ -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;
}

View File

@@ -1,13 +1,14 @@
#include "MqttClient.h"
#include "BufferExecute.h"
#include <LittleFS.h>
#include "items/vLogging.h"
#include "Class/NotAsync.h"
#include "Global.h"
#include "Init.h"
enum MqttBroker {MQTT_PRIMARY, MQTT_RESERVE};
MqttBroker activeBroker = MQTT_PRIMARY;
enum MqttBroker { MQTT_PRIMARY,
MQTT_RESERVE };
MqttBroker activeBroker = MQTT_PRIMARY;
String mqttPrefix;
String mqttRootDevice;
@@ -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;
}
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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));

View File

@@ -2,6 +2,8 @@
#include <Arduino.h>
#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);