Compare commits

...

11 Commits

Author SHA1 Message Date
Gunnar Skjold
dd87d70876 Free value on fuse size and production capacity 2022-01-28 18:29:50 +01:00
Gunnar Skjold
52992f09ee Stability adjustments 2022-01-28 17:41:48 +01:00
Gunnar Skjold
6aa02d54c8 Improvements for ENTSO-E 2022-01-23 17:52:10 +01:00
Gunnar Skjold
9dbf9137c7 Merge branch 'master' of github.com:gskjold/AmsToMqttBridge 2022-01-21 17:57:35 +01:00
Gunnar Skjold
c0a9a01b41 Fixed uint problem with current 2022-01-21 17:57:01 +01:00
Gunnar Skjold
7b203b196f Fixed voltage and current labels for esp8266 2022-01-21 17:50:02 +01:00
Gunnar Skjold
50c06e2cfe Removed ap pin from generic ESP32 2022-01-16 19:38:35 +01:00
Gunnar Skjold
e4d4753181 Fixed incorrect labels for IT/TT current 2022-01-16 17:24:57 +01:00
Gunnar Skjold
04daf551fb Fixed incorrect time diff for price fetch 2022-01-16 17:04:58 +01:00
Gunnar Skjold
4b51f0f235 Only update exchange rate once per day 2022-01-16 10:50:08 +01:00
Gunnar Skjold
5e4ccca663 Fetch prices between 13:30 and 14:00 2022-01-16 10:37:08 +01:00
12 changed files with 73 additions and 42 deletions

View File

@@ -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;

View File

@@ -18,7 +18,7 @@ ADC_MODE(ADC_VCC);
#if defined(ESP32)
#include <esp_task_wdt.h>
#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.
@@ -555,8 +559,10 @@ void setupHanPort(uint8_t pin, uint32_t baud, uint8_t parityOrdinal, bool invert
#if defined(ESP32)
hwSerial->begin(baud, serialConfig, -1, -1, invert);
hwSerial->setRxBufferSize(768);
#else
hwSerial->begin(baud, serialConfig, SERIAL_FULL, 1, invert);
hwSerial->setRxBufferSize(768);
#endif
#if defined(ESP8266)
@@ -701,7 +707,6 @@ bool readHanPort() {
while(hanSerial->available() && pos == HDLC_FRAME_INCOMPLETE) {
buf[len++] = hanSerial->read();
pos = HDLC_validate((uint8_t *) buf, len, hc, &timestamp);
delay(1);
}
if(len > 0) {
if(len >= BUF_SIZE) {
@@ -969,7 +974,9 @@ void WiFi_connect() {
} else if(dns1.toString().isEmpty()) {
dns2.fromString("208.67.220.220"); // Add OpenDNS as second by default if nothing is configured
}
WiFi.config(ip, gw, sn, dns1, dns2);
if(!WiFi.config(ip, gw, sn, dns1, dns2)) {
debugE("Static IP configuration is invalid, not using");
}
} else {
#if defined(ESP32)
// This trick does not work anymore...

View File

@@ -471,9 +471,8 @@ double IEC6205675::getNumber(CosemData* item) {
break;
}
case CosemTypeLongSigned: {
uint16_t u16 = ntohs(item->lu.data); // ntohs only works for uint16 ?
int16_t i16 = u16; // Cast to int16 before use?
ret = i16; // Who knows, got to try it all...
int16_t i16 = ntohs(item->ls.data);
ret = i16;
pos += 3;
break;
}

View File

@@ -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;

View File

@@ -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) {

View File

@@ -13,11 +13,14 @@ EntsoeApi::EntsoeApi(RemoteDebug* Debug) {
client.setInsecure();
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
https.setTimeout(20000);
// Entso-E uses CET/CEST
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60};
tz = new Timezone(CEST, CET);
tomorrowFetchMillis = 36000000 + (random(1800) * 1000); // Random between 13:30 and 14:00
}
void EntsoeApi::setup(EntsoeConfig& config) {
@@ -61,8 +64,10 @@ float EntsoeApi::getValueForHour(time_t cur, uint8_t hour) {
} else {
return ENTSOE_NO_VALUE;
}
multiplier *= getCurrencyMultiplier(tomorrow->getCurrency(), config->currency);
} else {
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);
@@ -71,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;
}
@@ -140,9 +147,11 @@ bool EntsoeApi::loop() {
}
}
// Prices for next day are published at 13:00 CE(S)T, but to avoid heavy server traffic at that time, we will
// fetch 1 hr after that (with some random delay) and retry every 15 minutes
if(tomorrow == NULL
&& midnightMillis - now < 39600000 // Fetch 11hrs before midnight (13:00 CE(S)T)
&& (lastTomorrowFetch == 0 || now - lastTomorrowFetch > 300000) // Retry every 5min
&& midnightMillis - now < tomorrowFetchMillis
&& (lastTomorrowFetch == 0 || now - lastTomorrowFetch > 900000)
) {
lastTomorrowFetch = now;
time_t e1 = time(nullptr);
@@ -261,10 +270,18 @@ float EntsoeApi::getCurrencyMultiplier(const char* from, const char* to) {
return 1.00;
uint64_t now = millis64();
if(lastCurrencyFetch == 0 || now - lastCurrencyFetch > (SECS_PER_HOUR * 1000)) {
if(now > lastCurrencyFetch && (now - lastCurrencyFetch) < 900000) {
lastCurrencyFetch = now;
char url[256];
DnbCurrParser p;
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
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);
@@ -278,11 +295,15 @@ 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 = now;
lastCurrencyFetch = midnightMillis;
}
return currencyMultiplier;
}

View File

@@ -35,6 +35,7 @@ private:
WiFiClientSecure client;
HTTPClient https;
uint32_t tomorrowFetchMillis = 36000000; // Number of ms before midnight. Default fetch 10hrs before midnight (14:00 CE(S)T)
uint64_t midnightMillis = 0;
uint64_t lastTodayFetch = 0;
uint64_t lastTomorrowFetch = 0;

View File

@@ -458,9 +458,6 @@ void AmsWebServer::configMeterHtml() {
html.replace("{d" + String(i) + "}", meterConfig->distributionSystem == i ? "selected" : "");
}
html.replace("{f}", String(meterConfig->mainFuse));
for(int i = 0; i<64; i++) {
html.replace("{f" + String(i) + "}", meterConfig->mainFuse == i ? "selected" : "");
}
html.replace("{p}", String(meterConfig->productionCapacity));
if(meterConfig->encryptionKey[0] != 0x00) {
@@ -757,7 +754,7 @@ void AmsWebServer::dataJson() {
if(eapi != NULL && strlen(eapi->getToken()) > 0)
price = eapi->getValueForHour(0);
char json[384];
char json[512];
snprintf_P(json, sizeof(json), DATA_JSON,
maxPwr == 0 ? meterState->isThreePhase() ? 20000 : 10000 : maxPwr,
meterConfig->productionCapacity,
@@ -791,9 +788,9 @@ void AmsWebServer::dataJson() {
mqttStatus,
mqtt == NULL ? 0 : (int) mqtt->lastError(),
price == ENTSOE_NO_VALUE ? "null" : String(price, 2).c_str(),
time(nullptr),
meterState->getMeterType(),
meterConfig->distributionSystem
meterConfig->distributionSystem,
(uint32_t) time(nullptr)
);
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
@@ -1040,7 +1037,6 @@ void AmsWebServer::handleSetup() {
break;
case 200: // ESP32
gpioConfig->hanPin = 16;
gpioConfig->apPin = 0;
gpioConfig->ledPin = 2;
gpioConfig->ledInverted = false;
break;
@@ -1128,6 +1124,7 @@ void AmsWebServer::handleSave() {
meterConfig->distributionSystem = server.arg("d").toInt();
meterConfig->mainFuse = server.arg("f").toInt();
meterConfig->productionCapacity = server.arg("p").toInt();
maxPwr = 0;
String encryptionKeyHex = server.arg("e");
if(!encryptionKeyHex.isEmpty()) {
@@ -1279,7 +1276,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...");
@@ -1603,6 +1599,7 @@ void AmsWebServer::firmwareDownload() {
printI("Downloading firmware...");
HTTPClient httpClient;
httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
httpClient.setTimeout(20000);
httpClient.addHeader("User-Agent", "ams2mqtt/" + String(VERSION));
#if defined(ESP8266)
@@ -1621,6 +1618,13 @@ void AmsWebServer::firmwareDownload() {
if(status == HTTP_CODE_OK) {
printD("Received OK from server");
if(LittleFS.begin()) {
#if defined(ESP32)
esp_task_wdt_delete(NULL);
esp_task_wdt_deinit();
#elif defined(ESP8266)
ESP.wdtDisable();
#endif
printI("Downloading firmware to LittleFS");
file = LittleFS.open(FILE_FIRMWARE, "w");
int len = httpClient.writeToStream(&file);

View File

@@ -717,7 +717,7 @@ var fetch = function() {
if(ap && json.mf) {
switch(ds) {
case 1:
ao.title = 'Current between';
ao.title = 'Line current';
break;
case 2:
ao.title = 'Phase current';
@@ -730,19 +730,19 @@ var fetch = function() {
var i1 = parseFloat(json.i1);
a = Math.max(a, i1);
var pct = (parseFloat(json.i1)/parseInt(json.mf))*100;
arr[r++] = [ds == 1 ? 'L1-L2' : 'L1', pct, "color: " + ampcol(pct) + ";opacity: 0.9;", i1 + "A"];
arr[r++] = ['L1', pct, "color: " + ampcol(pct) + ";opacity: 0.9;", i1 + "A"];
}
if(json.i2) {
var i2 = parseFloat(json.i2);
a = Math.max(a, i2);
var pct = (parseFloat(json.i2)/parseInt(json.mf))*100;
arr[r++] = [ds == 1 ? 'L1-L3' : 'L2', pct, "color: " + ampcol(pct) + ";opacity: 0.9;", i2 + "A"];
arr[r++] = ['L2', pct, "color: " + ampcol(pct) + ";opacity: 0.9;", i2 + "A"];
}
if(json.i3) {
var i3 = parseFloat(json.i3);
a = Math.max(a, i3);
var pct = (parseFloat(json.i3)/parseInt(json.mf))*100;
arr[r++] = [ds == 1 ? 'L2-L3' : 'L3', pct, "color: " + ampcol(pct) + ";opacity: 0.9;", i3 + "A"];
arr[r++] = ['L3', pct, "color: " + ampcol(pct) + ";opacity: 0.9;", i3 + "A"];
}
if(a > 0) {
aa = google.visualization.arrayToDataTable(arr);

View File

@@ -31,7 +31,7 @@
"mm" : %d,
"me" : %d,
"p" : %s,
"c" : %lu,
"mt" : %d,
"ds" : %d
"ds" : %d,
"c" : %lu
}

View File

@@ -61,7 +61,7 @@
</div>
</div>
<div class="row">
<div class="col-xl-3 col-lg-4 col-md-5 col-sm-6">
<div class="col-xl-3 col-lg-4 col-md-5 col-sm-7">
<div class="m-2 input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Distribution system</span>
@@ -73,22 +73,15 @@
</select>
</div>
</div>
<div class="col-xl-2 col-md-3 col-sm-5">
<div class="col-lg-3 col-md-4 col-sm-5">
<div class="m-2 input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Main fuse</span>
</div>
<select class="form-control" name="f">
<option value="0" {f0}></option>
<option value="16" {f16}>16A</option>
<option value="20" {f20}>20A</option>
<option value="25" {f25}>25A</option>
<option value="32" {f32}>32A</option>
<option value="35" {f35}>35A</option>
<option value="40" {f40}>40A</option>
<option value="50" {f50}>50A</option>
<option value="63" {f63}>63A</option>
</select>
<input class="form-control text-right" name="f" type="number" min="5" max="255" step="1" value="{f}"/>
<div class="input-group-append">
<span class="input-group-text">A</span>
</div>
</div>
</div>
<div class="col-xl-3 col-lg-4 col-md-5 col-sm-7">
@@ -96,7 +89,7 @@
<div class="input-group-prepend">
<span class="input-group-text">Production capacity</span>
</div>
<input class="form-control" name="p" type="number" min="0" max="50" value="{p}"/>
<input class="form-control text-right" name="p" type="number" min="0" max="255" step="1" value="{p}"/>
<div class="input-group-append">
<span class="input-group-text">kWp</span>
</div>

BIN
webui2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB