Skip to content

Commit

Permalink
Various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gskjold committed Jan 16, 2022
1 parent 1f7d845 commit 6b0ec39
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/AmsToMqttBridge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ bool readHanPort() {
debugD("Frame dump (%db):", len);
debugPrint(buf, 0, len);
}
if(hc != NULL && Debug.isActive(RemoteDebug::DEBUG)) {
if(hc != NULL && Debug.isActive(RemoteDebug::VERBOSE)) {
debugD("System title:");
debugPrint(hc->system_title, 0, 8);
debugD("Initialization vector:");
Expand All @@ -764,7 +764,7 @@ bool readHanPort() {
len = 0;
while(hanSerial->available()) hanSerial->read();
if(pos > 0) {
debugI("Valid data, start at byte %d", pos);
debugD("Valid data, start at byte %d", pos);
data = IEC6205675(((char *) (buf)) + pos, meterState.getMeterType(), meterConfig.distributionSystem, timestamp, hc);
} else {
if(Debug.isActive(RemoteDebug::WARNING)) {
Expand Down
20 changes: 18 additions & 2 deletions src/entsoe/DnbCurrParser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "DnbCurrParser.h"
#include "Arduino.h"
#include "HardwareSerial.h"

float DnbCurrParser::getValue() {
Expand Down Expand Up @@ -35,7 +36,22 @@ size_t DnbCurrParser::write(uint8_t byte) {
}
} else if(byte == '>') {
buf[pos++] = byte;
if(strncmp(buf, "<Obs", 4) == 0) {
if(strncmp(buf, "<Series", 7) == 0) {
for(int i = 0; i < pos; i++) {
if(strncmp(buf+i, "UNIT_MULT=\"", 11) == 0) {
pos = i + 11;
break;
}
}
for(int i = 0; i < 16; i++) {
uint8_t b = buf[pos+i];
if(b == '"') {
buf[pos+i] = '\0';
break;
}
}
scale = String(buf+pos).toInt();
} else if(strncmp(buf, "<Obs", 4) == 0) {
for(int i = 0; i < pos; i++) {
if(strncmp(buf+i, "OBS_VALUE=\"", 11) == 0) {
pos = i + 11;
Expand All @@ -49,7 +65,7 @@ size_t DnbCurrParser::write(uint8_t byte) {
break;
}
}
value = String(buf+pos).toFloat();
value = String(buf+pos).toFloat() / pow(10, scale);
}
pos = 0;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/entsoe/DnbCurrParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DnbCurrParser: public Stream {
size_t write(uint8_t);

private:
uint8_t scale = 0;
float value = 1.0;

char buf[64];
Expand Down
74 changes: 53 additions & 21 deletions src/entsoe/EntsoeApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
#include "TimeLib.h"
#include "DnbCurrParser.h"

#if defined(ESP8266)
#include <ESP8266HTTPClient.h>
#elif defined(ESP32) // ARDUINO_ARCH_ESP32
#include <HTTPClient.h>
#else
#warning "Unsupported board type"
#if defined(ESP32)
#include <esp_task_wdt.h>
#endif

EntsoeApi::EntsoeApi(RemoteDebug* Debug) {
debugger = Debug;

client.setInsecure();
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);

// Entso-E uses CET/CEST
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60};
Expand All @@ -26,6 +25,7 @@ void EntsoeApi::setup(EntsoeConfig& config) {
this->config = new EntsoeConfig();
}
memcpy(this->config, &config, sizeof(config));
lastCurrencyFetch = 0;
}

char* EntsoeApi::getToken() {
Expand Down Expand Up @@ -79,7 +79,6 @@ float EntsoeApi::getValueForHour(time_t cur, uint8_t hour) {
bool EntsoeApi::loop() {
if(strlen(getToken()) == 0)
return false;
bool ret = false;

uint64_t now = millis64();
if(now < 10000) return false; // Grace period
Expand Down Expand Up @@ -121,15 +120,23 @@ bool EntsoeApi::loop() {
d2.Year+1970, d2.Month, d2.Day, 23, 00,
config->area, config->area);

#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif


if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Fetching prices for today\n");
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", url);
EntsoeA44Parser* a44 = new EntsoeA44Parser();
if(retrieve(url, a44) && a44->getPoint(0) != ENTSOE_NO_VALUE) {
today = a44;
ret = true;
return true;
} else if(a44 != NULL) {
delete a44;
today = NULL;
return false;
}
}

Expand All @@ -151,23 +158,29 @@ bool EntsoeApi::loop() {
d2.Year+1970, d2.Month, d2.Day, 23, 00,
config->area, config->area);

#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif

if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Fetching prices for tomorrow\n");
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", url);
EntsoeA44Parser* a44 = new EntsoeA44Parser();
if(retrieve(url, a44) && a44->getPoint(0) != ENTSOE_NO_VALUE) {
tomorrow = a44;
ret = true;
return true;
} else if(a44 != NULL) {
delete a44;
tomorrow = NULL;
return false;
}
}
}
return ret;
return false;
}

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.
Expand All @@ -185,13 +198,15 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
*/
#endif

client.setInsecure();

HTTPClient https;
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);

if(https.begin(client, url)) {
printD("Connection established");

#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif

/*
#if defined(ESP8266)
if(!client.getMFLNStatus()) {
Expand All @@ -204,6 +219,13 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
*/

int status = https.GET();

#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif

if(status == HTTP_CODE_OK) {
printD("Receiving data");
https.writeToStream(doc);
Expand Down Expand Up @@ -241,15 +263,25 @@ float EntsoeApi::getCurrencyMultiplier(const char* from, const char* to) {
uint64_t now = millis64();
if(lastCurrencyFetch == 0 || now - lastCurrencyFetch > (SECS_PER_HOUR * 1000)) {
char url[256];
snprintf(url, sizeof(url), "https://data.norges-bank.no/api/data/EXR/M.%s.%s.SP?lastNObservations=1",
from,
to
);

DnbCurrParser p;

snprintf(url, sizeof(url), "https://data.norges-bank.no/api/data/EXR/M.%s.NOK.SP?lastNObservations=1", from);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Retrieving %s to NOK conversion\n", from);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", url);
if(retrieve(url, &p)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) got exchange rate %.4f\n", p.getValue());
currencyMultiplier = p.getValue();
if(strncmp(to, "NOK", 3) != 0) {
snprintf(url, sizeof(url), "https://data.norges-bank.no/api/data/EXR/M.%s.NOK.SP?lastNObservations=1", to);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Retrieving %s to NOK conversion\n", to);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", url);
if(retrieve(url, &p)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) got exchange rate %.4f\n", p.getValue());
currencyMultiplier /= p.getValue();
}
}
}
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) Resulting currency multiplier: %.4f\n", currencyMultiplier);
lastCurrencyFetch = now;
}
return currencyMultiplier;
Expand Down
10 changes: 10 additions & 0 deletions src/entsoe/EntsoeApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
#include "EntsoeA44Parser.h"
#include "AmsConfiguration.h"

#if defined(ESP8266)
#include <ESP8266HTTPClient.h>
#elif defined(ESP32) // ARDUINO_ARCH_ESP32
#include <HTTPClient.h>
#else
#warning "Unsupported board type"
#endif

#define ENTSOE_DEFAULT_MULTIPLIER 1.00
#define SSL_BUF_SIZE 512

Expand All @@ -24,6 +32,8 @@ class EntsoeApi {
private:
RemoteDebug* debugger;
EntsoeConfig* config = NULL;
WiFiClientSecure client;
HTTPClient https;

uint64_t midnightMillis = 0;
uint64_t lastTodayFetch = 0;
Expand Down
26 changes: 19 additions & 7 deletions src/web/AmsWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,21 @@ void AmsWebServer::configWifiHtml() {

html.replace("{s}", wifi.ssid);
html.replace("{p}", wifi.psk);
html.replace("{st}", strlen(wifi.ip) > 0 ? "checked" : "");
html.replace("{i}", wifi.ip);
html.replace("{g}", wifi.gateway);
html.replace("{sn}", wifi.subnet);
html.replace("{d1}", wifi.dns1);
html.replace("{d2}", wifi.dns2);
if(strlen(wifi.ip) > 0) {
html.replace("{st}", "checked");
html.replace("{i}", wifi.ip);
html.replace("{g}", wifi.gateway);
html.replace("{sn}", wifi.subnet);
html.replace("{d1}", wifi.dns1);
html.replace("{d2}", wifi.dns2);
} else {
html.replace("{st}", "");
html.replace("{i}", WiFi.localIP().toString());
html.replace("{g}", WiFi.gatewayIP().toString());
html.replace("{sn}", WiFi.subnetMask().toString());
html.replace("{d1}", WiFi.dnsIP().toString());
html.replace("{d2}", "");
}
html.replace("{h}", wifi.hostname);
html.replace("{m}", wifi.mdns ? "checked" : "");

Expand Down Expand Up @@ -783,7 +792,8 @@ void AmsWebServer::dataJson() {
mqtt == NULL ? 0 : (int) mqtt->lastError(),
price == ENTSOE_NO_VALUE ? "null" : String(price, 2).c_str(),
time(nullptr),
meterState->getMeterType()
meterState->getMeterType(),
meterConfig->distributionSystem
);

server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
Expand Down Expand Up @@ -1269,6 +1279,7 @@ 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...");
Expand Down Expand Up @@ -1564,6 +1575,7 @@ void AmsWebServer::firmwareUpload() {
server.send(500, "text/plain", "500: couldn't create file");
} else {
#if defined(ESP32)
esp_task_wdt_delete(NULL);
esp_task_wdt_deinit();
#elif defined(ESP8266)
ESP.wdtDisable();
Expand Down
Loading

0 comments on commit 6b0ec39

Please sign in to comment.