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); - -)=="==";