mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-27 20:48:36 +00:00
Continued work with v1.1.0
- Merge branch 'low_power' into dev-v1.1.0 - Corrected accumulated import/export from Aidon - Added VCC and RSSI to MQTT messages Changes in UI: - New top navbar - Show VCC and WiFi information - Show MQTT error messages - Show ESP, HAN, WiFi and MQTT status badge - Show accumulated Import/export
This commit is contained in:
@@ -4,6 +4,10 @@
|
||||
Author: roarf
|
||||
*/
|
||||
|
||||
#if defined(ESP8266)
|
||||
ADC_MODE(ADC_VCC);
|
||||
#endif
|
||||
|
||||
#include "AmsToMqttBridge.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <MQTT.h>
|
||||
@@ -59,8 +63,25 @@ void setup() {
|
||||
if (debugger) {
|
||||
debugger->println("");
|
||||
debugger->println("Started...");
|
||||
#if defined(ESP8266)
|
||||
debugger->print("Voltage: ");
|
||||
debugger->print(ESP.getVcc());
|
||||
debugger->println("mV");
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
if (ESP.getVcc() < 2800) {
|
||||
if(debugger) {
|
||||
debugger->print("Voltage is too low: ");
|
||||
debugger->print(ESP.getVcc());
|
||||
debugger->println("mV");
|
||||
debugger->flush();
|
||||
}
|
||||
ESP.deepSleep(10000000); //Deep sleep to allow output cap to charge up
|
||||
}
|
||||
#endif
|
||||
|
||||
// Flash the LED, to indicate we can boot as AP now
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
led_on();
|
||||
@@ -99,7 +120,7 @@ void setup() {
|
||||
// Compensate for the known Kaifa bug
|
||||
hanReader.compensateFor09HeaderBug = (config.meterType == 1);
|
||||
|
||||
ws.setup(&config, debugger);
|
||||
ws.setup(&config, debugger, &mqtt);
|
||||
}
|
||||
|
||||
// the loop function runs over and over again until power down or reset
|
||||
@@ -123,8 +144,16 @@ void loop() {
|
||||
}
|
||||
} else {
|
||||
// Continously flash the LED when AP mode
|
||||
if (millis() / 1000 % 2 == 0) led_on();
|
||||
if (millis() / 50 % 64 == 0) led_on();
|
||||
else led_off();
|
||||
|
||||
#if defined(ESP8266)
|
||||
// Make sure there is enough power to run
|
||||
delay(max(10, 3500-ESP.getVcc()));
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
|
||||
}
|
||||
readHanPort();
|
||||
ws.loop();
|
||||
@@ -208,6 +237,12 @@ void readHanPort() {
|
||||
json["id"] = WiFi.macAddress();
|
||||
json["up"] = millis();
|
||||
json["t"] = time;
|
||||
#if defined(ESP8266)
|
||||
json["vcc"] = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
float rssi = WiFi.RSSI();
|
||||
rssi = isnan(rssi) ? -100.0 : rssi;
|
||||
json["rssi"] = rssi;
|
||||
|
||||
// Add a sub-structure to the json object,
|
||||
// to keep the data from the meter itself
|
||||
@@ -400,6 +435,9 @@ void sendMqttData(String data)
|
||||
json["id"] = WiFi.macAddress();
|
||||
json["up"] = millis();
|
||||
json["data"] = data;
|
||||
#if defined(ESP8266)
|
||||
json["vcc"] = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
|
||||
// Stringify the json
|
||||
String msg;
|
||||
|
||||
@@ -14,9 +14,10 @@ ESP8266WebServer server(80);
|
||||
WebServer server(80);
|
||||
#endif
|
||||
|
||||
void AmsWebServer::setup(configuration* config, Stream* debugger) {
|
||||
void AmsWebServer::setup(configuration* config, Stream* debugger, MQTTClient* mqtt) {
|
||||
this->config = config;
|
||||
this->debugger = debugger;
|
||||
this->mqtt = mqtt;
|
||||
|
||||
server.on("/", std::bind(&AmsWebServer::indexHtml, this));
|
||||
server.on("/configuration", std::bind(&AmsWebServer::configurationHtml, this));
|
||||
@@ -59,7 +60,6 @@ void AmsWebServer::setJson(StaticJsonDocument<500> json) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(maxPwr == 0 && config->hasConfig() && config->fuseSize > 0 && config->distSys > 0) {
|
||||
int volt = config->distSys == 2 ? 400 : 230;
|
||||
if(u2 > 0) {
|
||||
@@ -68,6 +68,13 @@ void AmsWebServer::setJson(StaticJsonDocument<500> json) {
|
||||
maxPwr = config->fuseSize * 230;
|
||||
}
|
||||
}
|
||||
|
||||
if(json["data"].containsKey("tPI")) {
|
||||
tpi = json["data"]["tPI"].as<double>();
|
||||
tpo = json["data"]["tPO"].as<double>();
|
||||
tqi = json["data"]["tQI"].as<double>();
|
||||
tqo = json["data"]["tQO"].as<double>();
|
||||
}
|
||||
} else {
|
||||
if(u1 > 0) {
|
||||
json["data"]["U1"] = u1;
|
||||
@@ -81,6 +88,12 @@ void AmsWebServer::setJson(StaticJsonDocument<500> json) {
|
||||
json["data"]["U3"] = u3;
|
||||
json["data"]["I3"] = i3;
|
||||
}
|
||||
if(tpi > 0) {
|
||||
json["data"]["tPI"] = tpi;
|
||||
json["data"]["tPO"] = tpo;
|
||||
json["data"]["tQI"] = tqi;
|
||||
json["data"]["tQO"] = tqo;
|
||||
}
|
||||
}
|
||||
this->json = json;
|
||||
}
|
||||
@@ -136,6 +149,24 @@ void AmsWebServer::indexHtml() {
|
||||
html.replace("${data.I3}", u3 > 0 ? String(i3, 1) : "");
|
||||
html.replace("${display.P3}", u3 > 0 ? "" : "none");
|
||||
|
||||
html.replace("${data.tPI}", tpi > 0 ? String(tpi, 1) : "");
|
||||
html.replace("${data.tPO}", tpi > 0 ? String(tpo, 1) : "");
|
||||
html.replace("${data.tQI}", tpi > 0 ? String(tqi, 1) : "");
|
||||
html.replace("${data.tQO}", tpi > 0 ? String(tqo, 1) : "");
|
||||
html.replace("${display.accumulative}", tpi > 0 ? "" : "none");
|
||||
|
||||
double vcc = 0;
|
||||
#if defined(ESP8266)
|
||||
vcc = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
html.replace("${vcc}", vcc > 0 ? String(vcc, 2) : "");
|
||||
|
||||
float rssi = WiFi.RSSI();
|
||||
rssi = isnan(rssi) ? -100.0 : rssi;
|
||||
html.replace("${wifi.rssi}", vcc > 0 ? String(rssi, 0) : "");
|
||||
html.replace("${wifi.channel}", WiFi.channel() > 0 ? String(WiFi.channel()) : "");
|
||||
html.replace("${wifi.ssid}", !WiFi.SSID().isEmpty() ? String(WiFi.SSID()) : "");
|
||||
|
||||
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server.sendHeader("Pragma", "no-cache");
|
||||
server.sendHeader("Expires", "-1");
|
||||
@@ -209,8 +240,10 @@ void AmsWebServer::dataJson() {
|
||||
if(!checkSecurity(2))
|
||||
return;
|
||||
|
||||
StaticJsonDocument<500> json;
|
||||
|
||||
String jsonStr;
|
||||
if(!json.isNull()) {
|
||||
if(!this->json.isNull() && this->json.containsKey("data")) {
|
||||
println(" json has data");
|
||||
|
||||
int maxPwr = this->maxPwr;
|
||||
@@ -222,17 +255,91 @@ void AmsWebServer::dataJson() {
|
||||
}
|
||||
}
|
||||
|
||||
json["maxPower"] = maxPwr;
|
||||
json["pct"] = min(p*100/maxPwr, 100);
|
||||
json["meterType"] = config->meterType;
|
||||
json["currentMillis"] = millis();
|
||||
json["up"] = this->json["up"];
|
||||
json["t"] = this->json["t"];
|
||||
json["data"] = this->json["data"];
|
||||
|
||||
serializeJson(json, jsonStr);
|
||||
json["pct"] = min(p*100/maxPwr, 100);
|
||||
} else {
|
||||
json["pct"] = -1;
|
||||
println(" json is empty");
|
||||
jsonStr = "{}";
|
||||
}
|
||||
|
||||
unsigned long now = millis();
|
||||
json["id"] = WiFi.macAddress();
|
||||
json["maxPower"] = maxPwr;
|
||||
json["meterType"] = config->meterType;
|
||||
json["currentMillis"] = now;
|
||||
|
||||
double vcc = 0;
|
||||
#if defined(ESP8266)
|
||||
vcc = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
json["vcc"] = vcc;
|
||||
|
||||
json.createNestedObject("wifi");
|
||||
float rssi = WiFi.RSSI();
|
||||
rssi = isnan(rssi) ? -100.0 : rssi;
|
||||
json["wifi"]["ssid"] = WiFi.SSID();
|
||||
json["wifi"]["channel"] = (int) WiFi.channel();
|
||||
json["wifi"]["rssi"] = rssi;
|
||||
|
||||
json.createNestedObject("status");
|
||||
|
||||
String espStatus;
|
||||
if(vcc == 0) {
|
||||
espStatus = "secondary";
|
||||
} else if(vcc > 3.1) {
|
||||
espStatus = "success";
|
||||
} else if(vcc > 2.8) {
|
||||
espStatus = "warning";
|
||||
} else {
|
||||
espStatus = "danger";
|
||||
}
|
||||
json["status"]["esp"] = espStatus;
|
||||
|
||||
unsigned long lastHan = json.isNull() ? 0 : json["up"].as<unsigned long>();
|
||||
String hanStatus;
|
||||
if(config->meterType == 0) {
|
||||
hanStatus = "secondary";
|
||||
} else if(now - lastHan < 15000) {
|
||||
hanStatus = "success";
|
||||
} else if(now - lastHan < 30000) {
|
||||
hanStatus = "warning";
|
||||
} else {
|
||||
hanStatus = "danger";
|
||||
}
|
||||
json["status"]["han"] = hanStatus;
|
||||
|
||||
String wifiStatus;
|
||||
if(!config->ssid) {
|
||||
wifiStatus = "secondary";
|
||||
} else if(rssi > -75) {
|
||||
wifiStatus = "success";
|
||||
} else if(rssi > -95) {
|
||||
wifiStatus = "warning";
|
||||
} else {
|
||||
wifiStatus = "danger";
|
||||
}
|
||||
json["status"]["wifi"] = wifiStatus;
|
||||
|
||||
String mqttStatus;
|
||||
if(!config->mqttHost) {
|
||||
mqttStatus = "secondary";
|
||||
} else if(mqtt->connected()) {
|
||||
mqttStatus = "success";
|
||||
} else if(mqtt->lastError() == 0) {
|
||||
mqttStatus = "warning";
|
||||
} else {
|
||||
mqttStatus = "danger";
|
||||
}
|
||||
json["status"]["mqtt"] = mqttStatus;
|
||||
|
||||
json.createNestedObject("mqtt");
|
||||
json["mqtt"]["lastError"] = (int) mqtt->lastError();
|
||||
|
||||
serializeJson(json, jsonStr);
|
||||
|
||||
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server.sendHeader("Pragma", "no-cache");
|
||||
server.sendHeader("Expires", "-1");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define _AMSWEBSERVER_h
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <MQTT.h>
|
||||
#include "configuration.h"
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
@@ -22,17 +23,18 @@
|
||||
|
||||
class AmsWebServer {
|
||||
public:
|
||||
void setup(configuration* config, Stream* debugger);
|
||||
void setup(configuration* config, Stream* debugger, MQTTClient* mqtt);
|
||||
void loop();
|
||||
void setJson(StaticJsonDocument<500> json);
|
||||
|
||||
private:
|
||||
configuration* config;
|
||||
Stream* debugger;
|
||||
MQTTClient* mqtt;
|
||||
StaticJsonDocument<500> json;
|
||||
int maxPwr;
|
||||
int p;
|
||||
double u1, u2, u3, i1, i2, i3;
|
||||
double u1, u2, u3, i1, i2, i3, tpi, tpo, tqi, tqo;
|
||||
|
||||
#if defined(ESP8266)
|
||||
ESP8266WebServer server;
|
||||
|
||||
Reference in New Issue
Block a user