From fc1f1554d8f79e488ce9694ddbe934b40aedb8c9 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Fri, 31 Jan 2020 21:08:27 +0100 Subject: [PATCH 1/9] Extracted webroot into usable files --- .gitignore | 2 + platformio.ini | 9 +- addversion.py => scripts/addversion.py | 0 scripts/makeweb.py | 25 ++ src/AmsToMqttBridge.ino | 2 +- src/AmsToMqttBridge.ino.cpp | 380 ------------------ src/{ => web}/AmsWebServer.cpp | 22 +- src/{ => web}/AmsWebServer.h | 0 src/application_css.h => web/application.css | 2 - src/boot_css.h => web/boot.css | 2 - .../configuration.html | 6 +- src/gaugemeter_js.h => web/gaugemeter.js | 2 - src/index_html.h => web/index.html | 10 +- src/index_js.h => web/index.js | 3 - 14 files changed, 51 insertions(+), 414 deletions(-) rename addversion.py => scripts/addversion.py (100%) create mode 100644 scripts/makeweb.py delete mode 100644 src/AmsToMqttBridge.ino.cpp rename src/{ => web}/AmsWebServer.cpp (95%) rename src/{ => web}/AmsWebServer.h (100%) rename src/application_css.h => web/application.css (93%) rename src/boot_css.h => web/boot.css (99%) rename src/configuration_html.h => web/configuration.html (97%) rename src/gaugemeter_js.h => web/gaugemeter.js (99%) rename src/index_html.h => web/index.html (92%) rename src/index_js.h => web/index.js (97%) diff --git a/.gitignore b/.gitignore index 63ae6684..8f43c6fb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ .pio platformio-user.ini /src/version.h +/src/web/root +/src/AmsToMqttBridge.ino.cpp diff --git a/platformio.ini b/platformio.ini index 1b452904..40f5cfde 100755 --- a/platformio.ini +++ b/platformio.ini @@ -15,7 +15,8 @@ build_flags = -D HAS_DALLAS_TEMP_SENSOR=0 -D IS_CUSTOM_AMS_BOARD=0 extra_scripts = - pre:addversion.py + pre:scripts/addversion.py + scripts/makeweb.py [env:hw1esp12e] platform = espressif8266 @@ -26,7 +27,8 @@ build_flags = -D HAS_DALLAS_TEMP_SENSOR=1 -D IS_CUSTOM_AMS_BOARD=1 extra_scripts = - pre:addversion.py + pre:scripts/addversion.py + scripts/makeweb.py [env:featheresp32] platform = espressif32 @@ -37,4 +39,5 @@ build_flags = -D HAS_DALLAS_TEMP_SENSOR=0 -D IS_CUSTOM_AMS_BOARD=0 extra_scripts = - pre:addversion.py + pre:scripts/addversion.py + scripts/makeweb.py diff --git a/addversion.py b/scripts/addversion.py similarity index 100% rename from addversion.py rename to scripts/addversion.py diff --git a/scripts/makeweb.py b/scripts/makeweb.py new file mode 100644 index 00000000..89f8e9a9 --- /dev/null +++ b/scripts/makeweb.py @@ -0,0 +1,25 @@ +import os +import re + +webroot = "web" +srcroot = "src/web/root" + +if not os.path.exists(srcroot): + os.mkdir(srcroot) + +for filename in os.listdir(webroot): + basename = re.sub("[^0-9a-zA-Z]+", "_", filename) + + srcfile = webroot + "/" + filename + dstfile = srcroot + "/" + basename + ".h" + + varname = basename.upper() + + with open(dstfile, "w") as dst: + dst.write("const char ") + dst.write(varname) + dst.write("[] PROGMEM = R\"==\"==(\n") + with open(srcfile, "r") as src: + for line in src.readlines(): + dst.write(line) + dst.write("\n)==\"==\";\n") \ No newline at end of file diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 69ce40c6..331b9d39 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -22,7 +22,7 @@ #include #endif -#include "AmsWebServer.h" +#include "web/AmsWebServer.h" #include "HanConfigAp.h" #include "HanReader.h" #include "HanToJson.h" diff --git a/src/AmsToMqttBridge.ino.cpp b/src/AmsToMqttBridge.ino.cpp deleted file mode 100644 index 924fdb94..00000000 --- a/src/AmsToMqttBridge.ino.cpp +++ /dev/null @@ -1,380 +0,0 @@ -# 1 "/tmp/tmpfprbzre1" -#include -# 1 "/home/gunnar/src/AmsToMqttBridge/src/AmsToMqttBridge.ino" -# 11 "/home/gunnar/src/AmsToMqttBridge/src/AmsToMqttBridge.ino" -#include -#include - -#if HAS_DALLAS_TEMP_SENSOR -#include -#include -#endif - -#if defined(ESP8266) -#include -#elif defined(ESP32) -#include -#endif - -#include "AmsWebServer.h" -#include "HanConfigAp.h" -#include "HanReader.h" -#include "HanToJson.h" - -#define WIFI_CONNECTION_TIMEOUT 30000; - -#if IS_CUSTOM_AMS_BOARD -#define LED_PIN 2 -#define LED_ACTIVE_HIGH 0 -#define AP_BUTTON_PIN 0 -#else -#define LED_PIN LED_BUILTIN -#define LED_ACTIVE_HIGH 1 -#define AP_BUTTON_PIN INVALID_BUTTON_PIN -#endif - -#if HAS_DALLAS_TEMP_SENSOR -#define TEMP_SENSOR_PIN 5 - -OneWire oneWire(TEMP_SENSOR_PIN); -DallasTemperature tempSensor(&oneWire); -#endif - - -HanConfigAp ap; - -AmsWebServer ws; - - -WiFiClient *client; -MQTTClient mqtt(384); - - -HardwareSerial* debugger = NULL; - - -HanReader hanReader; -void setup(); -void loop(); -void led_on(); -void led_off(); -void setupWiFi(); -void mqttMessageReceived(String &topic, String &payload); -void readHanPort(); -void MQTT_connect(); -void sendMqttData(String data); -#line 65 "/home/gunnar/src/AmsToMqttBridge/src/AmsToMqttBridge.ino" -void setup() { - -#if DEBUG_MODE - debugger = &Serial; -#endif - - if (debugger) { - - debugger->begin(2400, SERIAL_8E1); - - while (!debugger); - debugger->println(""); - debugger->println("Started..."); - } - - - pinMode(LED_PIN, OUTPUT); - led_on(); - - delay(1000); - - - ap.setup(AP_BUTTON_PIN, debugger); - - led_off(); - - if (!ap.isActivated) - { - setupWiFi(); - - if(ap.config.meterType == 3) { - Serial.begin(2400, SERIAL_8N1); - } else { - Serial.begin(2400, SERIAL_8E1); - } - while (!Serial); - - hanReader.setup(&Serial, debugger); - - - hanReader.compensateFor09HeaderBug = (ap.config.meterType == 1); - } - - ws.setup(&ap.config, debugger); -} - - -void loop() -{ - - if (!ap.loop()) - { - - led_off(); - - - mqtt.loop(); - delay(10); - - - if (!mqtt.connected()) { - MQTT_connect(); - } else { - readHanPort(); - } - } - else - { - - if (millis() / 1000 % 2 == 0) led_on(); - else led_off(); - } - ws.loop(); -} - - -void led_on() -{ -#if LED_ACTIVE_HIGH - digitalWrite(LED_PIN, HIGH); -#else - digitalWrite(LED_PIN, LOW); -#endif -} - - -void led_off() -{ -#if LED_ACTIVE_HIGH - digitalWrite(LED_PIN, LOW); -#else - digitalWrite(LED_PIN, HIGH); -#endif -} - - -void setupWiFi() -{ - - WiFi.enableAP(false); - - - WiFi.mode(WIFI_STA); - WiFi.begin(ap.config.ssid, ap.config.ssidPassword); - - - if (debugger) debugger->print("\nWaiting for WiFi to connect..."); - while (WiFi.status() != WL_CONNECTED) { - if (debugger) debugger->print("."); - delay(500); - } - if (debugger) debugger->println(" connected"); - - client = new WiFiClient(); - mqtt.begin(ap.config.mqtt, *client); - - - if (ap.config.mqttSubscribeTopic != 0 && strlen(ap.config.mqttSubscribeTopic) > 0) { - mqtt.subscribe(ap.config.mqttSubscribeTopic); - mqtt.onMessage(mqttMessageReceived); - } - - - sendMqttData("Connected!"); -} - -void mqttMessageReceived(String &topic, String &payload) -{ - - if (debugger) { - debugger->println("Incoming MQTT message:"); - debugger->print("["); - debugger->print(topic); - debugger->print("] "); - debugger->println(payload); - } - - - -} - -void readHanPort() -{ - if (hanReader.read() && ap.config.hasConfig()) - { - - led_on(); - - - time_t time = hanReader.getPackageTime(); - if (debugger) debugger->print("Time of the package is: "); - if (debugger) debugger->println(time); - - - StaticJsonDocument<500> json; - - - json["id"] = WiFi.macAddress(); - json["up"] = millis(); - json["t"] = time; - - - - JsonObject data = json.createNestedObject("data"); - -#if HAS_DALLAS_TEMP_SENSOR - - tempSensor.requestTemperatures(); - data["temp"] = tempSensor.getTempCByIndex(0); -#endif - - hanToJson(data, ap.config.meterType, hanReader); - - if(ap.config.mqtt != 0 && strlen(ap.config.mqtt) != 0 && ap.config.mqttPublishTopic != 0 && strlen(ap.config.mqttPublishTopic) != 0) { - - if (debugger) { - debugger->print("Sending data to MQTT: "); - serializeJsonPretty(json, *debugger); - debugger->println(); - } - - - String msg; - serializeJson(json, msg); - - mqtt.publish(ap.config.mqttPublishTopic, msg.c_str()); - mqtt.loop(); - } - ws.setJson(json); - - - led_off(); - } -} - - - - -void MQTT_connect() -{ - - if (debugger) - { - debugger->println(); - debugger->println(); - debugger->print("Connecting to WiFi network "); - debugger->println(ap.config.ssid); - } - - if (WiFi.status() != WL_CONNECTED) - { - - WiFi.disconnect(); - WiFi.begin(ap.config.ssid, ap.config.ssidPassword); - delay(1000); - } - - - long vTimeout = millis() + WIFI_CONNECTION_TIMEOUT; - while (WiFi.status() != WL_CONNECTED) { - delay(50); - if (debugger) debugger->print("."); - - - if (vTimeout < millis()) - { - if (debugger) - { - debugger->print("Timout during connect. WiFi status is: "); - debugger->println(WiFi.status()); - } - WiFi.disconnect(); - WiFi.begin(ap.config.ssid, ap.config.ssidPassword); - vTimeout = millis() + WIFI_CONNECTION_TIMEOUT; - } - yield(); - } - - if (debugger) { - debugger->println(); - debugger->println("WiFi connected"); - debugger->println("IP address: "); - debugger->println(WiFi.localIP()); - debugger->print("\nconnecting to MQTT: "); - debugger->print(ap.config.mqtt); - debugger->print(", port: "); - debugger->print(ap.config.mqttPort); - debugger->println(); - } - - - while (!mqtt.connected()) { - - if ((ap.config.mqttUser == 0 && mqtt.connect(ap.config.mqttClientID)) || - (ap.config.mqttUser != 0 && mqtt.connect(ap.config.mqttClientID, ap.config.mqttUser, ap.config.mqttPass))) - { - if (debugger) debugger->println("\nSuccessfully connected to MQTT!"); - - - if (ap.config.mqttSubscribeTopic != 0 && strlen(ap.config.mqttSubscribeTopic) > 0) - { - mqtt.subscribe(ap.config.mqttSubscribeTopic); - if (debugger) debugger->printf(" Subscribing to [%s]\r\n", ap.config.mqttSubscribeTopic); - } - } - else - { - if (debugger) - { - debugger->print("."); - debugger->print("failed, "); - debugger->println(" trying again in 5 seconds"); - } - - - mqtt.disconnect(); - - delay(2000); - } - - - yield(); - delay(2000); - } -} - - -void sendMqttData(String data) -{ - - if (ap.config.mqttPublishTopic == 0 || strlen(ap.config.mqttPublishTopic) == 0) - return; - - - if (!client->connected() || !mqtt.connected()) { - MQTT_connect(); - } - - - StaticJsonDocument<500> json; - json["id"] = WiFi.macAddress(); - json["up"] = millis(); - json["data"] = data; - - - String msg; - serializeJson(json, msg); - - - mqtt.publish(ap.config.mqttPublishTopic, msg.c_str()); - - if (debugger) debugger->print("sendMqttData: "); - if (debugger) debugger->println(data); -} \ No newline at end of file diff --git a/src/AmsWebServer.cpp b/src/web/AmsWebServer.cpp similarity index 95% rename from src/AmsWebServer.cpp rename to src/web/AmsWebServer.cpp index 36abe811..4cbdd5ab 100644 --- a/src/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -1,12 +1,12 @@ #include "AmsWebServer.h" #include "version.h" -#include "index_html.h" -#include "configuration_html.h" -#include "boot_css.h" -#include "application_css.h" -#include "gaugemeter_js.h" -#include "index_js.h" +#include "root/index_html.h" +#include "root/configuration_html.h" +#include "root/boot_css.h" +#include "root/application_css.h" +#include "root/gaugemeter_js.h" +#include "root/index_js.h" #include "Base64.h" @@ -22,10 +22,10 @@ void AmsWebServer::setup(configuration* config, Stream* debugger) { server.on("/", std::bind(&AmsWebServer::indexHtml, this)); server.on("/configuration", std::bind(&AmsWebServer::configurationHtml, this)); - server.on("/css/boot.css", std::bind(&AmsWebServer::bootCss, this)); - server.on("/css/application.css", std::bind(&AmsWebServer::applicationCss, this)); - server.on("/js/gaugemeter.js", std::bind(&AmsWebServer::gaugemeterJs, this)); - server.on("/js/index.js", std::bind(&AmsWebServer::indexJs, this)); + server.on("/boot.css", std::bind(&AmsWebServer::bootCss, this)); + server.on("/application.css", std::bind(&AmsWebServer::applicationCss, this)); + server.on("/gaugemeter.js", std::bind(&AmsWebServer::gaugemeterJs, this)); + server.on("/index.js", std::bind(&AmsWebServer::indexJs, this)); server.on("/data.json", std::bind(&AmsWebServer::dataJson, this)); server.on("/save", std::bind(&AmsWebServer::handleSave, this)); @@ -234,7 +234,7 @@ void AmsWebServer::gaugemeterJs() { server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); - server.send(200, "application/javascript", GAUEGMETER_JS); + server.send(200, "application/javascript", GAUGEMETER_JS); } void AmsWebServer::indexJs() { diff --git a/src/AmsWebServer.h b/src/web/AmsWebServer.h similarity index 100% rename from src/AmsWebServer.h rename to src/web/AmsWebServer.h diff --git a/src/application_css.h b/web/application.css similarity index 93% rename from src/application_css.h rename to web/application.css index 661348ff..80c1b828 100644 --- a/src/application_css.h +++ b/web/application.css @@ -1,4 +1,3 @@ -const char APPLICATION_CSS[] PROGMEM = R"=="==( .bg-purple { background-color: var(--purple); } @@ -43,4 +42,3 @@ const char APPLICATION_CSS[] PROGMEM = R"=="==( font-weight: 200; opacity: .8; } -)=="=="; diff --git a/src/boot_css.h b/web/boot.css similarity index 99% rename from src/boot_css.h rename to web/boot.css index 342066a8..1584073e 100644 --- a/src/boot_css.h +++ b/web/boot.css @@ -1,4 +1,3 @@ -const char BOOT_CSS[] PROGMEM = R"=="==( /* Ripped necessary style from bootstrap 4.4.1 to make the page look good without internet access. Meant to be overridden by CSS from CDN */ :root { --blue: #007bff; @@ -324,4 +323,3 @@ hr { *, ::after, ::before { box-sizing: border-box; } -)=="=="; diff --git a/src/configuration_html.h b/web/configuration.html similarity index 97% rename from src/configuration_html.h rename to web/configuration.html index 28e8028c..129b1ff5 100644 --- a/src/configuration_html.h +++ b/web/configuration.html @@ -1,13 +1,12 @@ -const char CONFIGURATION_HTML[] PROGMEM = R"=="==( AMS reader - configuration - + - +
@@ -145,4 +144,3 @@ const char CONFIGURATION_HTML[] PROGMEM = R"=="==(
-)=="=="; diff --git a/src/gaugemeter_js.h b/web/gaugemeter.js similarity index 99% rename from src/gaugemeter_js.h rename to web/gaugemeter.js index 5eeb432a..fc4e5b9e 100644 --- a/src/gaugemeter_js.h +++ b/web/gaugemeter.js @@ -1,4 +1,3 @@ -const char GAUEGMETER_JS[] PROGMEM = R"=="==( /* * AshAlom Gauge Meter. Version 2.0.0 * Copyright AshAlom.com All rights reserved. @@ -274,4 +273,3 @@ const char GAUEGMETER_JS[] PROGMEM = R"=="==( }; } (jQuery); -)=="=="; diff --git a/src/index_html.h b/web/index.html similarity index 92% rename from src/index_html.h rename to web/index.html index 3fbeb677..ba7f56d2 100644 --- a/src/index_html.h +++ b/web/index.html @@ -1,15 +1,14 @@ -const char INDEX_HTML[] PROGMEM = R"=="==( AMS reader - + - + - +
@@ -73,7 +72,6 @@ const char INDEX_HTML[] PROGMEM = R"=="==(
- + -)=="=="; diff --git a/src/index_js.h b/web/index.js similarity index 97% rename from src/index_js.h rename to web/index.js index 5a02bfe4..f6447ba3 100644 --- a/src/index_js.h +++ b/web/index.js @@ -1,4 +1,3 @@ -const char INDEX_JS[] PROGMEM = R"=="==( $(".GaugeMeter").gaugeMeter(); var wait = 500; @@ -76,5 +75,3 @@ var fetch = function() { }); } setTimeout(fetch, nextrefresh); - -)=="=="; From 57d860379036189350ba1b91db7294ac0df20c5a Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Fri, 31 Jan 2020 21:30:20 +0100 Subject: [PATCH 2/9] Added option for electric distribution system (230/400) --- lib/HanConfigAp/src/configuration.cpp | 7 +++++- lib/HanConfigAp/src/configuration.h | 3 ++- src/web/AmsWebServer.cpp | 34 +++++++++++++++++---------- web/configuration.html | 18 ++++++++++---- web/index.html | 4 ++-- 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/lib/HanConfigAp/src/configuration.cpp b/lib/HanConfigAp/src/configuration.cpp index 0a14ae95..1db4e11b 100644 --- a/lib/HanConfigAp/src/configuration.cpp +++ b/lib/HanConfigAp/src/configuration.cpp @@ -8,7 +8,7 @@ bool configuration::hasConfig() { bool hasConfig = false; EEPROM.begin(EEPROM_SIZE); - hasConfig = EEPROM.read(EEPROM_CONFIG_ADDRESS) == EEPROM_CHECK_SUM; + hasConfig = EEPROM.read(EEPROM_CONFIG_ADDRESS) >= 71; EEPROM.end(); return hasConfig; } @@ -46,6 +46,7 @@ bool configuration::save() } address += saveInt(address, fuseSize); + address += saveInt(address, distSys); bool success = EEPROM.commit(); EEPROM.end(); @@ -73,6 +74,7 @@ bool configuration::load() authUser = 0; authPass = 0; fuseSize = 0; + distSys = 0; EEPROM.begin(EEPROM_SIZE); int cs = EEPROM.read(address); @@ -118,6 +120,9 @@ bool configuration::load() if(cs >= 73) { address += readInt(address, &fuseSize); } + if(cs >= 74) { + address += readByte(address, &distSys); + } EEPROM.end(); return success; } diff --git a/lib/HanConfigAp/src/configuration.h b/lib/HanConfigAp/src/configuration.h index 68a142ba..76586eaa 100644 --- a/lib/HanConfigAp/src/configuration.h +++ b/lib/HanConfigAp/src/configuration.h @@ -30,6 +30,7 @@ public: char* authPass; int fuseSize; + byte distSys; bool hasConfig(); bool isSecure(); @@ -41,7 +42,7 @@ protected: private: const int EEPROM_SIZE = 512; - const byte EEPROM_CHECK_SUM = 73; // Used to check if config is stored. Change if structure changes + const byte EEPROM_CHECK_SUM = 74; // Used to check if config is stored. Change if structure changes const int EEPROM_CONFIG_ADDRESS = 0; int saveString(int pAddress, char* pString); diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 4cbdd5ab..efa3ea06 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -39,12 +39,6 @@ void AmsWebServer::setup(configuration* config, Stream* debugger) { print(WiFi.localIP()); } println("/"); - - if(config->hasConfig() && config->fuseSize > 0) { - maxPwr = config->fuseSize * 230; - } else { - maxPwr = 20000; - } } void AmsWebServer::loop() { @@ -62,13 +56,12 @@ void AmsWebServer::setJson(StaticJsonDocument<500> json) { i2 = json["data"]["I2"].as(); i3 = json["data"]["I3"].as(); - if(config->hasConfig() && u1 > 0) { - maxPwr = config->fuseSize * u1; + if(maxPwr == 0 && config->hasConfig() && config->fuseSize > 0 && config->distSys > 0) { + int volt = config->distSys == 2 ? 400 : 230; if(u2 > 0) { - maxPwr += config->fuseSize * u2; - if(u3 > 0) { - maxPwr += config->fuseSize * u3; - } + maxPwr = config->fuseSize * sqrt(3) * volt; + } else { + maxPwr = config->fuseSize * 230; } } } else { @@ -182,6 +175,9 @@ void AmsWebServer::configurationHtml() { for(int i = 0; i<64; i++) { html.replace("${config.fuseSize" + String(i) + "}", config->fuseSize == i ? "selected" : ""); } + for(int i = 0; i<3; i++) { + html.replace("${config.distSys" + String(i) + "}", config->distSys == i ? "selected" : ""); + } } else { html.replace("${config.ssid}", ""); html.replace("${config.ssidPassword}", ""); @@ -206,6 +202,9 @@ void AmsWebServer::configurationHtml() { for(int i = 0; i<64; i++) { html.replace("${config.fuseSize" + String(i) + "}", i == 0 ? "selected" : ""); } + for(int i = 0; i<3; i++) { + html.replace("${config.distSys" + String(i) + "}", i == 0 ? "selected" : ""); + } } server.send(200, "text/html", html); } @@ -256,6 +255,15 @@ void AmsWebServer::dataJson() { if(!json.isNull()) { println(" json has data"); + int maxPwr = this->maxPwr; + if(maxPwr == 0) { + if(u2 > 0) { + maxPwr = 25000; + } else { + maxPwr = 15000; + } + } + json["maxPower"] = maxPwr; json["pct"] = min(p*100/maxPwr, 100); json["meterType"] = config->meterType; @@ -324,6 +332,8 @@ void AmsWebServer::handleSave() { config->fuseSize = (int)server.arg("fuseSize").toInt(); + config->distSys = (byte)server.arg("distSys").toInt(); + println("Saving configuration now..."); if (debugger) config->print(debugger); diff --git a/web/configuration.html b/web/configuration.html index 129b1ff5..d8317e81 100644 --- a/web/configuration.html +++ b/web/configuration.html @@ -37,8 +37,18 @@
AMS meter
- -
+ +
+ +
+
+
+ +
diff --git a/web/index.html b/web/index.html index ba7f56d2..69c2edad 100644 --- a/web/index.html +++ b/web/index.html @@ -68,10 +68,10 @@ Release notes
- + From 8f85b43fc3358f76ee917ec5b3bfa6bcfed82b04 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 1 Feb 2020 09:52:37 +0100 Subject: [PATCH 3/9] Made MQTT optional --- lib/HanConfigAp/src/configuration.cpp | 21 +++-- lib/HanConfigAp/src/configuration.h | 2 +- scripts/makeweb.py | 3 + src/AmsToMqttBridge.ino | 57 ++++++------ src/web/AmsWebServer.cpp | 90 ++++++++---------- src/web/AmsWebServer.h | 2 - web/application.css | 44 --------- web/boot.css | 4 + web/configuration.html | 47 ++++++++-- web/index.html | 126 +++++++++++++++++++++++++- web/index.js | 77 ---------------- 11 files changed, 251 insertions(+), 222 deletions(-) delete mode 100644 web/application.css delete mode 100644 web/index.js diff --git a/lib/HanConfigAp/src/configuration.cpp b/lib/HanConfigAp/src/configuration.cpp index 1db4e11b..27e64803 100644 --- a/lib/HanConfigAp/src/configuration.cpp +++ b/lib/HanConfigAp/src/configuration.cpp @@ -24,7 +24,7 @@ bool configuration::save() address += saveString(address, ssid); address += saveString(address, ssidPassword); address += saveByte(address, meterType); - address += saveString(address, mqtt); + address += saveString(address, mqttHost); address += saveInt(address, mqttPort); address += saveString(address, mqttClientID); address += saveString(address, mqttPublishTopic); @@ -63,7 +63,7 @@ bool configuration::load() ssid = (char*)String("").c_str(); ssidPassword = (char*)String("").c_str(); meterType = (byte)0; - mqtt = (char*)String("").c_str(); + mqttHost = (char*)String("").c_str(); mqttClientID = (char*)String("").c_str(); mqttPublishTopic = (char*)String("").c_str(); mqttSubscribeTopic = (char*)String("").c_str(); @@ -85,7 +85,7 @@ bool configuration::load() address += readString(address, &ssid); address += readString(address, &ssidPassword); address += readByte(address, &meterType); - address += readString(address, &mqtt); + address += readString(address, &mqttHost); address += readInt(address, &mqttPort); address += readString(address, &mqttClientID); address += readString(address, &mqttPublishTopic); @@ -182,11 +182,13 @@ void configuration::print(Stream* debugger) debugger->printf("ssid: %s\r\n", this->ssid); debugger->printf("ssidPassword: %s\r\n", this->ssidPassword); debugger->printf("meterType: %i\r\n", this->meterType); - debugger->printf("mqtt: %s\r\n", this->mqtt); - debugger->printf("mqttPort: %i\r\n", this->mqttPort); - debugger->printf("mqttClientID: %s\r\n", this->mqttClientID); - debugger->printf("mqttPublishTopic: %s\r\n", this->mqttPublishTopic); - debugger->printf("mqttSubscribeTopic: %s\r\n", this->mqttSubscribeTopic); + if(this->mqttHost) { + debugger->printf("mqttHost: %s\r\n", this->mqttHost); + debugger->printf("mqttPort: %i\r\n", this->mqttPort); + debugger->printf("mqttClientID: %s\r\n", this->mqttClientID); + debugger->printf("mqttPublishTopic: %s\r\n", this->mqttPublishTopic); + debugger->printf("mqttSubscribeTopic: %s\r\n", this->mqttSubscribeTopic); + } if (this->isSecure()) { @@ -202,6 +204,7 @@ void configuration::print(Stream* debugger) debugger->printf("authPass: %s\r\n", this->authPass); } debugger->printf("fuseSize: %i\r\n", this->fuseSize); + debugger->printf("distSys: %i\r\n", this->distSys); debugger->println("-----------------------------------------------"); } @@ -241,7 +244,7 @@ int configuration::readString(int pAddress, char* pString[]) int configuration::saveString(int pAddress, char* pString) { int address = 0; - int length = strlen(pString) + 1; + int length = pString ? strlen(pString) + 1 : 0; EEPROM.put(pAddress + address, length); address++; diff --git a/lib/HanConfigAp/src/configuration.h b/lib/HanConfigAp/src/configuration.h index 76586eaa..5c4347bb 100644 --- a/lib/HanConfigAp/src/configuration.h +++ b/lib/HanConfigAp/src/configuration.h @@ -16,7 +16,7 @@ class configuration { public: char* ssid; char* ssidPassword; - char* mqtt; + char* mqttHost; int mqttPort; char* mqttClientID; char* mqttPublishTopic; diff --git a/scripts/makeweb.py b/scripts/makeweb.py index 89f8e9a9..8a7bebb4 100644 --- a/scripts/makeweb.py +++ b/scripts/makeweb.py @@ -1,9 +1,12 @@ import os import re +import shutil webroot = "web" srcroot = "src/web/root" +shutil.rmtree(srcroot) + if not os.path.exists(srcroot): os.mkdir(srcroot) diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 331b9d39..6ad1119e 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -91,6 +91,13 @@ void setup() { if (!ap.isActivated) { setupWiFi(); + + if(ap.config.mqttHost) { + mqtt.begin(ap.config.mqttHost, *client); + + // Notify everyone we're here! + sendMqttData("Connected!"); + } // Configure uart for AMS data if(ap.config.meterType == 3) { Serial.begin(2400, SERIAL_8N1); @@ -117,16 +124,19 @@ void loop() // Turn off the LED led_off(); - // allow the MQTT client some resources - mqtt.loop(); - delay(10); // <- fixes some issues with WiFi stability - // Reconnect to WiFi and MQTT as needed - if (!mqtt.connected()) { - MQTT_connect(); - } else { - readHanPort(); + if (WiFi.status() != WL_CONNECTED) { + WiFi_connect(); } + + if (ap.config.mqttHost) { + mqtt.loop(); + delay(10); // <- fixes some issues with WiFi stability + if(!mqtt.connected()) { + MQTT_connect(); + } + } + readHanPort(); } else { @@ -176,16 +186,6 @@ void setupWiFi() if (debugger) debugger->println(" connected"); client = new WiFiClient(); - mqtt.begin(ap.config.mqtt, *client); - - // Direct incoming MQTT messages - if (ap.config.mqttSubscribeTopic != 0 && strlen(ap.config.mqttSubscribeTopic) > 0) { - mqtt.subscribe(ap.config.mqttSubscribeTopic); - mqtt.onMessage(mqttMessageReceived); - } - - // Notify everyone we're here! - sendMqttData("Connected!"); } void mqttMessageReceived(String &topic, String &payload) @@ -235,7 +235,7 @@ void readHanPort() hanToJson(data, ap.config.meterType, hanReader); - if(ap.config.mqtt != 0 && strlen(ap.config.mqtt) != 0 && ap.config.mqttPublishTopic != 0 && strlen(ap.config.mqttPublishTopic) != 0) { + if(ap.config.mqttHost != 0 && strlen(ap.config.mqttHost) != 0 && ap.config.mqttPublishTopic != 0 && strlen(ap.config.mqttPublishTopic) != 0) { // Write the json to the debug port if (debugger) { debugger->print("Sending data to MQTT: "); @@ -257,11 +257,7 @@ void readHanPort() } } - -// Function to connect and reconnect as necessary to the MQTT server. -// Should be called in the loop function and it will take care if connecting. -void MQTT_connect() -{ +void WiFi_connect() { // Connect to WiFi access point. if (debugger) { @@ -305,13 +301,20 @@ void MQTT_connect() debugger->println("WiFi connected"); debugger->println("IP address: "); debugger->println(WiFi.localIP()); - debugger->print("\nconnecting to MQTT: "); - debugger->print(ap.config.mqtt); + } +} + +// Function to connect and reconnect as necessary to the MQTT server. +// Should be called in the loop function and it will take care if connecting. +void MQTT_connect() +{ + if(debugger) { + debugger->print("Connecting to MQTT: "); + debugger->print(ap.config.mqttHost); debugger->print(", port: "); debugger->print(ap.config.mqttPort); debugger->println(); } - // Wait for the MQTT connection to complete while (!mqtt.connected()) { // Connect to a unsecure or secure MQTT server diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index efa3ea06..e83fbebb 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -4,9 +4,7 @@ #include "root/index_html.h" #include "root/configuration_html.h" #include "root/boot_css.h" -#include "root/application_css.h" #include "root/gaugemeter_js.h" -#include "root/index_js.h" #include "Base64.h" @@ -23,9 +21,7 @@ void AmsWebServer::setup(configuration* config, Stream* debugger) { server.on("/", std::bind(&AmsWebServer::indexHtml, this)); server.on("/configuration", std::bind(&AmsWebServer::configurationHtml, this)); server.on("/boot.css", std::bind(&AmsWebServer::bootCss, this)); - server.on("/application.css", std::bind(&AmsWebServer::applicationCss, this)); server.on("/gaugemeter.js", std::bind(&AmsWebServer::gaugemeterJs, this)); - server.on("/index.js", std::bind(&AmsWebServer::indexJs, this)); server.on("/data.json", std::bind(&AmsWebServer::dataJson, this)); server.on("/save", std::bind(&AmsWebServer::handleSave, this)); @@ -158,7 +154,8 @@ void AmsWebServer::configurationHtml() { for(int i = 0; i<4; i++) { html.replace("${config.meterType" + String(i) + "}", config->meterType == i ? "selected" : ""); } - html.replace("${config.mqtt}", config->mqtt); + html.replace("${config.mqtt}", config->mqttHost == 0 ? "" : "checked"); + html.replace("${config.mqttHost}", config->mqttHost); html.replace("${config.mqttPort}", String(config->mqttPort)); html.replace("${config.mqttClientID}", config->mqttClientID); html.replace("${config.mqttPublishTopic}", config->mqttPublishTopic); @@ -186,6 +183,7 @@ void AmsWebServer::configurationHtml() { html.replace("${config.meterType" + String(i) + "}", i == 0 ? "selected" : ""); } html.replace("${config.mqtt}", ""); + html.replace("${config.mqttHost}", ""); html.replace("${config.mqttPort}", "1883"); html.replace("${config.mqttClientID}", ""); html.replace("${config.mqttPublishTopic}", ""); @@ -218,15 +216,6 @@ void AmsWebServer::bootCss() { server.send(200, "text/css", BOOT_CSS); } -void AmsWebServer::applicationCss() { - println("Serving /application.css over http..."); - - server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); - server.sendHeader("Pragma", "no-cache"); - server.sendHeader("Expires", "-1"); - server.send(200, "text/css", APPLICATION_CSS); -} - void AmsWebServer::gaugemeterJs() { println("Serving /gaugemeter.js over http..."); @@ -236,15 +225,6 @@ void AmsWebServer::gaugemeterJs() { server.send(200, "application/javascript", GAUGEMETER_JS); } -void AmsWebServer::indexJs() { - println("Serving /index.js over http..."); - - server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); - server.sendHeader("Pragma", "no-cache"); - server.sendHeader("Expires", "-1"); - server.send(200, "application/javascript", INDEX_JS); -} - void AmsWebServer::dataJson() { println("Serving /data.json over http..."); @@ -258,9 +238,9 @@ void AmsWebServer::dataJson() { int maxPwr = this->maxPwr; if(maxPwr == 0) { if(u2 > 0) { - maxPwr = 25000; + maxPwr = 20000; } else { - maxPwr = 15000; + maxPwr = 10000; } } @@ -294,41 +274,51 @@ void AmsWebServer::handleSave() { config->meterType = (byte)server.arg("meterType").toInt(); - temp = server.arg("mqtt"); - config->mqtt = new char[temp.length() + 1]; - temp.toCharArray(config->mqtt, temp.length() + 1, 0); + if(server.hasArg("mqtt") && server.arg("mqtt") == "true") { + println("MQTT enabled"); + temp = server.arg("mqttHost"); + config->mqttHost = new char[temp.length() + 1]; + temp.toCharArray(config->mqttHost, temp.length() + 1, 0); - config->mqttPort = (int)server.arg("mqttPort").toInt(); + config->mqttPort = (int)server.arg("mqttPort").toInt(); - temp = server.arg("mqttClientID"); - config->mqttClientID = new char[temp.length() + 1]; - temp.toCharArray(config->mqttClientID, temp.length() + 1, 0); + temp = server.arg("mqttClientID"); + config->mqttClientID = new char[temp.length() + 1]; + temp.toCharArray(config->mqttClientID, temp.length() + 1, 0); - temp = server.arg("mqttPublishTopic"); - config->mqttPublishTopic = new char[temp.length() + 1]; - temp.toCharArray(config->mqttPublishTopic, temp.length() + 1, 0); + temp = server.arg("mqttPublishTopic"); + config->mqttPublishTopic = new char[temp.length() + 1]; + temp.toCharArray(config->mqttPublishTopic, temp.length() + 1, 0); - temp = server.arg("mqttSubscribeTopic"); - config->mqttSubscribeTopic = new char[temp.length() + 1]; - temp.toCharArray(config->mqttSubscribeTopic, temp.length() + 1, 0); + temp = server.arg("mqttSubscribeTopic"); + config->mqttSubscribeTopic = new char[temp.length() + 1]; + temp.toCharArray(config->mqttSubscribeTopic, temp.length() + 1, 0); - temp = server.arg("mqttUser"); - config->mqttUser = new char[temp.length() + 1]; - temp.toCharArray(config->mqttUser, temp.length() + 1, 0); + temp = server.arg("mqttUser"); + config->mqttUser = new char[temp.length() + 1]; + temp.toCharArray(config->mqttUser, temp.length() + 1, 0); - temp = server.arg("mqttPass"); - config->mqttPass = new char[temp.length() + 1]; - temp.toCharArray(config->mqttPass, temp.length() + 1, 0); + temp = server.arg("mqttPass"); + config->mqttPass = new char[temp.length() + 1]; + temp.toCharArray(config->mqttPass, temp.length() + 1, 0); + } else { + println("MQTT disabled"); + config->mqttHost = NULL; + config->mqttUser = NULL; + config->mqttPass = NULL; + } config->authSecurity = (byte)server.arg("authSecurity").toInt(); - temp = server.arg("authUser"); - config->authUser = new char[temp.length() + 1]; - temp.toCharArray(config->authUser, temp.length() + 1, 0); + if(config->authSecurity > 0) { + temp = server.arg("authUser"); + config->authUser = new char[temp.length() + 1]; + temp.toCharArray(config->authUser, temp.length() + 1, 0); - temp = server.arg("authPass"); - config->authPass = new char[temp.length() + 1]; - temp.toCharArray(config->authPass, temp.length() + 1, 0); + temp = server.arg("authPass"); + config->authPass = new char[temp.length() + 1]; + temp.toCharArray(config->authPass, temp.length() + 1, 0); + } config->fuseSize = (int)server.arg("fuseSize").toInt(); diff --git a/src/web/AmsWebServer.h b/src/web/AmsWebServer.h index 921fa3d8..cc9e0d01 100644 --- a/src/web/AmsWebServer.h +++ b/src/web/AmsWebServer.h @@ -45,9 +45,7 @@ private: void indexHtml(); void configurationHtml(); void bootCss(); - void applicationCss(); void gaugemeterJs(); - void indexJs(); void dataJson(); void handleSave(); diff --git a/web/application.css b/web/application.css deleted file mode 100644 index 80c1b828..00000000 --- a/web/application.css +++ /dev/null @@ -1,44 +0,0 @@ -.bg-purple { - background-color: var(--purple); -} - - -.GaugeMeter { - position: Relative; - text-align: Center; - overflow: Hidden; - cursor: Default; - display: inline-block; -} - -.GaugeMeter SPAN, .GaugeMeter B { - width: 54%; - position: Absolute; - text-align: Center; - display: Inline-Block; - color: RGBa(0,0,0,.8); - font-weight: 100; - font-family: "Open Sans", Arial; - overflow: Hidden; - white-space: NoWrap; - text-overflow: Ellipsis; - margin: 0 23%; -} - -.GaugeMeter[data-style="Semi"] B { - width: 80%; - margin: 0 10%; -} - -.GaugeMeter S, .GaugeMeter U { - text-decoration: None; - font-size: .60em; - font-weight: 200; - opacity: .6; -} - -.GaugeMeter B { - color: #000; - font-weight: 200; - opacity: .8; -} diff --git a/web/boot.css b/web/boot.css index 1584073e..c3239894 100644 --- a/web/boot.css +++ b/web/boot.css @@ -236,6 +236,10 @@ a { border-radius: .25rem; transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out; } +.form-control:disabled, .form-control[readonly] { + background-color: #e9ecef; + opacity: 1; +} input:not([type="image" i]) { box-sizing: border-box; } diff --git a/web/configuration.html b/web/configuration.html index d8317e81..f89f10cd 100644 --- a/web/configuration.html +++ b/web/configuration.html @@ -6,7 +6,12 @@ - + +
@@ -75,40 +80,46 @@
MQTT
+
+ +
+ +
+
- +
- +
- +
- +
- +
- +
@@ -119,7 +130,7 @@
- @@ -129,13 +140,13 @@
- +
- +
@@ -152,5 +163,21 @@
+ diff --git a/web/index.html b/web/index.html index 69c2edad..87a7a4e8 100644 --- a/web/index.html +++ b/web/index.html @@ -6,9 +6,53 @@ - +
@@ -72,6 +116,84 @@
- + diff --git a/web/index.js b/web/index.js deleted file mode 100644 index f6447ba3..00000000 --- a/web/index.js +++ /dev/null @@ -1,77 +0,0 @@ -$(".GaugeMeter").gaugeMeter(); - -var wait = 500; -var nextrefresh = wait; -var fetch = function() { - $.ajax({ - url: '/data.json', - dataType: 'json', - }).done(function(json) { - $(".SimpleMeter").hide(); - var el = $(".GaugeMeter"); - el.show(); - var rate = 2500; - if(json.data) { - el.data('percent', json.pct); - if(json.data.P) { - var num = parseFloat(json.data.P); - if(num > 1000) { - num = num / 1000; - el.data('text', num.toFixed(1)); - el.data('append','kW'); - } else { - el.data('text', num); - el.data('append','W'); - } - } - el.gaugeMeter(); - - for(var id in json.data) { - var str = json.data[id]; - if(isNaN(str)) { - $('#'+id).html(str); - } else { - var num = parseFloat(str); - $('#'+id).html(num.toFixed(1)); - } - } - - if(json.data.U1 > 0) { - $('#P1').show(); - } - - if(json.data.U2 > 0) { - $('#P2').show(); - } - - if(json.data.U3 > 0) { - $('#P3').show(); - } - - if(json.meterType == 3) { - rate = 10000; - } - if(json.currentMillis && json.up) { - nextrefresh = rate - ((json.currentMillis - json.up) % rate) + wait; - } else { - nextrefresh = 2500; - } - } else { - el.data('percent', 0); - el.data('text', '-'); - el.gaugeMeter(); - nextrefresh = 2500; - } - if(!nextrefresh || nextrefresh < 500) { - nextrefresh = 2500; - } - setTimeout(fetch, nextrefresh); - }).fail(function() { - el.data('percent', 0); - el.data('text', '-'); - el.gaugeMeter(); - nextrefresh = 10000; - setTimeout(fetch, nextrefresh); - }); -} -setTimeout(fetch, nextrefresh); From 20c62a63cf13030d8d0f970b5181f42b32539049 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 1 Feb 2020 09:54:58 +0100 Subject: [PATCH 4/9] Fixed script to generate webroot header files --- scripts/makeweb.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/makeweb.py b/scripts/makeweb.py index 8a7bebb4..33cb6269 100644 --- a/scripts/makeweb.py +++ b/scripts/makeweb.py @@ -5,9 +5,10 @@ import shutil webroot = "web" srcroot = "src/web/root" -shutil.rmtree(srcroot) -if not os.path.exists(srcroot): +if os.path.exists(srcroot): + shutil.rmtree(srcroot) +else: os.mkdir(srcroot) for filename in os.listdir(webroot): From d68ffc827d690ac7897bbd5fdb6c9b0cee74b84f Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 1 Feb 2020 09:56:14 +0100 Subject: [PATCH 5/9] Trigger build on changes in scripts and web --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d1a0a1a6..158595c8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,6 +5,8 @@ on: paths: - src/** - lib/** + - scripts/** + - web/** - platformio.ini branches: - master From 4a3d22e75cfaec0b93cb383c3cc631d323e867ca Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 1 Feb 2020 09:57:43 +0100 Subject: [PATCH 6/9] Fixed script to generate webroot header files --- scripts/makeweb.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/makeweb.py b/scripts/makeweb.py index 33cb6269..8158478b 100644 --- a/scripts/makeweb.py +++ b/scripts/makeweb.py @@ -8,6 +8,7 @@ srcroot = "src/web/root" if os.path.exists(srcroot): shutil.rmtree(srcroot) + os.mkdir(srcroot) else: os.mkdir(srcroot) From 9bb596aab19523db9ae08063737006e807741b15 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sun, 2 Feb 2020 08:15:49 +0100 Subject: [PATCH 7/9] Reverted what was intended as support for earlier configuration versions. --- lib/HanConfigAp/src/configuration.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/HanConfigAp/src/configuration.cpp b/lib/HanConfigAp/src/configuration.cpp index 27e64803..b7c82341 100644 --- a/lib/HanConfigAp/src/configuration.cpp +++ b/lib/HanConfigAp/src/configuration.cpp @@ -8,7 +8,7 @@ bool configuration::hasConfig() { bool hasConfig = false; EEPROM.begin(EEPROM_SIZE); - hasConfig = EEPROM.read(EEPROM_CONFIG_ADDRESS) >= 71; + hasConfig = EEPROM.read(EEPROM_CONFIG_ADDRESS) == EEPROM_CHECK_SUM; EEPROM.end(); return hasConfig; } From dbc551a41d5dac1f9a5b1c5d66c7ed0f6aa31c54 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Wed, 5 Feb 2020 20:43:37 +0100 Subject: [PATCH 8/9] Moved configuration object to main file and load the config before start. Fixed issue where partiy is switched for Kamstrup half way when in debug mode --- lib/HanConfigAp/src/HanConfigAp.cpp | 10 ++-- lib/HanConfigAp/src/HanConfigAp.h | 5 +- lib/HanConfigAp/src/configuration.cpp | 57 ++++++++++++---------- lib/HanConfigAp/src/configuration.h | 2 +- src/AmsToMqttBridge.ino | 69 ++++++++++++++------------- 5 files changed, 77 insertions(+), 66 deletions(-) diff --git a/lib/HanConfigAp/src/HanConfigAp.cpp b/lib/HanConfigAp/src/HanConfigAp.cpp index 8cfeef96..bd51b760 100644 --- a/lib/HanConfigAp/src/HanConfigAp.cpp +++ b/lib/HanConfigAp/src/HanConfigAp.cpp @@ -3,15 +3,16 @@ Stream* HanConfigAp::debugger; bool HanConfigAp::hasConfig() { - return config.hasConfig(); + return config->hasConfig(); } -void HanConfigAp::setup(int accessPointButtonPin, Stream* debugger) +void HanConfigAp::setup(int accessPointButtonPin, configuration* config, Stream* debugger) { this->debugger = debugger; + this->config = config; // Test if we're missing configuration - if (!config.hasConfig()) + if (!config->hasConfig()) { print("No config. We're booting as AP. Look for SSID "); println(this->AP_SSID); @@ -20,8 +21,7 @@ void HanConfigAp::setup(int accessPointButtonPin, Stream* debugger) else { // Load the configuration - config.load(); - if (this->debugger) config.print(this->debugger); + if (this->debugger) config->print(this->debugger); if (accessPointButtonPin != INVALID_BUTTON_PIN) { diff --git a/lib/HanConfigAp/src/HanConfigAp.h b/lib/HanConfigAp/src/HanConfigAp.h index e62dce16..401b8656 100644 --- a/lib/HanConfigAp/src/HanConfigAp.h +++ b/lib/HanConfigAp/src/HanConfigAp.h @@ -24,15 +24,16 @@ class HanConfigAp { public: - void setup(int accessPointButtonPin, Stream* debugger); + void setup(int accessPointButtonPin, configuration* config, Stream* debugger); bool loop(); bool hasConfig(); - configuration config; bool isActivated = false; private: const char* AP_SSID = "AMS2MQTT"; + configuration* config; + // DNS server const byte DNS_PORT = 53; DNSServer dnsServer; diff --git a/lib/HanConfigAp/src/configuration.cpp b/lib/HanConfigAp/src/configuration.cpp index b7c82341..3c14091c 100644 --- a/lib/HanConfigAp/src/configuration.cpp +++ b/lib/HanConfigAp/src/configuration.cpp @@ -24,11 +24,18 @@ bool configuration::save() address += saveString(address, ssid); address += saveString(address, ssidPassword); address += saveByte(address, meterType); - address += saveString(address, mqttHost); - address += saveInt(address, mqttPort); - address += saveString(address, mqttClientID); - address += saveString(address, mqttPublishTopic); - address += saveString(address, mqttSubscribeTopic); + + + if(mqttHost) { + address += saveBool(address, true); + address += saveString(address, mqttHost); + address += saveInt(address, mqttPort); + address += saveString(address, mqttClientID); + address += saveString(address, mqttPublishTopic); + address += saveString(address, mqttSubscribeTopic); + } else { + address += saveBool(address, false); + } if (isSecure()) { address += saveBool(address, true); @@ -60,13 +67,13 @@ bool configuration::load() int address = EEPROM_CONFIG_ADDRESS; bool success = false; - ssid = (char*)String("").c_str(); - ssidPassword = (char*)String("").c_str(); + ssid = 0; + ssidPassword = 0; meterType = (byte)0; - mqttHost = (char*)String("").c_str(); - mqttClientID = (char*)String("").c_str(); - mqttPublishTopic = (char*)String("").c_str(); - mqttSubscribeTopic = (char*)String("").c_str(); + mqttHost = 0; + mqttClientID = 0; + mqttPublishTopic = 0; + mqttSubscribeTopic = 0; mqttUser = 0; mqttPass = 0; mqttPort = 1883; @@ -78,22 +85,26 @@ bool configuration::load() EEPROM.begin(EEPROM_SIZE); int cs = EEPROM.read(address); - if (cs >= 71) + if (cs == EEPROM_CHECK_SUM) { address++; address += readString(address, &ssid); address += readString(address, &ssidPassword); address += readByte(address, &meterType); - address += readString(address, &mqttHost); - address += readInt(address, &mqttPort); - address += readString(address, &mqttClientID); - address += readString(address, &mqttPublishTopic); - address += readString(address, &mqttSubscribeTopic); + + bool mqtt = false; + address += readBool(address, &mqtt); + if(mqtt) { + address += readString(address, &mqttHost); + address += readInt(address, &mqttPort); + address += readString(address, &mqttClientID); + address += readString(address, &mqttPublishTopic); + address += readString(address, &mqttSubscribeTopic); + } bool secure = false; address += readBool(address, &secure); - if (secure) { address += readString(address, &mqttUser); @@ -105,9 +116,6 @@ bool configuration::load() mqttPass = 0; } - success = true; - } - if(cs >= 72) { address += readByte(address, &authSecurity); if (authSecurity > 0) { address += readString(address, &authUser); @@ -116,12 +124,11 @@ bool configuration::load() authUser = 0; authPass = 0; } - } - if(cs >= 73) { + address += readInt(address, &fuseSize); - } - if(cs >= 74) { address += readByte(address, &distSys); + + success = true; } EEPROM.end(); return success; diff --git a/lib/HanConfigAp/src/configuration.h b/lib/HanConfigAp/src/configuration.h index 5c4347bb..13640773 100644 --- a/lib/HanConfigAp/src/configuration.h +++ b/lib/HanConfigAp/src/configuration.h @@ -42,7 +42,7 @@ protected: private: const int EEPROM_SIZE = 512; - const byte EEPROM_CHECK_SUM = 74; // Used to check if config is stored. Change if structure changes + const byte EEPROM_CHECK_SUM = 75; // Used to check if config is stored. Change if structure changes const int EEPROM_CONFIG_ADDRESS = 0; int saveString(int pAddress, char* pString); diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 6ad1119e..812fec43 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -46,6 +46,8 @@ OneWire oneWire(TEMP_SENSOR_PIN); DallasTemperature tempSensor(&oneWire); #endif +configuration config; + // Object used to boot as Access Point HanConfigAp ap; @@ -68,11 +70,18 @@ void setup() { debugger = &Serial; #endif + if(config.hasConfig()) { + config.load(); + } + + if(config.meterType == 3) { + Serial.begin(2400, SERIAL_8N1); + } else { + Serial.begin(2400, SERIAL_8E1); + } + while (!Serial); + if (debugger) { - // Setup serial port for debugging - debugger->begin(2400, SERIAL_8E1); - //debugger->begin(115200); - while (!debugger); debugger->println(""); debugger->println("Started..."); } @@ -84,7 +93,7 @@ void setup() { delay(1000); // Initialize the AP - ap.setup(AP_BUTTON_PIN, debugger); + ap.setup(AP_BUTTON_PIN, &config, debugger); led_off(); @@ -92,27 +101,21 @@ void setup() { { setupWiFi(); - if(ap.config.mqttHost) { - mqtt.begin(ap.config.mqttHost, *client); + if(config.mqttHost) { + mqtt.begin(config.mqttHost, *client); // Notify everyone we're here! sendMqttData("Connected!"); } // Configure uart for AMS data - if(ap.config.meterType == 3) { - Serial.begin(2400, SERIAL_8N1); - } else { - Serial.begin(2400, SERIAL_8E1); - } - while (!Serial); hanReader.setup(&Serial, debugger); // Compensate for the known Kaifa bug - hanReader.compensateFor09HeaderBug = (ap.config.meterType == 1); + hanReader.compensateFor09HeaderBug = (config.meterType == 1); } - ws.setup(&ap.config, debugger); + ws.setup(&config, debugger); } // the loop function runs over and over again until power down or reset @@ -129,7 +132,7 @@ void loop() WiFi_connect(); } - if (ap.config.mqttHost) { + if (config.mqttHost) { mqtt.loop(); delay(10); // <- fixes some issues with WiFi stability if(!mqtt.connected()) { @@ -175,7 +178,7 @@ void setupWiFi() // Connect to WiFi WiFi.mode(WIFI_STA); - WiFi.begin(ap.config.ssid, ap.config.ssidPassword); + WiFi.begin(config.ssid, config.ssidPassword); // Wait for WiFi connection if (debugger) debugger->print("\nWaiting for WiFi to connect..."); @@ -205,7 +208,7 @@ void mqttMessageReceived(String &topic, String &payload) void readHanPort() { - if (hanReader.read() && ap.config.hasConfig()) + if (hanReader.read() && config.hasConfig()) { // Flash LED on, this shows us that data is received led_on(); @@ -233,9 +236,9 @@ void readHanPort() data["temp"] = tempSensor.getTempCByIndex(0); #endif - hanToJson(data, ap.config.meterType, hanReader); + hanToJson(data, config.meterType, hanReader); - if(ap.config.mqttHost != 0 && strlen(ap.config.mqttHost) != 0 && ap.config.mqttPublishTopic != 0 && strlen(ap.config.mqttPublishTopic) != 0) { + if(config.mqttHost != 0 && strlen(config.mqttHost) != 0 && config.mqttPublishTopic != 0 && strlen(config.mqttPublishTopic) != 0) { // Write the json to the debug port if (debugger) { debugger->print("Sending data to MQTT: "); @@ -247,7 +250,7 @@ void readHanPort() String msg; serializeJson(json, msg); - mqtt.publish(ap.config.mqttPublishTopic, msg.c_str()); + mqtt.publish(config.mqttPublishTopic, msg.c_str()); mqtt.loop(); } ws.setJson(json); @@ -264,14 +267,14 @@ void WiFi_connect() { debugger->println(); debugger->println(); debugger->print("Connecting to WiFi network "); - debugger->println(ap.config.ssid); + debugger->println(config.ssid); } if (WiFi.status() != WL_CONNECTED) { // Make one first attempt at connect, this seems to considerably speed up the first connection WiFi.disconnect(); - WiFi.begin(ap.config.ssid, ap.config.ssidPassword); + WiFi.begin(config.ssid, config.ssidPassword); delay(1000); } @@ -290,7 +293,7 @@ void WiFi_connect() { debugger->println(WiFi.status()); } WiFi.disconnect(); - WiFi.begin(ap.config.ssid, ap.config.ssidPassword); + WiFi.begin(config.ssid, config.ssidPassword); vTimeout = millis() + WIFI_CONNECTION_TIMEOUT; } yield(); @@ -310,24 +313,24 @@ void MQTT_connect() { if(debugger) { debugger->print("Connecting to MQTT: "); - debugger->print(ap.config.mqttHost); + debugger->print(config.mqttHost); debugger->print(", port: "); - debugger->print(ap.config.mqttPort); + debugger->print(config.mqttPort); debugger->println(); } // Wait for the MQTT connection to complete while (!mqtt.connected()) { // Connect to a unsecure or secure MQTT server - if ((ap.config.mqttUser == 0 && mqtt.connect(ap.config.mqttClientID)) || - (ap.config.mqttUser != 0 && mqtt.connect(ap.config.mqttClientID, ap.config.mqttUser, ap.config.mqttPass))) + if ((config.mqttUser == 0 && mqtt.connect(config.mqttClientID)) || + (config.mqttUser != 0 && mqtt.connect(config.mqttClientID, config.mqttUser, config.mqttPass))) { if (debugger) debugger->println("\nSuccessfully connected to MQTT!"); // Subscribe to the chosen MQTT topic, if set in configuration - if (ap.config.mqttSubscribeTopic != 0 && strlen(ap.config.mqttSubscribeTopic) > 0) + if (config.mqttSubscribeTopic != 0 && strlen(config.mqttSubscribeTopic) > 0) { - mqtt.subscribe(ap.config.mqttSubscribeTopic); - if (debugger) debugger->printf(" Subscribing to [%s]\r\n", ap.config.mqttSubscribeTopic); + mqtt.subscribe(config.mqttSubscribeTopic); + if (debugger) debugger->printf(" Subscribing to [%s]\r\n", config.mqttSubscribeTopic); } } else @@ -355,7 +358,7 @@ void MQTT_connect() void sendMqttData(String data) { // Make sure we have configured a publish topic - if (ap.config.mqttPublishTopic == 0 || strlen(ap.config.mqttPublishTopic) == 0) + if (config.mqttPublishTopic == 0 || strlen(config.mqttPublishTopic) == 0) return; // Make sure we're connected @@ -374,7 +377,7 @@ void sendMqttData(String data) serializeJson(json, msg); // Send the json over MQTT - mqtt.publish(ap.config.mqttPublishTopic, msg.c_str()); + mqtt.publish(config.mqttPublishTopic, msg.c_str()); if (debugger) debugger->print("sendMqttData: "); if (debugger) debugger->println(data); From 3a7f6078f461ed87aee5db93abdb18161ed9cc50 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Thu, 6 Feb 2020 19:02:05 +0100 Subject: [PATCH 9/9] Use builtin LED for Lolin D32 --- src/AmsToMqttBridge.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 812fec43..520a8872 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -33,6 +33,10 @@ #define LED_PIN 2 // The blue on-board LED of the ESP8266 custom AMS board #define LED_ACTIVE_HIGH 0 #define AP_BUTTON_PIN 0 +#elif defined(ARDUINO_LOLIN_D32) +#define LED_PIN 5 +#define LED_ACTIVE_HIGH 0 +#define AP_BUTTON_PIN INVALID_BUTTON_PIN #else #define LED_PIN LED_BUILTIN #define LED_ACTIVE_HIGH 1