mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-31 06:01:57 +00:00
Added remote debugging
This commit is contained in:
@@ -241,6 +241,30 @@ void AmsConfiguration::setProductionCapacity(int productionCapacity) {
|
||||
this->productionCapacity = productionCapacity;
|
||||
}
|
||||
|
||||
bool AmsConfiguration::isDebugTelnet() {
|
||||
return this->debugTelnet;
|
||||
}
|
||||
|
||||
void AmsConfiguration::setDebugTelnet(bool debugTelnet) {
|
||||
this->debugTelnet = debugTelnet;
|
||||
}
|
||||
|
||||
bool AmsConfiguration::isDebugSerial() {
|
||||
return this->debugSerial;
|
||||
}
|
||||
|
||||
void AmsConfiguration::setDebugSerial(bool debugSerial) {
|
||||
this->debugSerial = debugSerial;
|
||||
}
|
||||
|
||||
int AmsConfiguration::getDebugLevel() {
|
||||
return this->debugLevel;
|
||||
}
|
||||
|
||||
void AmsConfiguration::setDebugLevel(int debugLevel) {
|
||||
this->debugLevel = debugLevel;
|
||||
}
|
||||
|
||||
|
||||
bool AmsConfiguration::hasConfig() {
|
||||
if(configVersion == 0) {
|
||||
@@ -338,6 +362,10 @@ bool AmsConfiguration::loadConfig72(int address) {
|
||||
|
||||
ackWifiChange();
|
||||
|
||||
setDebugLevel(5); // 5=Error
|
||||
setDebugTelnet(false);
|
||||
setDebugSerial(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -405,6 +433,10 @@ bool AmsConfiguration::loadConfig75(int address) {
|
||||
|
||||
ackWifiChange();
|
||||
|
||||
setDebugLevel(5); // 5=Error
|
||||
setDebugTelnet(false);
|
||||
setDebugSerial(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -476,6 +508,10 @@ bool AmsConfiguration::loadConfig80(int address) {
|
||||
|
||||
ackWifiChange();
|
||||
|
||||
setDebugLevel(5); // 5=Error
|
||||
setDebugTelnet(false);
|
||||
setDebugSerial(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -557,6 +593,15 @@ bool AmsConfiguration::loadConfig81(int address) {
|
||||
address += readInt(address, &i);
|
||||
setProductionCapacity(i);
|
||||
|
||||
bool debugTelnet = false;
|
||||
address += readBool(address, &debugTelnet);
|
||||
setDebugTelnet(debugTelnet);
|
||||
bool debugSerial = false;
|
||||
address += readBool(address, &debugSerial);
|
||||
setDebugSerial(debugSerial);
|
||||
address += readInt(address, &i);
|
||||
setDebugLevel(i);
|
||||
|
||||
ackWifiChange();
|
||||
|
||||
return true;
|
||||
@@ -612,6 +657,10 @@ bool AmsConfiguration::save() {
|
||||
address += saveInt(address, mainFuse);
|
||||
address += saveInt(address, productionCapacity);
|
||||
|
||||
address += saveBool(address, debugTelnet);
|
||||
address += saveBool(address, debugSerial);
|
||||
address += saveInt(address, debugLevel);
|
||||
|
||||
bool success = EEPROM.commit();
|
||||
EEPROM.end();
|
||||
|
||||
@@ -704,7 +753,7 @@ template <class T> int AmsConfiguration::readAnything(int ee, T& value) {
|
||||
return i;
|
||||
}
|
||||
|
||||
void AmsConfiguration::print(Stream* debugger)
|
||||
void AmsConfiguration::print(Print* debugger)
|
||||
{
|
||||
debugger->println("Configuration:");
|
||||
debugger->println("-----------------------------------------------");
|
||||
|
||||
@@ -70,7 +70,14 @@ public:
|
||||
int getProductionCapacity();
|
||||
void setProductionCapacity(int productionCapacity);
|
||||
|
||||
void print(Stream* debugger);
|
||||
bool isDebugTelnet();
|
||||
void setDebugTelnet(bool debugTelnet);
|
||||
bool isDebugSerial();
|
||||
void setDebugSerial(bool debugSerial);
|
||||
int getDebugLevel();
|
||||
void setDebugLevel(int debugLevel);
|
||||
|
||||
void print(Print* debugger);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -103,6 +110,9 @@ private:
|
||||
|
||||
int meterType, distributionSystem, mainFuse, productionCapacity;
|
||||
|
||||
bool debugTelnet, debugSerial;
|
||||
int debugLevel;
|
||||
|
||||
const int EEPROM_SIZE = 512;
|
||||
const int EEPROM_CHECK_SUM = 81; // Used to check if config is stored. Change if structure changes
|
||||
const int EEPROM_CONFIG_ADDRESS = 0;
|
||||
|
||||
@@ -169,7 +169,6 @@ void AmsData::extractFromAidon(HanReader& hanReader, int listSize) {
|
||||
l2voltage = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL2)) / 10;
|
||||
l3voltage = ((double) hanReader.getInt( (int)Aidon_List3PhaseIT::VoltageL3)) / 10;
|
||||
//l2current = ((activeImportPower * sqrt(3)) - (l1voltage * l1current) - (l3voltage * l3current)) / l2voltage;
|
||||
threePhase = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,22 +16,19 @@
|
||||
#include "Update.h"
|
||||
#endif
|
||||
|
||||
#define RGB_RED 1
|
||||
#define RGB_GREEN 2
|
||||
#define RGB_YELLOW 3
|
||||
#define RGB_ON 1
|
||||
#define RGB_OFF 0
|
||||
|
||||
// Build settings for custom hardware by Roar Fredriksen
|
||||
#if HW_ROARFRED
|
||||
#define LED_PIN 2 // The blue on-board LED of the ESP8266 custom AMS board
|
||||
#define LED_ACTIVE_HIGH 0
|
||||
#define AP_BUTTON_PIN 0
|
||||
|
||||
#if DEBUG_MODE
|
||||
#if SOFTWARE_SERIAL
|
||||
#include <SoftwareSerial.h>
|
||||
SoftwareSerial *hanSerial = new SoftwareSerial(3);
|
||||
#else
|
||||
HardwareSerial *hanSerial = &Serial;
|
||||
#endif
|
||||
#else
|
||||
HardwareSerial *hanSerial = &Serial;
|
||||
#endif
|
||||
|
||||
// Build settings for Wemos Lolin D32
|
||||
#elif defined(ARDUINO_LOLIN_D32)
|
||||
|
||||
@@ -38,18 +38,21 @@ ADC_MODE(ADC_VCC);
|
||||
|
||||
#include "Uptime.h"
|
||||
|
||||
#define WEBSOCKET_DISABLED true
|
||||
#include "RemoteDebug.h"
|
||||
|
||||
HwTools hw;
|
||||
|
||||
DNSServer dnsServer;
|
||||
|
||||
AmsConfiguration config;
|
||||
|
||||
AmsWebServer ws;
|
||||
RemoteDebug Debug;
|
||||
|
||||
AmsWebServer ws(&Debug);
|
||||
|
||||
WiFiClient *client;
|
||||
MQTTClient mqtt(384);
|
||||
|
||||
Stream* debugger = NULL;
|
||||
MQTTClient mqtt(512);
|
||||
|
||||
HanReader hanReader;
|
||||
|
||||
@@ -58,41 +61,43 @@ void setup() {
|
||||
config.load();
|
||||
}
|
||||
|
||||
#if DEBUG_MODE
|
||||
#if HW_ROARFRED
|
||||
#if SOFTWARE_SERIAL
|
||||
SoftwareSerial *ser = new SoftwareSerial(-1, 1);
|
||||
ser->begin(115200, SWSERIAL_8N1);
|
||||
debugger = ser;
|
||||
#else
|
||||
HardwareSerial *ser = &Serial;
|
||||
if(config.getMeterType() == 3) {
|
||||
ser->begin(2400, SERIAL_8N1);
|
||||
Serial.begin(2400, SERIAL_8N1);
|
||||
} else {
|
||||
ser->begin(2400, SERIAL_8E1);
|
||||
Serial.begin(2400, SERIAL_8E1);
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
HardwareSerial *ser = &Serial;
|
||||
ser->begin(115200, SERIAL_8N1);
|
||||
Serial.begin(115200);
|
||||
#endif
|
||||
debugger = ser;
|
||||
|
||||
if(config.hasConfig()) {
|
||||
Debug.begin(config.getWifiHostname(), (uint8_t) config.getDebugLevel());
|
||||
if(config.getAuthSecurity() > 0) {
|
||||
Debug.setPassword(config.getAuthPassword());
|
||||
}
|
||||
Debug.setSerialEnabled(config.isDebugSerial());
|
||||
if(!config.isDebugTelnet()) {
|
||||
Debug.stop();
|
||||
}
|
||||
} else {
|
||||
#if DEBUG_MODE
|
||||
Debug.begin("localhost", RemoteDebug::DEBUG);
|
||||
Debug.setSerialEnabled(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
double vcc = hw.getVcc();
|
||||
|
||||
if (debugger) {
|
||||
debugger->println("");
|
||||
debugger->println("Started...");
|
||||
debugger->print("Voltage: ");
|
||||
debugger->print(vcc);
|
||||
debugger->println("V");
|
||||
if (Debug.isActive(RemoteDebug::INFO)) {
|
||||
debugI("AMS bridge started");
|
||||
debugI("Voltage: %dV", vcc);
|
||||
}
|
||||
|
||||
if (vcc > 0 && vcc < 3.1) {
|
||||
if(debugger) {
|
||||
debugger->println("Voltage is too low, sleeping");
|
||||
debugger->flush();
|
||||
if(Debug.isActive(RemoteDebug::INFO)) {
|
||||
debugI("Votltage is too low, sleeping");
|
||||
Debug.flush();
|
||||
}
|
||||
ESP.deepSleep(10000000); //Deep sleep to allow output cap to charge up
|
||||
}
|
||||
@@ -115,14 +120,14 @@ void setup() {
|
||||
if(SPIFFS.begin()) {
|
||||
bool flashed = false;
|
||||
if(SPIFFS.exists("/firmware.bin")) {
|
||||
if(debugger) debugger->println("Found firmware");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI("Found firmware");
|
||||
#if defined(ESP8266)
|
||||
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
|
||||
WiFi.forceSleepBegin();
|
||||
#endif
|
||||
int i = 0;
|
||||
while(hw.getVcc() < 3.3 && i < 3) {
|
||||
if(debugger) debugger->println(" vcc not optimal, light sleep 10s");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI(" vcc not optimal, light sleep 10s");
|
||||
#if defined(ESP8266)
|
||||
delay(10000);
|
||||
#elif defined(ESP32)
|
||||
@@ -132,13 +137,13 @@ void setup() {
|
||||
i++;
|
||||
}
|
||||
|
||||
if(debugger) debugger->println(" flashing");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI(" flashing");
|
||||
File firmwareFile = SPIFFS.open("/firmware.bin", "r");
|
||||
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
||||
if (!Update.begin(maxSketchSpace, U_FLASH)) {
|
||||
if(debugger) {
|
||||
debugger->println("Unable to start firmware update");
|
||||
Update.printError(*debugger);
|
||||
if(Debug.isActive(RemoteDebug::ERROR)) {
|
||||
debugE("Unable to start firmware update");
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else {
|
||||
while (firmwareFile.available()) {
|
||||
@@ -153,9 +158,9 @@ void setup() {
|
||||
}
|
||||
SPIFFS.end();
|
||||
if(flashed) {
|
||||
if(debugger) {
|
||||
debugger->println("Firmware update complete, restarting");
|
||||
debugger->flush();
|
||||
if(Debug.isActive(RemoteDebug::INFO)) {
|
||||
debugI("Firmware update complete, restarting");
|
||||
Debug.flush();
|
||||
}
|
||||
#if defined(ESP8266)
|
||||
ESP.reset();
|
||||
@@ -177,27 +182,27 @@ void setup() {
|
||||
}
|
||||
|
||||
if(config.hasConfig()) {
|
||||
if(debugger) config.print(debugger);
|
||||
if(Debug.isActive(RemoteDebug::INFO)) config.print(&Debug);
|
||||
WiFi_connect();
|
||||
client = new WiFiClient();
|
||||
} else {
|
||||
if(debugger) {
|
||||
debugger->println("No configuration, booting AP");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) {
|
||||
debugI("No configuration, booting AP");
|
||||
}
|
||||
swapWifiMode();
|
||||
}
|
||||
|
||||
#if SOFTWARE_SERIAL
|
||||
if(debugger) debugger->println("HAN has software serial");
|
||||
if(Debug.isActive(RemoteDebug::DEBUG)) debugD("HAN has software serial");
|
||||
if(config.getMeterType() == 3) {
|
||||
hanSerial->begin(2400, SWSERIAL_8N1);
|
||||
} else {
|
||||
hanSerial->begin(2400, SWSERIAL_8E1);
|
||||
}
|
||||
#else
|
||||
if(debugger) {
|
||||
debugger->println("HAN has hardware serial");
|
||||
debugger->flush();
|
||||
if(Debug.isActive(RemoteDebug::DEBUG)) {
|
||||
debugD("HAN has hardware serial");
|
||||
Debug.flush();
|
||||
}
|
||||
if(config.getMeterType() == 3) {
|
||||
hanSerial->begin(2400, SERIAL_8N1);
|
||||
@@ -209,7 +214,7 @@ void setup() {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
hanReader.setup(hanSerial, debugger);
|
||||
hanReader.setup(hanSerial, &Debug);
|
||||
|
||||
// Compensate for the known Kaifa bug
|
||||
hanReader.compensateFor09HeaderBug = (config.getMeterType() == 1);
|
||||
@@ -219,7 +224,7 @@ void setup() {
|
||||
hanSerial->read();
|
||||
}
|
||||
|
||||
ws.setup(&config, debugger, &mqtt);
|
||||
ws.setup(&config, &mqtt);
|
||||
|
||||
#if HAS_RGB_LED
|
||||
//Signal startup by blinking red / green / yellow
|
||||
@@ -249,6 +254,7 @@ unsigned long lastErrorBlink = 0;
|
||||
int lastError = 0;
|
||||
|
||||
void loop() {
|
||||
Debug.handle();
|
||||
unsigned long now = millis();
|
||||
if(AP_BUTTON_PIN != INVALID_BUTTON_PIN) {
|
||||
if (digitalRead(AP_BUTTON_PIN) == LOW) {
|
||||
@@ -292,13 +298,13 @@ void loop() {
|
||||
} else {
|
||||
if(!wifiConnected) {
|
||||
wifiConnected = true;
|
||||
if(debugger) {
|
||||
debugger->println("Successfully connected to WiFi!");
|
||||
debugger->println(WiFi.localIP());
|
||||
if(!config.getWifiHostname().isEmpty()) {
|
||||
MDNS.begin(config.getWifiHostname().c_str());
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
if(Debug.isActive(RemoteDebug::INFO)) {
|
||||
debugI("Successfully connected to WiFi!");
|
||||
debugI("IP: %s", WiFi.localIP().toString().c_str());
|
||||
}
|
||||
if(!config.getWifiHostname().isEmpty()) {
|
||||
MDNS.begin(config.getWifiHostname().c_str());
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
}
|
||||
if (!config.getMqttHost().isEmpty()) {
|
||||
@@ -388,14 +394,14 @@ void swapWifiMode() {
|
||||
yield();
|
||||
|
||||
if (mode != WIFI_AP || !config.hasConfig()) {
|
||||
if(debugger) debugger->println("Swapping to AP mode");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI("Swapping to AP mode");
|
||||
WiFi.softAP("AMS2MQTT");
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
|
||||
dnsServer.start(53, "*", WiFi.softAPIP());
|
||||
} else {
|
||||
if(debugger) debugger->println("Swapping to STA mode");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI("Swapping to STA mode");
|
||||
WiFi_connect();
|
||||
}
|
||||
delay(500);
|
||||
@@ -405,12 +411,8 @@ void swapWifiMode() {
|
||||
void mqttMessageReceived(String &topic, String &payload)
|
||||
{
|
||||
|
||||
if (debugger) {
|
||||
debugger->println("Incoming MQTT message:");
|
||||
debugger->print("[");
|
||||
debugger->print(topic);
|
||||
debugger->print("] ");
|
||||
debugger->println(payload);
|
||||
if (Debug.isActive(RemoteDebug::DEBUG)) {
|
||||
debugD("Incoming MQTT message: [%s] %s", topic.c_str(), payload.c_str());
|
||||
}
|
||||
|
||||
// Do whatever needed here...
|
||||
@@ -428,11 +430,7 @@ void readHanPort() {
|
||||
lastSuccessfulRead = millis();
|
||||
|
||||
if(config.getMeterType() > 0) {
|
||||
#if HAS_RGB_LED
|
||||
rgb_led(RGB_GREEN, 1);
|
||||
#else
|
||||
led_on();
|
||||
#endif
|
||||
rgb_led(RGB_GREEN, 2);
|
||||
|
||||
AmsData data(config.getMeterType(), hanReader);
|
||||
ws.setData(data);
|
||||
@@ -441,10 +439,11 @@ void readHanPort() {
|
||||
if(config.getMqttPayloadFormat() == 0) {
|
||||
StaticJsonDocument<512> json;
|
||||
hanToJson(json, data, hw, temperature);
|
||||
if (debugger) {
|
||||
debugger->print("Sending data to MQTT: ");
|
||||
serializeJsonPretty(json, *debugger);
|
||||
debugger->println();
|
||||
if (Debug.isActive(RemoteDebug::INFO)) {
|
||||
debugI("Sending data to MQTT");
|
||||
if (Debug.isActive(RemoteDebug::DEBUG)) {
|
||||
serializeJsonPretty(json, Debug);
|
||||
}
|
||||
}
|
||||
|
||||
String msg;
|
||||
@@ -507,12 +506,6 @@ void readHanPort() {
|
||||
mqtt.loop();
|
||||
delay(10);
|
||||
}
|
||||
|
||||
#if HAS_RGB_LED
|
||||
rgb_led(RGB_GREEN, 0);
|
||||
#else
|
||||
led_off();
|
||||
#endif
|
||||
} else {
|
||||
// Auto detect meter if not set
|
||||
for(int i = 1; i <= 3; i++) {
|
||||
@@ -532,15 +525,15 @@ void readHanPort() {
|
||||
list.toLowerCase();
|
||||
if(list.startsWith("kfm")) {
|
||||
config.setMeterType(1);
|
||||
if(debugger) debugger->println("Detected Kaifa meter");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI("Detected Kaifa meter");
|
||||
break;
|
||||
} else if(list.startsWith("aidon")) {
|
||||
config.setMeterType(2);
|
||||
if(debugger) debugger->println("Detected Aidon meter");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI("Detected Aidon meter");
|
||||
break;
|
||||
} else if(list.startsWith("kamstrup")) {
|
||||
config.setMeterType(3);
|
||||
if(debugger) debugger->println("Detected Kamstrup meter");
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI("Detected Kamstrup meter");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -552,7 +545,7 @@ void readHanPort() {
|
||||
// Switch parity if meter is still not detected
|
||||
if(config.getMeterType() == 0 && millis() - lastSuccessfulRead > 10000) {
|
||||
lastSuccessfulRead = millis();
|
||||
if(debugger) debugger->println("No data for current setting, switching parity");
|
||||
if(Debug.isActive(RemoteDebug::DEBUG)) debugD("No data for current setting, switching parity");
|
||||
#if SOFTWARE_SERIAL
|
||||
if(even) {
|
||||
hanSerial->begin(2400, SWSERIAL_8N1);
|
||||
@@ -579,12 +572,7 @@ void WiFi_connect() {
|
||||
}
|
||||
lastWifiRetry = millis();
|
||||
|
||||
if (debugger) {
|
||||
debugger->println();
|
||||
debugger->println();
|
||||
debugger->print("Connecting to WiFi network ");
|
||||
debugger->println(config.getWifiSsid());
|
||||
}
|
||||
if (Debug.isActive(RemoteDebug::INFO)) debugI("Connecting to WiFi network: %s", config.getWifiSsid().c_str());
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
MDNS.end();
|
||||
@@ -617,7 +605,7 @@ void WiFi_connect() {
|
||||
unsigned long lastMqttRetry = -10000;
|
||||
void MQTT_connect() {
|
||||
if(config.getMqttHost().isEmpty()) {
|
||||
if(debugger) debugger->println("No MQTT config");
|
||||
if(Debug.isActive(RemoteDebug::WARNING)) debugW("No MQTT config");
|
||||
return;
|
||||
}
|
||||
if(millis() - lastMqttRetry < 5000) {
|
||||
@@ -625,12 +613,8 @@ void MQTT_connect() {
|
||||
return;
|
||||
}
|
||||
lastMqttRetry = millis();
|
||||
if(debugger) {
|
||||
debugger->print("Connecting to MQTT: ");
|
||||
debugger->print(config.getMqttHost());
|
||||
debugger->print(", port: ");
|
||||
debugger->print(config.getMqttPort());
|
||||
debugger->println();
|
||||
if(Debug.isActive(RemoteDebug::INFO)) {
|
||||
debugI("Connecting to MQTT %s:%d", config.getMqttHost().c_str(), config.getMqttPort());
|
||||
}
|
||||
|
||||
mqtt.disconnect();
|
||||
@@ -641,13 +625,13 @@ void MQTT_connect() {
|
||||
// Connect to a unsecure or secure MQTT server
|
||||
if ((config.getMqttUser().isEmpty() && mqtt.connect(config.getMqttClientId().c_str())) ||
|
||||
(!config.getMqttUser().isEmpty() && mqtt.connect(config.getMqttClientId().c_str(), config.getMqttUser().c_str(), config.getMqttPassword().c_str()))) {
|
||||
if (debugger) debugger->println("\nSuccessfully connected to MQTT!");
|
||||
if (Debug.isActive(RemoteDebug::INFO)) debugI("Successfully connected to MQTT!");
|
||||
config.ackMqttChange();
|
||||
|
||||
// Subscribe to the chosen MQTT topic, if set in configuration
|
||||
if (!config.getMqttSubscribeTopic().isEmpty()) {
|
||||
mqtt.subscribe(config.getMqttSubscribeTopic());
|
||||
if (debugger) debugger->printf(" Subscribing to [%s]\r\n", config.getMqttSubscribeTopic().c_str());
|
||||
if (Debug.isActive(RemoteDebug::INFO)) debugI(" Subscribing to [%s]\r\n", config.getMqttSubscribeTopic().c_str());
|
||||
}
|
||||
|
||||
if(config.getMqttPayloadFormat() == 0) {
|
||||
@@ -656,9 +640,8 @@ void MQTT_connect() {
|
||||
sendSystemStatusToMqtt();
|
||||
}
|
||||
} else {
|
||||
if (debugger) {
|
||||
debugger->print(" failed, ");
|
||||
debugger->println(" trying again in 5 seconds");
|
||||
if (Debug.isActive(RemoteDebug::ERROR)) {
|
||||
debugI("Failed to connect to MQTT");
|
||||
}
|
||||
}
|
||||
yield();
|
||||
@@ -689,8 +672,8 @@ void sendMqttData(String data)
|
||||
// Send the json over MQTT
|
||||
mqtt.publish(config.getMqttPublishTopic(), msg.c_str());
|
||||
|
||||
if (debugger) debugger->print("sendMqttData: ");
|
||||
if (debugger) debugger->println(data);
|
||||
if (Debug.isActive(RemoteDebug::INFO)) debugI("Sending MQTT data");
|
||||
if (Debug.isActive(RemoteDebug::DEBUG)) debugD("[%s]", data.c_str());
|
||||
}
|
||||
|
||||
unsigned long lastSystemDataSent = -10000;
|
||||
@@ -724,21 +707,21 @@ void rgb_led(int color, int mode) {
|
||||
#endif
|
||||
int blinkduration = 50; // milliseconds
|
||||
switch (mode) {
|
||||
case 0: //OFF
|
||||
case RGB_OFF: //OFF
|
||||
digitalWrite(LEDPIN_RGB_RED, HIGH);
|
||||
digitalWrite(LEDPIN_RGB_GREEN, HIGH);
|
||||
break;
|
||||
case 1: //ON
|
||||
case RGB_ON: //ON
|
||||
switch (color) {
|
||||
case 1: //Red
|
||||
case RGB_RED: //Red
|
||||
digitalWrite(LEDPIN_RGB_RED, LOW);
|
||||
digitalWrite(LEDPIN_RGB_GREEN, HIGH);
|
||||
break;
|
||||
case 2: //Green
|
||||
case RGB_GREEN: //Green
|
||||
digitalWrite(LEDPIN_RGB_RED, HIGH);
|
||||
digitalWrite(LEDPIN_RGB_GREEN, LOW);
|
||||
break;
|
||||
case 3: //Yellow
|
||||
case RGB_YELLOW: //Yellow
|
||||
digitalWrite(LEDPIN_RGB_RED, LOW);
|
||||
digitalWrite(LEDPIN_RGB_GREEN, LOW);
|
||||
break;
|
||||
@@ -746,9 +729,9 @@ void rgb_led(int color, int mode) {
|
||||
break;
|
||||
default: // Blink
|
||||
for(int i = 1; i < mode; i++) {
|
||||
rgb_led(color, 1);
|
||||
rgb_led(color, RGB_ON);
|
||||
delay(blinkduration);
|
||||
rgb_led(color, 0);
|
||||
rgb_led(color, RGB_OFF);
|
||||
if(i != mode)
|
||||
delay(blinkduration);
|
||||
}
|
||||
|
||||
@@ -13,10 +13,12 @@
|
||||
|
||||
#include "Base64.h"
|
||||
|
||||
AmsWebServer::AmsWebServer(RemoteDebug* Debug) {
|
||||
this->debugger = Debug;
|
||||
}
|
||||
|
||||
void AmsWebServer::setup(AmsConfiguration* config, Stream* debugger, MQTTClient* mqtt) {
|
||||
void AmsWebServer::setup(AmsConfiguration* config, MQTTClient* mqtt) {
|
||||
this->config = config;
|
||||
this->debugger = debugger;
|
||||
this->mqtt = mqtt;
|
||||
|
||||
server.on("/", std::bind(&AmsWebServer::indexHtml, this));
|
||||
@@ -61,7 +63,7 @@ void AmsWebServer::setData(AmsData& data) {
|
||||
bool AmsWebServer::checkSecurity(byte level) {
|
||||
bool access = WiFi.getMode() == WIFI_AP || !config->hasConfig() || config->getAuthSecurity() < level;
|
||||
if(!access && config->getAuthSecurity() >= level && server.hasHeader("Authorization")) {
|
||||
println(" forcing web security");
|
||||
printD(" forcing web security");
|
||||
String expectedAuth = String(config->getAuthUser()) + ":" + String(config->getAuthPassword());
|
||||
|
||||
String providedPwd = server.header("Authorization");
|
||||
@@ -73,13 +75,12 @@ bool AmsWebServer::checkSecurity(byte level) {
|
||||
int decodedLength = Base64.decodedLength(inputString, inputStringLength);
|
||||
char decodedString[decodedLength];
|
||||
Base64.decode(decodedString, inputString, inputStringLength);
|
||||
print("Received auth: ");
|
||||
println(decodedString);
|
||||
printD("Received auth: %s", decodedString);
|
||||
access = String(decodedString).equals(expectedAuth);
|
||||
}
|
||||
|
||||
if(!access) {
|
||||
println(" no access, requesting user/pass");
|
||||
printD(" no access, requesting user/pass");
|
||||
server.sendHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"");
|
||||
server.setContentLength(0);
|
||||
server.send(401, "text/html", "");
|
||||
@@ -88,7 +89,7 @@ bool AmsWebServer::checkSecurity(byte level) {
|
||||
}
|
||||
|
||||
void AmsWebServer::indexHtml() {
|
||||
println("Serving /index.html over http...");
|
||||
printD("Serving /index.html over http...");
|
||||
|
||||
if(!checkSecurity(2))
|
||||
return;
|
||||
@@ -155,7 +156,7 @@ void AmsWebServer::indexHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configMeterHtml() {
|
||||
println("Serving /config-meter.html over http...");
|
||||
printD("Serving /config-meter.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -189,7 +190,7 @@ void AmsWebServer::configMeterHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configWifiHtml() {
|
||||
println("Serving /config-wifi.html over http...");
|
||||
printD("Serving /config-wifi.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -219,7 +220,7 @@ void AmsWebServer::configWifiHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configMqttHtml() {
|
||||
println("Serving /config-mqtt.html over http...");
|
||||
printD("Serving /config-mqtt.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -253,7 +254,7 @@ void AmsWebServer::configMqttHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configWebHtml() {
|
||||
println("Serving /config-web.html over http...");
|
||||
printD("Serving /config-web.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -280,7 +281,7 @@ void AmsWebServer::configWebHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::bootCss() {
|
||||
println("Serving /boot.css over http...");
|
||||
printD("Serving /boot.css over http...");
|
||||
|
||||
String css = String((const __FlashStringHelper*) BOOT_CSS);
|
||||
|
||||
@@ -291,7 +292,7 @@ void AmsWebServer::bootCss() {
|
||||
}
|
||||
|
||||
void AmsWebServer::gaugemeterJs() {
|
||||
println("Serving /gaugemeter.js over http...");
|
||||
printD("Serving /gaugemeter.js over http...");
|
||||
|
||||
String js = String((const __FlashStringHelper*) GAUGEMETER_JS);
|
||||
|
||||
@@ -302,7 +303,7 @@ void AmsWebServer::gaugemeterJs() {
|
||||
}
|
||||
|
||||
void AmsWebServer::dataJson() {
|
||||
println("Serving /data.json over http...");
|
||||
printD("Serving /data.json over http...");
|
||||
|
||||
if(!checkSecurity(2))
|
||||
return;
|
||||
@@ -500,11 +501,26 @@ void AmsWebServer::handleSave() {
|
||||
}
|
||||
}
|
||||
|
||||
println("Saving configuration now...");
|
||||
if(server.hasArg("sysConfig") && server.arg("sysConfig") == "true") {
|
||||
config->setDebugTelnet(server.hasArg("debugTelnet") && server.arg("debugTelnet") == "true");
|
||||
config->setDebugSerial(server.hasArg("debugSerial") && server.arg("debugSerial") == "true");
|
||||
config->setDebugLevel(server.arg("debugLevel").toInt());
|
||||
debugger->stop();
|
||||
debugger->begin(config->getWifiHostname(), (uint8_t) config->getDebugLevel());
|
||||
if(config->getAuthSecurity() > 0) {
|
||||
debugger->setPassword(config->getAuthPassword());
|
||||
}
|
||||
debugger->setSerialEnabled(config->isDebugSerial());
|
||||
if(!config->isDebugTelnet()) {
|
||||
debugger->stop();
|
||||
}
|
||||
}
|
||||
|
||||
if (debugger) config->print(debugger);
|
||||
printI("Saving configuration now...");
|
||||
|
||||
if (debugger->isActive(RemoteDebug::DEBUG)) config->print(debugger);
|
||||
if (config->save()) {
|
||||
println("Successfully saved.");
|
||||
printI("Successfully saved.");
|
||||
if(config->isWifiChanged()) {
|
||||
performRestart = true;
|
||||
server.sendHeader("Location","/restart-wait");
|
||||
@@ -514,14 +530,14 @@ void AmsWebServer::handleSave() {
|
||||
server.send (302, "text/plain", "");
|
||||
}
|
||||
} else {
|
||||
println("Error saving configuration");
|
||||
printE("Error saving configuration");
|
||||
String html = "<html><body><h1>Error saving configuration!</h1></form>";
|
||||
server.send(500, "text/html", html);
|
||||
}
|
||||
}
|
||||
|
||||
void AmsWebServer::configSystemHtml() {
|
||||
println("Serving /config-system.html over http...");
|
||||
printD("Serving /config-system.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -533,6 +549,13 @@ void AmsWebServer::configSystemHtml() {
|
||||
html.replace("boot.css", BOOTSTRAP_URL);
|
||||
}
|
||||
|
||||
html.replace("${config.debugTelnet}", config->isDebugTelnet() ? "checked" : "");
|
||||
html.replace("${config.debugSerial}", config->isDebugSerial() ? "checked" : "");
|
||||
html.replace("${config.debugLevel}", String(config->getDebugLevel()));
|
||||
for(int i = 0; i<=RemoteDebug::ANY; i++) {
|
||||
html.replace("${config.debugLevel" + String(i) + "}", config->getDebugLevel() == i ? "selected" : "");
|
||||
}
|
||||
|
||||
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server.sendHeader("Pragma", "no-cache");
|
||||
|
||||
@@ -551,11 +574,11 @@ void AmsWebServer::configSystemUpload() {
|
||||
if(!filename.endsWith(".bin")) {
|
||||
server.send(500, "text/plain", "500: couldn't create file");
|
||||
} else if (!SPIFFS.begin()) {
|
||||
println("An Error has occurred while mounting SPIFFS");
|
||||
printE("An Error has occurred while mounting SPIFFS");
|
||||
String html = "<html><body><h1>Error uploading!</h1></form>";
|
||||
server.send(500, "text/html", html);
|
||||
} else {
|
||||
print("handleFileUpload Name: "); println(filename.c_str());
|
||||
printD("handleFileUpload Name: %s", filename.c_str());
|
||||
firmwareFile = SPIFFS.open("/firmware.bin", "w");
|
||||
filename = String();
|
||||
}
|
||||
@@ -566,7 +589,7 @@ void AmsWebServer::configSystemUpload() {
|
||||
if(firmwareFile) {
|
||||
firmwareFile.close();
|
||||
SPIFFS.end();
|
||||
print("handleFileUpload Size: "); println(String(upload.totalSize).c_str());
|
||||
printD("handleFileUpload Size: %d", upload.totalSize);
|
||||
performRestart = true;
|
||||
server.sendHeader("Location","/restart-wait");
|
||||
server.send(303);
|
||||
@@ -577,7 +600,7 @@ void AmsWebServer::configSystemUpload() {
|
||||
}
|
||||
|
||||
void AmsWebServer::restartWaitHtml() {
|
||||
println("Serving /restart-wait.html over http...");
|
||||
printD("Serving /restart-wait.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -604,7 +627,7 @@ void AmsWebServer::restartWaitHtml() {
|
||||
yield();
|
||||
if(performRestart) {
|
||||
SPIFFS.end();
|
||||
println("Firmware uploaded, rebooting");
|
||||
printI("Firmware uploaded, rebooting");
|
||||
delay(1000);
|
||||
#if defined(ESP8266)
|
||||
ESP.reset();
|
||||
@@ -620,19 +643,30 @@ void AmsWebServer::isAliveCheck() {
|
||||
server.send(200);
|
||||
}
|
||||
|
||||
size_t AmsWebServer::print(const char* text)
|
||||
{
|
||||
if (debugger) debugger->print(text);
|
||||
void AmsWebServer::printD(String fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(String("(AmsWebServer)" + fmt + "\n").c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
size_t AmsWebServer::println(const char* text)
|
||||
{
|
||||
if (debugger) debugger->println(text);
|
||||
|
||||
void AmsWebServer::printI(String fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf(String("(AmsWebServer)" + fmt + "\n").c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
size_t AmsWebServer::print(const Printable& data)
|
||||
{
|
||||
if (debugger) debugger->print(data);
|
||||
|
||||
void AmsWebServer::printW(String fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf(String("(AmsWebServer)" + fmt + "\n").c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
size_t AmsWebServer::println(const Printable& data)
|
||||
{
|
||||
if (debugger) debugger->println(data);
|
||||
|
||||
void AmsWebServer::printE(String fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(String("(AmsWebServer)" + fmt + "\n").c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "HwTools.h"
|
||||
#include "AmsData.h"
|
||||
#include "Uptime.h"
|
||||
#include "RemoteDebug.h"
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
@@ -30,17 +31,18 @@
|
||||
|
||||
class AmsWebServer {
|
||||
public:
|
||||
void setup(AmsConfiguration* config, Stream* debugger, MQTTClient* mqtt);
|
||||
AmsWebServer(RemoteDebug* Debug);
|
||||
void setup(AmsConfiguration* config, MQTTClient* mqtt);
|
||||
void loop();
|
||||
|
||||
void setData(AmsData& data);
|
||||
|
||||
private:
|
||||
RemoteDebug* debugger;
|
||||
int maxPwr = 0;
|
||||
HwTools hw;
|
||||
AmsConfiguration* config;
|
||||
AmsData data;
|
||||
Stream* debugger;
|
||||
MQTTClient* mqtt;
|
||||
File firmwareFile;
|
||||
bool performRestart = false;
|
||||
@@ -70,11 +72,10 @@ private:
|
||||
void restartWaitHtml();
|
||||
void isAliveCheck();
|
||||
|
||||
size_t print(const char* text);
|
||||
size_t println(const char* text);
|
||||
size_t print(const Printable& data);
|
||||
size_t println(const Printable& data);
|
||||
|
||||
void printD(String fmt, ...);
|
||||
void printI(String fmt, ...);
|
||||
void printW(String fmt, ...);
|
||||
void printE(String fmt, ...);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user