diff --git a/lib/AmsDataStorage/include/AmsDataStorage.h b/lib/AmsDataStorage/include/AmsDataStorage.h index 1a1c74bb..64687880 100644 --- a/lib/AmsDataStorage/include/AmsDataStorage.h +++ b/lib/AmsDataStorage/include/AmsDataStorage.h @@ -8,7 +8,9 @@ #define _AMSDATASTORAGE_H #include "Arduino.h" #include "AmsData.h" +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "Timezone.h" struct DayDataPoints5 { @@ -53,7 +55,11 @@ struct MonthDataPoints { class AmsDataStorage { public: + #if defined(AMS_REMOTE_DEBUG) AmsDataStorage(RemoteDebug*); + #else + AmsDataStorage(Stream*); + #endif void setTimezone(Timezone*); bool update(AmsData*); 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, 10 }; + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger; + #else + Stream* debugger; + #endif }; #endif diff --git a/lib/AmsDataStorage/src/AmsDataStorage.cpp b/lib/AmsDataStorage/src/AmsDataStorage.cpp index 2602ff85..96befbe7 100644 --- a/lib/AmsDataStorage/src/AmsDataStorage.cpp +++ b/lib/AmsDataStorage/src/AmsDataStorage.cpp @@ -10,7 +10,11 @@ #include "AmsStorage.h" #include "FirmwareVersion.h" +#if defined(AMS_REMOTE_DEBUG) AmsDataStorage::AmsDataStorage(RemoteDebug* debugger) { +#else +AmsDataStorage::AmsDataStorage(Stream* debugger) { +#endif day.version = 6; day.accuracy = 1; month.version = 7; diff --git a/lib/AmsMqttHandler/include/AmsMqttHandler.h b/lib/AmsMqttHandler/include/AmsMqttHandler.h index 76f03915..826bccdd 100644 --- a/lib/AmsMqttHandler/include/AmsMqttHandler.h +++ b/lib/AmsMqttHandler/include/AmsMqttHandler.h @@ -21,6 +21,7 @@ class AmsMqttHandler { public: + #if defined(AMS_REMOTE_DEBUG) AmsMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) { this->mqttConfig = mqttConfig; this->mqttConfigChanged = true; @@ -28,6 +29,15 @@ public: this->json = buf; 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 setConfig(MqttConfig& mqttConfig); @@ -55,7 +65,11 @@ public: }; protected: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger; + #else + Stream* debugger; + #endif MqttConfig mqttConfig; bool mqttConfigChanged = true; MQTTClient mqtt = MQTTClient(256); diff --git a/lib/AmsMqttHandler/src/AmsMqttHandler.cpp b/lib/AmsMqttHandler/src/AmsMqttHandler.cpp index 1321cf2b..b8ba10cf 100644 --- a/lib/AmsMqttHandler/src/AmsMqttHandler.cpp +++ b/lib/AmsMqttHandler/src/AmsMqttHandler.cpp @@ -114,19 +114,31 @@ bool AmsMqttHandler::connect() { // Connect to a unsecure or secure MQTT server if ((strlen(mqttConfig.username) == 0 && mqtt.connect(mqttConfig.clientId)) || (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)); 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(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.loop(); return true; } 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()); #if defined(ESP8266) if(mqttSecureClient) { diff --git a/lib/CloudConnector/include/CloudConnector.h b/lib/CloudConnector/include/CloudConnector.h index e150768a..eb603351 100644 --- a/lib/CloudConnector/include/CloudConnector.h +++ b/lib/CloudConnector/include/CloudConnector.h @@ -7,7 +7,9 @@ #ifndef _CLOUDCONNECTOR_H #define _CLOUDCONNECTOR_H +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "mbedtls/ssl.h" #include "mbedtls/platform.h" #include "mbedtls/net.h" @@ -51,7 +53,11 @@ struct CloudData { class CloudConnector { public: + #if defined(AMS_REMOTE_DEBUG) CloudConnector(RemoteDebug*); + #else + CloudConnector(Stream*); + #endif bool setup(CloudConfig& config, MeterConfig& meter, SystemConfig& system, NtpConfig& ntp, HwTools* hw, ResetDataContainer* rdc); void setMqttHandler(AmsMqttHandler* mqttHandler); void update(AmsData& data, EnergyAccounting& ea); @@ -62,7 +68,11 @@ public: String generateSeed(); private: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger = NULL; + #else + Stream* debugger = NULL; + #endif HwTools* hw = NULL; ConnectionHandler* ch = NULL; ResetDataContainer* rdc = NULL; diff --git a/lib/CloudConnector/src/CloudConnector.cpp b/lib/CloudConnector/src/CloudConnector.cpp index 80757b40..585d2ac4 100644 --- a/lib/CloudConnector/src/CloudConnector.cpp +++ b/lib/CloudConnector/src/CloudConnector.cpp @@ -23,7 +23,11 @@ #include "esp32s3/rom/rtc.h" #endif +#if defined(AMS_REMOTE_DEBUG) CloudConnector::CloudConnector(RemoteDebug* debugger) { +#else +CloudConnector::CloudConnector(Stream* debugger) { +#endif this->debugger = debugger; http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); @@ -85,7 +89,10 @@ bool CloudConnector::init() { strcpy_P(config.hostname, PSTR("cloud.amsleser.no")); 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) WiFiClient client; client.setTimeout(5000); @@ -121,18 +128,36 @@ bool CloudConnector::init() { &entropy, (const unsigned char *) pers, strlen(pers)); 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; } 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); - 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 { - if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(CloudConnector) Communication error, returned status: %d\n"), status); - if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http.errorToString(status).c_str()); - if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http.getString().c_str()); + #if defined(AMS_REMOTE_DEBUG) +if (debugger->isActive(RemoteDebug::ERROR)) +#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(); } @@ -146,13 +171,19 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) { unsigned long now = millis(); if(now-lastUpdate < config.interval*1000) return; 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; } if(data.getListType() < 2) return; 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; } initialized = true; @@ -372,9 +403,15 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) { if(rsa == nullptr) return; int ret = mbedtls_rsa_check_pubkey(rsa); 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); - 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; } memset(encryptedBuffer, 0, rsa->len); @@ -388,9 +425,15 @@ void CloudConnector::update(AmsData& data, EnergyAccounting& ea) { udp.write(encryptedBuffer, rsa->len); delay(1); } 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); - 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(); diff --git a/lib/ConnectionHandler/include/EthernetConnectionHandler.h b/lib/ConnectionHandler/include/EthernetConnectionHandler.h index 0ccb09c9..fe20a1ad 100644 --- a/lib/ConnectionHandler/include/EthernetConnectionHandler.h +++ b/lib/ConnectionHandler/include/EthernetConnectionHandler.h @@ -9,13 +9,19 @@ #include "ConnectionHandler.h" #include +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #define CONNECTION_TIMEOUT 30000 class EthernetConnectionHandler : public ConnectionHandler { public: + #if defined(AMS_REMOTE_DEBUG) EthernetConnectionHandler(RemoteDebug* debugger); + #else + EthernetConnectionHandler(Stream* debugger); + #endif bool connect(NetworkConfig config, SystemConfig sys); void disconnect(unsigned long reconnectDelay); @@ -33,7 +39,11 @@ public: #endif private: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger; + #else + Stream* debugger; + #endif NetworkConfig config; bool connected = false; diff --git a/lib/ConnectionHandler/include/WiFiAccessPointConnectionHandler.h b/lib/ConnectionHandler/include/WiFiAccessPointConnectionHandler.h index 8f366fff..d9acade5 100644 --- a/lib/ConnectionHandler/include/WiFiAccessPointConnectionHandler.h +++ b/lib/ConnectionHandler/include/WiFiAccessPointConnectionHandler.h @@ -9,12 +9,18 @@ #include "ConnectionHandler.h" #include +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include class WiFiAccessPointConnectionHandler : public ConnectionHandler { public: + #if defined(AMS_REMOTE_DEBUG) WiFiAccessPointConnectionHandler(RemoteDebug* debugger); + #else + WiFiAccessPointConnectionHandler(Stream* debugger); + #endif bool connect(NetworkConfig config, SystemConfig sys); void disconnect(unsigned long reconnectDelay); @@ -32,7 +38,11 @@ public: #endif private: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger; + #else + Stream* debugger; + #endif NetworkConfig config; DNSServer dnsServer; diff --git a/lib/ConnectionHandler/include/WiFiClientConnectionHandler.h b/lib/ConnectionHandler/include/WiFiClientConnectionHandler.h index 4b9b51f9..11e556ce 100644 --- a/lib/ConnectionHandler/include/WiFiClientConnectionHandler.h +++ b/lib/ConnectionHandler/include/WiFiClientConnectionHandler.h @@ -9,7 +9,9 @@ #include "ConnectionHandler.h" #include +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #define CONNECTION_TIMEOUT 30000 #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 { public: + #if defined(AMS_REMOTE_DEBUG) WiFiClientConnectionHandler(RemoteDebug* debugger); + #else + WiFiClientConnectionHandler(Stream* debugger); + #endif bool connect(NetworkConfig config, SystemConfig sys); void disconnect(unsigned long reconnectDelay); @@ -38,7 +44,11 @@ public: #endif private: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger; + #else + Stream* debugger; + #endif NetworkConfig config; bool busPowered = false; bool firstConnect = true; diff --git a/lib/ConnectionHandler/src/EthernetConnectionHandler.cpp b/lib/ConnectionHandler/src/EthernetConnectionHandler.cpp index 6c6816da..a2e62a53 100644 --- a/lib/ConnectionHandler/src/EthernetConnectionHandler.cpp +++ b/lib/ConnectionHandler/src/EthernetConnectionHandler.cpp @@ -12,7 +12,11 @@ #include #endif +#if defined(AMS_REMOTE_DEBUG) EthernetConnectionHandler::EthernetConnectionHandler(RemoteDebug* debugger) { +#else +EthernetConnectionHandler::EthernetConnectionHandler(Stream* debugger) { +#endif this->debugger = debugger; this->mode = NETWORK_MODE_ETH_CLIENT; } @@ -59,7 +63,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys) } else if(sys.boardType == 244) { return false; // TODO } 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; } @@ -68,7 +75,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys) 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 defined(ESP32) @@ -97,7 +107,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys) } } } 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 @@ -105,7 +118,10 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys) } 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() { @@ -117,12 +133,18 @@ void EthernetConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t switch(event) { case ARDUINO_EVENT_ETH_CONNECTED: 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")); } break; 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("GW: %s\n"), getGateway().toString().c_str()); for(uint8_t i = 0; i < 3; i++) { @@ -132,7 +154,10 @@ void EthernetConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t } break; 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(); if(ipv6 == IPv6Address()) { // No IP @@ -153,7 +178,10 @@ void EthernetConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_t } case ARDUINO_EVENT_ETH_DISCONNECTED: 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")); } break; diff --git a/lib/ConnectionHandler/src/WiFiAccessPointConnectionHandler.cpp b/lib/ConnectionHandler/src/WiFiAccessPointConnectionHandler.cpp index 7b1809a3..6b0ed557 100644 --- a/lib/ConnectionHandler/src/WiFiAccessPointConnectionHandler.cpp +++ b/lib/ConnectionHandler/src/WiFiAccessPointConnectionHandler.cpp @@ -6,7 +6,11 @@ #include "WiFiAccessPointConnectionHandler.h" +#if defined(AMS_REMOTE_DEBUG) WiFiAccessPointConnectionHandler::WiFiAccessPointConnectionHandler(RemoteDebug* debugger) { +#else +WiFiAccessPointConnectionHandler::WiFiAccessPointConnectionHandler(Stream* debugger) { +#endif this->debugger = debugger; this->mode = NETWORK_MODE_WIFI_AP; } @@ -42,22 +46,37 @@ void WiFiAccessPointConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEvent IPAddress stationIP; switch(event) { 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; 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; case ARDUINO_EVENT_WIFI_AP_STACONNECTED: 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; case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: 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; case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: 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; } } diff --git a/lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp b/lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp index 4adcf6e1..5714b0eb 100644 --- a/lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp +++ b/lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp @@ -10,7 +10,11 @@ #include #endif +#if defined(AMS_REMOTE_DEBUG) WiFiClientConnectionHandler::WiFiClientConnectionHandler(RemoteDebug* debugger) { +#else +WiFiClientConnectionHandler::WiFiClientConnectionHandler(Stream* debugger) { +#endif this->debugger = debugger; this->mode = NETWORK_MODE_WIFI_CLIENT; } @@ -28,14 +32,20 @@ bool WiFiClientConnectionHandler::connect(NetworkConfig config, SystemConfig sys } 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); return false; } 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) { case 2: // spenceme case 3: // Pow-K UART0 @@ -111,7 +121,10 @@ bool WiFiClientConnectionHandler::connect(NetworkConfig config, SystemConfig sys } yield(); } 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; } @@ -208,7 +221,10 @@ void WiFiClientConnectionHandler::wifi_sta_config(wifi_config_t * wifi_config, c #endif 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) WiFiClient::stopAll(); #endif @@ -235,7 +251,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_ } break; 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()) { 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; if(esp_wifi_sta_get_negotiated_phymode(&phyMode) == ESP_OK) { 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; configChanged = true; return; @@ -284,7 +306,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_ #endif break; 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("GW: %s\n"), getGateway().toString().c_str()); for(uint8_t i = 0; i < 3; i++) { @@ -295,7 +320,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_ break; } 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(); if(ipv6 == IPv6Address()) { // No IP @@ -322,7 +350,10 @@ void WiFiClientConnectionHandler::eventHandler(WiFiEvent_t event, WiFiEventInfo_ break; default: 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); } disconnect(RECONNECT_TIMEOUT); diff --git a/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h b/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h index ec3f141d..d2d3613c 100644 --- a/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h +++ b/lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h @@ -12,9 +12,15 @@ class DomoticzMqttHandler : public AmsMqttHandler { public: + #if defined(AMS_REMOTE_DEBUG) DomoticzMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, DomoticzConfig config) : AmsMqttHandler(mqttConfig, debugger, buf) { 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 publishTemperatures(AmsConfiguration*, HwTools*); bool publishPrices(PriceService*); diff --git a/lib/EnergyAccounting/include/EnergyAccounting.h b/lib/EnergyAccounting/include/EnergyAccounting.h index bd4d68be..02aec2fd 100644 --- a/lib/EnergyAccounting/include/EnergyAccounting.h +++ b/lib/EnergyAccounting/include/EnergyAccounting.h @@ -80,7 +80,11 @@ struct EnergyAccountingRealtimeData { class EnergyAccounting { public: + #if defined(AMS_REMOTE_DEBUG) EnergyAccounting(RemoteDebug*, EnergyAccountingRealtimeData*); + #else + EnergyAccounting(Stream*, EnergyAccountingRealtimeData*); + #endif void setup(AmsDataStorage *ds, EnergyAccountingConfig *config); void setPriceService(PriceService *ps); void setTimezone(Timezone*); @@ -123,7 +127,11 @@ public: float getPriceForHour(uint8_t d, uint8_t h); private: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger = NULL; + #else + Stream* debugger = NULL; + #endif bool init = false, initPrice = false; AmsDataStorage *ds = NULL; PriceService *ps = NULL; diff --git a/lib/EnergyAccounting/src/EnergyAccounting.cpp b/lib/EnergyAccounting/src/EnergyAccounting.cpp index 8b1b1ef2..54fd83db 100644 --- a/lib/EnergyAccounting/src/EnergyAccounting.cpp +++ b/lib/EnergyAccounting/src/EnergyAccounting.cpp @@ -9,7 +9,11 @@ #include "AmsStorage.h" #include "FirmwareVersion.h" +#if defined(AMS_REMOTE_DEBUG) EnergyAccounting::EnergyAccounting(RemoteDebug* debugger, EnergyAccountingRealtimeData* rtd) { +#else +EnergyAccounting::EnergyAccounting(Stream* Stream, EnergyAccountingRealtimeData* rtd) { +#endif data.version = 1; this->debugger = debugger; if(rtd->magic != 0x6A) { diff --git a/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h b/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h index 800f4d2b..1ac35213 100644 --- a/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h +++ b/lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h @@ -14,7 +14,11 @@ class HomeAssistantMqttHandler : public AmsMqttHandler { public: + #if defined(AMS_REMOTE_DEBUG) 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; l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false; diff --git a/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp b/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp index 5e6b81e3..225efeca 100644 --- a/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp +++ b/lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp @@ -617,7 +617,10 @@ bool HomeAssistantMqttHandler::publishRaw(String data) { void HomeAssistantMqttHandler::onMessage(String &topic, String &payload) { if(topic.equals(statusTopic)) { 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; for(uint8_t i = 0; i < 32; i++) tInit[i] = false; for(uint8_t i = 0; i < 38; i++) prInit[i] = false; diff --git a/lib/JsonMqttHandler/include/JsonMqttHandler.h b/lib/JsonMqttHandler/include/JsonMqttHandler.h index 3e9d73bb..2beb1585 100644 --- a/lib/JsonMqttHandler/include/JsonMqttHandler.h +++ b/lib/JsonMqttHandler/include/JsonMqttHandler.h @@ -11,9 +11,15 @@ class JsonMqttHandler : public AmsMqttHandler { public: + #if defined(AMS_REMOTE_DEBUG) JsonMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf, HwTools* hw) : AmsMqttHandler(mqttConfig, debugger, buf) { 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 publishTemperatures(AmsConfiguration*, HwTools*); bool publishPrices(PriceService*); diff --git a/lib/PriceService/include/PriceService.h b/lib/PriceService/include/PriceService.h index 043a6e3c..b0a619a2 100644 --- a/lib/PriceService/include/PriceService.h +++ b/lib/PriceService/include/PriceService.h @@ -11,7 +11,9 @@ #include "TimeLib.h" #include "Timezone.h" +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "AmsConfiguration.h" #include "EntsoeA44Parser.h" @@ -63,7 +65,11 @@ struct PricePart { class PriceService { public: + #if defined(AMS_REMOTE_DEBUG) PriceService(RemoteDebug*); + #else + PriceService(Stream*); + #endif void setup(PriceServiceConfig&); bool loop(); @@ -88,7 +94,11 @@ public: bool save(); private: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger; + #else + Stream* debugger; + #endif PriceServiceConfig* config = NULL; HTTPClient* http = NULL; diff --git a/lib/PriceService/src/PriceService.cpp b/lib/PriceService/src/PriceService.cpp index 84e830cb..5a8893b7 100644 --- a/lib/PriceService/src/PriceService.cpp +++ b/lib/PriceService/src/PriceService.cpp @@ -20,7 +20,11 @@ #include #endif +#if defined(AMS_REMOTE_DEBUG) PriceService::PriceService(RemoteDebug* Debug) : priceConfig(std::vector()) { +#else +PriceService::PriceService(Stream* Debug) : priceConfig(std::vector()) { +#endif this->buf = (char*) malloc(BufferSize); debugger = Debug; @@ -323,9 +327,18 @@ bool PriceService::retrieve(const char* url, Stream* doc) { } else { nextFetchDelayMinutes = 2; } - if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Communication error, returned status: %d\n"), status); - if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http->errorToString(status).c_str()); - if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str()); + #if defined(AMS_REMOTE_DEBUG) +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()); + #if defined(AMS_REMOTE_DEBUG) +if (debugger->isActive(RemoteDebug::DEBUG)) +#endif +debugger->printf(http->getString().c_str()); http->end(); return false; @@ -371,13 +384,19 @@ float PriceService::getCurrencyMultiplier(const char* from, const char* to, time } } 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; breakTime(t, tm); lastCurrencyFetch = now + (SECS_PER_DAY * 1000) - (((((tm.Hour * 60) + tm.Minute) * 60) + tm.Second) * 1000) + (3600000 * 6) + (tomorrowFetchMinute * 60); this->currencyMultiplier = currencyMultiplier; } 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); if(this->currencyMultiplier == 1) return 0; } @@ -407,8 +426,14 @@ PricesContainer* PriceService::fetchPrices(time_t t) { ESP.wdtFeed(); #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(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("(PriceService) url: %s\n"), buf); + #if defined(AMS_REMOTE_DEBUG) +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; if(retrieve(buf, &a44) && a44.getPoint(0) != PRICE_NO_VALUE) { PricesContainer* ret = new PricesContainer(); @@ -429,8 +454,14 @@ PricesContainer* PriceService::fetchPrices(time_t t) { tm.Day, 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(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("(PriceService) url: %s\n"), buf); + #if defined(AMS_REMOTE_DEBUG) +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) WiFiClient client; client.setTimeout(5000); @@ -471,7 +502,10 @@ PricesContainer* PriceService::fetchPrices(time_t t) { } else { lastError = gcmRet; 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 { lastError = status; @@ -482,12 +516,21 @@ PricesContainer* PriceService::fetchPrices(time_t t) { } else { nextFetchDelayMinutes = 5; } - if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Communication error, returned status: %d\n"), status); - if(debugger->isActive(RemoteDebug::ERROR)) { + #if defined(AMS_REMOTE_DEBUG) +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->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(); } @@ -539,11 +582,17 @@ void PriceService::cropPriceConfig(uint8_t size) { bool PriceService::save() { 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; } - 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; File file = LittleFS.open(FILE_PRICE_CONF, "w"); @@ -565,13 +614,19 @@ bool PriceService::save() { bool PriceService::load() { 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; } if(!LittleFS.exists(FILE_PRICE_CONF)) { 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(); diff --git a/lib/RawMqttHandler/include/RawMqttHandler.h b/lib/RawMqttHandler/include/RawMqttHandler.h index 4425525d..b4c590c7 100644 --- a/lib/RawMqttHandler/include/RawMqttHandler.h +++ b/lib/RawMqttHandler/include/RawMqttHandler.h @@ -11,10 +11,17 @@ class RawMqttHandler : public AmsMqttHandler { public: + #if defined(AMS_REMOTE_DEBUG) RawMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) { full = mqttConfig.payloadFormat == 2; 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 publishTemperatures(AmsConfiguration*, HwTools*); bool publishPrices(PriceService*); diff --git a/lib/SvelteUi/include/AmsWebServer.h b/lib/SvelteUi/include/AmsWebServer.h index 2f7e8dc5..09a55e69 100644 --- a/lib/SvelteUi/include/AmsWebServer.h +++ b/lib/SvelteUi/include/AmsWebServer.h @@ -16,7 +16,9 @@ #include "AmsDataStorage.h" #include "EnergyAccounting.h" #include "Uptime.h" +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "PriceService.h" #include "RealtimePlot.h" #include "ConnectionHandler.h" @@ -46,7 +48,11 @@ class AmsWebServer { public: + #if defined(AMS_REMOTE_DEBUG) 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 loop(); #if defined(_CLOUDCONNECTOR_H) @@ -61,7 +67,11 @@ public: void setConnectionHandler(ConnectionHandler* ch); private: - RemoteDebug* debugger; + #if defined(AMS_REMOTE_DEBUG) + RemoteDebug* debugger; + #else + Stream* debugger; + #endif ResetDataContainer* rdc; bool mqttEnabled = false; int maxPwr = 0; diff --git a/lib/SvelteUi/src/AmsWebServer.cpp b/lib/SvelteUi/src/AmsWebServer.cpp index 2480507f..adc9dc5a 100644 --- a/lib/SvelteUi/src/AmsWebServer.cpp +++ b/lib/SvelteUi/src/AmsWebServer.cpp @@ -51,7 +51,11 @@ #include "esp32s3/rom/rtc.h" #endif +#if defined(AMS_REMOTE_DEBUG) 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->hw = hw; this->buf = (char*) buf; @@ -83,7 +87,10 @@ void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, AmsDa if(context.length() == 1) { context = ""; } 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); 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() { - 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_PRAGMA, PRAGMA_NO_CACHE); @@ -453,7 +466,10 @@ void AmsWebServer::sysinfoJson() { if(ds != NULL) { 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(); delay(1000); rdc->cause = 1; @@ -1377,11 +1393,15 @@ void AmsWebServer::handleSave() { if(!pass.equals("***")) { strcpy(webConfig.password, pass.c_str()); } + #if defined(AMS_REMOTE_DEBUG) debugger->setPassword(webConfig.password); + #endif } else { memset(webConfig.username, 0, 37); memset(webConfig.password, 0, 37); + #if defined(AMS_REMOTE_DEBUG) debugger->setPassword(F("")); + #endif } strcpy(webConfig.context, server.arg(F("gc")).c_str()); config->setWebConfig(webConfig); @@ -1444,6 +1464,7 @@ void AmsWebServer::handleSave() { debug.serial = server.hasArg(F("ds")) && server.arg(F("ds")) == F("true"); debug.level = server.arg(F("dl")).toInt(); + #if defined(AMS_REMOTE_DEBUG) if(debug.telnet || debug.serial) { if(webConfig.security > 0) { debugger->setPassword(webConfig.password); @@ -1461,6 +1482,7 @@ void AmsWebServer::handleSave() { } else if(active) { performRestart = true; } + #endif config->setDebugConfig(debug); } @@ -1580,14 +1602,23 @@ void AmsWebServer::handleSave() { ps->cropPriceConfig(count); ps->save(); } 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(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) { performRestart = true; } else { @@ -1612,7 +1643,10 @@ void AmsWebServer::handleSave() { if(ds != NULL) { 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(); delay(1000); rdc->cause = 2; @@ -1630,7 +1664,10 @@ void AmsWebServer::reboot() { server.handleClient(); 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(); delay(1000); rdc->cause = 3; @@ -1784,7 +1821,10 @@ void AmsWebServer::firmwareUpload() { if(upload.status == UPLOAD_FILE_START) { String filename = upload.filename; 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; } if(!filename.endsWith(F(".bin"))) { @@ -1810,10 +1850,16 @@ HTTPUpload& AmsWebServer::uploadFile(const char* path) { HTTPUpload& upload = server.upload(); if(upload.status == UPLOAD_FILE_START) { 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("

Upload already in progress!

")); } 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("

Unable to mount LittleFS!

")); } else { uploading = true; @@ -1832,7 +1878,10 @@ HTTPUpload& AmsWebServer::uploadFile(const char* path) { file.close(); 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, "false", "File size does not match", @@ -1888,7 +1937,10 @@ void AmsWebServer::factoryResetPost() { server.handleClient(); 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(); delay(1000); rdc->cause = 5; @@ -2477,7 +2529,10 @@ void AmsWebServer::configFileUpload() { if(ds != NULL) { 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(); delay(1000); rdc->cause = 6; diff --git a/platformio.ini b/platformio.ini index ed19f7d0..49f6bc43 100755 --- a/platformio.ini +++ b/platformio.ini @@ -11,6 +11,7 @@ extra_scripts = lib/SvelteUi/scripts/generate_includes.py build_flags = -D WEBSOCKET_DISABLED=1 + -D AMS_REMOTE_DEBUG=1 -D NO_AMS2MQTT_PRICE_KEY -D NO_AMS2MQTT_PRICE_AUTHENTICATION -D NO_AMS2MQTT_SC_KEY diff --git a/src/AmsToMqttBridge.cpp b/src/AmsToMqttBridge.cpp index b1f7ea8c..6418445d 100644 --- a/src/AmsToMqttBridge.cpp +++ b/src/AmsToMqttBridge.cpp @@ -87,6 +87,7 @@ ADC_MODE(ADC_VCC); #include "Uptime.h" +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" #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 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();} +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) @@ -108,8 +119,6 @@ DNSServer* dnsServer = NULL; AmsConfiguration config; -RemoteDebug Debug; - PriceService* ps = NULL; Timezone* tz = NULL; @@ -381,20 +390,20 @@ void setup() { Serial.begin(115200); } + #if defined(AMS_REMOTE_DEBUG) Debug.setSerialEnabled(true); yield(); + #endif float vcc = hw.getVcc(); - if (Debug.isActive(RemoteDebug::INFO)) { - debugI_P(PSTR("AMS bridge started")); - debugI_P(PSTR("Voltage: %.2fV"), vcc); - } + debugI_P(PSTR("AMS bridge started")); + 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 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(Debug.isActive(RemoteDebug::INFO)) { + { Debug.printf_P(PSTR("(setup) Voltage is too low (%.2f < %.2f), sleeping\n"), vcc, vccBootLimit); Serial.flush(); } @@ -436,14 +445,14 @@ void setup() { if (!config.hasConfig()) { debugI_P(PSTR("Device has no config, yet a firmware file exists, deleting file.")); } 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) WiFi.setSleepMode(WIFI_LIGHT_SLEEP); WiFi.forceSleepBegin(); #endif int i = 0; 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) delay(10000); #elif defined(ESP32) @@ -459,10 +468,8 @@ void setup() { uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; debugD_P(PSTR(" available: %d"), maxSketchSpace); if (!Update.begin(maxSketchSpace, U_FLASH)) { - if(Debug.isActive(RemoteDebug::ERROR)) { - debugE_P(PSTR("Unable to start firmware update")); - Update.printError(Serial); - } + debugE_P(PSTR("Unable to start firmware update")); + Update.printError(Serial); } else { while (firmwareFile.available()) { uint8_t ibuffer[128]; @@ -478,15 +485,13 @@ void setup() { } LittleFS.remove(FILE_FIRMWARE); } else if(LittleFS.exists(FILE_CFG)) { - if(Debug.isActive(RemoteDebug::INFO)) debugI_P(PSTR("Found config")); + debugI_P(PSTR("Found config")); configFileParse(); flashed = true; } if(flashed) { - if(Debug.isActive(RemoteDebug::INFO)) { - debugI_P(PSTR("Firmware update complete, restarting")); - Debug.flush(); - } + debugI_P(PSTR("Firmware update complete, restarting")); + Debug.flush(); delay(250); ESP.restart(); return; @@ -495,14 +500,12 @@ void setup() { yield(); if(config.hasConfig()) { - if(Debug.isActive(RemoteDebug::INFO)) config.print(&Debug); + config.print(&Debug); connectToNetwork(); handleNtpChange(); ds.load(); } else { - if(Debug.isActive(RemoteDebug::INFO)) { - debugI_P(PSTR("No configuration, booting AP")); - } + debugI_P(PSTR("No configuration, booting AP")); toggleSetupMode(); } @@ -554,7 +557,9 @@ int lastError = 0; void loop() { unsigned long now = millis(); unsigned long start = now; + #if defined(AMS_REMOTE_DEBUG) Debug.handle(); + #endif unsigned long end = millis(); if(end - start > 1000) { debugW_P(PSTR("Used %dms to handle debug"), millis()-start); @@ -570,7 +575,10 @@ void loop() { if (!setupMode) { if (ch != NULL && !ch->isConnected()) { if(networkConnected) { + #if defined(AMS_REMOTE_DEBUG) Debug.stop(); + #endif + MDNS.end(); if(mqttHandler != NULL) { mqttHandler->disconnect(); @@ -1200,7 +1208,7 @@ void toggleSetupMode() { yield(); 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 @@ -1238,7 +1246,7 @@ void toggleSetupMode() { digitalWrite(gpioConfig.ledDisablePin, LOW); } } else { - if(Debug.isActive(RemoteDebug::INFO)) debugI_P(PSTR("Exiting setup mode")); + debugI_P(PSTR("Exiting setup mode")); if(dnsServer != NULL) { delete dnsServer; dnsServer = NULL; @@ -1364,6 +1372,7 @@ void postConnect() { config.setNetworkConfig(network); } WebConfig web; + #if defined(AMS_REMOTE_DEBUG) if(config.getWebConfig(web) && web.security > 0) { Debug.setPassword(web.password); } @@ -1376,6 +1385,7 @@ void postConnect() { } else { Debug.stop(); } + #endif mdnsEnabled = false; if(strlen(network.hostname) > 0 && network.mdns) { debugD_P(PSTR("mDNS is enabled, using host: %s"), network.hostname); @@ -1434,7 +1444,7 @@ void MQTT_connect() { MqttConfig mqttConfig; 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); mqttEnabled = false; return; diff --git a/src/KmpCommunicator.h b/src/KmpCommunicator.h index 80ed62ee..f0952856 100644 --- a/src/KmpCommunicator.h +++ b/src/KmpCommunicator.h @@ -7,7 +7,9 @@ #pragma once #include "PassiveMeterCommunicator.h" +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "AmsConfiguration.h" #include "Timezone.h" #include "ImpulseAmsData.h" @@ -18,7 +20,11 @@ class KmpCommunicator : public PassiveMeterCommunicator { public: + #if defined(AMS_REMOTE_DEBUG) KmpCommunicator(RemoteDebug* debugger) : PassiveMeterCommunicator(debugger) {}; + #else + KmpCommunicator(Stream* debugger) : PassiveMeterCommunicator(debugger) {}; + #endif void configure(MeterConfig&, Timezone*); bool loop(); AmsData* getData(AmsData& meterState); diff --git a/src/LNG.cpp b/src/LNG.cpp index bd400e4c..9ba42d99 100644 --- a/src/LNG.cpp +++ b/src/LNG.cpp @@ -9,7 +9,7 @@ #include "ntohll.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; if(h->tag == CosemTypeStructure && h->arrayTag == CosemTypeArray) { apply(meterState); diff --git a/src/LNG.h b/src/LNG.h index d84e00ec..3805638c 100644 --- a/src/LNG.h +++ b/src/LNG.h @@ -11,7 +11,6 @@ #include "AmsConfiguration.h" #include "DataParser.h" #include "Cosem.h" -#include "RemoteDebug.h" struct LngHeader { uint8_t tag; @@ -31,7 +30,7 @@ struct LngObisDescriptor { class LNG : public AmsData { 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); }; diff --git a/src/LNG2.cpp b/src/LNG2.cpp index b0202b49..6b87a4b2 100644 --- a/src/LNG2.cpp +++ b/src/LNG2.cpp @@ -7,7 +7,7 @@ #include "LNG2.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; if(h->length == 0x0e) { apply(meterState); diff --git a/src/LNG2.h b/src/LNG2.h index 7ef3eeef..752a88d2 100644 --- a/src/LNG2.h +++ b/src/LNG2.h @@ -11,7 +11,6 @@ #include "AmsConfiguration.h" #include "DataParser.h" #include "Cosem.h" -#include "RemoteDebug.h" struct Lng2Data_3p { CosemBasic header; @@ -33,7 +32,7 @@ struct Lng2Data_3p { class LNG2 : public AmsData { 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: uint8_t getString(CosemData* item, char* target); diff --git a/src/MeterCommunicator.h b/src/MeterCommunicator.h index 73c39197..73e99e76 100644 --- a/src/MeterCommunicator.h +++ b/src/MeterCommunicator.h @@ -8,7 +8,9 @@ #define _METERCOMMUNICATOR_H #include +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "AmsData.h" #include "AmsConfiguration.h" diff --git a/src/PassiveMeterCommunicator.cpp b/src/PassiveMeterCommunicator.cpp index f6c30a21..e47d7a95 100644 --- a/src/PassiveMeterCommunicator.cpp +++ b/src/PassiveMeterCommunicator.cpp @@ -14,9 +14,15 @@ #include #endif +#if defined(AMS_REMOTE_DEBUG) PassiveMeterCommunicator::PassiveMeterCommunicator(RemoteDebug* debugger) { this->debugger = debugger; } +#else +PassiveMeterCommunicator::PassiveMeterCommunicator(Stream* debugger) { + this->debugger = debugger; +} +#endif void PassiveMeterCommunicator::configure(MeterConfig& meterConfig, Timezone* tz) { this->meterConfig = meterConfig; @@ -61,7 +67,10 @@ bool PassiveMeterCommunicator::loop() { if(len >= hanBufferSize) { hanSerial->readBytes(hanBuffer, hanBufferSize); 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; } hanBuffer[len++] = hanSerial->read(); @@ -70,26 +79,47 @@ bool PassiveMeterCommunicator::loop() { if(ctx.type > 0 && pos >= 0) { switch(ctx.type) { 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; 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; 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; 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; 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; 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; default: // 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; return false; } @@ -98,16 +128,25 @@ bool PassiveMeterCommunicator::loop() { } end = millis(); 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) { return false; } 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; 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")); debugPrint(hanBuffer, 0, len); } @@ -124,7 +163,10 @@ bool PassiveMeterCommunicator::loop() { if(pt != NULL) { 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")); debugPrint(hanBuffer, 0, len); } @@ -134,10 +176,16 @@ bool PassiveMeterCommunicator::loop() { } 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; 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")); debugPrint(hanBuffer, 0, len); } @@ -171,13 +219,22 @@ AmsData* PassiveMeterCommunicator::getData(AmsData& meterState) { pt->publishBytes((uint8_t*) payload, ctx.length); } - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("Using application data:\n")); - if(debugger->isActive(RemoteDebug::VERBOSE)) debugPrint((byte*) payload, 0, ctx.length); + #if defined(AMS_REMOTE_DEBUG) +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 if(payload[0] == CosemTypeStructure && payload[2] == CosemTypeArray && payload[1] == payload[3]) { - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("LNG\n")); - LNG lngData = LNG(meterState, payload, meterState.getMeterType(), &meterConfig, ctx, debugger); + #if defined(AMS_REMOTE_DEBUG) +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) { data = new AmsData(); data->apply(meterState); @@ -191,15 +248,21 @@ AmsData* PassiveMeterCommunicator::getData(AmsData& meterState) { payload[14] == CosemTypeLongUnsigned && payload[17] == CosemTypeLongUnsigned ) { - if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("LNG2\n")); - LNG2 lngData = LNG2(meterState, payload, meterState.getMeterType(), &meterConfig, ctx, debugger); + #if defined(AMS_REMOTE_DEBUG) +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) { data = new AmsData(); data->apply(meterState); data->apply(lngData); } } 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 data = new IEC6205675(payload, meterState.getMeterType(), &meterConfig, ctx, meterState); } @@ -221,7 +284,10 @@ int PassiveMeterCommunicator::getLastError() { #if defined ESP8266 if(hwSerial != NULL) { 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; } if(hwSerial->hasOverrun()) { @@ -293,7 +359,10 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co doRet = true; break; 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; } lastTag = tag; @@ -301,53 +370,89 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co return res; } 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.length = 0; return false; } switch(tag) { 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) { pt->publishBytes(buf, curLen); } break; 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) { pt->publishBytes(buf, curLen); } break; 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; 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; 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; 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; 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) { pt->publishString((char*) buf); } break; 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; 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; 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; } - 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(tag == DATA_TAG_MBUS) { 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 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; } @@ -391,40 +499,76 @@ void PassiveMeterCommunicator::debugPrint(byte *buffer, int start, int length) { } void PassiveMeterCommunicator::printHanReadError(int pos) { - if(debugger->isActive(RemoteDebug::WARNING)) { + #if defined(AMS_REMOTE_DEBUG) +if (debugger->isActive(RemoteDebug::WARNING)) +#endif +{ switch(pos) { 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; 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; 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; 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; 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; 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; 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; 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; 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; 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; 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; } - 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) { parityOrdinal = 3; // 8N1 @@ -464,7 +611,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal #endif 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; } @@ -472,7 +622,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal if(meterConfig.bufferSize > 64) meterConfig.bufferSize = 64; 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(); #if defined(ESP8266) SerialConfig serialConfig; @@ -508,10 +661,16 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal #if defined(ESP8266) 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); } 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); } #endif @@ -543,7 +702,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal #endif } else { #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(); if(swSerial == NULL) { @@ -575,7 +737,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal #if defined(ESP8266) if(bufferSize > 2) bufferSize = 2; #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); hanSerial = swSerial; @@ -583,7 +748,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal Serial.begin(115200); hwSerial = NULL; #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; #endif } @@ -596,7 +764,10 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal // The library automatically sets the pullup in Serial.begin() 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); } @@ -628,23 +799,38 @@ void PassiveMeterCommunicator::rxerr(int err) { if(err == 0) return; switch(err) { 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++; if(rxBufferErrors > 1 && meterConfig.bufferSize < 8) { 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; rxBufferErrors = 0; } break; 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; 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; 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(); if(now - meterAutodetectLastChange < 120000) { switch(autodetectParity) { @@ -701,13 +887,23 @@ void PassiveMeterCommunicator::handleAutodetect(unsigned long now) { autodetectCount = 0; } 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); setupHanPort(autodetectBaud, autodetectParity, autodetectInvert); meterAutodetectLastChange = now; } } 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; meterConfig.baud = autodetectBaud; meterConfig.parity = autodetectParity; diff --git a/src/PassiveMeterCommunicator.h b/src/PassiveMeterCommunicator.h index 61dac8e7..964280f6 100644 --- a/src/PassiveMeterCommunicator.h +++ b/src/PassiveMeterCommunicator.h @@ -8,7 +8,9 @@ #define _PASSIVEMETERCOMMUNICATOR_H #include "MeterCommunicator.h" +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "AmsConfiguration.h" #include "DataParsers.h" #include "Timezone.h" @@ -22,7 +24,11 @@ const uint32_t AUTO_BAUD_RATES[] = { 2400, 115200 }; class PassiveMeterCommunicator : public MeterCommunicator { public: + #if defined(AMS_REMOTE_DEBUG) PassiveMeterCommunicator(RemoteDebug* debugger); + #else + PassiveMeterCommunicator(Stream* debugger); + #endif void configure(MeterConfig&, Timezone*); bool loop(); AmsData* getData(AmsData& meterState); @@ -35,7 +41,11 @@ public: void rxerr(int err); protected: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger = NULL; + #else + Stream* debugger = NULL; + #endif MeterConfig meterConfig; bool configChanged = false; Timezone* tz; diff --git a/src/PassthroughMqttHandler.h b/src/PassthroughMqttHandler.h index d30a53f5..421db913 100644 --- a/src/PassthroughMqttHandler.h +++ b/src/PassthroughMqttHandler.h @@ -11,9 +11,15 @@ class PassthroughMqttHandler : public AmsMqttHandler { public: + #if defined(AMS_REMOTE_DEBUG) PassthroughMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) : AmsMqttHandler(mqttConfig, debugger, buf) { 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 publishTemperatures(AmsConfiguration*, HwTools*); bool publishPrices(PriceService*); diff --git a/src/PulseMeterCommunicator.cpp b/src/PulseMeterCommunicator.cpp index 5225570d..161079a5 100644 --- a/src/PulseMeterCommunicator.cpp +++ b/src/PulseMeterCommunicator.cpp @@ -1,7 +1,11 @@ #include "PulseMeterCommunicator.h" #include "Uptime.h" +#if defined(AMS_REMOTE_DEBUG) PulseMeterCommunicator::PulseMeterCommunicator(RemoteDebug* debugger) { +#else +PulseMeterCommunicator::PulseMeterCommunicator(Stream* debugger) { +#endif this->debugger = debugger; } @@ -42,7 +46,10 @@ void PulseMeterCommunicator::getCurrentConfig(MeterConfig& meterConfig) { } 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) { pinMode(meterConfig.rxPin, meterConfig.rxPinPullup ? INPUT_PULLUP : INPUT); } diff --git a/src/PulseMeterCommunicator.h b/src/PulseMeterCommunicator.h index d95ff10c..aa1a797b 100644 --- a/src/PulseMeterCommunicator.h +++ b/src/PulseMeterCommunicator.h @@ -8,14 +8,20 @@ #define _PULSEMETERCOMMUNICATOR_H #include "MeterCommunicator.h" +#if defined(AMS_REMOTE_DEBUG) #include "RemoteDebug.h" +#endif #include "AmsConfiguration.h" #include "Timezone.h" #include "ImpulseAmsData.h" class PulseMeterCommunicator : public MeterCommunicator { public: + #if defined(AMS_REMOTE_DEBUG) PulseMeterCommunicator(RemoteDebug* debugger); + #else + PulseMeterCommunicator(Stream* debugger); + #endif void configure(MeterConfig& config, Timezone* tz); bool loop(); AmsData* getData(AmsData& meterState); @@ -26,7 +32,11 @@ public: void onPulse(uint8_t pulses); protected: + #if defined(AMS_REMOTE_DEBUG) RemoteDebug* debugger = NULL; + #else + Stream* debugger = NULL; + #endif MeterConfig meterConfig; bool configChanged = false; Timezone* tz;