diff --git a/lib/HanReader/src/DlmsReader.cpp b/lib/HanReader/src/DlmsReader.cpp index cce664b4..aaa44557 100644 --- a/lib/HanReader/src/DlmsReader.cpp +++ b/lib/HanReader/src/DlmsReader.cpp @@ -14,7 +14,7 @@ void DlmsReader::Clear() this->frameFormatType = 0; } -bool DlmsReader::Read(byte data) +bool DlmsReader::Read(byte data, Print* debugger) { if (position == 0 && data != 0x7E) { @@ -24,12 +24,21 @@ bool DlmsReader::Read(byte data) else { // We have completed reading of one package, so clear and be ready for the next - if (dataLength > 0 && position >= dataLength + 2) + if (dataLength > 0 && position >= dataLength + 2) { + if(debugger != NULL) { + debugger->printf("Preparing for next frame\n"); + } Clear(); + } // Check if we're about to run into a buffer overflow - if (position >= DLMS_READER_BUFFER_SIZE) + if (position >= DLMS_READER_BUFFER_SIZE) { + if(debugger != NULL) { + debugger->printf("Buffer overflow\n"); + debugPrint(buffer, 0, position, debugger); + } Clear(); + } // Check if this is a second start flag, which indicates the previous one was a stop from the last package if (position == 1 && data == 0x7E) @@ -50,8 +59,13 @@ bool DlmsReader::Read(byte data) { // Capture the Frame Format Type frameFormatType = (byte)(data & 0xF0); - if (!IsValidFrameFormat(frameFormatType)) + if (!IsValidFrameFormat(frameFormatType)) { + if(debugger != NULL) { + debugger->printf("Incorrect frame format %02X\n", frameFormatType); + debugPrint(buffer, 0, position, debugger); + } Clear(); + } return false; } else if (position == 3) @@ -64,32 +78,52 @@ bool DlmsReader::Read(byte data) { // Capture the destination address destinationAddressLength = GetAddress(3, destinationAddress, 0, DLMS_READER_MAX_ADDRESS_SIZE); - if (destinationAddressLength > 3) + if (destinationAddressLength > 3) { + if(debugger != NULL) { + debugger->printf("Destination address length incorrect\n"); + debugPrint(buffer, 0, position, debugger); + } Clear(); + } return false; } else if (sourceAddressLength == 0) { // Capture the source address sourceAddressLength = GetAddress(3 + destinationAddressLength, sourceAddress, 0, DLMS_READER_MAX_ADDRESS_SIZE); - if (sourceAddressLength > 3) + if (sourceAddressLength > 3) { + if(debugger != NULL) { + debugger->printf("Source address length incorrect\n"); + debugPrint(buffer, 0, position, debugger); + } Clear(); + } return false; } else if (position == 4 + destinationAddressLength + sourceAddressLength + 2) { // Verify the header checksum ushort headerChecksum = GetChecksum(position - 3); - if (headerChecksum != Crc16.ComputeChecksum(buffer, 1, position - 3)) + if (headerChecksum != Crc16.ComputeChecksum(buffer, 1, position - 3)) { + if(debugger != NULL) { + debugger->printf("Header checksum is incorrect %02X\n", headerChecksum); + debugPrint(buffer, 0, position, debugger); + } Clear(); + } return false; } else if (position == dataLength + 1) { // Verify the data package checksum ushort checksum = this->GetChecksum(position - 3); - if (checksum != Crc16.ComputeChecksum(buffer, 1, position - 3)) + if (checksum != Crc16.ComputeChecksum(buffer, 1, position - 3)) { + if(debugger != NULL) { + debugger->printf("Frame checksum is incorrect %02X\n", checksum); + debugPrint(buffer, 0, position, debugger); + } Clear(); + } return false; } else if (position == dataLength + 2) @@ -99,6 +133,10 @@ bool DlmsReader::Read(byte data) return true; else { + if(debugger != NULL) { + debugger->printf("Received incorrect end marker %02X\n", data); + debugPrint(buffer, 0, position, debugger); + } Clear(); return false; } @@ -152,3 +190,19 @@ ushort DlmsReader::GetChecksum(int checksumPosition) return (ushort)(buffer[checksumPosition + 2] << 8 | buffer[checksumPosition + 1]); } + +void DlmsReader::debugPrint(byte *buffer, int start, int length, Print* debugger) { + for (int i = start; i < start + length; i++) { + if (buffer[i] < 0x10) + debugger->print("0"); + debugger->print(buffer[i], HEX); + debugger->print(" "); + if ((i - start + 1) % 16 == 0) + debugger->println(""); + else if ((i - start + 1) % 4 == 0) + debugger->print(" "); + + yield(); // Let other get some resources too + } + debugger->println(""); +} diff --git a/lib/HanReader/src/DlmsReader.h b/lib/HanReader/src/DlmsReader.h index ffc8691b..d82eb4a1 100644 --- a/lib/HanReader/src/DlmsReader.h +++ b/lib/HanReader/src/DlmsReader.h @@ -16,7 +16,7 @@ class DlmsReader { public: DlmsReader(); - bool Read(byte data); + bool Read(byte data, Print* Debug); int GetRawData(byte *buffer, int start, int length); protected: @@ -37,6 +37,7 @@ class DlmsReader unsigned short GetChecksum(int checksumPosition); bool IsValidFrameFormat(byte frameFormatType); void WriteBuffer(); + void debugPrint(byte *buffer, int start, int length, Print* debugger); }; #endif diff --git a/lib/HanReader/src/HanReader.cpp b/lib/HanReader/src/HanReader.cpp index 8eaa1d32..4d18d501 100644 --- a/lib/HanReader/src/HanReader.cpp +++ b/lib/HanReader/src/HanReader.cpp @@ -31,7 +31,7 @@ void HanReader::setAuthenticationKey(uint8_t* authentication_key) { bool HanReader::read(byte data) { - if (reader.Read(data)) { + if (reader.Read(data, debugger->isActive(RemoteDebug::DEBUG) ? debugger : NULL)) { bytesRead = reader.GetRawData(buffer, 0, 512); if (debugger->isActive(RemoteDebug::INFO)) { printI("Got valid DLMS data (%d bytes)", bytesRead); diff --git a/src/AmsConfiguration.cpp b/src/AmsConfiguration.cpp index 4682ce98..508b6dcd 100644 --- a/src/AmsConfiguration.cpp +++ b/src/AmsConfiguration.cpp @@ -968,6 +968,7 @@ void AmsConfiguration::print(Print* debugger) debugger->println("-----------------------------------------------"); debugger->printf("WiFi SSID: '%s'\r\n", this->getWifiSsid()); debugger->printf("WiFi Psk: '%s'\r\n", this->getWifiPassword()); + delay(1); if(strlen(getWifiIp()) > 0) { debugger->printf("IP: '%s'\r\n", this->getWifiIp()); @@ -976,6 +977,7 @@ void AmsConfiguration::print(Print* debugger) debugger->printf("Primary DNS: '%s'\r\n", this->getWifiDns1()); debugger->printf("Secondary DNS: '%s'\r\n", this->getWifiDns2()); } + delay(1); debugger->printf("WiFi Host: '%s'\r\n", this->getWifiHostname()); @@ -992,6 +994,7 @@ void AmsConfiguration::print(Print* debugger) } debugger->printf("payload format: %i\r\n", this->getMqttPayloadFormat()); } + delay(1); if (this->getAuthSecurity()) { debugger->printf("WEB AUTH:\r\n"); @@ -1006,6 +1009,7 @@ void AmsConfiguration::print(Print* debugger) debugger->printf("productionCapacity: %i\r\n", this->getProductionCapacity()); debugger->printf("Substitute missing: %s\r\n", this->isSubstituteMissing() ? "Yes" : "No"); Serial.flush(); + delay(1); debugger->printf("HAN pin: %i\r\n", this->getHanPin()); debugger->printf("LED pin: %i\r\n", this->getLedPin()); @@ -1017,6 +1021,7 @@ void AmsConfiguration::print(Print* debugger) debugger->printf("AP pin: %i\r\n", this->getApPin()); debugger->printf("Temperature pin: %i\r\n", this->getTempSensorPin()); Serial.flush(); + delay(1); debugger->printf("Vcc pin: %i\r\n", this->getVccPin()); debugger->printf("Vcc multiplier: %f\r\n", this->getVccMultiplier()); @@ -1030,6 +1035,7 @@ void AmsConfiguration::print(Print* debugger) debugger->printf("Domoticz VL3IDX: %i\r\n", this->getDomoVL3IDX()); debugger->printf("Domoticz CL1IDX: %i\r\n", this->getDomoCL1IDX()); } + delay(1); debugger->printf("NTP: %s\r\n", this->isNtpEnable() ? "Yes" : "No"); if(this->isNtpEnable()) { @@ -1038,6 +1044,7 @@ void AmsConfiguration::print(Print* debugger) debugger->printf("NTP server: %s\r\n", this->getNtpServer()); debugger->printf("NTP DHCP: %s\r\n", this->isNtpDhcp() ? "Yes" : "No"); } + delay(1); debugger->printf("Temp sensor count: %i\r\n", this->getTempSensorCount()); diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 8e3ce27d..9588a49a 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -122,6 +122,7 @@ void setup() { config.setTempSensorPin(14); #endif } + delay(1); hw.setLed(config.getLedPin(), config.isLedInverted()); hw.setLedRgb(config.getLedPinRed(), config.getLedPinGreen(), config.getLedPinBlue(), config.isLedRgbInverted()); @@ -157,6 +158,13 @@ void setup() { Debug.setSerialEnabled(true); #endif } + delay(1); + + uint8_t c = config.getTempSensorCount(); + for(int i = 0; i < c; i++) { + TempSensorConfig* tsc = config.getTempSensorConfig(i); + hw.confTempSensor(tsc->address, tsc->name, tsc->common); + } double vcc = hw.getVcc(); @@ -188,6 +196,7 @@ void setup() { debugD("ESP8266 SPIFFS"); spiffs = SPIFFS.begin(); #endif + delay(1); if(spiffs) { bool flashed = false; @@ -244,6 +253,7 @@ void setup() { return; } } + delay(1); if(config.hasConfig()) { if(Debug.isActive(RemoteDebug::INFO)) config.print(&Debug); @@ -325,9 +335,10 @@ void loop() { hw.updateTemperatures(); lastTemperatureRead = now; + uint8_t c = hw.getTempSensorCount(); + if(strlen(config.getMqttHost()) > 0) { bool anyChanged = false; - uint8_t c = hw.getTempSensorCount(); for(int i = 0; i < c; i++) { bool changed = false; TempSensorData* data = hw.getTempSensorData(i); diff --git a/src/HwTools.cpp b/src/HwTools.cpp index cb07d4d6..6dabb67a 100644 --- a/src/HwTools.cpp +++ b/src/HwTools.cpp @@ -97,14 +97,12 @@ bool HwTools::updateTemperatures() { oneWire = new OneWire(tempSensorPin); sensorApi = new DallasTemperature(this->oneWire); sensorApi->begin(); - delay(50); + delay(100); tempSensorInit = true; DeviceAddress addr; sensorApi->requestTemperatures(); int c = sensorApi->getDeviceCount(); - //Serial.print("Sensors found: "); - //Serial.println(c); for(int i = 0; i < c; i++) { bool found = false; sensorApi->getAddress(addr, i); diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 4862cfd7..d067c87a 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -135,9 +135,10 @@ void AmsWebServer::temperature() { String html; int c = hw->getTempSensorCount(); + int num = 8; int start = server.hasArg("start") && !server.arg("start").isEmpty() ? server.arg("start").toInt() : 0; - int end = min(start + 4, c); + int end = min(start + num, c); for(int i = start; i < end; i++) { TempSensorData* data = hw->getTempSensorData(i); @@ -160,7 +161,7 @@ void AmsWebServer::temperature() { html += "