diff --git a/lib/HanReader/src/HanReader.cpp b/lib/HanReader/src/HanReader.cpp index a4a46902..5354f402 100644 --- a/lib/HanReader/src/HanReader.cpp +++ b/lib/HanReader/src/HanReader.cpp @@ -110,6 +110,12 @@ String HanReader::getString(int objectId) { return getString(objectId, buffer, 0, bytesRead); } +int HanReader::getBuffer(byte* buf) { + for (int i = 0; i < bytesRead; i++) { + buf[i] = buffer[i]; + } + return bytesRead; +} int HanReader::findValuePosition(int dataPosition, byte *buffer, int start, int length) { // The first byte after the header gives the length diff --git a/lib/HanReader/src/HanReader.h b/lib/HanReader/src/HanReader.h index 488d5313..2b6d91ab 100644 --- a/lib/HanReader/src/HanReader.h +++ b/lib/HanReader/src/HanReader.h @@ -29,6 +29,7 @@ public: uint32_t getUint(int objectId); // Only for uint32 String getString(int objectId); time_t getTime(int objectId); + int getBuffer(byte* buf); private: RemoteDebug* debugger; diff --git a/scripts/makeweb.py b/scripts/makeweb.py index df8323f6..4b054d82 100644 --- a/scripts/makeweb.py +++ b/scripts/makeweb.py @@ -39,4 +39,12 @@ for filename in os.listdir(webroot): dst.write(varname) dst.write("[] PROGMEM = R\"==\"==(") dst.write(content) - dst.write(")==\"==\";\n") \ No newline at end of file + dst.write(")==\"==\";\n") + dst.write("const int "); + dst.write(varname) + dst.write("_LEN = sizeof("); + dst.write(varname) + dst.write(")/sizeof("); + dst.write(varname) + dst.write("[0]);"); + \ No newline at end of file diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index d64ccb52..03f4670e 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -155,7 +155,7 @@ void setup() { } double vccBootLimit = config.getVccBootLimit(); - if(vccBootLimit > 0 && (config.getApPin() == -1 || digitalRead(config.getApPin()) == HIGH)) { // Skip if user is holding AP button while booting (HIGH = button is released) + if(vccBootLimit > 0 && (config.getApPin() == 0xFF || digitalRead(config.getApPin()) == HIGH)) { // Skip if user is holding AP button while booting (HIGH = button is released) if (vcc < vccBootLimit) { if(Debug.isActive(RemoteDebug::INFO)) { debugI("Voltage is too low, sleeping"); @@ -268,7 +268,7 @@ double energy = -1.0; void loop() { Debug.handle(); unsigned long now = millis(); - if(config.getApPin() != INVALID_BUTTON_PIN) { + if(config.getApPin() != 0xFF) { if (digitalRead(config.getApPin()) == LOW) { if (buttonActive == false) { buttonActive = true; @@ -715,6 +715,21 @@ void readHanPort() { mqtt.loop(); delay(10); } + } else { + if(strlen(config.getMqttHost()) > 0 && strlen(config.getMqttPublishTopic()) > 0) { + byte buf[512]; + int length = hanReader.getBuffer(buf); + String hexstring = ""; + + for(int i = 0; i < length; i++) { + if(buf[i] < 0x10) { + hexstring += '0'; + } + + hexstring += String(buf[i], HEX); + } + mqtt.publish(String(config.getMqttPublishTopic()), hexstring); + } } } else { // Auto detect meter if not set @@ -813,14 +828,14 @@ void MQTT_connect() { } lastMqttRetry = millis(); if(Debug.isActive(RemoteDebug::INFO)) { - debugI("Connecting to MQTT %s:%d", config.getMqttHost(), config.getMqttPort()); + debugD("Disconnecting MQTT before connecting"); } mqtt.disconnect(); yield(); - WiFiClientSecure *secureClient; - Client *client; + WiFiClientSecure *secureClient = NULL; + Client *client = NULL; if(config.isMqttSsl()) { debugI("MQTT SSL is configured"); @@ -858,10 +873,16 @@ void MQTT_connect() { client = new WiFiClient(); } + if(Debug.isActive(RemoteDebug::INFO)) { + debugI("Connecting to MQTT %s:%d", config.getMqttHost(), config.getMqttPort()); + } mqtt.begin(config.getMqttHost(), config.getMqttPort(), *client); #if defined(ESP8266) - if(secureClient) secureClient->setX509Time(timeClient.getEpochTime()); + if(secureClient) { + debugD("Setting NTP time for secure MQTT connection"); + secureClient->setX509Time(timeClient.getEpochTime()); + } #endif // Connect to a unsecure or secure MQTT server diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 7735a342..07b82b46 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -2,6 +2,8 @@ #include "version.h" #include "AmsStorage.h" +#include "root/head_html.h" +#include "root/foot_html.h" #include "root/index_html.h" #include "root/index_js.h" #include "root/setup_html.h" @@ -126,7 +128,6 @@ void AmsWebServer::indexHtml() { return; String html = String((const __FlashStringHelper*) INDEX_HTML); - html.replace("${version}", VERSION); double u1 = data.getL1Voltage(); double u2 = data.getL2Voltage(); @@ -193,7 +194,6 @@ void AmsWebServer::configMeterHtml() { return; String html = String((const __FlashStringHelper*) CONFIGMETER_HTML); - html.replace("${version}", VERSION); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); @@ -224,7 +224,6 @@ void AmsWebServer::configWifiHtml() { return; String html = String((const __FlashStringHelper*) CONFIGWIFI_HTML); - html.replace("${version}", VERSION); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); @@ -250,7 +249,6 @@ void AmsWebServer::configMqttHtml() { return; String html = String((const __FlashStringHelper*) CONFIGMQTT_HTML); - html.replace("${version}", VERSION); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); @@ -292,8 +290,10 @@ void AmsWebServer::configMqttHtml() { html.replace("${display.key.file}", "none"); } - server.setContentLength(html.length()); - server.send(200, "text/html", html); + server.setContentLength(html.length() + HEAD_HTML_LEN + FOOT_HTML_LEN); + server.send_P(200, "text/html", HEAD_HTML); + server.sendContent(html); + server.sendContent_P(FOOT_HTML); } void AmsWebServer::configDomoticzHtml() { @@ -303,7 +303,6 @@ void AmsWebServer::configDomoticzHtml() { return; String html = String((const __FlashStringHelper*) CONFIGDOMOTICZ_HTML); - html.replace("${version}", VERSION); if(WiFi.getMode() != WIFI_AP) { html.replace("boot.css", BOOTSTRAP_URL); @@ -335,7 +334,6 @@ void AmsWebServer::configWebHtml() { return; String html = String((const __FlashStringHelper*) CONFIGWEB_HTML); - html.replace("${version}", VERSION); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); @@ -765,7 +763,6 @@ void AmsWebServer::configSystemHtml() { return; String html = String((const __FlashStringHelper*) CONFIGSYSTEM_HTML); - html.replace("${version}", VERSION); #if defined(ESP32) html.replace("${gpio.max}", "39"); @@ -912,7 +909,6 @@ void AmsWebServer::restartWaitHtml() { return; String html = String((const __FlashStringHelper*) RESTARTWAIT_HTML); - html.replace("${version}", VERSION); if(WiFi.getMode() != WIFI_AP) { html.replace("boot.css", BOOTSTRAP_URL); diff --git a/web/configmqtt.html b/web/configmqtt.html index 44732867..abab7964 100644 --- a/web/configmqtt.html +++ b/web/configmqtt.html @@ -1,43 +1,3 @@ - - - - - AMS reader - MQTT configuration - - - - - -
-
@@ -173,7 +133,6 @@
-
- - diff --git a/web/foot.html b/web/foot.html new file mode 100644 index 00000000..98653613 --- /dev/null +++ b/web/foot.html @@ -0,0 +1,3 @@ + + + diff --git a/web/head.html b/web/head.html new file mode 100644 index 00000000..103ea372 --- /dev/null +++ b/web/head.html @@ -0,0 +1,39 @@ + + + + + AMS reader - WiFi configuration + + + + +
+