mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 03:49:13 +03:00
@@ -3,9 +3,11 @@
|
|||||||
//===========Firmware=============================================================================================================================================
|
//===========Firmware=============================================================================================================================================
|
||||||
#define FIRMWARE_VERSION 273
|
#define FIRMWARE_VERSION 273
|
||||||
//#define FLASH_SIZE_1MB true
|
//#define FLASH_SIZE_1MB true
|
||||||
|
//===========FileSystem==============================================================================================================================================
|
||||||
|
#define USE_LITTLEFS true
|
||||||
//==================================================================================================================================================================
|
//==================================================================================================================================================================
|
||||||
#define NUM_BUTTONS 6
|
#define NUM_BUTTONS 6
|
||||||
#define LED_PIN 2
|
#define LED_PIN LED_BUILTIN
|
||||||
//===========MQTT=================================================================================================================================================
|
//===========MQTT=================================================================================================================================================
|
||||||
#define MQTT_RECONNECT_INTERVAL 20000
|
#define MQTT_RECONNECT_INTERVAL 20000
|
||||||
//==========Telemetry=============================================================================================================================================
|
//==========Telemetry=============================================================================================================================================
|
||||||
|
|||||||
36
include/FileSystem.h
Normal file
36
include/FileSystem.h
Normal file
@@ -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 <LittleFS.h>
|
||||||
|
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);
|
||||||
|
}
|
||||||
@@ -6,8 +6,25 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "Init.h"
|
#include "Init.h"
|
||||||
|
|
||||||
|
enum MqttBroker {MQTT_PRIMARY, MQTT_RESERVE};
|
||||||
|
MqttBroker activeBroker = MQTT_PRIMARY;
|
||||||
|
|
||||||
String mqttPrefix;
|
String mqttPrefix;
|
||||||
String mqttRootDevice;
|
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() {
|
void mqttInit() {
|
||||||
myNotAsyncActions->add(
|
myNotAsyncActions->add(
|
||||||
@@ -29,6 +46,16 @@ void mqttInit() {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
SerialPrint("E", "MQTT", "lost connection");
|
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();
|
mqttConnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,25 +104,35 @@ void mqttSubscribe() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean mqttConnect() {
|
bool readBrokerParams(MqttBroker broker) {
|
||||||
SerialPrint("I", "MQTT", "start connection");
|
if(!checkBrokerParams(broker)) {
|
||||||
String addr = jsonReadStr(configSetupJson, "mqttServer");
|
|
||||||
if (!addr) {
|
|
||||||
SerialPrint("E", "MQTT", "no broker address");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int port = jsonReadInt(configSetupJson, "mqttPort");
|
mqttServer = jsonReadStr(configSetupJson, getParamName("Server", broker));
|
||||||
String user = jsonReadStr(configSetupJson, "mqttUser");
|
mqttPort = jsonReadInt(configSetupJson, getParamName("Port", broker));
|
||||||
String pass = jsonReadStr(configSetupJson, "mqttPass");
|
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");
|
mqttPrefix = jsonReadStr(configSetupJson, "mqttPrefix");
|
||||||
mqttRootDevice = mqttPrefix + "/" + chipId;
|
mqttRootDevice = mqttPrefix + "/" + chipId;
|
||||||
SerialPrint("I", "MQTT", "broker " + addr + ":" + String(port, DEC));
|
SerialPrint("I", "MQTT", "broker " + mqttServer + ":" + String(mqttPort, DEC));
|
||||||
SerialPrint("I", "MQTT", "topic " + mqttRootDevice);
|
SerialPrint("I", "MQTT", "topic " + mqttRootDevice);
|
||||||
setLedStatus(LED_FAST);
|
setLedStatus(LED_FAST);
|
||||||
mqtt.setServer(addr.c_str(), port);
|
mqtt.setServer(mqttServer.c_str(), mqttPort);
|
||||||
bool res = false;
|
bool res = false;
|
||||||
if (!mqtt.connected()) {
|
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");
|
SerialPrint("I", "MQTT", "connected");
|
||||||
setLedStatus(LED_OFF);
|
setLedStatus(LED_OFF);
|
||||||
mqttSubscribe();
|
mqttSubscribe();
|
||||||
|
|||||||
Reference in New Issue
Block a user