mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-03-10 20:54:24 +00:00
Extracted webroot into usable files
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,3 +8,5 @@
|
||||
.pio
|
||||
platformio-user.ini
|
||||
/src/version.h
|
||||
/src/web/root
|
||||
/src/AmsToMqttBridge.ino.cpp
|
||||
|
||||
@@ -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
|
||||
|
||||
25
scripts/makeweb.py
Normal file
25
scripts/makeweb.py
Normal file
@@ -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")
|
||||
@@ -22,7 +22,7 @@
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
#include "AmsWebServer.h"
|
||||
#include "web/AmsWebServer.h"
|
||||
#include "HanConfigAp.h"
|
||||
#include "HanReader.h"
|
||||
#include "HanToJson.h"
|
||||
|
||||
@@ -1,380 +0,0 @@
|
||||
# 1 "/tmp/tmpfprbzre1"
|
||||
#include <Arduino.h>
|
||||
# 1 "/home/gunnar/src/AmsToMqttBridge/src/AmsToMqttBridge.ino"
|
||||
# 11 "/home/gunnar/src/AmsToMqttBridge/src/AmsToMqttBridge.ino"
|
||||
#include <ArduinoJson.h>
|
||||
#include <MQTT.h>
|
||||
|
||||
#if HAS_DALLAS_TEMP_SENSOR
|
||||
#include <DallasTemperature.h>
|
||||
#include <OneWire.h>
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(ESP32)
|
||||
#include <WiFi.h>
|
||||
#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);
|
||||
}
|
||||
@@ -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() {
|
||||
@@ -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;
|
||||
}
|
||||
)=="==";
|
||||
@@ -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;
|
||||
}
|
||||
)=="==";
|
||||
@@ -1,13 +1,12 @@
|
||||
const char CONFIGURATION_HTML[] PROGMEM = R"=="==(
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>AMS reader - configuration</title>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<link rel="stylesheet" type="text/css" href="/css/boot.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="boot.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="/css/application.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="application.css"/>
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<main role="main" class="container">
|
||||
@@ -145,4 +144,3 @@ const char CONFIGURATION_HTML[] PROGMEM = R"=="==(
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
)=="==";
|
||||
@@ -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);
|
||||
)=="==";
|
||||
@@ -1,15 +1,14 @@
|
||||
const char INDEX_HTML[] PROGMEM = R"=="==(
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>AMS reader</title>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<link rel="stylesheet" type="text/css" href="/css/boot.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="boot.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="/css/application.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="application.css"/>
|
||||
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
|
||||
<script src="/js/gaugemeter.js"></script>
|
||||
<script src="gaugemeter.js"></script>
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<main role="main" class="container">
|
||||
@@ -73,7 +72,6 @@ const char INDEX_HTML[] PROGMEM = R"=="==(
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
<script src="/js/index.js"></script>
|
||||
<script src="/index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
)=="==";
|
||||
@@ -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);
|
||||
|
||||
)=="==";
|
||||
Reference in New Issue
Block a user