From f0461a7cdb903418f70eadef8540bde7e8f39f59 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Mon, 17 Apr 2023 11:49:22 +0200 Subject: [PATCH] Some adjustments --- lib/JsonMqttHandler/src/JsonMqttHandler.cpp | 12 ++++++------ lib/SvelteUi/src/AmsWebServer.cpp | 2 +- src/AmsToMqttBridge.ino | 18 ++++++++++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/JsonMqttHandler/src/JsonMqttHandler.cpp b/lib/JsonMqttHandler/src/JsonMqttHandler.cpp index 84bb703c..e22d3c2e 100644 --- a/lib/JsonMqttHandler/src/JsonMqttHandler.cpp +++ b/lib/JsonMqttHandler/src/JsonMqttHandler.cpp @@ -157,13 +157,13 @@ bool JsonMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw) return false; } - snprintf(json, 24, "{\"temperatures\":{"); + snprintf_P(json, 24, PSTR("{\"temperatures\":{")); for(int i = 0; i < count; i++) { TempSensorData* data = hw->getTempSensorData(i); if(data != NULL) { char* pos = json+strlen(json); - snprintf(pos, 26, "\"%s\":%.2f,", + snprintf_P(pos, 26, PSTR("\"%s\":%.2f,"), toHex(data->address, 8).c_str(), data->lastRead ); @@ -172,7 +172,7 @@ bool JsonMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw) } } char* pos = json+strlen(json); - snprintf(count == 0 ? pos : pos-1, 8, "}}"); + snprintf_P(count == 0 ? pos : pos-1, 8, PSTR("}}")); bool ret = mqtt->publish(topic, json); mqtt->loop(); delay(10); @@ -243,7 +243,7 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) { time_t ts = now + (SECS_PER_HOUR * min1hrIdx); tmElements_t tm; breakTime(ts, tm); - sprintf(ts1hr, "%04d-%02d-%02dT%02d:00:00Z", tm.Year+1970, tm.Month, tm.Day, tm.Hour); + sprintf_P(ts1hr, PSTR("%04d-%02d-%02dT%02d:00:00Z"), tm.Year+1970, tm.Month, tm.Day, tm.Hour); } char ts3hr[24]; memset(ts3hr, 0, 24); @@ -251,7 +251,7 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) { time_t ts = now + (SECS_PER_HOUR * min3hrIdx); tmElements_t tm; breakTime(ts, tm); - sprintf(ts3hr, "%04d-%02d-%02dT%02d:00:00Z", tm.Year+1970, tm.Month, tm.Day, tm.Hour); + sprintf_P(ts3hr, PSTR("%04d-%02d-%02dT%02d:00:00Z"), tm.Year+1970, tm.Month, tm.Day, tm.Hour); } char ts6hr[24]; memset(ts6hr, 0, 24); @@ -259,7 +259,7 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) { time_t ts = now + (SECS_PER_HOUR * min6hrIdx); tmElements_t tm; breakTime(ts, tm); - sprintf(ts6hr, "%04d-%02d-%02dT%02d:00:00Z", tm.Year+1970, tm.Month, tm.Day, tm.Hour); + sprintf_P(ts6hr, PSTR("%04d-%02d-%02dT%02d:00:00Z"), tm.Year+1970, tm.Month, tm.Day, tm.Hour); } snprintf_P(json, BufferSize, JSONPRICES_JSON, diff --git a/lib/SvelteUi/src/AmsWebServer.cpp b/lib/SvelteUi/src/AmsWebServer.cpp index 04892541..5250125c 100644 --- a/lib/SvelteUi/src/AmsWebServer.cpp +++ b/lib/SvelteUi/src/AmsWebServer.cpp @@ -417,7 +417,7 @@ void AmsWebServer::dataJson() { mqttStatus = 3; } - float price = ea->getPriceForHour(0); + double price = ea->getPriceForHour(0); String peaks = ""; for(uint8_t i = 1; i <= ea->getConfig()->hours; i++) { diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index ef6462c4..3b1f2a13 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -100,7 +100,7 @@ AmsMqttHandler* mqttHandler = NULL; Stream *hanSerial; SoftwareSerial *swSerial = NULL; HardwareSerial *hwSerial = NULL; -size_t rxBufferSize = 64; +size_t rxBufferSize = 128; GpioConfig gpioConfig; MeterConfig meterConfig; @@ -752,6 +752,7 @@ void setupHanPort(GpioConfig& gpioConfig, uint32_t baud, uint8_t parityOrdinal, serialConfig = SERIAL_8E1; break; } + if(rxBufferSize < 256) rxBufferSize = 256; // 64 is default for software serial, 256 for hardware hwSerial->setRxBufferSize(rxBufferSize); #if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) @@ -777,14 +778,19 @@ void setupHanPort(GpioConfig& gpioConfig, uint32_t baud, uint8_t parityOrdinal, hwSerial->onReceiveError(rxerr); #endif hanSerial = hwSerial; - swSerial = NULL; + if(swSerial != NULL) { + swSerial->end(); + delete swSerial; + swSerial = NULL; + } } else { debugD("Software serial"); Serial.flush(); - if(swSerial != NULL) { + if(swSerial == NULL) { + swSerial = new SoftwareSerial(pin, -1, invert); + } else { swSerial->end(); - delete swSerial; } SoftwareSerialConfig serialConfig; @@ -803,7 +809,6 @@ void setupHanPort(GpioConfig& gpioConfig, uint32_t baud, uint8_t parityOrdinal, break; } - swSerial = new SoftwareSerial(pin, -1, invert); swSerial->begin(baud, serialConfig, pin, -1, invert, rxBufferSize); hanSerial = swSerial; @@ -814,6 +819,7 @@ void setupHanPort(GpioConfig& gpioConfig, uint32_t baud, uint8_t parityOrdinal, // The library automatically sets the pullup in Serial.begin() if(!gpioConfig.hanPinPullup) { + debugI("HAN pin pullup disabled"); pinMode(gpioConfig.hanPin, INPUT); } @@ -1415,7 +1421,7 @@ void MQTT_connect() { } yield(); } else { - mqtt = new MQTTClient(1500); + mqtt = new MQTTClient(1024); ws.setMqtt(mqtt); }