Build option to disable remote debug

This commit is contained in:
Gunnar Skjold
2024-06-07 18:48:26 +02:00
parent 93e55f457a
commit 59bf0ce066
36 changed files with 784 additions and 169 deletions

View File

@@ -8,7 +8,9 @@
#define _AMSDATASTORAGE_H #define _AMSDATASTORAGE_H
#include "Arduino.h" #include "Arduino.h"
#include "AmsData.h" #include "AmsData.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "Timezone.h" #include "Timezone.h"
struct DayDataPoints5 { struct DayDataPoints5 {
@@ -53,7 +55,11 @@ struct MonthDataPoints {
class AmsDataStorage { class AmsDataStorage {
public: public:
#if defined(AMS_REMOTE_DEBUG)
AmsDataStorage(RemoteDebug*); AmsDataStorage(RemoteDebug*);
#else
AmsDataStorage(Stream*);
#endif
void setTimezone(Timezone*); void setTimezone(Timezone*);
bool update(AmsData*); bool update(AmsData*);
uint32_t getHourImport(uint8_t); uint32_t getHourImport(uint8_t);
@@ -100,7 +106,11 @@ private:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10 10
}; };
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger; RemoteDebug* debugger;
#else
Stream* debugger;
#endif
}; };
#endif #endif

View File

@@ -10,7 +10,11 @@
#include "AmsStorage.h" #include "AmsStorage.h"
#include "FirmwareVersion.h" #include "FirmwareVersion.h"
#if defined(AMS_REMOTE_DEBUG)
AmsDataStorage::AmsDataStorage(RemoteDebug* debugger) { AmsDataStorage::AmsDataStorage(RemoteDebug* debugger) {
#else
AmsDataStorage::AmsDataStorage(Stream* debugger) {
#endif
day.version = 6; day.version = 6;
day.accuracy = 1; day.accuracy = 1;
month.version = 7; month.version = 7;

View File

@@ -21,6 +21,7 @@
class AmsMqttHandler { class AmsMqttHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
AmsMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) { AmsMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) {
this->mqttConfig = mqttConfig; this->mqttConfig = mqttConfig;
this->mqttConfigChanged = true; this->mqttConfigChanged = true;
@@ -28,6 +29,15 @@ public:
this->json = buf; this->json = buf;
mqtt.dropOverflow(true); mqtt.dropOverflow(true);
}; };
#else
AmsMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf) {
this->mqttConfig = mqttConfig;
this->mqttConfigChanged = true;
this->debugger = debugger;
this->json = buf;
mqtt.dropOverflow(true);
};
#endif
void setCaVerification(bool); void setCaVerification(bool);
void setConfig(MqttConfig& mqttConfig); void setConfig(MqttConfig& mqttConfig);
@@ -55,7 +65,11 @@ public:
}; };
protected: protected:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger; RemoteDebug* debugger;
#else
Stream* debugger;
#endif
MqttConfig mqttConfig; MqttConfig mqttConfig;
bool mqttConfigChanged = true; bool mqttConfigChanged = true;
MQTTClient mqtt = MQTTClient(256); MQTTClient mqtt = MQTTClient(256);

View File

@@ -114,19 +114,31 @@ bool AmsMqttHandler::connect() {
// Connect to a unsecure or secure MQTT server // Connect to a unsecure or secure MQTT server
if ((strlen(mqttConfig.username) == 0 && mqtt.connect(mqttConfig.clientId)) || if ((strlen(mqttConfig.username) == 0 && mqtt.connect(mqttConfig.clientId)) ||
(strlen(mqttConfig.username) > 0 && mqtt.connect(mqttConfig.clientId, mqttConfig.username, mqttConfig.password))) { (strlen(mqttConfig.username) > 0 && mqtt.connect(mqttConfig.clientId, mqttConfig.username, mqttConfig.password))) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully connected to MQTT\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Successfully connected to MQTT\n"));
mqtt.onMessage(std::bind(&AmsMqttHandler::onMessage, this, std::placeholders::_1, std::placeholders::_2)); mqtt.onMessage(std::bind(&AmsMqttHandler::onMessage, this, std::placeholders::_1, std::placeholders::_2));
if(strlen(mqttConfig.subscribeTopic) > 0) { if(strlen(mqttConfig.subscribeTopic) > 0) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR(" Subscribing to [%s]\n"), mqttConfig.subscribeTopic); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR(" Subscribing to [%s]\n"), mqttConfig.subscribeTopic);
if(!mqtt.subscribe(mqttConfig.subscribeTopic)) { if(!mqtt.subscribe(mqttConfig.subscribeTopic)) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), mqttConfig.subscribeTopic); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), mqttConfig.subscribeTopic);
} }
} }
mqtt.publish(statusTopic, "online", true, 0); mqtt.publish(statusTopic, "online", true, 0);
mqtt.loop(); mqtt.loop();
return true; return true;
} else { } else {
if (debugger->isActive(RemoteDebug::ERROR)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
{
debugger->printf_P(PSTR("Failed to connect to MQTT: %d\n"), mqtt.lastError()); debugger->printf_P(PSTR("Failed to connect to MQTT: %d\n"), mqtt.lastError());
#if defined(ESP8266) #if defined(ESP8266)
if(mqttSecureClient) { if(mqttSecureClient) {

View File

@@ -7,7 +7,9 @@
#ifndef _CLOUDCONNECTOR_H #ifndef _CLOUDCONNECTOR_H
#define _CLOUDCONNECTOR_H #define _CLOUDCONNECTOR_H
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "mbedtls/ssl.h" #include "mbedtls/ssl.h"
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
#include "mbedtls/net.h" #include "mbedtls/net.h"
@@ -51,7 +53,11 @@ struct CloudData {
class CloudConnector { class CloudConnector {
public: public:
#if defined(AMS_REMOTE_DEBUG)
CloudConnector(RemoteDebug*); CloudConnector(RemoteDebug*);
#else
CloudConnector(Stream*);
#endif
bool setup(CloudConfig& config, MeterConfig& meter, SystemConfig& system, NtpConfig& ntp, HwTools* hw, ResetDataContainer* rdc); bool setup(CloudConfig& config, MeterConfig& meter, SystemConfig& system, NtpConfig& ntp, HwTools* hw, ResetDataContainer* rdc);
void setMqttHandler(AmsMqttHandler* mqttHandler); void setMqttHandler(AmsMqttHandler* mqttHandler);
void update(AmsData& data, EnergyAccounting& ea); void update(AmsData& data, EnergyAccounting& ea);
@@ -62,7 +68,11 @@ public:
String generateSeed(); String generateSeed();
private: private:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger = NULL; RemoteDebug* debugger = NULL;
#else
Stream* debugger = NULL;
#endif
HwTools* hw = NULL; HwTools* hw = NULL;
ConnectionHandler* ch = NULL; ConnectionHandler* ch = NULL;
ResetDataContainer* rdc = NULL; ResetDataContainer* rdc = NULL;

View File

@@ -23,7 +23,11 @@
#include "esp32s3/rom/rtc.h" #include "esp32s3/rom/rtc.h"
#endif #endif
#if defined(AMS_REMOTE_DEBUG)
CloudConnector::CloudConnector(RemoteDebug* debugger) { CloudConnector::CloudConnector(RemoteDebug* debugger) {
#else
CloudConnector::CloudConnector(Stream* debugger) {
#endif
this->debugger = debugger; this->debugger = debugger;
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
@@ -85,7 +89,10 @@ bool CloudConnector::init() {
strcpy_P(config.hostname, PSTR("cloud.amsleser.no")); strcpy_P(config.hostname, PSTR("cloud.amsleser.no"));
snprintf_P(clearBuffer, CC_BUF_SIZE, PSTR("http://%s/hub/cloud/public.key"), config.hostname); snprintf_P(clearBuffer, CC_BUF_SIZE, PSTR("http://%s/hub/cloud/public.key"), config.hostname);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("(CloudConnector) Downloading public key from %s\n"), clearBuffer); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("(CloudConnector) Downloading public key from %s\n"), clearBuffer);
#if defined(ESP8266) #if defined(ESP8266)
WiFiClient client; WiFiClient client;
client.setTimeout(5000); client.setTimeout(5000);
@@ -121,18 +128,36 @@ bool CloudConnector::init() {
&entropy, (const unsigned char *) pers, &entropy, (const unsigned char *) pers,
strlen(pers)); strlen(pers));
if(ret != 0) { if(ret != 0) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("mbedtls_ctr_drbg_seed return code: %d\n"), ret); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("mbedtls_ctr_drbg_seed return code: %d\n"), ret);
} }
return ret == 0; return ret == 0;
} else { } else {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf("RSA public key read error: "); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf("RSA public key read error: ");
mbedtls_strerror(error_code, clearBuffer, CC_BUF_SIZE); mbedtls_strerror(error_code, clearBuffer, CC_BUF_SIZE);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf("%s\n", clearBuffer); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf("%s\n", clearBuffer);
} }
} else { } else {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(CloudConnector) Communication error, returned status: %d\n"), status); #if defined(AMS_REMOTE_DEBUG)
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http.errorToString(status).c_str()); if (debugger->isActive(RemoteDebug::ERROR))
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http.getString().c_str()); #endif
debugger->printf_P(PSTR("(CloudConnector) Communication error, returned status: %d\n"), status);
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf(http.errorToString(status).c_str());
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf(http.getString().c_str());
http.end(); http.end();
} }
@@ -146,13 +171,19 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) {
unsigned long now = millis(); unsigned long now = millis();
if(now-lastUpdate < config.interval*1000) return; if(now-lastUpdate < config.interval*1000) return;
if(!ESPRandom::isValidV4Uuid(config.clientId)) { if(!ESPRandom::isValidV4Uuid(config.clientId)) {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("(CloudConnector) Client ID is not valid\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("(CloudConnector) Client ID is not valid\n"));
return; return;
} }
if(data.getListType() < 2) return; if(data.getListType() < 2) return;
if(!initialized && !init()) { if(!initialized && !init()) {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Unable to initialize cloud connector\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Unable to initialize cloud connector\n"));
return; return;
} }
initialized = true; initialized = true;
@@ -372,9 +403,15 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) {
if(rsa == nullptr) return; if(rsa == nullptr) return;
int ret = mbedtls_rsa_check_pubkey(rsa); int ret = mbedtls_rsa_check_pubkey(rsa);
if(ret != 0) { if(ret != 0) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("mbedtls_rsa_pkcs1_encrypt return code: %d\n"), ret); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("mbedtls_rsa_pkcs1_encrypt return code: %d\n"), ret);
mbedtls_strerror(ret, clearBuffer, CC_BUF_SIZE); mbedtls_strerror(ret, clearBuffer, CC_BUF_SIZE);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("%s\n"), clearBuffer); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("%s\n"), clearBuffer);
return; return;
} }
memset(encryptedBuffer, 0, rsa->len); memset(encryptedBuffer, 0, rsa->len);
@@ -388,9 +425,15 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) {
udp.write(encryptedBuffer, rsa->len); udp.write(encryptedBuffer, rsa->len);
delay(1); delay(1);
} else { } else {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("mbedtls_rsa_pkcs1_encrypt return code: %d\n"), ret); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("mbedtls_rsa_pkcs1_encrypt return code: %d\n"), ret);
mbedtls_strerror(ret, clearBuffer, CC_BUF_SIZE); mbedtls_strerror(ret, clearBuffer, CC_BUF_SIZE);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("%s\n"), clearBuffer); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("%s\n"), clearBuffer);
} }
} }
udp.endPacket(); udp.endPacket();

View File

@@ -9,13 +9,19 @@
#include "ConnectionHandler.h" #include "ConnectionHandler.h"
#include <Arduino.h> #include <Arduino.h>
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#define CONNECTION_TIMEOUT 30000 #define CONNECTION_TIMEOUT 30000
class EthernetConnectionHandler : public ConnectionHandler { class EthernetConnectionHandler : public ConnectionHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
EthernetConnectionHandler(RemoteDebug* debugger); EthernetConnectionHandler(RemoteDebug* debugger);
#else
EthernetConnectionHandler(Stream* debugger);
#endif
bool connect(NetworkConfig config, SystemConfig sys); bool connect(NetworkConfig config, SystemConfig sys);
void disconnect(unsigned long reconnectDelay); void disconnect(unsigned long reconnectDelay);
@@ -33,7 +39,11 @@ public:
#endif #endif
private: private:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger; RemoteDebug* debugger;
#else
Stream* debugger;
#endif
NetworkConfig config; NetworkConfig config;
bool connected = false; bool connected = false;

View File

@@ -9,12 +9,18 @@
#include "ConnectionHandler.h" #include "ConnectionHandler.h"
#include <Arduino.h> #include <Arduino.h>
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include <DNSServer.h> #include <DNSServer.h>
class WiFiAccessPointConnectionHandler : public ConnectionHandler { class WiFiAccessPointConnectionHandler : public ConnectionHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
WiFiAccessPointConnectionHandler(RemoteDebug* debugger); WiFiAccessPointConnectionHandler(RemoteDebug* debugger);
#else
WiFiAccessPointConnectionHandler(Stream* debugger);
#endif
bool connect(NetworkConfig config, SystemConfig sys); bool connect(NetworkConfig config, SystemConfig sys);
void disconnect(unsigned long reconnectDelay); void disconnect(unsigned long reconnectDelay);
@@ -32,7 +38,11 @@ public:
#endif #endif
private: private:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger; RemoteDebug* debugger;
#else
Stream* debugger;
#endif
NetworkConfig config; NetworkConfig config;
DNSServer dnsServer; DNSServer dnsServer;

View File

@@ -9,7 +9,9 @@
#include "ConnectionHandler.h" #include "ConnectionHandler.h"
#include <Arduino.h> #include <Arduino.h>
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#define CONNECTION_TIMEOUT 30000 #define CONNECTION_TIMEOUT 30000
#define RECONNECT_TIMEOUT 5000 #define RECONNECT_TIMEOUT 5000
@@ -20,7 +22,11 @@ esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=INA
class WiFiClientConnectionHandler : public ConnectionHandler { class WiFiClientConnectionHandler : public ConnectionHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
WiFiClientConnectionHandler(RemoteDebug* debugger); WiFiClientConnectionHandler(RemoteDebug* debugger);
#else
WiFiClientConnectionHandler(Stream* debugger);
#endif
bool connect(NetworkConfig config, SystemConfig sys); bool connect(NetworkConfig config, SystemConfig sys);
void disconnect(unsigned long reconnectDelay); void disconnect(unsigned long reconnectDelay);
@@ -38,7 +44,11 @@ public:
#endif #endif
private: private:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger; RemoteDebug* debugger;
#else
Stream* debugger;
#endif
NetworkConfig config; NetworkConfig config;
bool busPowered = false; bool busPowered = false;
bool firstConnect = true; bool firstConnect = true;

View File

@@ -12,7 +12,11 @@
#include <lwip/dns.h> #include <lwip/dns.h>
#endif #endif
#if defined(AMS_REMOTE_DEBUG)
EthernetConnectionHandler::EthernetConnectionHandler(RemoteDebug* debugger) { EthernetConnectionHandler::EthernetConnectionHandler(RemoteDebug* debugger) {
#else
EthernetConnectionHandler::EthernetConnectionHandler(Stream* debugger) {
#endif
this->debugger = debugger; this->debugger = debugger;
this->mode = NETWORK_MODE_ETH_CLIENT; this->mode = NETWORK_MODE_ETH_CLIENT;
} }
@@ -59,7 +63,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
} else if(sys.boardType == 244) { } else if(sys.boardType == 244) {
return false; // TODO return false; // TODO
} else { } else {
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Board type %d incompatible with ETH\n"), sys.boardType); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Board type %d incompatible with ETH\n"), sys.boardType);
return false; return false;
} }
@@ -68,7 +75,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
digitalWrite(ethEnablePin, 1); digitalWrite(ethEnablePin, 1);
} }
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to Ethernet\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Connecting to Ethernet\n"));
if(ETH.begin(ethAddr, ethPowerPin, ethMdc, ethMdio, ethType, ethClkMode)) { if(ETH.begin(ethAddr, ethPowerPin, ethMdc, ethMdio, ethType, ethClkMode)) {
#if defined(ESP32) #if defined(ESP32)
@@ -97,7 +107,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
} }
} }
} else { } else {
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unable to start Ethernet\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Unable to start Ethernet\n"));
} }
} }
#endif #endif
@@ -105,7 +118,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
} }
void EthernetConnectionHandler::disconnect(unsigned long reconnectDelay) { void EthernetConnectionHandler::disconnect(unsigned long reconnectDelay) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Disconnecting!\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Disconnecting!\n"));
} }
bool EthernetConnectionHandler::isConnected() { bool EthernetConnectionHandler::isConnected() {
@@ -117,12 +133,18 @@ void EthernetConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t
switch(event) { switch(event) {
case ARDUINO_EVENT_ETH_CONNECTED: case ARDUINO_EVENT_ETH_CONNECTED:
connected = true; connected = true;
if(debugger->isActive(RemoteDebug::INFO)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
{
debugger->printf_P(PSTR("Successfully connected to Ethernet!\n")); debugger->printf_P(PSTR("Successfully connected to Ethernet!\n"));
} }
break; break;
case ARDUINO_EVENT_ETH_GOT_IP: case ARDUINO_EVENT_ETH_GOT_IP:
if(debugger->isActive(RemoteDebug::INFO)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
{
debugger->printf_P(PSTR("IP: %s\n"), getIP().toString().c_str()); debugger->printf_P(PSTR("IP: %s\n"), getIP().toString().c_str());
debugger->printf_P(PSTR("GW: %s\n"), getGateway().toString().c_str()); debugger->printf_P(PSTR("GW: %s\n"), getGateway().toString().c_str());
for(uint8_t i = 0; i < 3; i++) { for(uint8_t i = 0; i < 3; i++) {
@@ -132,7 +154,10 @@ void EthernetConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t
} }
break; break;
case ARDUINO_EVENT_ETH_GOT_IP6: { case ARDUINO_EVENT_ETH_GOT_IP6: {
if(debugger->isActive(RemoteDebug::INFO)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
{
IPv6Address ipv6 = getIPv6(); IPv6Address ipv6 = getIPv6();
if(ipv6 == IPv6Address()) { if(ipv6 == IPv6Address()) {
// No IP // No IP
@@ -153,7 +178,10 @@ void EthernetConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t
} }
case ARDUINO_EVENT_ETH_DISCONNECTED: case ARDUINO_EVENT_ETH_DISCONNECTED:
connected = false; connected = false;
if(debugger->isActive(RemoteDebug::WARNING)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
{
debugger->printf_P(PSTR("Ethernet was disconnected!\n")); debugger->printf_P(PSTR("Ethernet was disconnected!\n"));
} }
break; break;

View File

@@ -6,7 +6,11 @@
#include "WiFiAccessPointConnectionHandler.h" #include "WiFiAccessPointConnectionHandler.h"
#if defined(AMS_REMOTE_DEBUG)
WiFiAccessPointConnectionHandler::WiFiAccessPointConnectionHandler(RemoteDebug* debugger) { WiFiAccessPointConnectionHandler::WiFiAccessPointConnectionHandler(RemoteDebug* debugger) {
#else
WiFiAccessPointConnectionHandler::WiFiAccessPointConnectionHandler(Stream* debugger) {
#endif
this->debugger = debugger; this->debugger = debugger;
this->mode = NETWORK_MODE_WIFI_AP; this->mode = NETWORK_MODE_WIFI_AP;
} }
@@ -42,22 +46,37 @@ void WiFiAccessPointConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEvent
IPAddress stationIP; IPAddress stationIP;
switch(event) { switch(event) {
case ARDUINO_EVENT_WIFI_AP_START: case ARDUINO_EVENT_WIFI_AP_START:
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("WiFi access point started with SSID %s\n"), config.ssid); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("WiFi access point started with SSID %s\n"), config.ssid);
break; break;
case ARDUINO_EVENT_WIFI_AP_STOP: case ARDUINO_EVENT_WIFI_AP_STOP:
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("WiFi access point stopped!\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("WiFi access point stopped!\n"));
break; break;
case ARDUINO_EVENT_WIFI_AP_STACONNECTED: case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
memcpy(mac, info.wifi_ap_staconnected.mac, 6); memcpy(mac, info.wifi_ap_staconnected.mac, 6);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Client connected to AP, client MAC: %02x:%02x:%02x:%02x:%02x:%02x\n"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Client connected to AP, client MAC: %02x:%02x:%02x:%02x:%02x:%02x\n"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
break; break;
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
memcpy(mac, info.wifi_ap_staconnected.mac, 6); memcpy(mac, info.wifi_ap_staconnected.mac, 6);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Client disconnected from AP, client MAC: %02x:%02x:%02x:%02x:%02x:%02x\n"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Client disconnected from AP, client MAC: %02x:%02x:%02x:%02x:%02x:%02x\n"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
break; break;
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
stationIP = info.wifi_ap_staipassigned.ip.addr; stationIP = info.wifi_ap_staipassigned.ip.addr;
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Client was assigned IP %s\n"), stationIP.toString().c_str()); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Client was assigned IP %s\n"), stationIP.toString().c_str());
break; break;
} }
} }

View File

@@ -10,7 +10,11 @@
#include <lwip/dns.h> #include <lwip/dns.h>
#endif #endif
#if defined(AMS_REMOTE_DEBUG)
WiFiClientConnectionHandler::WiFiClientConnectionHandler(RemoteDebug* debugger) { WiFiClientConnectionHandler::WiFiClientConnectionHandler(RemoteDebug* debugger) {
#else
WiFiClientConnectionHandler::WiFiClientConnectionHandler(Stream* debugger) {
#endif
this->debugger = debugger; this->debugger = debugger;
this->mode = NETWORK_MODE_WIFI_CLIENT; this->mode = NETWORK_MODE_WIFI_CLIENT;
} }
@@ -28,14 +32,20 @@ bool WiFiClientConnectionHandler::connect(NetworkConfig config, SystemConfig sys
} }
if(WiFi.getMode() != WIFI_OFF) { if(WiFi.getMode() != WIFI_OFF) {
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Not connected to WiFi, closing resources\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Not connected to WiFi, closing resources\n"));
disconnect(RECONNECT_TIMEOUT); disconnect(RECONNECT_TIMEOUT);
return false; return false;
} }
timeout = CONNECTION_TIMEOUT; timeout = CONNECTION_TIMEOUT;
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to WiFi network: %s\n"), config.ssid); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Connecting to WiFi network: %s\n"), config.ssid);
switch(sys.boardType) { switch(sys.boardType) {
case 2: // spenceme case 2: // spenceme
case 3: // Pow-K UART0 case 3: // Pow-K UART0
@@ -111,7 +121,10 @@ bool WiFiClientConnectionHandler::connect(NetworkConfig config, SystemConfig sys
} }
yield(); yield();
} else { } else {
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unable to start WiFi\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Unable to start WiFi\n"));
} }
return true; return true;
} }
@@ -208,7 +221,10 @@ void WiFiClientConnectionHandler::wifi_sta_config(wifi_config_t * wifi_config, c
#endif #endif
void WiFiClientConnectionHandler::disconnect(unsigned long reconnectDelay) { void WiFiClientConnectionHandler::disconnect(unsigned long reconnectDelay) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Disconnecting!\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Disconnecting!\n"));
#if defined(ESP8266) #if defined(ESP8266)
WiFiClient::stopAll(); WiFiClient::stopAll();
#endif #endif
@@ -235,7 +251,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_
} }
break; break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED: case ARDUINO_EVENT_WIFI_STA_CONNECTED:
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully connected to WiFi!\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Successfully connected to WiFi!\n"));
if(config.ipv6 && !WiFi.enableIpV6()) { if(config.ipv6 && !WiFi.enableIpV6()) {
debugger->printf_P(PSTR("Unable to enable IPv6\n")); debugger->printf_P(PSTR("Unable to enable IPv6\n"));
} }
@@ -246,7 +265,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_
wifi_phy_mode_t phyMode; wifi_phy_mode_t phyMode;
if(esp_wifi_sta_get_negotiated_phymode(&phyMode) == ESP_OK) { if(esp_wifi_sta_get_negotiated_phymode(&phyMode) == ESP_OK) {
if(phyMode > WIFI_PHY_MODE_11B) { if(phyMode > WIFI_PHY_MODE_11B) {
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("WiFi supports better rates than 802.11b, disabling\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("WiFi supports better rates than 802.11b, disabling\n"));
config.use11b = false; config.use11b = false;
configChanged = true; configChanged = true;
return; return;
@@ -284,7 +306,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_
#endif #endif
break; break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP: { case ARDUINO_EVENT_WIFI_STA_GOT_IP: {
if(debugger->isActive(RemoteDebug::INFO)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
{
debugger->printf_P(PSTR("IP: %s\n"), getIP().toString().c_str()); debugger->printf_P(PSTR("IP: %s\n"), getIP().toString().c_str());
debugger->printf_P(PSTR("GW: %s\n"), getGateway().toString().c_str()); debugger->printf_P(PSTR("GW: %s\n"), getGateway().toString().c_str());
for(uint8_t i = 0; i < 3; i++) { for(uint8_t i = 0; i < 3; i++) {
@@ -295,7 +320,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_
break; break;
} }
case ARDUINO_EVENT_WIFI_STA_GOT_IP6: { case ARDUINO_EVENT_WIFI_STA_GOT_IP6: {
if(debugger->isActive(RemoteDebug::INFO)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
{
IPv6Address ipv6 = getIPv6(); IPv6Address ipv6 = getIPv6();
if(ipv6 == IPv6Address()) { if(ipv6 == IPv6Address()) {
// No IP // No IP
@@ -322,7 +350,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_
break; break;
default: default:
if(strlen(descr) > 0) { if(strlen(descr) > 0) {
if(debugger->isActive(RemoteDebug::WARNING)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
{
debugger->printf_P(PSTR("WiFi disconnected, reason %s\n"), descr); debugger->printf_P(PSTR("WiFi disconnected, reason %s\n"), descr);
} }
disconnect(RECONNECT_TIMEOUT); disconnect(RECONNECT_TIMEOUT);

View File

@@ -12,9 +12,15 @@
class DomoticzMqttHandler : public AmsMqttHandler { class DomoticzMqttHandler : public AmsMqttHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
DomoticzMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, DomoticzConfig config) : AmsMqttHandler(mqttConfig, debugger, buf) { DomoticzMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, DomoticzConfig config) : AmsMqttHandler(mqttConfig, debugger, buf) {
this->config = config; this->config = config;
}; };
#else
DomoticzMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf, DomoticzConfig config) : AmsMqttHandler(mqttConfig, debugger, buf) {
this->config = config;
};
#endif
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps); bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps);
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);

View File

@@ -80,7 +80,11 @@ struct EnergyAccountingRealtimeData {
class EnergyAccounting { class EnergyAccounting {
public: public:
#if defined(AMS_REMOTE_DEBUG)
EnergyAccounting(RemoteDebug*, EnergyAccountingRealtimeData*); EnergyAccounting(RemoteDebug*, EnergyAccountingRealtimeData*);
#else
EnergyAccounting(Stream*, EnergyAccountingRealtimeData*);
#endif
void setup(AmsDataStorage *ds, EnergyAccountingConfig *config); void setup(AmsDataStorage *ds, EnergyAccountingConfig *config);
void setPriceService(PriceService *ps); void setPriceService(PriceService *ps);
void setTimezone(Timezone*); void setTimezone(Timezone*);
@@ -123,7 +127,11 @@ public:
float getPriceForHour(uint8_t d, uint8_t h); float getPriceForHour(uint8_t d, uint8_t h);
private: private:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger = NULL; RemoteDebug* debugger = NULL;
#else
Stream* debugger = NULL;
#endif
bool init = false, initPrice = false; bool init = false, initPrice = false;
AmsDataStorage *ds = NULL; AmsDataStorage *ds = NULL;
PriceService *ps = NULL; PriceService *ps = NULL;

View File

@@ -9,7 +9,11 @@
#include "AmsStorage.h" #include "AmsStorage.h"
#include "FirmwareVersion.h" #include "FirmwareVersion.h"
#if defined(AMS_REMOTE_DEBUG)
EnergyAccounting::EnergyAccounting(RemoteDebug* debugger, EnergyAccountingRealtimeData* rtd) { EnergyAccounting::EnergyAccounting(RemoteDebug* debugger, EnergyAccountingRealtimeData* rtd) {
#else
EnergyAccounting::EnergyAccounting(Stream* Stream, EnergyAccountingRealtimeData* rtd) {
#endif
data.version = 1; data.version = 1;
this->debugger = debugger; this->debugger = debugger;
if(rtd->magic != 0x6A) { if(rtd->magic != 0x6A) {

View File

@@ -14,7 +14,11 @@
class HomeAssistantMqttHandler : public AmsMqttHandler { class HomeAssistantMqttHandler : public AmsMqttHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
HomeAssistantMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, uint8_t boardType, HomeAssistantConfig config, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) { HomeAssistantMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, uint8_t boardType, HomeAssistantConfig config, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) {
#else
HomeAssistantMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf, uint8_t boardType, HomeAssistantConfig config, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) {
#endif
this->hw = hw; this->hw = hw;
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false; l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false;

View File

@@ -617,7 +617,10 @@ bool HomeAssistantMqttHandler::publishRaw(String data) {
void HomeAssistantMqttHandler::onMessage(String &topic, String &payload) { void HomeAssistantMqttHandler::onMessage(String &topic, String &payload) {
if(topic.equals(statusTopic)) { if(topic.equals(statusTopic)) {
if(payload.equals("online")) { if(payload.equals("online")) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Received online status from HA, resetting sensor status\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Received online status from HA, resetting sensor status\n"));
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false; l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false;
for(uint8_t i = 0; i < 32; i++) tInit[i] = false; for(uint8_t i = 0; i < 32; i++) tInit[i] = false;
for(uint8_t i = 0; i < 38; i++) prInit[i] = false; for(uint8_t i = 0; i < 38; i++) prInit[i] = false;

View File

@@ -11,9 +11,15 @@
class JsonMqttHandler : public AmsMqttHandler { class JsonMqttHandler : public AmsMqttHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
JsonMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) { JsonMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) {
this->hw = hw; this->hw = hw;
}; };
#else
JsonMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) {
this->hw = hw;
};
#endif
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps); bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps);
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);

View File

@@ -11,7 +11,9 @@
#include "TimeLib.h" #include "TimeLib.h"
#include "Timezone.h" #include "Timezone.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "EntsoeA44Parser.h" #include "EntsoeA44Parser.h"
@@ -63,7 +65,11 @@ struct PricePart {
class PriceService { class PriceService {
public: public:
#if defined(AMS_REMOTE_DEBUG)
PriceService(RemoteDebug*); PriceService(RemoteDebug*);
#else
PriceService(Stream*);
#endif
void setup(PriceServiceConfig&); void setup(PriceServiceConfig&);
bool loop(); bool loop();
@@ -88,7 +94,11 @@ public:
bool save(); bool save();
private: private:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger; RemoteDebug* debugger;
#else
Stream* debugger;
#endif
PriceServiceConfig* config = NULL; PriceServiceConfig* config = NULL;
HTTPClient* http = NULL; HTTPClient* http = NULL;

View File

@@ -20,7 +20,11 @@
#include <esp_task_wdt.h> #include <esp_task_wdt.h>
#endif #endif
#if defined(AMS_REMOTE_DEBUG)
PriceService::PriceService(RemoteDebug* Debug) : priceConfig(std::vector<PriceConfig>()) { PriceService::PriceService(RemoteDebug* Debug) : priceConfig(std::vector<PriceConfig>()) {
#else
PriceService::PriceService(Stream* Debug) : priceConfig(std::vector<PriceConfig>()) {
#endif
this->buf = (char*) malloc(BufferSize); this->buf = (char*) malloc(BufferSize);
debugger = Debug; debugger = Debug;
@@ -323,9 +327,18 @@ bool PriceService::retrieve(const char* url, Stream* doc) {
} else { } else {
nextFetchDelayMinutes = 2; nextFetchDelayMinutes = 2;
} }
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Communication error, returned status: %d\n"), status); #if defined(AMS_REMOTE_DEBUG)
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http->errorToString(status).c_str()); if (debugger->isActive(RemoteDebug::ERROR))
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str()); #endif
debugger->printf_P(PSTR("(PriceService) Communication error, returned status: %d\n"), status);
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf(http->errorToString(status).c_str());
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf(http->getString().c_str());
http->end(); http->end();
return false; return false;
@@ -371,13 +384,19 @@ float PriceService::getCurrencyMultiplier(const char* from, const char* to, time
} }
} }
if(currencyMultiplier != 0) { if(currencyMultiplier != 0) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("(PriceService) Resulting currency multiplier: %.4f\n"), currencyMultiplier); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("(PriceService) Resulting currency multiplier: %.4f\n"), currencyMultiplier);
tmElements_t tm; tmElements_t tm;
breakTime(t, tm); breakTime(t, tm);
lastCurrencyFetch = now + (SECS_PER_DAY * 1000) - (((((tm.Hour * 60) + tm.Minute) * 60) + tm.Second) * 1000) + (3600000 * 6) + (tomorrowFetchMinute * 60); lastCurrencyFetch = now + (SECS_PER_DAY * 1000) - (((((tm.Hour * 60) + tm.Minute) * 60) + tm.Second) * 1000) + (3600000 * 6) + (tomorrowFetchMinute * 60);
this->currencyMultiplier = currencyMultiplier; this->currencyMultiplier = currencyMultiplier;
} else { } else {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("(PriceService) Multiplier ended in success, but without value\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("(PriceService) Multiplier ended in success, but without value\n"));
lastCurrencyFetch = now + (SECS_PER_HOUR * 1000); lastCurrencyFetch = now + (SECS_PER_HOUR * 1000);
if(this->currencyMultiplier == 1) return 0; if(this->currencyMultiplier == 1) return 0;
} }
@@ -407,8 +426,14 @@ PricesContainer* PriceService::fetchPrices(time_t t) {
ESP.wdtFeed(); ESP.wdtFeed();
#endif #endif
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("(PriceService) Fetching prices for %02d.%02d.%04d\n"), tm.Day, tm.Month, tm.Year+1970); #if defined(AMS_REMOTE_DEBUG)
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("(PriceService) url: %s\n"), buf); if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("(PriceService) Fetching prices for %02d.%02d.%04d\n"), tm.Day, tm.Month, tm.Year+1970);
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("(PriceService) url: %s\n"), buf);
EntsoeA44Parser a44; EntsoeA44Parser a44;
if(retrieve(buf, &a44) && a44.getPoint(0) != PRICE_NO_VALUE) { if(retrieve(buf, &a44) && a44.getPoint(0) != PRICE_NO_VALUE) {
PricesContainer* ret = new PricesContainer(); PricesContainer* ret = new PricesContainer();
@@ -429,8 +454,14 @@ PricesContainer* PriceService::fetchPrices(time_t t) {
tm.Day, tm.Day,
config->currency config->currency
); );
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("(PriceService) Fetching prices for %02d.%02d.%04d\n"), tm.Day, tm.Month, tm.Year+1970); #if defined(AMS_REMOTE_DEBUG)
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("(PriceService) url: %s\n"), buf); if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("(PriceService) Fetching prices for %02d.%02d.%04d\n"), tm.Day, tm.Month, tm.Year+1970);
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("(PriceService) url: %s\n"), buf);
#if defined(ESP8266) #if defined(ESP8266)
WiFiClient client; WiFiClient client;
client.setTimeout(5000); client.setTimeout(5000);
@@ -471,7 +502,10 @@ PricesContainer* PriceService::fetchPrices(time_t t) {
} else { } else {
lastError = gcmRet; lastError = gcmRet;
nextFetchDelayMinutes = 60; nextFetchDelayMinutes = 60;
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Error code while decrypting prices: %d\n"), gcmRet); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("(PriceService) Error code while decrypting prices: %d\n"), gcmRet);
} }
} else { } else {
lastError = status; lastError = status;
@@ -482,12 +516,21 @@ PricesContainer* PriceService::fetchPrices(time_t t) {
} else { } else {
nextFetchDelayMinutes = 5; nextFetchDelayMinutes = 5;
} }
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Communication error, returned status: %d\n"), status); #if defined(AMS_REMOTE_DEBUG)
if(debugger->isActive(RemoteDebug::ERROR)) { if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("(PriceService) Communication error, returned status: %d\n"), status);
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
{
debugger->printf(http->errorToString(status).c_str()); debugger->printf(http->errorToString(status).c_str());
debugger->println(); debugger->println();
} }
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str()); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf(http->getString().c_str());
http->end(); http->end();
} }
@@ -539,11 +582,17 @@ void PriceService::cropPriceConfig(uint8_t size) {
bool PriceService::save() { bool PriceService::save() {
if(!LittleFS.begin()) { if(!LittleFS.begin()) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Unable to load LittleFS\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("(PriceService) Unable to load LittleFS\n"));
return false; return false;
} }
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("(PriceService) Saving price config\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("(PriceService) Saving price config\n"));
PriceConfig pc; PriceConfig pc;
File file = LittleFS.open(FILE_PRICE_CONF, "w"); File file = LittleFS.open(FILE_PRICE_CONF, "w");
@@ -565,13 +614,19 @@ bool PriceService::save() {
bool PriceService::load() { bool PriceService::load() {
if(!LittleFS.begin()) { if(!LittleFS.begin()) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Unable to load LittleFS\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("(PriceService) Unable to load LittleFS\n"));
return false; return false;
} }
if(!LittleFS.exists(FILE_PRICE_CONF)) { if(!LittleFS.exists(FILE_PRICE_CONF)) {
return false; return false;
} }
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("(PriceService) Loading price config\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("(PriceService) Loading price config\n"));
this->priceConfig.clear(); this->priceConfig.clear();

View File

@@ -11,10 +11,17 @@
class RawMqttHandler : public AmsMqttHandler { class RawMqttHandler : public AmsMqttHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
RawMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) { RawMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) {
full = mqttConfig.payloadFormat == 2; full = mqttConfig.payloadFormat == 2;
topic = String(mqttConfig.publishTopic); topic = String(mqttConfig.publishTopic);
}; };
#else
RawMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) {
full = mqttConfig.payloadFormat == 2;
topic = String(mqttConfig.publishTopic);
};
#endif
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps); bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps);
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);

View File

@@ -16,7 +16,9 @@
#include "AmsDataStorage.h" #include "AmsDataStorage.h"
#include "EnergyAccounting.h" #include "EnergyAccounting.h"
#include "Uptime.h" #include "Uptime.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "PriceService.h" #include "PriceService.h"
#include "RealtimePlot.h" #include "RealtimePlot.h"
#include "ConnectionHandler.h" #include "ConnectionHandler.h"
@@ -46,7 +48,11 @@
class AmsWebServer { class AmsWebServer {
public: public:
#if defined(AMS_REMOTE_DEBUG)
AmsWebServer(uint8_t* buf, RemoteDebug* Debug, HwTools* hw, ResetDataContainer* rdc); AmsWebServer(uint8_t* buf, RemoteDebug* Debug, HwTools* hw, ResetDataContainer* rdc);
#else
AmsWebServer(uint8_t* buf, Stream* Debug, HwTools* hw, ResetDataContainer* rdc);
#endif
void setup(AmsConfiguration*, GpioConfig*, AmsData*, AmsDataStorage*, EnergyAccounting*, RealtimePlot*); void setup(AmsConfiguration*, GpioConfig*, AmsData*, AmsDataStorage*, EnergyAccounting*, RealtimePlot*);
void loop(); void loop();
#if defined(_CLOUDCONNECTOR_H) #if defined(_CLOUDCONNECTOR_H)
@@ -61,7 +67,11 @@ public:
void setConnectionHandler(ConnectionHandler* ch); void setConnectionHandler(ConnectionHandler* ch);
private: private:
RemoteDebug* debugger; #if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger;
#else
Stream* debugger;
#endif
ResetDataContainer* rdc; ResetDataContainer* rdc;
bool mqttEnabled = false; bool mqttEnabled = false;
int maxPwr = 0; int maxPwr = 0;

View File

@@ -51,7 +51,11 @@
#include "esp32s3/rom/rtc.h" #include "esp32s3/rom/rtc.h"
#endif #endif
#if defined(AMS_REMOTE_DEBUG)
AmsWebServer::AmsWebServer(uint8_t* buf, RemoteDebug* Debug, HwTools* hw, ResetDataContainer* rdc) { AmsWebServer::AmsWebServer(uint8_t* buf, RemoteDebug* Debug, HwTools* hw, ResetDataContainer* rdc) {
#else
AmsWebServer::AmsWebServer(uint8_t* buf, Stream* Debug, HwTools* hw, ResetDataContainer* rdc) {
#endif
this->debugger = Debug; this->debugger = Debug;
this->hw = hw; this->hw = hw;
this->buf = (char*) buf; this->buf = (char*) buf;
@@ -83,7 +87,10 @@ void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, AmsDa
if(context.length() == 1) { if(context.length() == 1) {
context = ""; context = "";
} else { } else {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Using context path: '%s'\n"), context.c_str()); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Using context path: '%s'\n"), context.c_str());
} }
} }
@@ -229,7 +236,10 @@ bool AmsWebServer::checkSecurity(byte level, bool send401) {
access = providedPwd.equals(expectedBase64); access = providedPwd.equals(expectedBase64);
if(!access) { if(!access) {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Unsuccessful login: '%s'\n"), providedPwd.c_str()); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Unsuccessful login: '%s'\n"), providedPwd.c_str());
} }
} }
@@ -242,7 +252,10 @@ bool AmsWebServer::checkSecurity(byte level, bool send401) {
} }
void AmsWebServer::notFound() { void AmsWebServer::notFound() {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("URI '%s' was not found\n"), server.uri().c_str()); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("URI '%s' was not found\n"), server.uri().c_str());
server.sendHeader(HEADER_CACHE_CONTROL, CACHE_CONTROL_NO_CACHE); server.sendHeader(HEADER_CACHE_CONTROL, CACHE_CONTROL_NO_CACHE);
server.sendHeader(HEADER_PRAGMA, PRAGMA_NO_CACHE); server.sendHeader(HEADER_PRAGMA, PRAGMA_NO_CACHE);
@@ -453,7 +466,10 @@ void AmsWebServer::sysinfoJson() {
if(ds != NULL) { if(ds != NULL) {
ds->save(); ds->save();
} }
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush(); debugger->flush();
delay(1000); delay(1000);
rdc->cause = 1; rdc->cause = 1;
@@ -1377,11 +1393,15 @@ void AmsWebServer::handleSave() {
if(!pass.equals("***")) { if(!pass.equals("***")) {
strcpy(webConfig.password, pass.c_str()); strcpy(webConfig.password, pass.c_str());
} }
#if defined(AMS_REMOTE_DEBUG)
debugger->setPassword(webConfig.password); debugger->setPassword(webConfig.password);
#endif
} else { } else {
memset(webConfig.username, 0, 37); memset(webConfig.username, 0, 37);
memset(webConfig.password, 0, 37); memset(webConfig.password, 0, 37);
#if defined(AMS_REMOTE_DEBUG)
debugger->setPassword(F("")); debugger->setPassword(F(""));
#endif
} }
strcpy(webConfig.context, server.arg(F("gc")).c_str()); strcpy(webConfig.context, server.arg(F("gc")).c_str());
config->setWebConfig(webConfig); config->setWebConfig(webConfig);
@@ -1444,6 +1464,7 @@ void AmsWebServer::handleSave() {
debug.serial = server.hasArg(F("ds")) && server.arg(F("ds")) == F("true"); debug.serial = server.hasArg(F("ds")) && server.arg(F("ds")) == F("true");
debug.level = server.arg(F("dl")).toInt(); debug.level = server.arg(F("dl")).toInt();
#if defined(AMS_REMOTE_DEBUG)
if(debug.telnet || debug.serial) { if(debug.telnet || debug.serial) {
if(webConfig.security > 0) { if(webConfig.security > 0) {
debugger->setPassword(webConfig.password); debugger->setPassword(webConfig.password);
@@ -1461,6 +1482,7 @@ void AmsWebServer::handleSave() {
} else if(active) { } else if(active) {
performRestart = true; performRestart = true;
} }
#endif
config->setDebugConfig(debug); config->setDebugConfig(debug);
} }
@@ -1580,14 +1602,23 @@ void AmsWebServer::handleSave() {
ps->cropPriceConfig(count); ps->cropPriceConfig(count);
ps->save(); ps->save();
} else { } else {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Price service missing...\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Price service missing...\n"));
} }
} }
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Saving configuration now...\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Saving configuration now...\n"));
if (config->save()) { if (config->save()) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully saved.\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Successfully saved.\n"));
if(config->isNetworkConfigChanged() || performRestart) { if(config->isNetworkConfigChanged() || performRestart) {
performRestart = true; performRestart = true;
} else { } else {
@@ -1612,7 +1643,10 @@ void AmsWebServer::handleSave() {
if(ds != NULL) { if(ds != NULL) {
ds->save(); ds->save();
} }
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush(); debugger->flush();
delay(1000); delay(1000);
rdc->cause = 2; rdc->cause = 2;
@@ -1630,7 +1664,10 @@ void AmsWebServer::reboot() {
server.handleClient(); server.handleClient();
delay(250); delay(250);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush(); debugger->flush();
delay(1000); rdc->cause = 3; delay(1000); rdc->cause = 3;
@@ -1784,7 +1821,10 @@ void AmsWebServer::firmwareUpload() {
if(upload.status == UPLOAD_FILE_START) { if(upload.status == UPLOAD_FILE_START) {
String filename = upload.filename; String filename = upload.filename;
if(filename.isEmpty()) { if(filename.isEmpty()) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("No file, falling back to post\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("No file, falling back to post\n"));
return; return;
} }
if(!filename.endsWith(F(".bin"))) { if(!filename.endsWith(F(".bin"))) {
@@ -1810,10 +1850,16 @@ HTTPUpload& AmsWebServer::uploadFile(const char* path) {
HTTPUpload& upload = server.upload(); HTTPUpload& upload = server.upload();
if(upload.status == UPLOAD_FILE_START) { if(upload.status == UPLOAD_FILE_START) {
if(uploading) { if(uploading) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Upload already in progress\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Upload already in progress\n"));
server.send_P(500, MIME_HTML, PSTR("<html><body><h1>Upload already in progress!</h1></body></html>")); server.send_P(500, MIME_HTML, PSTR("<html><body><h1>Upload already in progress!</h1></body></html>"));
} else if (!LittleFS.begin()) { } else if (!LittleFS.begin()) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("An Error has occurred while mounting LittleFS\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("An Error has occurred while mounting LittleFS\n"));
server.send_P(500, MIME_HTML, PSTR("<html><body><h1>Unable to mount LittleFS!</h1></body></html>")); server.send_P(500, MIME_HTML, PSTR("<html><body><h1>Unable to mount LittleFS!</h1></body></html>"));
} else { } else {
uploading = true; uploading = true;
@@ -1832,7 +1878,10 @@ HTTPUpload& AmsWebServer::uploadFile(const char* path) {
file.close(); file.close();
LittleFS.remove(path); LittleFS.remove(path);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("An Error has occurred while writing file\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("An Error has occurred while writing file\n"));
snprintf_P(buf, BufferSize, RESPONSE_JSON, snprintf_P(buf, BufferSize, RESPONSE_JSON,
"false", "false",
"File size does not match", "File size does not match",
@@ -1888,7 +1937,10 @@ void AmsWebServer::factoryResetPost() {
server.handleClient(); server.handleClient();
delay(250); delay(250);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush(); debugger->flush();
delay(1000); delay(1000);
rdc->cause = 5; rdc->cause = 5;
@@ -2477,7 +2529,10 @@ void AmsWebServer::configFileUpload() {
if(ds != NULL) { if(ds != NULL) {
ds->save(); ds->save();
} }
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush(); debugger->flush();
delay(1000); delay(1000);
rdc->cause = 6; rdc->cause = 6;

View File

@@ -11,6 +11,7 @@ extra_scripts =
lib/SvelteUi/scripts/generate_includes.py lib/SvelteUi/scripts/generate_includes.py
build_flags = build_flags =
-D WEBSOCKET_DISABLED=1 -D WEBSOCKET_DISABLED=1
-D AMS_REMOTE_DEBUG=1
-D NO_AMS2MQTT_PRICE_KEY -D NO_AMS2MQTT_PRICE_KEY
-D NO_AMS2MQTT_PRICE_AUTHENTICATION -D NO_AMS2MQTT_PRICE_AUTHENTICATION
-D NO_AMS2MQTT_SC_KEY -D NO_AMS2MQTT_SC_KEY

View File

@@ -87,6 +87,7 @@ ADC_MODE(ADC_VCC);
#include "Uptime.h" #include "Uptime.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#define debugV_P(x, ...) if (Debug.isActive(Debug.VERBOSE)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();} #define debugV_P(x, ...) if (Debug.isActive(Debug.VERBOSE)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();}
@@ -95,6 +96,16 @@ ADC_MODE(ADC_VCC);
#define debugW_P(x, ...) if (Debug.isActive(Debug.WARNING)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();} #define debugW_P(x, ...) if (Debug.isActive(Debug.WARNING)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();}
#define debugE_P(x, ...) if (Debug.isActive(Debug.ERROR)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();} #define debugE_P(x, ...) if (Debug.isActive(Debug.ERROR)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();}
#define debugA_P(x, ...) if (Debug.isActive(Debug.ANY)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();} #define debugA_P(x, ...) if (Debug.isActive(Debug.ANY)) {Debug.printf_P(x, ##__VA_ARGS__);Debug.println();}
RemoteDebug Debug;
#else
#define debugV_P(x, ...) {Serial.printf_P(x, ##__VA_ARGS__);Serial.println();}
#define debugD_P(x, ...) {Serial.printf_P(x, ##__VA_ARGS__);Serial.println();}
#define debugI_P(x, ...) {Serial.printf_P(x, ##__VA_ARGS__);Serial.println();}
#define debugW_P(x, ...) {Serial.printf_P(x, ##__VA_ARGS__);Serial.println();}
#define debugE_P(x, ...) {Serial.printf_P(x, ##__VA_ARGS__);Serial.println();}
#define debugA_P(x, ...) {Serial.printf_P(x, ##__VA_ARGS__);Serial.println();}
HardwareSerial Debug = Serial;
#endif
#define BUF_SIZE_COMMON (2048) #define BUF_SIZE_COMMON (2048)
@@ -108,8 +119,6 @@ DNSServer* dnsServer = NULL;
AmsConfiguration config; AmsConfiguration config;
RemoteDebug Debug;
PriceService* ps = NULL; PriceService* ps = NULL;
Timezone* tz = NULL; Timezone* tz = NULL;
@@ -381,20 +390,20 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
} }
#if defined(AMS_REMOTE_DEBUG)
Debug.setSerialEnabled(true); Debug.setSerialEnabled(true);
yield(); yield();
#endif
float vcc = hw.getVcc(); float vcc = hw.getVcc();
if (Debug.isActive(RemoteDebug::INFO)) { debugI_P(PSTR("AMS bridge started"));
debugI_P(PSTR("AMS bridge started")); debugI_P(PSTR("Voltage: %.2fV"), vcc);
debugI_P(PSTR("Voltage: %.2fV"), vcc);
}
float vccBootLimit = gpioConfig.vccBootLimit == 0 ? 0 : min(3.29, gpioConfig.vccBootLimit / 10.0); // Make sure it is never above 3.3v float vccBootLimit = gpioConfig.vccBootLimit == 0 ? 0 : min(3.29, gpioConfig.vccBootLimit / 10.0); // Make sure it is never above 3.3v
if(vccBootLimit > 2.5 && vccBootLimit < 3.3 && (gpioConfig.apPin == 0xFF || digitalRead(gpioConfig.apPin) == HIGH)) { // Skip if user is holding AP button while booting (HIGH = button is released) if(vccBootLimit > 2.5 && vccBootLimit < 3.3 && (gpioConfig.apPin == 0xFF || digitalRead(gpioConfig.apPin) == HIGH)) { // Skip if user is holding AP button while booting (HIGH = button is released)
if (vcc < vccBootLimit) { if (vcc < vccBootLimit) {
if(Debug.isActive(RemoteDebug::INFO)) { {
Debug.printf_P(PSTR("(setup) Voltage is too low (%.2f < %.2f), sleeping\n"), vcc, vccBootLimit); Debug.printf_P(PSTR("(setup) Voltage is too low (%.2f < %.2f), sleeping\n"), vcc, vccBootLimit);
Serial.flush(); Serial.flush();
} }
@@ -436,14 +445,14 @@ void setup() {
if (!config.hasConfig()) { if (!config.hasConfig()) {
debugI_P(PSTR("Device has no config, yet a firmware file exists, deleting file.")); debugI_P(PSTR("Device has no config, yet a firmware file exists, deleting file."));
} else if (gpioConfig.apPin == 0xFF || digitalRead(gpioConfig.apPin) == HIGH) { } else if (gpioConfig.apPin == 0xFF || digitalRead(gpioConfig.apPin) == HIGH) {
if(Debug.isActive(RemoteDebug::INFO)) debugI_P(PSTR("Found firmware")); debugI_P(PSTR("Found firmware"));
#if defined(ESP8266) #if defined(ESP8266)
WiFi.setSleepMode(WIFI_LIGHT_SLEEP); WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
WiFi.forceSleepBegin(); WiFi.forceSleepBegin();
#endif #endif
int i = 0; int i = 0;
while(hw.getVcc() > 1.0 && hw.getVcc() < 3.2 && i < 3) { while(hw.getVcc() > 1.0 && hw.getVcc() < 3.2 && i < 3) {
if(Debug.isActive(RemoteDebug::INFO)) debugI_P(PSTR(" vcc not optimal, light sleep 10s")); debugI_P(PSTR(" vcc not optimal, light sleep 10s"));
#if defined(ESP8266) #if defined(ESP8266)
delay(10000); delay(10000);
#elif defined(ESP32) #elif defined(ESP32)
@@ -459,10 +468,8 @@ void setup() {
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
debugD_P(PSTR(" available: %d"), maxSketchSpace); debugD_P(PSTR(" available: %d"), maxSketchSpace);
if (!Update.begin(maxSketchSpace, U_FLASH)) { if (!Update.begin(maxSketchSpace, U_FLASH)) {
if(Debug.isActive(RemoteDebug::ERROR)) { debugE_P(PSTR("Unable to start firmware update"));
debugE_P(PSTR("Unable to start firmware update")); Update.printError(Serial);
Update.printError(Serial);
}
} else { } else {
while (firmwareFile.available()) { while (firmwareFile.available()) {
uint8_t ibuffer[128]; uint8_t ibuffer[128];
@@ -478,15 +485,13 @@ void setup() {
} }
LittleFS.remove(FILE_FIRMWARE); LittleFS.remove(FILE_FIRMWARE);
} else if(LittleFS.exists(FILE_CFG)) { } else if(LittleFS.exists(FILE_CFG)) {
if(Debug.isActive(RemoteDebug::INFO)) debugI_P(PSTR("Found config")); debugI_P(PSTR("Found config"));
configFileParse(); configFileParse();
flashed = true; flashed = true;
} }
if(flashed) { if(flashed) {
if(Debug.isActive(RemoteDebug::INFO)) { debugI_P(PSTR("Firmware update complete, restarting"));
debugI_P(PSTR("Firmware update complete, restarting")); Debug.flush();
Debug.flush();
}
delay(250); delay(250);
ESP.restart(); ESP.restart();
return; return;
@@ -495,14 +500,12 @@ void setup() {
yield(); yield();
if(config.hasConfig()) { if(config.hasConfig()) {
if(Debug.isActive(RemoteDebug::INFO)) config.print(&Debug); config.print(&Debug);
connectToNetwork(); connectToNetwork();
handleNtpChange(); handleNtpChange();
ds.load(); ds.load();
} else { } else {
if(Debug.isActive(RemoteDebug::INFO)) { debugI_P(PSTR("No configuration, booting AP"));
debugI_P(PSTR("No configuration, booting AP"));
}
toggleSetupMode(); toggleSetupMode();
} }
@@ -554,7 +557,9 @@ int lastError = 0;
void loop() { void loop() {
unsigned long now = millis(); unsigned long now = millis();
unsigned long start = now; unsigned long start = now;
#if defined(AMS_REMOTE_DEBUG)
Debug.handle(); Debug.handle();
#endif
unsigned long end = millis(); unsigned long end = millis();
if(end - start > 1000) { if(end - start > 1000) {
debugW_P(PSTR("Used %dms to handle debug"), millis()-start); debugW_P(PSTR("Used %dms to handle debug"), millis()-start);
@@ -570,7 +575,10 @@ void loop() {
if (!setupMode) { if (!setupMode) {
if (ch != NULL && !ch->isConnected()) { if (ch != NULL && !ch->isConnected()) {
if(networkConnected) { if(networkConnected) {
#if defined(AMS_REMOTE_DEBUG)
Debug.stop(); Debug.stop();
#endif
MDNS.end(); MDNS.end();
if(mqttHandler != NULL) { if(mqttHandler != NULL) {
mqttHandler->disconnect(); mqttHandler->disconnect();
@@ -1200,7 +1208,7 @@ void toggleSetupMode() {
yield(); yield();
if (!setupMode || !config.hasConfig()) { if (!setupMode || !config.hasConfig()) {
if(Debug.isActive(RemoteDebug::INFO)) debugI_P(PSTR("Entering setup mode")); debugI_P(PSTR("Entering setup mode"));
//wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, 0); // Disable default gw //wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, 0); // Disable default gw
@@ -1238,7 +1246,7 @@ void toggleSetupMode() {
digitalWrite(gpioConfig.ledDisablePin, LOW); digitalWrite(gpioConfig.ledDisablePin, LOW);
} }
} else { } else {
if(Debug.isActive(RemoteDebug::INFO)) debugI_P(PSTR("Exiting setup mode")); debugI_P(PSTR("Exiting setup mode"));
if(dnsServer != NULL) { if(dnsServer != NULL) {
delete dnsServer; delete dnsServer;
dnsServer = NULL; dnsServer = NULL;
@@ -1364,6 +1372,7 @@ void postConnect() {
config.setNetworkConfig(network); config.setNetworkConfig(network);
} }
WebConfig web; WebConfig web;
#if defined(AMS_REMOTE_DEBUG)
if(config.getWebConfig(web) && web.security > 0) { if(config.getWebConfig(web) && web.security > 0) {
Debug.setPassword(web.password); Debug.setPassword(web.password);
} }
@@ -1376,6 +1385,7 @@ void postConnect() {
} else { } else {
Debug.stop(); Debug.stop();
} }
#endif
mdnsEnabled = false; mdnsEnabled = false;
if(strlen(network.hostname) > 0 && network.mdns) { if(strlen(network.hostname) > 0 && network.mdns) {
debugD_P(PSTR("mDNS is enabled, using host: %s"), network.hostname); debugD_P(PSTR("mDNS is enabled, using host: %s"), network.hostname);
@@ -1434,7 +1444,7 @@ void MQTT_connect() {
MqttConfig mqttConfig; MqttConfig mqttConfig;
if(!config.getMqttConfig(mqttConfig) || strlen(mqttConfig.host) == 0) { if(!config.getMqttConfig(mqttConfig) || strlen(mqttConfig.host) == 0) {
if(Debug.isActive(RemoteDebug::WARNING)) debugW_P(PSTR("No MQTT config")); debugW_P(PSTR("No MQTT config"));
ws.setMqttEnabled(false); ws.setMqttEnabled(false);
mqttEnabled = false; mqttEnabled = false;
return; return;

View File

@@ -7,7 +7,9 @@
#pragma once #pragma once
#include "PassiveMeterCommunicator.h" #include "PassiveMeterCommunicator.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "Timezone.h" #include "Timezone.h"
#include "ImpulseAmsData.h" #include "ImpulseAmsData.h"
@@ -18,7 +20,11 @@
class KmpCommunicator : public PassiveMeterCommunicator { class KmpCommunicator : public PassiveMeterCommunicator {
public: public:
#if defined(AMS_REMOTE_DEBUG)
KmpCommunicator(RemoteDebug* debugger) : PassiveMeterCommunicator(debugger) {}; KmpCommunicator(RemoteDebug* debugger) : PassiveMeterCommunicator(debugger) {};
#else
KmpCommunicator(Stream* debugger) : PassiveMeterCommunicator(debugger) {};
#endif
void configure(MeterConfig&, Timezone*); void configure(MeterConfig&, Timezone*);
bool loop(); bool loop();
AmsData* getData(AmsData& meterState); AmsData* getData(AmsData& meterState);

View File

@@ -9,7 +9,7 @@
#include "ntohll.h" #include "ntohll.h"
#include "Uptime.h" #include "Uptime.h"
LNG::LNG(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx, RemoteDebug* debugger) { LNG::LNG(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx) {
LngHeader* h = (LngHeader*) payload; LngHeader* h = (LngHeader*) payload;
if(h->tag == CosemTypeStructure && h->arrayTag == CosemTypeArray) { if(h->tag == CosemTypeStructure && h->arrayTag == CosemTypeArray) {
apply(meterState); apply(meterState);

View File

@@ -11,7 +11,6 @@
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "DataParser.h" #include "DataParser.h"
#include "Cosem.h" #include "Cosem.h"
#include "RemoteDebug.h"
struct LngHeader { struct LngHeader {
uint8_t tag; uint8_t tag;
@@ -31,7 +30,7 @@ struct LngObisDescriptor {
class LNG : public AmsData { class LNG : public AmsData {
public: public:
LNG(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx, RemoteDebug* debugger); LNG(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx);
uint64_t getNumber(CosemData* item); uint64_t getNumber(CosemData* item);
}; };

View File

@@ -7,7 +7,7 @@
#include "LNG2.h" #include "LNG2.h"
#include "Uptime.h" #include "Uptime.h"
LNG2::LNG2(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx, RemoteDebug* debugger) { LNG2::LNG2(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx) {
CosemBasic* h = (CosemBasic*) payload; CosemBasic* h = (CosemBasic*) payload;
if(h->length == 0x0e) { if(h->length == 0x0e) {
apply(meterState); apply(meterState);

View File

@@ -11,7 +11,6 @@
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "DataParser.h" #include "DataParser.h"
#include "Cosem.h" #include "Cosem.h"
#include "RemoteDebug.h"
struct Lng2Data_3p { struct Lng2Data_3p {
CosemBasic header; CosemBasic header;
@@ -33,7 +32,7 @@ struct Lng2Data_3p {
class LNG2 : public AmsData { class LNG2 : public AmsData {
public: public:
LNG2(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx, RemoteDebug* debugger); LNG2(AmsData& meterState, const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx);
private: private:
uint8_t getString(CosemData* item, char* target); uint8_t getString(CosemData* item, char* target);

View File

@@ -8,7 +8,9 @@
#define _METERCOMMUNICATOR_H #define _METERCOMMUNICATOR_H
#include <Arduino.h> #include <Arduino.h>
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "AmsData.h" #include "AmsData.h"
#include "AmsConfiguration.h" #include "AmsConfiguration.h"

View File

@@ -14,9 +14,15 @@
#include <driver/uart.h> #include <driver/uart.h>
#endif #endif
#if defined(AMS_REMOTE_DEBUG)
PassiveMeterCommunicator::PassiveMeterCommunicator(RemoteDebug* debugger) { PassiveMeterCommunicator::PassiveMeterCommunicator(RemoteDebug* debugger) {
this->debugger = debugger; this->debugger = debugger;
} }
#else
PassiveMeterCommunicator::PassiveMeterCommunicator(Stream* debugger) {
this->debugger = debugger;
}
#endif
void PassiveMeterCommunicator::configure(MeterConfig& meterConfig, Timezone* tz) { void PassiveMeterCommunicator::configure(MeterConfig& meterConfig, Timezone* tz) {
this->meterConfig = meterConfig; this->meterConfig = meterConfig;
@@ -61,7 +67,10 @@ bool PassiveMeterCommunicator::loop() {
if(len >= hanBufferSize) { if(len >= hanBufferSize) {
hanSerial->readBytes(hanBuffer, hanBufferSize); hanSerial->readBytes(hanBuffer, hanBufferSize);
len = 0; len = 0;
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Buffer overflow, resetting\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Buffer overflow, resetting\n"));
return false; return false;
} }
hanBuffer[len++] = hanSerial->read(); hanBuffer[len++] = hanSerial->read();
@@ -70,26 +79,47 @@ bool PassiveMeterCommunicator::loop() {
if(ctx.type > 0 && pos >= 0) { if(ctx.type > 0 && pos >= 0) {
switch(ctx.type) { switch(ctx.type) {
case DATA_TAG_DLMS: case DATA_TAG_DLMS:
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received valid DLMS at %d\n"), pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Received valid DLMS at %d\n"), pos);
break; break;
case DATA_TAG_DSMR: case DATA_TAG_DSMR:
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received valid DSMR at %d\n"), pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Received valid DSMR at %d\n"), pos);
break; break;
case DATA_TAG_SNRM: case DATA_TAG_SNRM:
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received valid SNMR at %d\n"), pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Received valid SNMR at %d\n"), pos);
break; break;
case DATA_TAG_AARE: case DATA_TAG_AARE:
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received valid AARE at %d\n"), pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Received valid AARE at %d\n"), pos);
break; break;
case DATA_TAG_RES: case DATA_TAG_RES:
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received valid Get Response at %d\n"), pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Received valid Get Response at %d\n"), pos);
break; break;
case DATA_TAG_HDLC: case DATA_TAG_HDLC:
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received valid HDLC at %d\n"), pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Received valid HDLC at %d\n"), pos);
break; break;
default: default:
// TODO: Move this so that payload is sent to MQTT // TODO: Move this so that payload is sent to MQTT
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unknown tag %02X at pos %d\n"), ctx.type, pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Unknown tag %02X at pos %d\n"), ctx.type, pos);
len = 0; len = 0;
return false; return false;
} }
@@ -98,16 +128,25 @@ bool PassiveMeterCommunicator::loop() {
} }
end = millis(); end = millis();
if(end-start > 1000) { if(end-start > 1000) {
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Used %dms to unwrap HAN data\n"), end-start); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Used %dms to unwrap HAN data\n"), end-start);
} }
if(pos == DATA_PARSE_INCOMPLETE) { if(pos == DATA_PARSE_INCOMPLETE) {
return false; return false;
} else if(pos == DATA_PARSE_UNKNOWN_DATA) { } else if(pos == DATA_PARSE_UNKNOWN_DATA) {
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Unknown data received\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Unknown data received\n"));
lastError = pos; lastError = pos;
len = len + hanSerial->readBytes(hanBuffer+len, hanBufferSize-len); len = len + hanSerial->readBytes(hanBuffer+len, hanBufferSize-len);
if(debugger->isActive(RemoteDebug::VERBOSE)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
{
debugger->printf_P(PSTR(" payload:\n")); debugger->printf_P(PSTR(" payload:\n"));
debugPrint(hanBuffer, 0, len); debugPrint(hanBuffer, 0, len);
} }
@@ -124,7 +163,10 @@ bool PassiveMeterCommunicator::loop() {
if(pt != NULL) { if(pt != NULL) {
pt->publishBytes(hanBuffer, len); pt->publishBytes(hanBuffer, len);
} }
if(debugger->isActive(RemoteDebug::VERBOSE)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
{
debugger->printf_P(PSTR(" payload:\n")); debugger->printf_P(PSTR(" payload:\n"));
debugPrint(hanBuffer, 0, len); debugPrint(hanBuffer, 0, len);
} }
@@ -134,10 +176,16 @@ bool PassiveMeterCommunicator::loop() {
} }
if(ctx.type == 0) { if(ctx.type == 0) {
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Ended up with context type %d, return code %d and length: %lu/%lu\n"), ctx.type, pos, ctx.length, len); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Ended up with context type %d, return code %d and length: %lu/%lu\n"), ctx.type, pos, ctx.length, len);
lastError = pos; lastError = pos;
len = len + hanSerial->readBytes(hanBuffer+len, hanBufferSize-len); len = len + hanSerial->readBytes(hanBuffer+len, hanBufferSize-len);
if(debugger->isActive(RemoteDebug::VERBOSE)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
{
debugger->printf_P(PSTR(" payload:\n")); debugger->printf_P(PSTR(" payload:\n"));
debugPrint(hanBuffer, 0, len); debugPrint(hanBuffer, 0, len);
} }
@@ -171,13 +219,22 @@ AmsData* PassiveMeterCommunicator::getData(AmsData& meterState) {
pt->publishBytes((uint8_t*) payload, ctx.length); pt->publishBytes((uint8_t*) payload, ctx.length);
} }
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("Using application data:\n")); #if defined(AMS_REMOTE_DEBUG)
if(debugger->isActive(RemoteDebug::VERBOSE)) debugPrint((byte*) payload, 0, ctx.length); if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("Using application data:\n"));
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugPrint((byte*) payload, 0, ctx.length);
// Rudimentary detector for L&G proprietary format, this is terrible code... Fix later // Rudimentary detector for L&G proprietary format, this is terrible code... Fix later
if(payload[0] == CosemTypeStructure && payload[2] == CosemTypeArray && payload[1] == payload[3]) { if(payload[0] == CosemTypeStructure && payload[2] == CosemTypeArray && payload[1] == payload[3]) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("LNG\n")); #if defined(AMS_REMOTE_DEBUG)
LNG lngData = LNG(meterState, payload, meterState.getMeterType(), &meterConfig, ctx, debugger); if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("LNG\n"));
LNG lngData = LNG(meterState, payload, meterState.getMeterType(), &meterConfig, ctx);
if(lngData.getListType() >= 1) { if(lngData.getListType() >= 1) {
data = new AmsData(); data = new AmsData();
data->apply(meterState); data->apply(meterState);
@@ -191,15 +248,21 @@ AmsData* PassiveMeterCommunicator::getData(AmsData& meterState) {
payload[14] == CosemTypeLongUnsigned && payload[14] == CosemTypeLongUnsigned &&
payload[17] == CosemTypeLongUnsigned payload[17] == CosemTypeLongUnsigned
) { ) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("LNG2\n")); #if defined(AMS_REMOTE_DEBUG)
LNG2 lngData = LNG2(meterState, payload, meterState.getMeterType(), &meterConfig, ctx, debugger); if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("LNG2\n"));
LNG2 lngData = LNG2(meterState, payload, meterState.getMeterType(), &meterConfig, ctx);
if(lngData.getListType() >= 1) { if(lngData.getListType() >= 1) {
data = new AmsData(); data = new AmsData();
data->apply(meterState); data->apply(meterState);
data->apply(lngData); data->apply(lngData);
} }
} else { } else {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("DLMS\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("DLMS\n"));
// TODO: Split IEC6205675 into DataParserKaifa and DataParserObis. This way we can add other means of parsing, for those other proprietary formats // TODO: Split IEC6205675 into DataParserKaifa and DataParserObis. This way we can add other means of parsing, for those other proprietary formats
data = new IEC6205675(payload, meterState.getMeterType(), &meterConfig, ctx, meterState); data = new IEC6205675(payload, meterState.getMeterType(), &meterConfig, ctx, meterState);
} }
@@ -221,7 +284,10 @@ int PassiveMeterCommunicator::getLastError() {
#if defined ESP8266 #if defined ESP8266
if(hwSerial != NULL) { if(hwSerial != NULL) {
if(hwSerial->hasRxError()) { if(hwSerial->hasRxError()) {
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Serial RX error\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Serial RX error\n"));
lastError = 96; lastError = 96;
} }
if(hwSerial->hasOverrun()) { if(hwSerial->hasOverrun()) {
@@ -293,7 +359,10 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co
doRet = true; doRet = true;
break; break;
default: default:
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Ended up in default case while unwrapping...(tag %02X)\n"), tag); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Ended up in default case while unwrapping...(tag %02X)\n"), tag);
return DATA_PARSE_UNKNOWN_DATA; return DATA_PARSE_UNKNOWN_DATA;
} }
lastTag = tag; lastTag = tag;
@@ -301,53 +370,89 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co
return res; return res;
} }
if(context.length > end) { if(context.length > end) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("Context length %lu > %lu:\n"), context.length, end); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("Context length %lu > %lu:\n"), context.length, end);
context.type = 0; context.type = 0;
context.length = 0; context.length = 0;
return false; return false;
} }
switch(tag) { switch(tag) {
case DATA_TAG_HDLC: case DATA_TAG_HDLC:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("HDLC frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("HDLC frame:\n"));
if(pt != NULL) { if(pt != NULL) {
pt->publishBytes(buf, curLen); pt->publishBytes(buf, curLen);
} }
break; break;
case DATA_TAG_MBUS: case DATA_TAG_MBUS:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("MBUS frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("MBUS frame:\n"));
if(pt != NULL) { if(pt != NULL) {
pt->publishBytes(buf, curLen); pt->publishBytes(buf, curLen);
} }
break; break;
case DATA_TAG_GBT: case DATA_TAG_GBT:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("GBT frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("GBT frame:\n"));
break; break;
case DATA_TAG_GCM: case DATA_TAG_GCM:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("GCM frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("GCM frame:\n"));
break; break;
case DATA_TAG_LLC: case DATA_TAG_LLC:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("LLC frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("LLC frame:\n"));
break; break;
case DATA_TAG_DLMS: case DATA_TAG_DLMS:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("DLMS frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("DLMS frame:\n"));
break; break;
case DATA_TAG_DSMR: case DATA_TAG_DSMR:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("DSMR frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("DSMR frame:\n"));
if(pt != NULL) { if(pt != NULL) {
pt->publishString((char*) buf); pt->publishString((char*) buf);
} }
break; break;
case DATA_TAG_SNRM: case DATA_TAG_SNRM:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("SNMR frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("SNMR frame:\n"));
break; break;
case DATA_TAG_AARE: case DATA_TAG_AARE:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("AARE frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("AARE frame:\n"));
break; break;
case DATA_TAG_RES: case DATA_TAG_RES:
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("RES frame:\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugger->printf_P(PSTR("RES frame:\n"));
break; break;
} }
if(debugger->isActive(RemoteDebug::VERBOSE)) debugPrint(buf, 0, curLen); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE))
#endif
debugPrint(buf, 0, curLen);
if(res == DATA_PARSE_FINAL_SEGMENT) { if(res == DATA_PARSE_FINAL_SEGMENT) {
if(tag == DATA_TAG_MBUS) { if(tag == DATA_TAG_MBUS) {
res = mbusParser->write(buf, context); res = mbusParser->write(buf, context);
@@ -370,7 +475,10 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co
// Use start byte of new buffer position as tag for next round in loop // Use start byte of new buffer position as tag for next round in loop
tag = (*buf); tag = (*buf);
} }
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Got to end of unwrap method...\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Got to end of unwrap method...\n"));
return DATA_PARSE_UNKNOWN_DATA; return DATA_PARSE_UNKNOWN_DATA;
} }
@@ -391,40 +499,76 @@ void PassiveMeterCommunicator::debugPrint(byte *buffer, int start, int length) {
} }
void PassiveMeterCommunicator::printHanReadError(int pos) { void PassiveMeterCommunicator::printHanReadError(int pos) {
if(debugger->isActive(RemoteDebug::WARNING)) { #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
{
switch(pos) { switch(pos) {
case DATA_PARSE_BOUNDRY_FLAG_MISSING: case DATA_PARSE_BOUNDRY_FLAG_MISSING:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Boundry flag missing\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Boundry flag missing\n"));
break; break;
case DATA_PARSE_HEADER_CHECKSUM_ERROR: case DATA_PARSE_HEADER_CHECKSUM_ERROR:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Header checksum error\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Header checksum error\n"));
break; break;
case DATA_PARSE_FOOTER_CHECKSUM_ERROR: case DATA_PARSE_FOOTER_CHECKSUM_ERROR:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Frame checksum error\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Frame checksum error\n"));
break; break;
case DATA_PARSE_INCOMPLETE: case DATA_PARSE_INCOMPLETE:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Received frame is incomplete\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Received frame is incomplete\n"));
break; break;
case GCM_AUTH_FAILED: case GCM_AUTH_FAILED:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Decrypt authentication failed\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Decrypt authentication failed\n"));
break; break;
case GCM_ENCRYPTION_KEY_FAILED: case GCM_ENCRYPTION_KEY_FAILED:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Setting decryption key failed\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Setting decryption key failed\n"));
break; break;
case GCM_DECRYPT_FAILED: case GCM_DECRYPT_FAILED:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Decryption failed\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Decryption failed\n"));
break; break;
case MBUS_FRAME_LENGTH_NOT_EQUAL: case MBUS_FRAME_LENGTH_NOT_EQUAL:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Frame length mismatch\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Frame length mismatch\n"));
break; break;
case DATA_PARSE_INTERMEDIATE_SEGMENT: case DATA_PARSE_INTERMEDIATE_SEGMENT:
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Intermediate segment received\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Intermediate segment received\n"));
break; break;
case DATA_PARSE_UNKNOWN_DATA: case DATA_PARSE_UNKNOWN_DATA:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Unknown data format %02X\n"), hanBuffer[0]); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Unknown data format %02X\n"), hanBuffer[0]);
break; break;
default: default:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Unspecified error while reading data: %d\n"), pos); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Unspecified error while reading data: %d\n"), pos);
} }
} }
} }
@@ -437,7 +581,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
baud = 2400; baud = 2400;
} }
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("(setupHanPort) Setting up HAN on pin %d/%d with baud %d and parity %d\n"), rxpin, txpin, baud, parityOrdinal); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("(setupHanPort) Setting up HAN on pin %d/%d with baud %d and parity %d\n"), rxpin, txpin, baud, parityOrdinal);
if(parityOrdinal == 0) { if(parityOrdinal == 0) {
parityOrdinal = 3; // 8N1 parityOrdinal = 3; // 8N1
@@ -464,7 +611,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
#endif #endif
if(rxpin == 0) { if(rxpin == 0) {
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Invalid GPIO configured for HAN\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Invalid GPIO configured for HAN\n"));
return; return;
} }
@@ -472,7 +622,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
if(meterConfig.bufferSize > 64) meterConfig.bufferSize = 64; if(meterConfig.bufferSize > 64) meterConfig.bufferSize = 64;
if(hwSerial != NULL) { if(hwSerial != NULL) {
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Hardware serial\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Hardware serial\n"));
Serial.flush(); Serial.flush();
#if defined(ESP8266) #if defined(ESP8266)
SerialConfig serialConfig; SerialConfig serialConfig;
@@ -508,10 +661,16 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
#if defined(ESP8266) #if defined(ESP8266)
if(rxpin == 3) { if(rxpin == 3) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Switching UART0 to pin 1 & 3\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Switching UART0 to pin 1 & 3\n"));
Serial.pins(1,3); Serial.pins(1,3);
} else if(rxpin == 113) { } else if(rxpin == 113) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Switching UART0 to pin 15 & 13\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Switching UART0 to pin 15 & 13\n"));
Serial.pins(15,13); Serial.pins(15,13);
} }
#endif #endif
@@ -543,7 +702,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
#endif #endif
} else { } else {
#if defined(ESP8266) #if defined(ESP8266)
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Software serial\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Software serial\n"));
Serial.flush(); Serial.flush();
if(swSerial == NULL) { if(swSerial == NULL) {
@@ -575,7 +737,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
#if defined(ESP8266) #if defined(ESP8266)
if(bufferSize > 2) bufferSize = 2; if(bufferSize > 2) bufferSize = 2;
#endif #endif
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Using serial buffer size %d\n"), 64 * bufferSize); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Using serial buffer size %d\n"), 64 * bufferSize);
swSerial->begin(baud, serialConfig, rxpin, txpin, invert, meterConfig.bufferSize * 64); swSerial->begin(baud, serialConfig, rxpin, txpin, invert, meterConfig.bufferSize * 64);
hanSerial = swSerial; hanSerial = swSerial;
@@ -583,7 +748,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
Serial.begin(115200); Serial.begin(115200);
hwSerial = NULL; hwSerial = NULL;
#else #else
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Software serial not available\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Software serial not available\n"));
return; return;
#endif #endif
} }
@@ -596,7 +764,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
// The library automatically sets the pullup in Serial.begin() // The library automatically sets the pullup in Serial.begin()
if(!meterConfig.rxPinPullup) { if(!meterConfig.rxPinPullup) {
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("HAN pin pullup disabled\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("HAN pin pullup disabled\n"));
pinMode(meterConfig.rxPin, INPUT); pinMode(meterConfig.rxPin, INPUT);
} }
@@ -628,23 +799,38 @@ void PassiveMeterCommunicator::rxerr(int err) {
if(err == 0) return; if(err == 0) return;
switch(err) { switch(err) {
case 2: case 2:
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Serial buffer overflow\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Serial buffer overflow\n"));
rxBufferErrors++; rxBufferErrors++;
if(rxBufferErrors > 1 && meterConfig.bufferSize < 8) { if(rxBufferErrors > 1 && meterConfig.bufferSize < 8) {
meterConfig.bufferSize += 2; meterConfig.bufferSize += 2;
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Increasing RX buffer to %d bytes\n"), meterConfig.bufferSize * 64); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Increasing RX buffer to %d bytes\n"), meterConfig.bufferSize * 64);
configChanged = true; configChanged = true;
rxBufferErrors = 0; rxBufferErrors = 0;
} }
break; break;
case 3: case 3:
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Serial FIFO overflow\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR("Serial FIFO overflow\n"));
break; break;
case 4: case 4:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Serial frame error\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Serial frame error\n"));
break; break;
case 5: case 5:
if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Serial parity error\n")); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::WARNING))
#endif
debugger->printf_P(PSTR("Serial parity error\n"));
unsigned long now = millis(); unsigned long now = millis();
if(now - meterAutodetectLastChange < 120000) { if(now - meterAutodetectLastChange < 120000) {
switch(autodetectParity) { switch(autodetectParity) {
@@ -701,13 +887,23 @@ void PassiveMeterCommunicator::handleAutodetect(unsigned long now) {
autodetectCount = 0; autodetectCount = 0;
} }
autodetectBaud = AUTO_BAUD_RATES[autodetectCount++]; autodetectBaud = AUTO_BAUD_RATES[autodetectCount++];
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Meter serial autodetect, swapping to: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false"); #if defined(ESP_REMOTE_DEBUG)
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Meter serial autodetect, swapping to: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false");
#endif
meterConfig.bufferSize = max((uint32_t) 1, autodetectBaud / 14400); meterConfig.bufferSize = max((uint32_t) 1, autodetectBaud / 14400);
setupHanPort(autodetectBaud, autodetectParity, autodetectInvert); setupHanPort(autodetectBaud, autodetectParity, autodetectInvert);
meterAutodetectLastChange = now; meterAutodetectLastChange = now;
} }
} else if(autodetect) { } else if(autodetect) {
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Meter serial autodetected, saving: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false"); #if defined(ESP_REMOTE_DEBUG)
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::INFO))
#endif
debugger->printf_P(PSTR("Meter serial autodetected, saving: %d, %d, %s\n"), autodetectBaud, autodetectParity, autodetectInvert ? "true" : "false");
#endif
autodetect = false; autodetect = false;
meterConfig.baud = autodetectBaud; meterConfig.baud = autodetectBaud;
meterConfig.parity = autodetectParity; meterConfig.parity = autodetectParity;

View File

@@ -8,7 +8,9 @@
#define _PASSIVEMETERCOMMUNICATOR_H #define _PASSIVEMETERCOMMUNICATOR_H
#include "MeterCommunicator.h" #include "MeterCommunicator.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "DataParsers.h" #include "DataParsers.h"
#include "Timezone.h" #include "Timezone.h"
@@ -22,7 +24,11 @@ const uint32_t AUTO_BAUD_RATES[] = { 2400, 115200 };
class PassiveMeterCommunicator : public MeterCommunicator { class PassiveMeterCommunicator : public MeterCommunicator {
public: public:
#if defined(AMS_REMOTE_DEBUG)
PassiveMeterCommunicator(RemoteDebug* debugger); PassiveMeterCommunicator(RemoteDebug* debugger);
#else
PassiveMeterCommunicator(Stream* debugger);
#endif
void configure(MeterConfig&, Timezone*); void configure(MeterConfig&, Timezone*);
bool loop(); bool loop();
AmsData* getData(AmsData& meterState); AmsData* getData(AmsData& meterState);
@@ -35,7 +41,11 @@ public:
void rxerr(int err); void rxerr(int err);
protected: protected:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger = NULL; RemoteDebug* debugger = NULL;
#else
Stream* debugger = NULL;
#endif
MeterConfig meterConfig; MeterConfig meterConfig;
bool configChanged = false; bool configChanged = false;
Timezone* tz; Timezone* tz;

View File

@@ -11,9 +11,15 @@
class PassthroughMqttHandler : public AmsMqttHandler { class PassthroughMqttHandler : public AmsMqttHandler {
public: public:
#if defined(AMS_REMOTE_DEBUG)
PassthroughMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) { PassthroughMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) {
this->topic = String(mqttConfig.publishTopic); this->topic = String(mqttConfig.publishTopic);
}; };
#else
PassthroughMqttHandler(MqttConfig& mqttConfig, Stream* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) {
this->topic = String(mqttConfig.publishTopic);
};
#endif
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps); bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps);
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);

View File

@@ -1,7 +1,11 @@
#include "PulseMeterCommunicator.h" #include "PulseMeterCommunicator.h"
#include "Uptime.h" #include "Uptime.h"
#if defined(AMS_REMOTE_DEBUG)
PulseMeterCommunicator::PulseMeterCommunicator(RemoteDebug* debugger) { PulseMeterCommunicator::PulseMeterCommunicator(RemoteDebug* debugger) {
#else
PulseMeterCommunicator::PulseMeterCommunicator(Stream* debugger) {
#endif
this->debugger = debugger; this->debugger = debugger;
} }
@@ -42,7 +46,10 @@ void PulseMeterCommunicator::getCurrentConfig(MeterConfig& meterConfig) {
} }
void PulseMeterCommunicator::setupGpio() { void PulseMeterCommunicator::setupGpio() {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Setting up Pulse Meter GPIO, rx: %d, tx: %d\n"), meterConfig.rxPin, meterConfig.txPin); #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::DEBUG))
#endif
debugger->printf_P(PSTR("Setting up Pulse Meter GPIO, rx: %d, tx: %d\n"), meterConfig.rxPin, meterConfig.txPin);
if(meterConfig.rxPin != NOT_A_PIN) { if(meterConfig.rxPin != NOT_A_PIN) {
pinMode(meterConfig.rxPin, meterConfig.rxPinPullup ? INPUT_PULLUP : INPUT); pinMode(meterConfig.rxPin, meterConfig.rxPinPullup ? INPUT_PULLUP : INPUT);
} }

View File

@@ -8,14 +8,20 @@
#define _PULSEMETERCOMMUNICATOR_H #define _PULSEMETERCOMMUNICATOR_H
#include "MeterCommunicator.h" #include "MeterCommunicator.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h" #include "RemoteDebug.h"
#endif
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "Timezone.h" #include "Timezone.h"
#include "ImpulseAmsData.h" #include "ImpulseAmsData.h"
class PulseMeterCommunicator : public MeterCommunicator { class PulseMeterCommunicator : public MeterCommunicator {
public: public:
#if defined(AMS_REMOTE_DEBUG)
PulseMeterCommunicator(RemoteDebug* debugger); PulseMeterCommunicator(RemoteDebug* debugger);
#else
PulseMeterCommunicator(Stream* debugger);
#endif
void configure(MeterConfig& config, Timezone* tz); void configure(MeterConfig& config, Timezone* tz);
bool loop(); bool loop();
AmsData* getData(AmsData& meterState); AmsData* getData(AmsData& meterState);
@@ -26,7 +32,11 @@ public:
void onPulse(uint8_t pulses); void onPulse(uint8_t pulses);
protected: protected:
#if defined(AMS_REMOTE_DEBUG)
RemoteDebug* debugger = NULL; RemoteDebug* debugger = NULL;
#else
Stream* debugger = NULL;
#endif
MeterConfig meterConfig; MeterConfig meterConfig;
bool configChanged = false; bool configChanged = false;
Timezone* tz; Timezone* tz;