From 4c9c280d45b93c364d588557287d0ad842cfca8b Mon Sep 17 00:00:00 2001 From: Marcus Date: Thu, 24 Oct 2024 00:00:35 +0200 Subject: [PATCH] try to add config.system.outputs to json, but esp8266 actually crashes at loading json --- Arduino/CanGrow/include/CanGrow.h | 25 +++++++--- Arduino/CanGrow/include/CanGrow_ESP32.h | 4 +- Arduino/CanGrow/include/CanGrow_ESP8266.h | 4 +- Arduino/CanGrow/include/CanGrow_LittleFS.h | 56 ++++++++++++++++++---- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index 04c38d5..3a5d28a 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -74,7 +74,7 @@ struct Pin_Index { const byte note; }; - +const byte Max_Outputs = 16; /* * @@ -104,15 +104,28 @@ struct Config_WiFi { * Config System Outputs * * - type: output type like GPIO, I2C, URL - * - gpio: which gpio + * 1 - GPIO + * 2 - I2C + * 3 - URL + * - gpio: which gpio is used + * - gpio_invert: invert gpio output + * - gpio_pwm: enable pwm for output + * - i2c: + * - ip: ip to smart plug (tasmota e.g.) + * - ip_path: uri path + * - enabled: enable output * */ struct Config_System_Outputs { byte type; + char name[32]; byte gpio; - char url[64]; - bool httpLogSerial; - unsigned short schedulerInterval = 1000; + bool gpio_invert; + bool gpio_pwm; + char i2c[8]; + byte ip[4]; + char ip_path[32]; + bool enabled; }; /* main System struct */ @@ -124,7 +137,7 @@ struct Config_System { char httpPass[32]; bool httpLogSerial; unsigned short schedulerInterval = 1000; - Config_System_Outputs outputs; + Config_System_Outputs outputs[Max_Outputs]; }; diff --git a/Arduino/CanGrow/include/CanGrow_ESP32.h b/Arduino/CanGrow/include/CanGrow_ESP32.h index 369a927..4ba7f0c 100644 --- a/Arduino/CanGrow/include/CanGrow_ESP32.h +++ b/Arduino/CanGrow/include/CanGrow_ESP32.h @@ -30,8 +30,8 @@ #define PinWIPE 2 #define PinWIPE_default LOW -#define Pin_I2C_SCL = 22; -#define Pin_I2C_SDA = 21; +#define Pin_I2C_SCL = 22 +#define Pin_I2C_SDA = 21 /* https://randomnerdtutorials.com/esp32-pinout-reference-gpios/ * diff --git a/Arduino/CanGrow/include/CanGrow_ESP8266.h b/Arduino/CanGrow/include/CanGrow_ESP8266.h index d7cf1a3..4129ffe 100644 --- a/Arduino/CanGrow/include/CanGrow_ESP8266.h +++ b/Arduino/CanGrow/include/CanGrow_ESP8266.h @@ -31,8 +31,8 @@ // GPIO 2 Boot fails if pulled to LOW #define PinWIPE 2 #define PinWIPE_default HIGH -#define Pin_I2C_SCL = 5; -#define Pin_I2C_SDA = 4; +#define Pin_I2C_SCL = 5 +#define Pin_I2C_SDA = 4 /* https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/ * diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 2d08c39..7feba2d 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -179,15 +179,15 @@ bool LoadConfig() { // Copy strings from the JsonDocument to the Config struct as char strlcpy(config.test, doc["test"], sizeof(config.test)); - // * WiFi * + /* WiFi */ JsonObject objWifi = doc["wifi"][0]; strlcpy(config.wifi.ssid, objWifi["ssid"], sizeof(config.wifi.ssid)); strlcpy(config.wifi.password, objWifi["password"], sizeof(config.wifi.password)); // Copy bool / int directly into struct config.wifi.dhcp = objWifi["dhcp"]; // load the ip addresses as array - int i; - for(i=0; i <4 ; i++) { + + for(byte i=0; i < 4 ; i++) { config.wifi.ip[i] = objWifi["ip"][i]; config.wifi.netmask[i] = objWifi["netmask"][i]; config.wifi.gateway[i] = objWifi["gateway"][i]; @@ -195,7 +195,7 @@ bool LoadConfig() { } - // * System * + /* System */ JsonObject objSystem = doc["system"][0]; config.system.ntpOffset = objSystem["ntpOffset"]; config.system.maintenanceDuration = objSystem["maintenanceDuration"]; @@ -205,7 +205,22 @@ bool LoadConfig() { config.system.httpLogSerial = objSystem["httpLogSerial"]; config.system.schedulerInterval = objSystem["schedulerInterval"]; - // * Grow * + /* System Outputs */ + for(byte i=0; i < Max_Outputs; i++) { + JsonObject objSystemOutputs = doc["system"]["outputs"][i]; + config.system.outputs[i].type = objSystemOutputs["type"]; + strlcpy(config.system.outputs[i].name, objSystemOutputs["name"], sizeof(config.system.outputs[i].name)); + config.system.outputs[i].gpio = objSystemOutputs["gpio"]; + config.system.outputs[i].gpio_invert = objSystemOutputs["gpio_invert"]; + config.system.outputs[i].gpio_pwm = objSystemOutputs["gpio_pwm"]; + strlcpy(config.system.outputs[i].name, objSystemOutputs["name"], sizeof(config.system.outputs[i].i2c)); + for(byte j=0; j < 4 ; j++) { + config.system.outputs[i].ip[j] = objSystemOutputs["ip"][j]; + } + config.system.outputs[i].enabled = objSystemOutputs["enabled"]; + } + + /* Grow */ JsonObject objGrow = doc["grow"][0]; strlcpy(config.grow.growName, objGrow["growName"], sizeof(config.grow.growName)); config.grow.dayOfGrow = objGrow["dayOfGrow"]; @@ -224,6 +239,10 @@ bool LoadConfig() { // Close the file (Curiously, File's destructor doesn't close the file) file.close(); Serial.println(":: [LittleFS:LoadConfig] config successfully loaded"); + Serial.println(":: [LittleFS:LoadConfig] --- runtime config ---"); + serializeJsonPretty(doc, Serial); + Serial.println(""); + Serial.println(":: [LittleFS:LoadConfig] ----------------------"); return true; } @@ -233,10 +252,10 @@ bool SaveConfig(bool writeToSerial = false) { */ JsonDocument doc; - // * Root * + /* Root */ doc["test"] = config.test; - // * WiFi * + /* WiFi */ JsonObject objWifi = doc["wifi"].add(); objWifi["ssid"] = config.wifi.ssid; objWifi["password"] = config.wifi.password; @@ -250,7 +269,7 @@ bool SaveConfig(bool writeToSerial = false) { } objWifi["dhcp"] = config.wifi.dhcp; - // * System * + /* System */ JsonObject objSystem = doc["system"].add(); objSystem["ntpOffset"] = config.system.ntpOffset; objSystem["maintenanceDuration"] = config.system.maintenanceDuration; @@ -260,7 +279,24 @@ bool SaveConfig(bool writeToSerial = false) { objSystem["httpLogSerial"] = config.system.httpLogSerial; objSystem["schedulerInterval"] = config.system.schedulerInterval; - // * Grow * + /* System Outputs */ + JsonObject objSystemOutputs = doc["system"]["outputs"].add(); + for(byte i=0; i < Max_Outputs; i++) { + objSystemOutputs["type"] = config.system.outputs[i].type; + objSystemOutputs["name"] = config.system.outputs[i].name; + objSystemOutputs["gpio"] = config.system.outputs[i].gpio; + objSystemOutputs["gpio_invert"] = config.system.outputs[i].gpio_invert; + objSystemOutputs["gpio_pwm"] = config.system.outputs[i].gpio_pwm; + objSystemOutputs["i2c"] = config.system.outputs[i].i2c; + for(byte j=0; j < 4 ; j++) { + objSystemOutputs["ip"][j] = config.system.outputs[i].ip[j]; + } + objSystemOutputs["ip_path"] = config.system.outputs[i].ip_path; + objSystemOutputs["enabled"] = config.system.outputs[i].enabled; + } + + + /* Grow */ JsonObject objGrow = doc["grow"].add(); objGrow["growName"] = config.grow.growName; objGrow["dayOfGrow"] = config.grow.dayOfGrow; @@ -306,7 +342,7 @@ bool SaveConfig(bool writeToSerial = false) { Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG); serializeJson(doc, Serial); Serial.println(""); - Serial.printf(":: [LittleFS:SaveConfig] --- %s ---\n", CANGROW_CFG); + Serial.printf(":: [LittleFS:SaveConfig] ----------------------\n", CANGROW_CFG); } return true;