mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
ESP8266 persistance/compilation fixes
This commit is contained in:
@@ -2,3 +2,5 @@
|
|||||||
-DCUSTOM_FIRMWARE_MAC=de:ad:be:ef:fe:07
|
-DCUSTOM_FIRMWARE_MAC=de:ad:be:ef:fe:07
|
||||||
-DWiz5100
|
-DWiz5100
|
||||||
#-DPID_DISABLE
|
#-DPID_DISABLE
|
||||||
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
|
|||||||
@@ -45,4 +45,5 @@
|
|||||||
-DFS_STORAGE
|
-DFS_STORAGE
|
||||||
-DFS_PREPARE
|
-DFS_PREPARE
|
||||||
|
|
||||||
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
-D CORS=\"http://lazyhome.ru\"
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
|
|||||||
@@ -11,3 +11,9 @@
|
|||||||
-DARDUINO_OTA_MDNS_DISABLE
|
-DARDUINO_OTA_MDNS_DISABLE
|
||||||
##-DMDNS_ENABLE
|
##-DMDNS_ENABLE
|
||||||
-DMCP23017
|
-DMCP23017
|
||||||
|
|
||||||
|
-DFS_STORAGE
|
||||||
|
-DFS_PREPARE
|
||||||
|
|
||||||
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
|
#-D CORS=\"http://lazyhome.ru\"
|
||||||
|
|||||||
@@ -16,3 +16,6 @@
|
|||||||
|
|
||||||
-DFS_STORAGE
|
-DFS_STORAGE
|
||||||
-DFS_PREPARE
|
-DFS_PREPARE
|
||||||
|
|
||||||
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
@@ -5,3 +5,6 @@
|
|||||||
#-DPID_DISABLE
|
#-DPID_DISABLE
|
||||||
-DARDUINO_OTA_MDNS_DISABLE
|
-DARDUINO_OTA_MDNS_DISABLE
|
||||||
-DMDNS_ENABLE
|
-DMDNS_ENABLE
|
||||||
|
|
||||||
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
|
|||||||
@@ -5,3 +5,6 @@
|
|||||||
#-DPID_DISABLE
|
#-DPID_DISABLE
|
||||||
-DARDUINO_OTA_MDNS_DISABLE
|
-DARDUINO_OTA_MDNS_DISABLE
|
||||||
-DMDNS_ENABLE
|
-DMDNS_ENABLE
|
||||||
|
|
||||||
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
@@ -18,3 +18,5 @@
|
|||||||
-DOTA
|
-DOTA
|
||||||
-DARDUINO_OTA_MDNS_DISABLE
|
-DARDUINO_OTA_MDNS_DISABLE
|
||||||
#-DMDNS_ENABLE
|
#-DMDNS_ENABLE
|
||||||
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
@@ -18,7 +18,9 @@
|
|||||||
#-DWiz5100
|
#-DWiz5100
|
||||||
-DARDUINO_OTA_MDNS_DISABLE
|
-DARDUINO_OTA_MDNS_DISABLE
|
||||||
#-DMDNS_ENABLE
|
#-DMDNS_ENABLE
|
||||||
|
|
||||||
-DRESTART_LAN_ON_MQTT_ERRORS
|
-DRESTART_LAN_ON_MQTT_ERRORS
|
||||||
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
|
|
||||||
# Example of UARTBRIDGE configuration
|
# Example of UARTBRIDGE configuration
|
||||||
#-DUARTBRIDGE_ENABLE
|
#-DUARTBRIDGE_ENABLE
|
||||||
|
|||||||
@@ -13,3 +13,4 @@
|
|||||||
-DMBUS_DISABLE
|
-DMBUS_DISABLE
|
||||||
-DPID_DISABLE
|
-DPID_DISABLE
|
||||||
#-DMCP23017
|
#-DMCP23017
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ return res;
|
|||||||
int systemConfig::openStream(char mode)
|
int systemConfig::openStream(char mode)
|
||||||
{
|
{
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
stream->open("/config.bin",mode);
|
return stream->open("/config.bin",mode);
|
||||||
#else
|
#else
|
||||||
stream->open(FN_CONFIG_BIN,mode);
|
return stream->open(FN_CONFIG_BIN,mode);
|
||||||
#endif
|
#endif
|
||||||
//stream->setSize(SYSCONF_SIZE);
|
//stream->setSize(SYSCONF_SIZE);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266) && !defined(FS_STORAGE)
|
||||||
#include <ESP_EEPROM.h>
|
#include <ESP_EEPROM.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -53,24 +53,31 @@ NRFFlashStorage EEPROM;
|
|||||||
int flashStream::open(String _filename, char mode)
|
int flashStream::open(String _filename, char mode)
|
||||||
{
|
{
|
||||||
char modestr[4];
|
char modestr[4];
|
||||||
modestr[0]=mode; modestr[1]='b'; modestr[2]='+'; modestr[3]='\0';
|
int paramPos=-1;
|
||||||
|
//modestr[0]=mode; modestr[1]='b'; modestr[2]='+'; modestr[3]='\0';
|
||||||
|
modestr[0]=mode; modestr[1]='+'; modestr[2]='\0';
|
||||||
if (fs) fs.close();
|
if (fs) fs.close();
|
||||||
filename=_filename;
|
|
||||||
|
|
||||||
if (fs = SPIFFS.open(_filename,modestr))
|
if ((paramPos=_filename.indexOf('?'))>=0)
|
||||||
|
{
|
||||||
|
filename=_filename.substring(0,paramPos);
|
||||||
|
}
|
||||||
|
else filename=_filename;
|
||||||
|
|
||||||
|
if (fs = SPIFFS.open(filename,modestr))
|
||||||
{
|
{
|
||||||
openedMode=mode;
|
openedMode=mode;
|
||||||
streamSize = DEFAULT_FILESIZE_LIMIT;
|
streamSize = DEFAULT_FILESIZE_LIMIT;
|
||||||
|
|
||||||
if (_filename.endsWith(".json")) {contentType=HTTP_TEXT_JSON;textMode=true;streamSize = MAX_JSON_CONF_SIZE; }
|
if (filename.endsWith(".json")) {contentType=HTTP_TEXT_JSON;textMode=true;streamSize = MAX_JSON_CONF_SIZE; }
|
||||||
else if (_filename.endsWith(".bin")) {contentType=HTTP_OCTET_STREAM;textMode=false;streamSize = SYSCONF_SIZE; }
|
else if (filename.endsWith(".bin")) {contentType=HTTP_OCTET_STREAM;textMode=false;streamSize = SYSCONF_SIZE; }
|
||||||
else if (_filename.endsWith(".txt")) {contentType=HTTP_TEXT_PLAIN;textMode=true;}
|
else if (filename.endsWith(".txt")) {contentType=HTTP_TEXT_PLAIN;textMode=true;}
|
||||||
else if (_filename.endsWith(".html")) {contentType=HTTP_TEXT_HTML;textMode=true;}
|
else if (filename.endsWith(".html")) {contentType=HTTP_TEXT_HTML;textMode=true;}
|
||||||
else if (_filename.endsWith(".gif")) {contentType=HTTP_IMAGE_GIF;textMode=false;}
|
else if (filename.endsWith(".gif")) {contentType=HTTP_IMAGE_GIF;textMode=false;}
|
||||||
else if (_filename.endsWith(".jpg")) {contentType=HTTP_IMAGE_JPEG;textMode=false;}
|
else if (filename.endsWith(".jpg")) {contentType=HTTP_IMAGE_JPEG;textMode=false;}
|
||||||
|
|
||||||
|
|
||||||
debugSerial<<(F("Opened ("))<<modestr<<(F(")"))<<filename<<endl;
|
debugSerial<<(F("Opened ("))<<modestr<<(F(")"))<<filename<<F(" Size:")<<streamSize<<endl;
|
||||||
return fs;
|
return fs;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -144,6 +151,13 @@ NRFFlashStorage EEPROM;
|
|||||||
|
|
||||||
int flashStream::open(String _filename, char mode)
|
int flashStream::open(String _filename, char mode)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
int paramPos;
|
||||||
|
if ((paramPos=_filename.indexOf('?'))>=0)
|
||||||
|
{
|
||||||
|
_filename=_filename.substring(0,paramPos);
|
||||||
|
}
|
||||||
|
|
||||||
if (_filename == "/config.json") return open (FN_CONFIG_JSON,mode);
|
if (_filename == "/config.json") return open (FN_CONFIG_JSON,mode);
|
||||||
else if (_filename == "/config.bin") return open (FN_CONFIG_BIN,mode);
|
else if (_filename == "/config.bin") return open (FN_CONFIG_BIN,mode);
|
||||||
else return 0;
|
else return 0;
|
||||||
|
|||||||
@@ -11,8 +11,10 @@
|
|||||||
|
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
|
#if defined ARDUINO_ARCH_ESP32
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#define FN_CONFIG_JSON 1
|
#define FN_CONFIG_JSON 1
|
||||||
#define FN_CONFIG_BIN 2
|
#define FN_CONFIG_BIN 2
|
||||||
|
|||||||
@@ -280,6 +280,7 @@ void Item::setCmd(uint8_t cmdValue) {
|
|||||||
aJsonObject *itemCmd = aJson.getArrayItem(itemArr, I_CMD);
|
aJsonObject *itemCmd = aJson.getArrayItem(itemArr, I_CMD);
|
||||||
if (itemCmd)
|
if (itemCmd)
|
||||||
{
|
{
|
||||||
|
itemCmd->type = aJson_Int;
|
||||||
itemCmd->valueint = cmdValue & CMD_MASK | itemCmd->valueint & FLAG_MASK; // Preserve special bits
|
itemCmd->valueint = cmdValue & CMD_MASK | itemCmd->valueint & FLAG_MASK; // Preserve special bits
|
||||||
debugSerial<<F("SetCmd:")<<cmdValue<<endl;
|
debugSerial<<F("SetCmd:")<<cmdValue<<endl;
|
||||||
}
|
}
|
||||||
@@ -300,6 +301,7 @@ void Item::setFlag (short flag)
|
|||||||
aJsonObject *itemCmd = aJson.getArrayItem(itemArr, I_CMD);
|
aJsonObject *itemCmd = aJson.getArrayItem(itemArr, I_CMD);
|
||||||
if (itemCmd)
|
if (itemCmd)
|
||||||
{
|
{
|
||||||
|
itemCmd->type = aJson_Int;
|
||||||
itemCmd->valueint |= flag & FLAG_MASK; // Preserve CMD bits
|
itemCmd->valueint |= flag & FLAG_MASK; // Preserve CMD bits
|
||||||
// debugSerial<<F("SetFlag:")<<flag<<endl;
|
// debugSerial<<F("SetFlag:")<<flag<<endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,21 +111,11 @@ typedef union
|
|||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
uint8_t cmdCode;
|
uint8_t cmdCode;
|
||||||
union {
|
uint8_t suffixCode:4;
|
||||||
uint8_t cmdFlag;
|
|
||||||
/*
|
|
||||||
struct
|
|
||||||
{ uint8_t suffixCode:4;
|
|
||||||
uint8_t itemArgType:4;
|
uint8_t itemArgType:4;
|
||||||
};
|
|
||||||
*/
|
uint8_t cmdEffect; //Reserve
|
||||||
};
|
uint8_t cmdParam; //Reserve
|
||||||
struct
|
|
||||||
{ uint8_t suffixCode:4;
|
|
||||||
uint8_t itemArgType:4;
|
|
||||||
};
|
|
||||||
// uint8_t cmdEffect;
|
|
||||||
uint8_t cmdParam;
|
|
||||||
};
|
};
|
||||||
} itemCmdStore;
|
} itemCmdStore;
|
||||||
|
|
||||||
|
|||||||
@@ -253,8 +253,6 @@ uint16_t httpHandler(Client& client, String request, uint8_t method, long conten
|
|||||||
+String(F("&name="))+deviceName
|
+String(F("&name="))+deviceName
|
||||||
);
|
);
|
||||||
|
|
||||||
//response+=(F("&ip="));
|
|
||||||
//todo response+=(Ethernet.localIP());
|
|
||||||
client.println();
|
client.println();
|
||||||
delay(100);
|
delay(100);
|
||||||
client.stop();
|
client.stop();
|
||||||
@@ -294,8 +292,39 @@ uint16_t httpHandler(Client& client, String request, uint8_t method, long conten
|
|||||||
return 200 | HTTP_TEXT_PLAIN;
|
return 200 | HTTP_TEXT_PLAIN;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else if (method == HTTP_GET && request.startsWith(F("/ram/")))
|
||||||
|
{
|
||||||
|
if (!authorized) return 401;
|
||||||
|
aJsonObject * dumpObject;
|
||||||
|
request.remove(0,5);
|
||||||
|
|
||||||
|
if (request == "." && items) dumpObject = items;
|
||||||
|
else if (request != "" && items)
|
||||||
|
{
|
||||||
|
dumpObject = aJson.getObjectItem(items, request.c_str());
|
||||||
|
if (!dumpObject) return 404;
|
||||||
|
}
|
||||||
|
else dumpObject = root;
|
||||||
|
|
||||||
|
ArduinoOTA.sendHttpResponse(client,200 | HTTP_TEXT_JSON,false); // Send only HTTP header, no close socket
|
||||||
|
client.println();
|
||||||
|
// char* outBuf = (char*) malloc(MAX_JSON_CONF_SIZE); /* XXX: Dynamic size. */
|
||||||
|
// if (outBuf == NULL) return 500;
|
||||||
|
aJsonStream socketStream = aJsonStream(&client);
|
||||||
|
//aJsonStringStream stringStream(NULL, outBuf, MAX_JSON_CONF_SIZE);
|
||||||
|
aJson.print(dumpObject, &socketStream);
|
||||||
|
delay(100);
|
||||||
|
client.stop();
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
//size_t res = sysConfStream.write((byte*) outBuf,len);
|
||||||
|
//free (outBuf);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
else if (method == HTTP_POST && request.startsWith(F("/command/")))
|
else if (method == HTTP_POST && request.startsWith(F("/command/")))
|
||||||
{
|
{
|
||||||
|
int result = 400;
|
||||||
if (!authorized) return 401;
|
if (!authorized) return 401;
|
||||||
request.remove(0,9);
|
request.remove(0,9);
|
||||||
String body=client.readStringUntil('\n');
|
String body=client.readStringUntil('\n');
|
||||||
@@ -306,8 +335,9 @@ uint16_t httpHandler(Client& client, String request, uint8_t method, long conten
|
|||||||
debugSerial<<F("Cmd: ")<<request<<endl;
|
debugSerial<<F("Cmd: ")<<request<<endl;
|
||||||
if (request=="reboot ") ArduinoOTA.sendHttpResponse(client,200);
|
if (request=="reboot ") ArduinoOTA.sendHttpResponse(client,200);
|
||||||
const char* res=request.c_str();
|
const char* res=request.c_str();
|
||||||
cmd_parse((char*) res);
|
result = cmd_parse((char*) res);
|
||||||
return 200;
|
if (! result) return 404;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
else return 0; //Unknown
|
else return 0; //Unknown
|
||||||
}
|
}
|
||||||
@@ -527,9 +557,19 @@ lan_status lanLoop() {
|
|||||||
if (configLocked) return LIBS_INITIALIZED;
|
if (configLocked) return LIBS_INITIALIZED;
|
||||||
|
|
||||||
if (!configOk)
|
if (!configOk)
|
||||||
lanStatus = loadConfigFromHttp(0, NULL);
|
{
|
||||||
else lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
if (loadConfigFromHttp(0, NULL)) lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
||||||
|
else if (configLoaded) {
|
||||||
|
infoSerial<<F("Continue with previously loaded config")<<endl;
|
||||||
|
lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
||||||
|
}
|
||||||
|
else lanStatus = READ_RE_CONFIG; //Load from NVRAM
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
infoSerial<<F("Config is valid")<<endl;
|
||||||
|
lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER:
|
case IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER:
|
||||||
@@ -1065,7 +1105,7 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
|
|||||||
|
|
||||||
#endif //_owire
|
#endif //_owire
|
||||||
|
|
||||||
void cmdFunctionHelp(int arg_cnt, char **args)
|
int cmdFunctionHelp(int arg_cnt, char **args)
|
||||||
{
|
{
|
||||||
printFirmwareVersionAndBuildOptions();
|
printFirmwareVersionAndBuildOptions();
|
||||||
printCurentLanConfig();
|
printCurentLanConfig();
|
||||||
@@ -1082,7 +1122,9 @@ void cmdFunctionHelp(int arg_cnt, char **args)
|
|||||||
"'kill' - test watchdog\n"
|
"'kill' - test watchdog\n"
|
||||||
"'clear' - clear EEPROM\n"
|
"'clear' - clear EEPROM\n"
|
||||||
"'reboot' - reboot controller");
|
"'reboot' - reboot controller");
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printCurentLanConfig() {
|
void printCurentLanConfig() {
|
||||||
infoSerial << F("Current LAN config(ip,dns,gw,subnet):");
|
infoSerial << F("Current LAN config(ip,dns,gw,subnet):");
|
||||||
printIPAddress(Ethernet.localIP());
|
printIPAddress(Ethernet.localIP());
|
||||||
@@ -1094,16 +1136,18 @@ void printCurentLanConfig() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionKill(int arg_cnt, char **args) {
|
int cmdFunctionKill(int arg_cnt, char **args) {
|
||||||
for (byte i = 1; i < 20; i++) {
|
for (byte i = 1; i < 20; i++) {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
infoSerial<<i;
|
infoSerial<<i;
|
||||||
};
|
};
|
||||||
|
return 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionReboot(int arg_cnt, char **args) {
|
int cmdFunctionReboot(int arg_cnt, char **args) {
|
||||||
infoSerial<<F("Soft rebooting...");
|
infoSerial<<F("Soft rebooting...");
|
||||||
softRebootFunc();
|
softRebootFunc();
|
||||||
|
return 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
void applyConfig() {
|
void applyConfig() {
|
||||||
@@ -1236,8 +1280,14 @@ void printConfigSummary() {
|
|||||||
infoSerial<<F("RAM=")<<freeRam()<<endl;
|
infoSerial<<F("RAM=")<<freeRam()<<endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionLoad(int arg_cnt, char **args) {
|
int cmdFunctionLoad(int arg_cnt, char **args) {
|
||||||
if (!loadConfigFromEEPROM()) lanStatus=DO_REINIT;
|
|
||||||
|
if (!loadConfigFromEEPROM())
|
||||||
|
{
|
||||||
|
lanStatus=DO_REINIT;
|
||||||
|
return 500;
|
||||||
|
}
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1275,14 +1325,14 @@ int loadConfigFromEEPROM()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionSave(int arg_cnt, char **args)
|
int cmdFunctionSave(int arg_cnt, char **args)
|
||||||
{
|
{
|
||||||
if (arg_cnt>1)
|
if (arg_cnt>1)
|
||||||
{
|
{
|
||||||
if (!strcasecmp_P(args[1],ON_P)) sysConf.setSaveSuccedConfig(true);
|
if (!strcasecmp_P(args[1],ON_P)) sysConf.setSaveSuccedConfig(true);
|
||||||
if (!strcasecmp_P(args[1],OFF_P)) sysConf.setSaveSuccedConfig(false);
|
if (!strcasecmp_P(args[1],OFF_P)) sysConf.setSaveSuccedConfig(false);
|
||||||
infoSerial<<F("Config autosave:")<<sysConf.getSaveSuccedConfig()<<endl;
|
infoSerial<<F("Config autosave:")<<sysConf.getSaveSuccedConfig()<<endl;
|
||||||
return;
|
return 200;
|
||||||
}
|
}
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
sysConfStream.open("/config.json",'w');
|
sysConfStream.open("/config.json",'w');
|
||||||
@@ -1295,7 +1345,7 @@ if (arg_cnt>1)
|
|||||||
if (outBuf == NULL)
|
if (outBuf == NULL)
|
||||||
{
|
{
|
||||||
sysConfStream.close();
|
sysConfStream.close();
|
||||||
return;
|
return 500;
|
||||||
}
|
}
|
||||||
infoSerial<<F("Saving config to EEPROM..")<<endl;
|
infoSerial<<F("Saving config to EEPROM..")<<endl;
|
||||||
aJsonStringStream stringStream(NULL, outBuf, MAX_JSON_CONF_SIZE);
|
aJsonStringStream stringStream(NULL, outBuf, MAX_JSON_CONF_SIZE);
|
||||||
@@ -1311,29 +1361,35 @@ if (arg_cnt>1)
|
|||||||
infoSerial<<F("Saving config to EEPROM..");
|
infoSerial<<F("Saving config to EEPROM..");
|
||||||
aJson.print(root, &jsonEEPROMStream);
|
aJson.print(root, &jsonEEPROMStream);
|
||||||
sysConfStream.putEOF();
|
sysConfStream.putEOF();
|
||||||
|
sysConfStream.flush();
|
||||||
sysConfStream.close();
|
sysConfStream.close();
|
||||||
infoSerial<<F("Saved to EEPROM")<<endl;
|
infoSerial<<F("Saved to EEPROM")<<endl;
|
||||||
#endif
|
#endif
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void cmdFunctionLoglevel(int arg_cnt, char **args)
|
int cmdFunctionLoglevel(int arg_cnt, char **args)
|
||||||
{
|
{
|
||||||
|
int res = 400;
|
||||||
if (arg_cnt>1)
|
if (arg_cnt>1)
|
||||||
{
|
{
|
||||||
serialDebugLevel=atoi(args[1]);
|
serialDebugLevel=atoi(args[1]);
|
||||||
sysConf.setSerialDebuglevel(serialDebugLevel);
|
sysConf.setSerialDebuglevel(serialDebugLevel);
|
||||||
|
res = 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arg_cnt>2)
|
if (arg_cnt>2)
|
||||||
{
|
{
|
||||||
udpDebugLevel=atoi(args[2]);
|
udpDebugLevel=atoi(args[2]);
|
||||||
sysConf.setUdpDebuglevel(udpDebugLevel);
|
sysConf.setUdpDebuglevel(udpDebugLevel);
|
||||||
|
res = 200;
|
||||||
}
|
}
|
||||||
infoSerial<<F("Serial debug level:")<<serialDebugLevel<<F("\nSyslog debug level:")<<udpDebugLevel<<endl;
|
infoSerial<<F("Serial debug level:")<<serialDebugLevel<<F("\nSyslog debug level:")<<udpDebugLevel<<endl;
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionIp(int arg_cnt, char **args)
|
int cmdFunctionIp(int arg_cnt, char **args)
|
||||||
{
|
{
|
||||||
IPAddress ip0(0, 0, 0, 0);
|
IPAddress ip0(0, 0, 0, 0);
|
||||||
IPAddress ip;
|
IPAddress ip;
|
||||||
@@ -1383,61 +1439,76 @@ void cmdFunctionIp(int arg_cnt, char **args)
|
|||||||
printIPAddress(current_mask); */
|
printIPAddress(current_mask); */
|
||||||
//}
|
//}
|
||||||
infoSerial<<F("Saved\n");
|
infoSerial<<F("Saved\n");
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionClearEEPROM(int arg_cnt, char **args){
|
int cmdFunctionClearEEPROM(int arg_cnt, char **args){
|
||||||
if (sysConf.clear())
|
#ifdef FS_STORAGE
|
||||||
infoSerial<<F("EEPROM cleared\n");
|
if (SPIFFS.format()) infoSerial<<F("FS Formatted\n");
|
||||||
|
#endif
|
||||||
|
if (sysConf.clear()) infoSerial<<F("EEPROM cleared\n");
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionPwd(int arg_cnt, char **args)
|
int cmdFunctionPwd(int arg_cnt, char **args)
|
||||||
{ //char empty[]="";
|
{ //char empty[]="";
|
||||||
if (arg_cnt)
|
if (arg_cnt)
|
||||||
sysConf.setMQTTpwd(args[1]);
|
sysConf.setMQTTpwd(args[1]);
|
||||||
else sysConf.setMQTTpwd();
|
else sysConf.setMQTTpwd();
|
||||||
infoSerial<<F("MQTT Password updated\n");
|
infoSerial<<F("MQTT Password updated\n");
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionOTAPwd(int arg_cnt, char **args)
|
int cmdFunctionOTAPwd(int arg_cnt, char **args)
|
||||||
{ //char empty[]="";
|
{ //char empty[]="";
|
||||||
if (arg_cnt)
|
if (arg_cnt)
|
||||||
sysConf.setOTApwd(args[1]);
|
sysConf.setOTApwd(args[1]);
|
||||||
else sysConf.setOTApwd();
|
else sysConf.setOTApwd();
|
||||||
infoSerial<<F("OTA Password updated\n");
|
infoSerial<<F("OTA Password updated\n");
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionSetMac(int arg_cnt, char **args) {
|
int cmdFunctionSetMac(int arg_cnt, char **args) {
|
||||||
char dummy;
|
char dummy;
|
||||||
uint8_t mac[6];
|
uint8_t mac[6];
|
||||||
if (sscanf(args[1], "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5], &dummy) < 6) {
|
if (sscanf(args[1], "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5], &dummy) < 6) {
|
||||||
errorSerial<<F("could not parse: ")<<args[1];
|
errorSerial<<F("could not parse: ")<<args[1];
|
||||||
return;
|
return 400;
|
||||||
}
|
}
|
||||||
sysConf.setMAC(mac);
|
sysConf.setMAC(mac);
|
||||||
printMACAddress();
|
printMACAddress();
|
||||||
|
|
||||||
//for (short i = 0; i < 6; i++) { EEPROM.write(i, mac[i]); }
|
//for (short i = 0; i < 6; i++) { EEPROM.write(i, mac[i]); }
|
||||||
infoSerial<<F("Updated\n");
|
infoSerial<<F("Updated\n");
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmdFunctionGet(int arg_cnt, char **args) {
|
int cmdFunctionGet(int arg_cnt, char **args) {
|
||||||
|
int result;
|
||||||
if (arg_cnt>1)
|
if (arg_cnt>1)
|
||||||
{
|
{
|
||||||
if (!strcasecmp_P(args[1],ON_P)) {sysConf.setLoadHTTPConfig(true); return;};
|
if (!strcasecmp_P(args[1],ON_P)) {sysConf.setLoadHTTPConfig(true); return 200;};
|
||||||
if (!strcasecmp_P(args[1],OFF_P)) {sysConf.setLoadHTTPConfig(false); return;};
|
if (!strcasecmp_P(args[1],OFF_P)) {sysConf.setLoadHTTPConfig(false); return 200;};
|
||||||
|
|
||||||
infoSerial<<F("Loading HTTP config on startup:")<<sysConf.getLoadHTTPConfig()<<endl;
|
infoSerial<<F("Loading HTTP config on startup:")<<sysConf.getLoadHTTPConfig()<<endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lanStatus= loadConfigFromHttp(arg_cnt, args);
|
if (loadConfigFromHttp(arg_cnt, args))
|
||||||
//ethClient.stop(); //Refresh MQTT connect to get retained info
|
{
|
||||||
|
lanStatus =IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
||||||
|
return 200;
|
||||||
|
}
|
||||||
|
// Not Loaded
|
||||||
|
if (configLoaded) lanStatus =IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
||||||
|
else lanStatus = READ_RE_CONFIG;
|
||||||
|
return 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printBool(bool arg) { (arg) ? infoSerial<<F("+") : infoSerial<<F("-"); }
|
void printBool(bool arg) { (arg) ? infoSerial<<F("+") : infoSerial<<F("-"); }
|
||||||
|
|
||||||
|
|
||||||
lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
bool loadConfigFromHttp(int arg_cnt, char **args)
|
||||||
{
|
{
|
||||||
//macAddress * mac = sysConf.getMAC();
|
//macAddress * mac = sysConf.getMAC();
|
||||||
int responseStatusCode = 0;
|
int responseStatusCode = 0;
|
||||||
@@ -1493,28 +1564,27 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
|||||||
interrupts();
|
interrupts();
|
||||||
hclient.closeStream(configStream); // this is very important -- be sure to close the STREAM
|
hclient.closeStream(configStream); // this is very important -- be sure to close the STREAM
|
||||||
|
|
||||||
if (!root) {
|
if (!root)
|
||||||
|
{
|
||||||
errorSerial<<F("Config parsing failed\n");
|
errorSerial<<F("Config parsing failed\n");
|
||||||
// timerLanCheckTime = millis();// + 15000;
|
return false;
|
||||||
return READ_RE_CONFIG;//-11;
|
}
|
||||||
} else {
|
else {
|
||||||
infoSerial<<F("Applying.\n");
|
infoSerial<<F("Applying.\n");
|
||||||
applyConfig();
|
applyConfig();
|
||||||
infoSerial<<F("Done.\n");
|
infoSerial<<F("Done.\n");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
errorSerial<<F("ERROR: Server returned ");
|
errorSerial<<F("ERROR: Server returned ");
|
||||||
errorSerial<<responseStatusCode<<endl;
|
errorSerial<<responseStatusCode<<endl;
|
||||||
if (configLoaded) return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
return false;
|
||||||
else return READ_RE_CONFIG; //Load from NVRAM
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
debugSerial<<F("failed to connect\n");
|
debugSerial<<F("failed to connect\n");
|
||||||
if (configLoaded) return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
return false;
|
||||||
else return READ_RE_CONFIG; //Load from NVRAM
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if defined(__SAM3X8E__) || defined(ARDUINO_ARCH_STM32) || defined (NRF5) //|| defined(ARDUINO_ARCH_ESP32) //|| defined(ARDUINO_ARCH_ESP8266)
|
#if defined(__SAM3X8E__) || defined(ARDUINO_ARCH_STM32) || defined (NRF5) //|| defined(ARDUINO_ARCH_ESP32) //|| defined(ARDUINO_ARCH_ESP8266)
|
||||||
@@ -1555,33 +1625,36 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
|||||||
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
errorSerial<<F("Config parsing failed\n");
|
errorSerial<<F("Config parsing failed\n");
|
||||||
return READ_RE_CONFIG;//-11; //Load from NVRAM
|
return false;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
debugSerial<<F("Parsed. Free:")<<freeRam()<<endl;
|
debugSerial<<F("Parsed. Free:")<<freeRam()<<endl;
|
||||||
//debugSerial<<response;
|
//debugSerial<<response;
|
||||||
applyConfig();
|
applyConfig();
|
||||||
infoSerial<<F("Done.\n");
|
infoSerial<<F("Done.\n");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
errorSerial<<F("Config retrieving failed\n");
|
errorSerial<<F("Config retrieving failed\n");
|
||||||
if (configLoaded) return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
return false;
|
||||||
else return READ_RE_CONFIG; //Load from NVRAM
|
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
errorSerial<<F("Connect failed\n");
|
errorSerial<<F("Connect failed\n");
|
||||||
if (configLoaded) return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
return false;
|
||||||
else return READ_RE_CONFIG; //Load from NVRAM
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) //|| defined (NRF5)
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) //|| defined (NRF5)
|
||||||
HTTPClient httpClient;
|
|
||||||
// WiFiClient wifiClient;
|
|
||||||
#if defined(WIFI_ENABLE)
|
#if defined(WIFI_ENABLE)
|
||||||
WiFiClient configEthClient;
|
WiFiClient configEthClient;
|
||||||
#else
|
#else
|
||||||
EthernetClient configEthClient;
|
EthernetClient configEthClient;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
HTTPClient httpClient;
|
||||||
|
|
||||||
String fullURI = "http://";
|
String fullURI = "http://";
|
||||||
fullURI+=configServer;
|
fullURI+=configServer;
|
||||||
fullURI+=URI;
|
fullURI+=URI;
|
||||||
@@ -1593,36 +1666,40 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
int httpResponseCode = httpClient.GET();
|
int httpResponseCode = httpClient.GET();
|
||||||
|
|
||||||
if (httpResponseCode > 0) {
|
if (httpResponseCode > 0) {
|
||||||
infoSerial.printf("[HTTP] GET... code: %d\n", httpResponseCode);
|
infoSerial.printf("[HTTP] GET... code: %d\n", httpResponseCode);
|
||||||
if (httpResponseCode == HTTP_CODE_OK) {
|
if (httpResponseCode == HTTP_CODE_OK) {
|
||||||
String response = httpClient.getString();
|
String response = httpClient.getString();
|
||||||
|
|
||||||
|
httpClient.end();
|
||||||
|
|
||||||
debugSerial<<response;
|
debugSerial<<response;
|
||||||
cleanConf();
|
cleanConf();
|
||||||
root = aJson.parse((char *) response.c_str());
|
root = aJson.parse((char *) response.c_str());
|
||||||
if (!root) {
|
if (!root) {
|
||||||
errorSerial<<F("Config parsing failed\n");
|
errorSerial<<F("Config parsing failed\n");
|
||||||
return READ_RE_CONFIG;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
infoSerial<<F("Config OK, Applying\n");
|
infoSerial<<F("Config OK, Applying\n");
|
||||||
applyConfig();
|
applyConfig();
|
||||||
infoSerial<<F("Done.\n");
|
infoSerial<<F("Done.\n");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
httpClient.end();
|
||||||
errorSerial<<F("Config retrieving failed\n");
|
errorSerial<<F("Config retrieving failed\n");
|
||||||
if (configLoaded) return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
return false;
|
||||||
else return READ_RE_CONFIG; //Load from NVRAM
|
|
||||||
}
|
}
|
||||||
} else {
|
} else
|
||||||
|
{
|
||||||
errorSerial.printf("[HTTP] GET... failed, error: %s\n", httpClient.errorToString(httpResponseCode).c_str());
|
errorSerial.printf("[HTTP] GET... failed, error: %s\n", httpClient.errorToString(httpResponseCode).c_str());
|
||||||
httpClient.end();
|
httpClient.end();
|
||||||
if (configLoaded) return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
return false;
|
||||||
else return READ_RE_CONFIG; //Load from NVRAM
|
|
||||||
}
|
}
|
||||||
httpClient.end();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void preTransmission() {
|
void preTransmission() {
|
||||||
@@ -1691,7 +1768,11 @@ void setup_main() {
|
|||||||
#if defined (FS_STORAGE)
|
#if defined (FS_STORAGE)
|
||||||
#if defined(FS_PREPARE)
|
#if defined(FS_PREPARE)
|
||||||
//Initialize File System
|
//Initialize File System
|
||||||
|
#if defined ARDUINO_ARCH_ESP32
|
||||||
if(SPIFFS.begin(true))
|
if(SPIFFS.begin(true))
|
||||||
|
#else
|
||||||
|
if(SPIFFS.begin())
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
debugSerial<<("SPIFFS Initialize....ok")<<endl;
|
debugSerial<<("SPIFFS Initialize....ok")<<endl;
|
||||||
}
|
}
|
||||||
@@ -1704,7 +1785,7 @@ void setup_main() {
|
|||||||
#if defined(ESP8266) || defined(ESP32)
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
EEPROM.begin(ESP_EEPROM_SIZE);
|
EEPROM.begin(ESP_EEPROM_SIZE);
|
||||||
int streamSize = EEPROM.length();
|
int streamSize = EEPROM.length();
|
||||||
JSONStream.setSize(streamSize-EEPROM_offsetJSON);
|
sysConfStream.setSize(streamSize-EEPROM_offsetJSON);
|
||||||
debugSerial<<F("FLASH Init: ")<<streamSize<<endl;
|
debugSerial<<F("FLASH Init: ")<<streamSize<<endl;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -23,7 +23,9 @@
|
|||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
||||||
//#include "SPIFFS.h"
|
//#include "SPIFFS.h"
|
||||||
//#include <ESP_EEPROM.h>
|
#ifndef FS_STORAGE
|
||||||
|
#include <ESP_EEPROM.h>
|
||||||
|
#endif
|
||||||
//#include <ESP8266HTTPClient.h>
|
//#include <ESP8266HTTPClient.h>
|
||||||
//#include <ArduinoHttpClient.h>
|
//#include <ArduinoHttpClient.h>
|
||||||
//#include "HttpClient.h"
|
//#include "HttpClient.h"
|
||||||
@@ -239,13 +241,13 @@ void Changed(int i, DeviceAddress addr, float currentTemp);
|
|||||||
|
|
||||||
void modbusIdle(void);
|
void modbusIdle(void);
|
||||||
|
|
||||||
void cmdFunctionHelp(int arg_cnt, char **args);
|
int cmdFunctionHelp(int arg_cnt, char **args);
|
||||||
|
|
||||||
void cmdFunctionKill(int arg_cnt, char **args);
|
int cmdFunctionKill(int arg_cnt, char **args);
|
||||||
|
|
||||||
void applyConfig();
|
void applyConfig();
|
||||||
|
|
||||||
void cmdFunctionLoad(int arg_cnt, char **args);
|
int cmdFunctionLoad(int arg_cnt, char **args);
|
||||||
|
|
||||||
int loadConfigFromEEPROM();
|
int loadConfigFromEEPROM();
|
||||||
|
|
||||||
@@ -255,13 +257,13 @@ int loadConfigFromEEPROM();
|
|||||||
|
|
||||||
//int mqttConfigResp(char *as);
|
//int mqttConfigResp(char *as);
|
||||||
|
|
||||||
void cmdFunctionSave(int arg_cnt, char **args);
|
int cmdFunctionSave(int arg_cnt, char **args);
|
||||||
|
|
||||||
void cmdFunctionSetMac(int arg_cnt, char **args);
|
int cmdFunctionSetMac(int arg_cnt, char **args);
|
||||||
|
|
||||||
void cmdFunctionGet(int arg_cnt, char **args);
|
int cmdFunctionGet(int arg_cnt, char **args);
|
||||||
|
|
||||||
void cmdFunctionLoglevel(int arg_cnt, char **args);
|
int cmdFunctionLoglevel(int arg_cnt, char **args);
|
||||||
|
|
||||||
void printBool(bool arg);
|
void printBool(bool arg);
|
||||||
/*
|
/*
|
||||||
@@ -274,7 +276,7 @@ void saveFlash(short n, IPAddress& ip);
|
|||||||
int ipLoadFromFlash(short n, IPAddress &ip);
|
int ipLoadFromFlash(short n, IPAddress &ip);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
lan_status loadConfigFromHttp(int arg_cnt = 0, char **args = NULL);
|
bool loadConfigFromHttp(int arg_cnt = 0, char **args = NULL);
|
||||||
|
|
||||||
void preTransmission();
|
void preTransmission();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user