diff --git a/src/AmsConfiguration.cpp b/src/AmsConfiguration.cpp index 919dd102..62819081 100644 --- a/src/AmsConfiguration.cpp +++ b/src/AmsConfiguration.cpp @@ -451,6 +451,9 @@ bool AmsConfiguration::getEntsoeConfig(EntsoeConfig& config) { EEPROM.begin(EEPROM_SIZE); EEPROM.get(CONFIG_ENTSOE_START, config); EEPROM.end(); + if(strlen(config.token) != 0 && strlen(config.token) != 36) { + clearEntsoe(config); + } return true; } else { return false; diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 3bc5f680..f057bbdd 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -18,7 +18,7 @@ ADC_MODE(ADC_VCC); #if defined(ESP32) #include #endif -#define WDT_TIMEOUT 10 +#define WDT_TIMEOUT 30 #include "AmsToMqttBridge.h" #include "AmsStorage.h" @@ -137,12 +137,14 @@ void setup() { hw.ledBlink(LED_GREEN, 1); hw.ledBlink(LED_BLUE, 1); + #if defined(ESP32) EntsoeConfig entsoe; if(config.getEntsoeConfig(entsoe) && strlen(entsoe.token) > 0) { eapi = new EntsoeApi(&Debug); eapi->setup(entsoe); ws.setEntsoeApi(eapi); } + #endif bool shared = false; config.getMeterConfig(meterConfig); @@ -436,6 +438,7 @@ void loop() { mqtt->disconnect(); } + #if defined(ESP32) if(eapi != NULL && ntpEnabled) { if(eapi->loop() && mqtt != NULL && mqttHandler != NULL && mqtt->connected()) { mqttHandler->publishPrices(eapi); @@ -453,10 +456,11 @@ void loop() { } else if(eapi != NULL) { delete eapi; eapi = NULL; - ws.setEntsoeApi(eapi); + ws.setEntsoeApi(NULL); } config.ackEntsoeChange(); } + #endif ws.loop(); } if(mqtt != NULL) { // Run loop regardless, to let MQTT do its work. diff --git a/src/entsoe/DnbCurrParser.cpp b/src/entsoe/DnbCurrParser.cpp index 00b6dc53..82fcf278 100644 --- a/src/entsoe/DnbCurrParser.cpp +++ b/src/entsoe/DnbCurrParser.cpp @@ -30,6 +30,7 @@ size_t DnbCurrParser::write(const uint8_t *buffer, size_t size) { } size_t DnbCurrParser::write(uint8_t byte) { + if(pos >= 64) pos = 0; if(pos == 0) { if(byte == '<') { buf[pos++] = byte; diff --git a/src/entsoe/EntsoeA44Parser.cpp b/src/entsoe/EntsoeA44Parser.cpp index c01504df..1d7fa9b2 100644 --- a/src/entsoe/EntsoeA44Parser.cpp +++ b/src/entsoe/EntsoeA44Parser.cpp @@ -14,6 +14,7 @@ char* EntsoeA44Parser::getMeasurementUnit() { } float EntsoeA44Parser::getPoint(uint8_t position) { + if(position >= 24) return ENTSOE_NO_VALUE; return points[position]; } @@ -41,6 +42,7 @@ size_t EntsoeA44Parser::write(const uint8_t *buffer, size_t size) { } size_t EntsoeA44Parser::write(uint8_t byte) { + if(pos >= 64) pos = 0; if(docPos == DOCPOS_CURRENCY) { buf[pos++] = byte; if(pos == 3) { diff --git a/src/entsoe/EntsoeApi.cpp b/src/entsoe/EntsoeApi.cpp index 1eeec954..c0ccf25a 100644 --- a/src/entsoe/EntsoeApi.cpp +++ b/src/entsoe/EntsoeApi.cpp @@ -13,6 +13,7 @@ EntsoeApi::EntsoeApi(RemoteDebug* Debug) { client.setInsecure(); https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); + https.setTimeout(5000); // Entso-E uses CET/CEST TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; @@ -63,8 +64,10 @@ float EntsoeApi::getValueForHour(time_t cur, uint8_t hour) { } else { return ENTSOE_NO_VALUE; } - multiplier *= getCurrencyMultiplier(tomorrow->getCurrency(), config->currency); - } else if(pos > 0) { + float mult = getCurrencyMultiplier(tomorrow->getCurrency(), config->currency); + if(mult == 0) return ENTSOE_NO_VALUE; + multiplier *= mult; + } else if(pos >= 0) { if(today == NULL) return ENTSOE_NO_VALUE; value = today->getPoint(pos); @@ -73,7 +76,9 @@ float EntsoeApi::getValueForHour(time_t cur, uint8_t hour) { } else { return ENTSOE_NO_VALUE; } - multiplier *= getCurrencyMultiplier(today->getCurrency(), config->currency); + float mult = getCurrencyMultiplier(today->getCurrency(), config->currency); + if(mult == 0) return ENTSOE_NO_VALUE; + multiplier *= mult; } return value * multiplier; } @@ -288,8 +293,12 @@ float EntsoeApi::getCurrencyMultiplier(const char* from, const char* to) { if(retrieve(url, &p)) { if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) got exchange rate %.4f\n", p.getValue()); currencyMultiplier /= p.getValue(); + } else { + return 0; } } + } else { + return 0; } if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) Resulting currency multiplier: %.4f\n", currencyMultiplier); lastCurrencyFetch = midnightMillis; diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index c0e5d94f..968a520d 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -1278,7 +1278,6 @@ void AmsWebServer::handleSave() { strcpy(entsoe.currency, server.arg("ecu").c_str()); entsoe.multiplier = server.arg("em").toFloat() * 1000; config->setEntsoeConfig(entsoe); - eapi->setup(entsoe); } printI("Saving configuration now...");