Some changes after testing

This commit is contained in:
Gunnar Skjold 2021-01-20 07:41:09 +01:00
parent f15cf5d75e
commit ff84278edf
9 changed files with 94 additions and 35 deletions

View File

@ -9,6 +9,7 @@ lib_deps = file://lib/HanReader, file://lib/Timezone, MQTT@2.4.8, DallasTemperat
[env:esp8266]
platform = espressif8266@2.6.2
board = esp12e
board_build.ldscript = eagle.flash.4m3m.ld
framework = ${common.framework}
lib_deps = ${common.lib_deps}
extra_scripts =

View File

@ -441,6 +441,15 @@ bool AmsConfiguration::getEntsoeConfig(EntsoeConfig& config) {
}
bool AmsConfiguration::setEntsoeConfig(EntsoeConfig& config) {
EntsoeConfig existing;
if(getEntsoeConfig(existing)) {
entsoeChanged |= strcmp(config.token, existing.token) != 0;
entsoeChanged |= strcmp(config.area, existing.area) != 0;
entsoeChanged |= strcmp(config.currency, existing.currency) != 0;
entsoeChanged |= config.multiplier != existing.multiplier;
} else {
entsoeChanged = true;
}
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_ENTSOE_START, config);
bool ret = EEPROM.commit();
@ -448,6 +457,21 @@ bool AmsConfiguration::setEntsoeConfig(EntsoeConfig& config) {
return ret;
}
void AmsConfiguration::clearEntsoe(EntsoeConfig& config) {
strcpy(config.token, "");
strcpy(config.area, "");
strcpy(config.currency, "");
config.multiplier = 1000;
}
bool AmsConfiguration::isEntsoeChanged() {
return entsoeChanged;
}
void AmsConfiguration::ackEntsoeChange() {
entsoeChanged = false;
}
void AmsConfiguration::clear() {
MeterConfig meter;
clearMeter(meter);
@ -473,6 +497,10 @@ void AmsConfiguration::clear() {
clearNtp(ntp);
setNtpConfig(ntp);
EntsoeConfig entsoe;
clearEntsoe(entsoe);
setEntsoeConfig(entsoe);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(EEPROM_CONFIG_ADDRESS, -1);
EEPROM.commit();

View File

@ -294,6 +294,9 @@ public:
bool getEntsoeConfig(EntsoeConfig&);
bool setEntsoeConfig(EntsoeConfig&);
void clearEntsoe(EntsoeConfig&);
bool isEntsoeChanged();
void ackEntsoeChange();
uint8_t getTempSensorCount();
TempSensorConfig* getTempSensorConfig(uint8_t address[8]);
@ -308,7 +311,7 @@ protected:
private:
uint8_t configVersion = 0;
bool wifiChanged, mqttChanged, meterChanged = true, domoChanged, ntpChanged = true;
bool wifiChanged, mqttChanged, meterChanged = true, domoChanged, ntpChanged = true, entsoeChanged = false;
uint8_t tempSensorCount = 0;
TempSensorConfig** tempSensors;

View File

@ -57,11 +57,11 @@ AmsConfiguration config;
RemoteDebug Debug;
EntsoeApi eapi(&Debug);
EntsoeApi* eapi = NULL;
Timezone* tz;
AmsWebServer ws(&Debug, &hw, &eapi);
AmsWebServer ws(&Debug, &hw);
MQTTClient mqtt(512);
AmsMqttHandler* mqttHandler = NULL;
@ -100,8 +100,6 @@ void setup() {
gpioConfig.ledPin = 5;
gpioConfig.ledInverted = true;
gpioConfig.tempSensorPin = 14;
gpioConfig.vccPin = 35;
gpioConfig.vccMultiplier = 2250;
#elif defined(ARDUINO_FEATHER_ESP32)
gpioConfig.hanPin = 16;
gpioConfig.ledPin = 2;
@ -133,8 +131,10 @@ void setup() {
hw.ledBlink(LED_BLUE, 1);
EntsoeConfig entsoe;
if(config.getEntsoeConfig(entsoe)) {
eapi.setup(entsoe);
if(config.getEntsoeConfig(entsoe) && strlen(entsoe.token) > 0) {
eapi = new EntsoeApi(&Debug);
eapi->setup(entsoe);
ws.setEntsoeApi(eapi);
}
bool shared = false;
@ -406,6 +406,29 @@ void loop() {
} else if(mqtt.connected()) {
mqtt.disconnect();
}
if(eapi != NULL) {
if(eapi->loop() && mqttHandler != NULL && mqtt.connected()) {
mqttHandler->publishPrices(eapi);
}
}
if(config.isEntsoeChanged()) {
EntsoeConfig entsoe;
if(config.getEntsoeConfig(entsoe) && strlen(entsoe.token) > 0) {
if(eapi == NULL) {
eapi = new EntsoeApi(&Debug);
ws.setEntsoeApi(eapi);
}
eapi->setup(entsoe);
} else if(eapi != NULL) {
delete eapi;
eapi = NULL;
ws.setEntsoeApi(eapi);
}
config.ackEntsoeChange();
}
}
} else {
if(dnsServer != NULL) {
@ -426,11 +449,6 @@ void loop() {
}
delay(1);
readHanPort();
if(WiFi.status() == WL_CONNECTED) {
if(eapi.loop() && mqttHandler != NULL) {
mqttHandler->publishPrices(&eapi);
}
}
ws.loop();
delay(1); // Needed for auto modem sleep
}
@ -591,14 +609,11 @@ void readHanPort() {
if(data.getListType() > 0) {
if(mqttEnabled && mqttHandler != NULL) {
if(mqttHandler->publish(&data, &meterState)) {
delay(1);
if(data.getListType() == 3) {
mqttHandler->publishPrices(&eapi);
delay(1);
if(eapi != NULL && data.getListType() == 3) {
mqttHandler->publishPrices(eapi);
}
if(data.getListType() >= 2) {
mqttHandler->publishSystem(&hw);
delay(1);
}
}
mqtt.loop();

View File

@ -108,7 +108,7 @@ bool EntsoeApi::loop() {
char url[256];
snprintf(url, sizeof(url), "%s?securityToken=%s&documentType=A44&periodStart=%04d%02d%02d%02d%02d&periodEnd=%04d%02d%02d%02d%02d&in_Domain=%s&out_Domain=%s",
"https://gunnar.origin.no/api.xml", config->token,
"https://transparency.entsoe.eu/api", config->token,
d1.Year+1970, d1.Month, d1.Day, 23, 00,
d2.Year+1970, d2.Month, d2.Day, 23, 00,
config->area, config->area);
@ -162,6 +162,9 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
WiFiClientSecure client;
#if defined(ESP8266)
// https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/bearssl-client-secure-class.html#mfln-or-maximum-fragment-length-negotiation-saving-ram
/* Rumor has it that a client cannot request a lower max_fragment_length, so I guess thats why the following does not work.
And there is currently not enough heap space to go around in this project to do a full HTTPS request on ESP8266
int bufSize = 512;
while(!client.probeMaxFragmentLength("transparency.entsoe.eu", 443, bufSize) && bufSize <= 4096) {
bufSize += 512;
@ -171,6 +174,7 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
printD(String(bufSize));
client.setBufferSizes(bufSize, bufSize);
}
*/
client.setInsecure();
#endif
@ -182,6 +186,7 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
if(https.begin(client, url)) {
printD("Connection established");
/*
#if defined(ESP8266)
if(!client.getMFLNStatus()) {
printE("Negotiated MFLN was not respected");
@ -190,9 +195,9 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
return false;
}
#endif
//ESP.wdtDisable();
*/
int status = https.GET();
//ESP.wdtEnable(5000);
if(status == HTTP_CODE_OK) {
printD("Receiving data");
https.writeToStream(doc);

View File

@ -33,10 +33,9 @@
#include "base64.h"
AmsWebServer::AmsWebServer(RemoteDebug* Debug, HwTools* hw, EntsoeApi* eapi) {
AmsWebServer::AmsWebServer(RemoteDebug* Debug, HwTools* hw) {
this->debugger = Debug;
this->hw = hw;
this->eapi = eapi;
}
void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, MeterConfig* meterConfig, AmsData* meterState, MQTTClient* mqtt) {
@ -106,6 +105,10 @@ void AmsWebServer::setMqttEnabled(bool enabled) {
mqttEnabled = enabled;
}
void AmsWebServer::setEntsoeApi(EntsoeApi* eapi) {
this->eapi = eapi;
}
void AmsWebServer::loop() {
server.handleClient();
@ -241,11 +244,15 @@ void AmsWebServer::price() {
sprintf(ts, "%02d:00", tm.Hour);
html.replace("${time" + String(i) + "}", String(ts));
double price = eapi->getValueForHour(i);
if(price == ENTSOE_NO_VALUE) {
html.replace("${price" + String(i) + "}", "--");
if(eapi != NULL) {
double price = eapi->getValueForHour(i);
if(price == ENTSOE_NO_VALUE) {
html.replace("${price" + String(i) + "}", "--");
} else {
html.replace("${price" + String(i) + "}", String(price, 4));
}
} else {
html.replace("${price" + String(i) + "}", String(price, 4));
html.replace("${price" + String(i) + "}", "--");
}
}
@ -706,6 +713,8 @@ void AmsWebServer::handleSetup() {
} else {
SystemConfig sys { server.arg("board").toInt() };
config->clear();
config->clearGpio(*gpioConfig);
config->clearMeter(*meterConfig);
@ -754,8 +763,6 @@ void AmsWebServer::handleSetup() {
gpioConfig->ledPin = 5;
gpioConfig->ledInverted = true;
gpioConfig->tempSensorPin = 14;
gpioConfig->vccPin = 35;
gpioConfig->vccMultiplier = 2250;
break;
case 202: // Feather
gpioConfig->hanPin = 16;
@ -1009,7 +1016,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...");

View File

@ -27,11 +27,12 @@
class AmsWebServer {
public:
AmsWebServer(RemoteDebug* Debug, HwTools* hw, EntsoeApi* eapi);
AmsWebServer(RemoteDebug* Debug, HwTools* hw);
void setup(AmsConfiguration*, GpioConfig*, MeterConfig*, AmsData*, MQTTClient*);
void loop();
void setTimezone(Timezone* tz);
void setMqttEnabled(bool);
void setEntsoeApi(EntsoeApi* eapi);
private:
RemoteDebug* debugger;
@ -39,7 +40,7 @@ private:
int maxPwr = 0;
HwTools* hw;
Timezone* tz;
EntsoeApi* eapi;
EntsoeApi* eapi = NULL;
AmsConfiguration* config;
GpioConfig* gpioConfig;
MeterConfig* meterConfig;

View File

@ -1,8 +1,8 @@
{
"id" : "%s",
"name" : "%s",
"up" : %d,
"t" : %d,
"up" : %lu,
"t" : %lu,
"vcc" : %.3f,
"rssi": %d,
"temp": %.2f,

View File

@ -1,8 +1,8 @@
{
"id" : "%s",
"name" : "%s",
"up" : %d,
"t" : %d,
"up" : %lu,
"t" : %lu,
"vcc" : %.3f,
"rssi": %d,
"temp": %.2f,