mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-27 12:43:08 +00:00
Made MQTT optional
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -45,9 +45,7 @@ private:
|
||||
void indexHtml();
|
||||
void configurationHtml();
|
||||
void bootCss();
|
||||
void applicationCss();
|
||||
void gaugemeterJs();
|
||||
void indexJs();
|
||||
void dataJson();
|
||||
|
||||
void handleSave();
|
||||
|
||||
Reference in New Issue
Block a user