Merge branch 'master' into mqtt_changes

This commit is contained in:
Gunnar Skjold 2023-11-08 20:34:39 +01:00
commit bbaf96f532
13 changed files with 108 additions and 55 deletions

View File

@ -1268,6 +1268,7 @@ void AmsConfiguration::print(Print* debugger)
debugger->printf_P(PSTR("Baud: %i\r\n"), meter.baud);
debugger->printf_P(PSTR("Parity: %i\r\n"), meter.parity);
debugger->printf_P(PSTR("Invert serial: %s\r\n"), meter.invert ? "Yes" : "No");
debugger->printf_P(PSTR("Buffer size: %i\r\n"), meter.bufferSize * 64);
debugger->printf_P(PSTR("Distribution system: %i\r\n"), meter.distributionSystem);
debugger->printf_P(PSTR("Main fuse: %i\r\n"), meter.mainFuse);
debugger->printf_P(PSTR("Production Capacity: %i\r\n"), meter.productionCapacity);

View File

@ -26,6 +26,7 @@ public:
char* getToken();
char* getCurrency();
char* getArea();
char* getSource();
float getValueForHour(int8_t);
float getValueForHour(time_t, int8_t);
@ -34,7 +35,7 @@ public:
private:
RemoteDebug* debugger;
EntsoeConfig* config = NULL;
HTTPClient http;
HTTPClient* http = NULL;
uint8_t currentDay = 0, currentHour = 0;
uint8_t tomorrowFetchMinute = 15; // How many minutes over 13:00 should it fetch prices

View File

@ -4,5 +4,6 @@ struct PricesContainer {
char currency[4];
char measurementUnit[4];
int32_t points[25];
char source[4];
};
#endif

View File

@ -108,8 +108,11 @@ size_t EntsoeA44Parser::write(uint8_t byte) {
}
void EntsoeA44Parser::get(PricesContainer* container) {
memset(container, 0, sizeof(*container));
strcpy(container->currency, currency);
strcpy(container->measurementUnit, measurementUnit);
strcpy(container->source, "EOE");
for(uint8_t i = 0; i < 25; i++) {
container->points[i] = points[i] == ENTSOE_NO_VALUE ? ENTSOE_NO_VALUE : points[i] * 10000;

View File

@ -34,11 +34,14 @@ void EntsoeApi::setup(EntsoeConfig& config) {
if(tomorrow != NULL) delete tomorrow;
today = tomorrow = NULL;
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.setReuse(false);
http.setTimeout(60000);
http.setUserAgent("ams2mqtt/" + String(FirmwareVersion::VersionString));
http.useHTTP10(true);
if(http != NULL) {
delete http;
}
http = new HTTPClient();
http->setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http->setReuse(false);
http->setTimeout(60000);
http->setUserAgent("ams2mqtt/" + String(FirmwareVersion::VersionString));
#if defined(AMS2MQTT_PRICE_KEY)
key = new uint8_t[16] AMS2MQTT_PRICE_KEY;
@ -67,6 +70,21 @@ char* EntsoeApi::getArea() {
return this->config->area;
}
char* EntsoeApi::getSource() {
if(this->today != NULL && this->tomorrow != NULL) {
if(strcmp(this->today->source, this->tomorrow->source) == 0) {
return this->today->source;
} else {
return "MIX";
}
} else if(today != NULL) {
return this->today->source;
} else if(tomorrow != NULL) {
return this->tomorrow->source;
}
return "";
}
float EntsoeApi::getValueForHour(int8_t hour) {
time_t cur = time(nullptr);
return getValueForHour(cur, hour);
@ -209,7 +227,7 @@ bool EntsoeApi::loop() {
bool EntsoeApi::retrieve(const char* url, Stream* doc) {
#if defined(ESP32)
if(http.begin(url)) {
if(http->begin(url)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Connection established\n"));
#if defined(ESP32)
@ -218,7 +236,7 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
ESP.wdtFeed();
#endif
int status = http.GET();
int status = http->GET();
#if defined(ESP32)
esp_task_wdt_reset();
@ -228,8 +246,8 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
if(status == HTTP_CODE_OK) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data\n"));
http.writeToStream(doc);
http.end();
http->writeToStream(doc);
http->end();
lastError = 0;
nextFetchDelayMinutes = 1;
return true;
@ -238,15 +256,15 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
if(status == 429) {
nextFetchDelayMinutes = 15;
} else if(status == 404) {
nextFetchDelayMinutes = 60;
nextFetchDelayMinutes = 10;
} else {
nextFetchDelayMinutes = 30;
nextFetchDelayMinutes = 2;
}
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(EntsoeApi) Communication error, returned status: %d\n"), status);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http.errorToString(status).c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http.getString().c_str());
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http->errorToString(status).c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str());
http.end();
http->end();
return false;
}
} else {
@ -349,11 +367,11 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {
#if defined(ESP8266)
WiFiClient client;
client.setTimeout(5000);
if(http.begin(client, buf)) {
if(http->begin(client, buf)) {
#elif defined(ESP32)
if(http.begin(buf)) {
if(http->begin(buf)) {
#endif
int status = http.GET();
int status = http->GET();
#if defined(ESP32)
esp_task_wdt_reset();
@ -363,8 +381,8 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {
if(status == HTTP_CODE_OK) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data\n"));
data = http.getString();
http.end();
data = http->getString();
http->end();
uint8_t* content = (uint8_t*) (data.c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) {
@ -403,15 +421,18 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {
if(status == 429) {
nextFetchDelayMinutes = 60;
} else if(status == 404) {
nextFetchDelayMinutes = 180;
nextFetchDelayMinutes = 15;
} else {
nextFetchDelayMinutes = 30;
nextFetchDelayMinutes = 5;
}
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(EntsoeApi) Communication error, returned status: %d\n"), status);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http.errorToString(status).c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http.getString().c_str());
if(debugger->isActive(RemoteDebug::ERROR)) {
debugger->printf(http->errorToString(status).c_str());
debugger->println();
}
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str());
http.end();
http->end();
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -351,7 +351,7 @@
<option value={10}>7E1</option>
<option value={11}>8E1</option>
</select>
<input name="ms" type="number" bind:value={configuration.m.s} min={64} max={4096} step={64} class="in-l tr w-1/2">
<input name="ms" type="number" bind:value={configuration.m.s} min={64} max={sysinfo.chip == 'esp8266' ? configuration.i.h.p == 3 || configuration.i.h.p == 113 ? 512 : 128 : 4096} step={64} class="in-l tr w-1/2">
</div>
</div>
<div class="my-1">

View File

@ -145,6 +145,8 @@ export function mqttError(err) {
export function priceError(err) {
switch(err) {
case 400:
return "Unrecognized data in request";
case 401:
case 403:
return "Unauthorized, check API key";
@ -156,6 +158,7 @@ export function priceError(err) {
return "Exceeded API rate limit";
case 500:
return "Internal server error";
case -1: return "Connection error";
case -2: return "Incomplete data received";
case -3: return "Invalid data, tag missing";
case -51: return "Authentication failed";
@ -229,4 +232,12 @@ export function getResetReason(sysinfo) {
default: return "Unknown";
}
}
}
export function getPriceSourceName(code) {
if(code == "EOE") return "ENTSO-E";
if(code == "HKS") return "hvakosterstrommen.no";
if(code == "EDS") return "Energy Data Service";
if(code == "MIX") return "Mixed sources";
return "Unknown (" + code + ")";
}

View File

@ -1,5 +1,5 @@
<script>
import { zeropad, addHours } from './Helpers.js';
import { zeropad, addHours, getPriceSourceName } from './Helpers.js';
import BarChart from './BarChart.svelte';
export let json;
@ -122,5 +122,5 @@
</script>
<a href="https://transparency.entsoe.eu/" target="_blank" class="text-xs float-right z-40">Provided by ENTSO-E</a>
<a href="https://transparency.entsoe.eu/" target="_blank" class="text-xs float-right z-40">Provided by: {getPriceSourceName(json.source)}</a>
<BarChart config={config} />

View File

@ -17,21 +17,21 @@ export default defineConfig({
plugins: [svelte()],
server: {
proxy: {
"/data.json": "http://192.168.233.244",
"/energyprice.json": "http://192.168.233.244",
"/dayplot.json": "http://192.168.233.244",
"/monthplot.json": "http://192.168.233.244",
"/temperature.json": "http://192.168.233.244",
"/sysinfo.json": "http://192.168.233.244",
"/configuration.json": "http://192.168.233.244",
"/tariff.json": "http://192.168.233.244",
"/save": "http://192.168.233.244",
"/reboot": "http://192.168.233.244",
"/configfile": "http://192.168.233.244",
"/upgrade": "http://192.168.233.244",
"/mqtt-ca": "http://192.168.233.244",
"/mqtt-cert": "http://192.168.233.244",
"/mqtt-key": "http://192.168.233.244",
"/data.json": "http://192.168.233.237",
"/energyprice.json": "http://192.168.233.237",
"/dayplot.json": "http://192.168.233.237",
"/monthplot.json": "http://192.168.233.237",
"/temperature.json": "http://192.168.233.237",
"/sysinfo.json": "http://192.168.233.237",
"/configuration.json": "http://192.168.233.237",
"/tariff.json": "http://192.168.233.237",
"/save": "http://192.168.233.237",
"/reboot": "http://192.168.233.237",
"/configfile": "http://192.168.233.237",
"/upgrade": "http://192.168.233.237",
"/mqtt-ca": "http://192.168.233.237",
"/mqtt-cert": "http://192.168.233.237",
"/mqtt-key": "http://192.168.233.237",
}
}
})

View File

@ -1,5 +1,6 @@
{
"currency" : "%s",
"source" : "%s",
"00" : %s,
"01" : %s,
"02" : %s,

View File

@ -667,6 +667,7 @@ void AmsWebServer::energyPriceJson() {
snprintf_P(buf, BufferSize, ENERGYPRICE_JSON,
eapi == NULL ? "" : eapi->getCurrency(),
eapi == NULL ? "" : eapi->getSource(),
prices[0] == ENTSOE_NO_VALUE ? "null" : String(prices[0], 4).c_str(),
prices[1] == ENTSOE_NO_VALUE ? "null" : String(prices[1], 4).c_str(),
prices[2] == ENTSOE_NO_VALUE ? "null" : String(prices[2], 4).c_str(),

View File

@ -791,9 +791,19 @@ void handleSystem(unsigned long now) {
// After one hour, adjust buffer size to match the largest payload
if(!maxDetectPayloadDetectDone && now > 3600000) {
if(maxDetectedPayloadSize * 1.5 > meterConfig.bufferSize * 64) {
meterConfig.bufferSize = min((double) 64, ceil((maxDetectedPayloadSize * 1.5) / 64));
debugI_P(PSTR("Increasing RX buffer to %d bytes"), meterConfig.bufferSize * 64);
config.setMeterConfig(meterConfig);
int bufferSize = min((double) 64, ceil((maxDetectedPayloadSize * 1.5) / 64));
#if defined(ESP8266)
if(gpioConfig.hanPin != 3 && gpioConfig.hanPin != 113) {
bufferSize = min(bufferSize, 2);
} else {
bufferSize = min(bufferSize, 8);
}
#endif
if(bufferSize != meterConfig.bufferSize) {
debugI_P(PSTR("Increasing RX buffer to %d bytes"), bufferSize * 64);
meterConfig.bufferSize = bufferSize;
config.setMeterConfig(meterConfig);
}
}
maxDetectPayloadDetectDone = true;
}
@ -1099,6 +1109,9 @@ void setupHanPort(GpioConfig& gpioConfig, uint32_t baud, uint8_t parityOrdinal,
break;
}
#if defined(ESP8266)
if(meterConfig.bufferSize > 2) meterConfig.bufferSize = 2;
#endif
swSerial->begin(baud, serialConfig, pin, -1, invert, meterConfig.bufferSize * 64);
hanSerial = swSerial;