Some modifications to increase stability

This commit is contained in:
Gunnar Skjold
2020-05-08 22:58:16 +02:00
parent c3c0ca0a1b
commit 953f2d4110
8 changed files with 91 additions and 60 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -39,4 +39,12 @@ for filename in os.listdir(webroot):
dst.write(varname)
dst.write("[] PROGMEM = R\"==\"==(")
dst.write(content)
dst.write(")==\"==\";\n")
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]);");

View File

@@ -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

View File

@@ -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);

View File

@@ -1,43 +1,3 @@
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AMS reader - MQTT configuration</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
</head>
<body class="bg-light">
<main role="main" class="container">
<header class="navbar navbar-expand navbar-dark flex-column flex-md-row bg-purple rounded mt-2 mb-4" style="background-color: var(--purple);">
<a href="/" class=""><h6 class="navbar-brand">AMS reader <small>${version}</small></h6></a>
<div class="navbar-nav-scroll">
<ul class="navbar-nav bd-navbar-nav flex-row">
<li class="nav-item">
<a class="nav-link" href="/config-meter">Meter</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/config-wifi">WiFi</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/config-mqtt">MQTT</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/config-web">Web</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/config-system">System</a>
</li>
</ul>
</div>
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item">
<a class="nav-link p-2" href="https://github.com/gskjold/AmsToMqttBridge" target="_blank" rel="noopener" aria-label="GitHub">
<img class="d-inline-block align-text-top" style="width: 2rem; height: 2rem;" src="github.svg"/>
</a>
</li>
</ul>
</header>
<form method="post" action="/save">
<input type="hidden" name="mqttConfig" value="true"/>
<div class="my-3 p-3 bg-white rounded shadow">
@@ -173,7 +133,6 @@
</div>
</div>
</form>
</main>
<script>
$('#mqttEnable').on('change', function() {
var inputs = $('.mqtt-config');
@@ -192,5 +151,3 @@
$('#mqttPayloadFormat').trigger('change');
});
</script>
</body>
</html>

3
web/foot.html Normal file
View File

@@ -0,0 +1,3 @@
</main>
</body>
</html>

39
web/head.html Normal file
View File

@@ -0,0 +1,39 @@
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AMS reader - WiFi configuration</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body class="bg-light">
<main role="main" class="container">
<header class="navbar navbar-expand navbar-dark flex-column flex-md-row bg-purple rounded mt-2 mb-4" style="background-color: var(--purple);">
<a href="/" class=""><h6 class="navbar-brand">AMS reader <small>${version}</small></h6></a>
<div class="navbar-nav-scroll">
<ul class="navbar-nav bd-navbar-nav flex-row">
<li class="nav-item">
<a class="nav-link" href="/config-meter">Meter</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/config-wifi">WiFi</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/config-mqtt">MQTT</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/config-web">Web</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/config-system">System</a>
</li>
</ul>
</div>
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item">
<a class="nav-link p-2" href="https://github.com/gskjold/AmsToMqttBridge" target="_blank" rel="noopener" aria-label="GitHub">
<img class="d-inline-block align-text-top" style="width: 2rem; height: 2rem;" src="github.svg"/>
</a>
</li>
</ul>
</header>