Experimenting

This commit is contained in:
Gunnar Skjold 2021-12-05 13:08:34 +01:00
parent 3da5275624
commit 6054e900e6
4 changed files with 113 additions and 119 deletions

View File

@ -2,7 +2,7 @@
bool AmsConfiguration::getSystemConfig(SystemConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_SYSTEM_START, config);
EEPROM.end();
return true;
@ -12,7 +12,7 @@ bool AmsConfiguration::getSystemConfig(SystemConfig& config) {
}
bool AmsConfiguration::setSystemConfig(SystemConfig& config) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_SYSTEM_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -21,7 +21,7 @@ bool AmsConfiguration::setSystemConfig(SystemConfig& config) {
bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_WIFI_START, config);
EEPROM.end();
return true;
@ -45,7 +45,7 @@ bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) {
} else {
wifiChanged = true;
}
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_WIFI_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -85,7 +85,7 @@ void AmsConfiguration::ackWifiChange() {
bool AmsConfiguration::getMqttConfig(MqttConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_MQTT_START, config);
EEPROM.end();
return true;
@ -110,7 +110,7 @@ bool AmsConfiguration::setMqttConfig(MqttConfig& config) {
} else {
mqttChanged = true;
}
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_MQTT_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -143,7 +143,7 @@ void AmsConfiguration::ackMqttChange() {
bool AmsConfiguration::getWebConfig(WebConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_WEB_START, config);
EEPROM.end();
return true;
@ -154,7 +154,7 @@ bool AmsConfiguration::getWebConfig(WebConfig& config) {
}
bool AmsConfiguration::setWebConfig(WebConfig& config) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_WEB_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -169,7 +169,7 @@ void AmsConfiguration::clearAuth(WebConfig& config) {
bool AmsConfiguration::getMeterConfig(MeterConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_METER_START, config);
EEPROM.end();
return true;
@ -193,7 +193,7 @@ bool AmsConfiguration::setMeterConfig(MeterConfig& config) {
} else {
meterChanged = true;
}
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_METER_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -221,7 +221,7 @@ void AmsConfiguration::ackMeterChanged() {
bool AmsConfiguration::getDebugConfig(DebugConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_DEBUG_START, config);
EEPROM.end();
return true;
@ -232,7 +232,7 @@ bool AmsConfiguration::getDebugConfig(DebugConfig& config) {
}
bool AmsConfiguration::setDebugConfig(DebugConfig& config) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_DEBUG_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -247,7 +247,7 @@ void AmsConfiguration::clearDebug(DebugConfig& config) {
bool AmsConfiguration::getDomoticzConfig(DomoticzConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_DOMOTICZ_START, config);
EEPROM.end();
return true;
@ -269,7 +269,7 @@ bool AmsConfiguration::setDomoticzConfig(DomoticzConfig& config) {
domoChanged = true;
}
mqttChanged = domoChanged;
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_DOMOTICZ_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -310,7 +310,7 @@ bool AmsConfiguration::pinUsed(uint8_t pin, GpioConfig& config) {
bool AmsConfiguration::getGpioConfig(GpioConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_GPIO_START, config);
EEPROM.end();
return true;
@ -366,7 +366,7 @@ bool AmsConfiguration::setGpioConfig(GpioConfig& config) {
if(config.apPin >= 0)
pinMode(config.apPin, INPUT_PULLUP);
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_GPIO_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -394,7 +394,7 @@ void AmsConfiguration::clearGpio(GpioConfig& config) {
bool AmsConfiguration::getNtpConfig(NtpConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_NTP_START, config);
EEPROM.end();
return true;
@ -421,7 +421,7 @@ bool AmsConfiguration::setNtpConfig(NtpConfig& config) {
} else {
ntpChanged = true;
}
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_NTP_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -446,7 +446,7 @@ void AmsConfiguration::clearNtp(NtpConfig& config) {
bool AmsConfiguration::getEntsoeConfig(EntsoeConfig& config) {
if(hasConfig()) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_ENTSOE_START, config);
EEPROM.end();
return true;
@ -465,7 +465,7 @@ bool AmsConfiguration::setEntsoeConfig(EntsoeConfig& config) {
} else {
entsoeChanged = true;
}
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_ENTSOE_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@ -488,7 +488,7 @@ void AmsConfiguration::ackEntsoeChange() {
}
void AmsConfiguration::clear() {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
MeterConfig meter;
clearMeter(meter);
EEPROM.put(CONFIG_METER_START, meter);
@ -524,7 +524,7 @@ void AmsConfiguration::clear() {
bool AmsConfiguration::hasConfig() {
if(configVersion == 0) {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
configVersion = EEPROM.read(EEPROM_CONFIG_ADDRESS);
EEPROM.end();
}
@ -577,7 +577,7 @@ int AmsConfiguration::getConfigVersion() {
}
void AmsConfiguration::loadTempSensors() {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
TempSensorConfig* tempSensors[32];
int address = EEPROM_TEMP_CONFIG_ADDRESS;
int c = 0;
@ -616,7 +616,7 @@ void AmsConfiguration::saveTempSensors() {
bool AmsConfiguration::loadConfig83(int address) {
ConfigObject83 c;
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(address, c);
EntsoeConfig entsoe {"", "", "", 1000};
@ -727,7 +727,7 @@ bool AmsConfiguration::loadConfig83(int address) {
bool AmsConfiguration::relocateConfig86() {
MqttConfig86 mqtt86;
MqttConfig mqtt;
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_MQTT_START_86, mqtt86);
strcpy(mqtt.host, mqtt86.host);
mqtt.port = mqtt86.port;
@ -748,7 +748,7 @@ bool AmsConfiguration::relocateConfig86() {
bool AmsConfiguration::relocateConfig87() {
MeterConfig87 meter87;
MeterConfig meter;
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_METER_START_87, meter87);
if(meter87.type < 5) {
meter.baud = 2400;
@ -771,7 +771,7 @@ bool AmsConfiguration::relocateConfig87() {
bool AmsConfiguration::relocateConfig88() {
GpioConfig88 gpio88;
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_GPIO_START_88, gpio88);
GpioConfig gpio {
@ -800,7 +800,7 @@ bool AmsConfiguration::relocateConfig88() {
}
bool AmsConfiguration::save() {
EEPROM.begin(SPI_FLASH_SEC_SIZE);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CHECK_SUM);
saveTempSensors();
bool success = EEPROM.commit();

View File

@ -3,6 +3,7 @@
#include <EEPROM.h>
#include "Arduino.h"
#define EEPROM_SIZE 1024*3
#define EEPROM_CHECK_SUM 89 // Used to check if config is stored. Change if structure changes
#define EEPROM_CONFIG_ADDRESS 0
#define EEPROM_TEMP_CONFIG_ADDRESS 2048

View File

@ -280,6 +280,7 @@ void setup() {
return;
}
}
LittleFS.end();
delay(1);
if(config.hasConfig()) {
@ -362,7 +363,7 @@ void loop() {
if (WiFi.status() != WL_CONNECTED) {
wifiConnected = false;
Debug.stop();
WiFi_connect();
//WiFi_connect();
} else {
wifiReconnectCount = 0;
if(!wifiConnected) {
@ -654,17 +655,6 @@ void swapWifiMode() {
}
}
void mqttMessageReceived(String &topic, String &payload)
{
if (Debug.isActive(RemoteDebug::DEBUG)) {
debugD("Incoming MQTT message: [%s] %s", topic.c_str(), payload.c_str());
}
// Do whatever needed here...
// Ideas could be to query for values or to initiate OTA firmware update
}
int len = 0;
uint8_t buf[BUF_SIZE];
HDLCConfig* hc = NULL;
@ -1027,12 +1017,6 @@ void MQTT_connect() {
(strlen(mqttConfig.username) > 0 && mqtt->connect(mqttConfig.clientId, mqttConfig.username, mqttConfig.password))) {
if (Debug.isActive(RemoteDebug::INFO)) debugI("Successfully connected to MQTT!");
config.ackMqttChange();
// Subscribe to the chosen MQTT topic, if set in configuration
if (strlen(mqttConfig.subscribeTopic) > 0) {
mqtt->subscribe(mqttConfig.subscribeTopic);
if (Debug.isActive(RemoteDebug::INFO)) debugI(" Subscribing to [%s]\r\n", mqttConfig.subscribeTopic);
}
if(mqttHandler != NULL) {
mqttHandler->publishSystem(&hw);

View File

@ -793,87 +793,94 @@ void AmsWebServer::dataJson() {
void AmsWebServer::dayplotJson() {
printD("Serving /dayplot.json over http...");
if(ds == NULL) {
notFound();
} else {
char json[384];
snprintf_P(json, sizeof(json), DAYPLOT_JSON,
ds->getHour(0) / 1000.0,
ds->getHour(1) / 1000.0,
ds->getHour(2) / 1000.0,
ds->getHour(3) / 1000.0,
ds->getHour(4) / 1000.0,
ds->getHour(5) / 1000.0,
ds->getHour(6) / 1000.0,
ds->getHour(7) / 1000.0,
ds->getHour(8) / 1000.0,
ds->getHour(9) / 1000.0,
ds->getHour(10) / 1000.0,
ds->getHour(11) / 1000.0,
ds->getHour(12) / 1000.0,
ds->getHour(13) / 1000.0,
ds->getHour(14) / 1000.0,
ds->getHour(15) / 1000.0,
ds->getHour(16) / 1000.0,
ds->getHour(17) / 1000.0,
ds->getHour(18) / 1000.0,
ds->getHour(19) / 1000.0,
ds->getHour(20) / 1000.0,
ds->getHour(21) / 1000.0,
ds->getHour(22) / 1000.0,
ds->getHour(23) / 1000.0
);
char json[384];
snprintf_P(json, sizeof(json), DAYPLOT_JSON,
ds->getHour(0) / 1000.0,
ds->getHour(1) / 1000.0,
ds->getHour(2) / 1000.0,
ds->getHour(3) / 1000.0,
ds->getHour(4) / 1000.0,
ds->getHour(5) / 1000.0,
ds->getHour(6) / 1000.0,
ds->getHour(7) / 1000.0,
ds->getHour(8) / 1000.0,
ds->getHour(9) / 1000.0,
ds->getHour(10) / 1000.0,
ds->getHour(11) / 1000.0,
ds->getHour(12) / 1000.0,
ds->getHour(13) / 1000.0,
ds->getHour(14) / 1000.0,
ds->getHour(15) / 1000.0,
ds->getHour(16) / 1000.0,
ds->getHour(17) / 1000.0,
ds->getHour(18) / 1000.0,
ds->getHour(19) / 1000.0,
ds->getHour(20) / 1000.0,
ds->getHour(21) / 1000.0,
ds->getHour(22) / 1000.0,
ds->getHour(23) / 1000.0
);
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(strlen(json));
server.send(200, "application/json", json);
server.setContentLength(strlen(json));
server.send(200, "application/json", json);
}
}
void AmsWebServer::monthplotJson() {
printD("Serving /monthplot.json over http...");
char json[512];
snprintf_P(json, sizeof(json), MONTHPLOT_JSON,
ds->getDay(1) / 1000.0,
ds->getDay(2) / 1000.0,
ds->getDay(3) / 1000.0,
ds->getDay(4) / 1000.0,
ds->getDay(5) / 1000.0,
ds->getDay(6) / 1000.0,
ds->getDay(7) / 1000.0,
ds->getDay(8) / 1000.0,
ds->getDay(9) / 1000.0,
ds->getDay(10) / 1000.0,
ds->getDay(11) / 1000.0,
ds->getDay(12) / 1000.0,
ds->getDay(13) / 1000.0,
ds->getDay(14) / 1000.0,
ds->getDay(15) / 1000.0,
ds->getDay(16) / 1000.0,
ds->getDay(17) / 1000.0,
ds->getDay(18) / 1000.0,
ds->getDay(19) / 1000.0,
ds->getDay(20) / 1000.0,
ds->getDay(21) / 1000.0,
ds->getDay(22) / 1000.0,
ds->getDay(23) / 1000.0,
ds->getDay(24) / 1000.0,
ds->getDay(25) / 1000.0,
ds->getDay(26) / 1000.0,
ds->getDay(27) / 1000.0,
ds->getDay(28) / 1000.0,
ds->getDay(29) / 1000.0,
ds->getDay(30) / 1000.0,
ds->getDay(31) / 1000.0
);
if(ds == NULL) {
notFound();
} else {
char json[512];
snprintf_P(json, sizeof(json), MONTHPLOT_JSON,
ds->getDay(1) / 1000.0,
ds->getDay(2) / 1000.0,
ds->getDay(3) / 1000.0,
ds->getDay(4) / 1000.0,
ds->getDay(5) / 1000.0,
ds->getDay(6) / 1000.0,
ds->getDay(7) / 1000.0,
ds->getDay(8) / 1000.0,
ds->getDay(9) / 1000.0,
ds->getDay(10) / 1000.0,
ds->getDay(11) / 1000.0,
ds->getDay(12) / 1000.0,
ds->getDay(13) / 1000.0,
ds->getDay(14) / 1000.0,
ds->getDay(15) / 1000.0,
ds->getDay(16) / 1000.0,
ds->getDay(17) / 1000.0,
ds->getDay(18) / 1000.0,
ds->getDay(19) / 1000.0,
ds->getDay(20) / 1000.0,
ds->getDay(21) / 1000.0,
ds->getDay(22) / 1000.0,
ds->getDay(23) / 1000.0,
ds->getDay(24) / 1000.0,
ds->getDay(25) / 1000.0,
ds->getDay(26) / 1000.0,
ds->getDay(27) / 1000.0,
ds->getDay(28) / 1000.0,
ds->getDay(29) / 1000.0,
ds->getDay(30) / 1000.0,
ds->getDay(31) / 1000.0
);
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(strlen(json));
server.send(200, "application/json", json);
server.setContentLength(strlen(json));
server.send(200, "application/json", json);
}
}
void AmsWebServer::energyPriceJson() {
@ -1687,7 +1694,9 @@ void AmsWebServer::restartWaitHtml() {
yield();
if(performRestart) {
ds->save();
if(ds != NULL) {
ds->save();
}
printI("Rebooting");
delay(1000);
#if defined(ESP8266)