Skip to content

Commit

Permalink
Limit buffer size on ESP8266 to avoid reboots
Browse files Browse the repository at this point in the history
  • Loading branch information
gskjold committed Nov 8, 2023
1 parent 445973f commit f04e15f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/AmsConfiguration/src/AmsConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ void AmsConfiguration::print(Print* debugger)
debugger->printf_P(PSTR("Baud: %i\r\n"), meter.baud);
debugger->printf_P(PSTR("Parity: %i\r\n"), meter.parity);
debugger->printf_P(PSTR("Invert serial: %s\r\n"), meter.invert ? "Yes" : "No");
debugger->printf_P(PSTR("Buffer size: %i\r\n"), meter.bufferSize * 64);
debugger->printf_P(PSTR("Distribution system: %i\r\n"), meter.distributionSystem);
debugger->printf_P(PSTR("Main fuse: %i\r\n"), meter.mainFuse);
debugger->printf_P(PSTR("Production Capacity: %i\r\n"), meter.productionCapacity);
Expand Down
19 changes: 16 additions & 3 deletions src/AmsToMqttBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,19 @@ void handleSystem(unsigned long now) {
// After one hour, adjust buffer size to match the largest payload
if(!maxDetectPayloadDetectDone && now > 3600000) {
if(maxDetectedPayloadSize * 1.5 > meterConfig.bufferSize * 64) {
meterConfig.bufferSize = min((double) 64, ceil((maxDetectedPayloadSize * 1.5) / 64));
debugI_P(PSTR("Increasing RX buffer to %d bytes"), meterConfig.bufferSize * 64);
config.setMeterConfig(meterConfig);
int bufferSize = min((double) 64, ceil((maxDetectedPayloadSize * 1.5) / 64));
#if defined(ESP8266)
if(gpioConfig.hanPin != 3 && gpioConfig.hanPin != 113) {
bufferSize = min(bufferSize, 2);
} else {
bufferSize = min(bufferSize, 8);
}
#endif
if(bufferSize != meterConfig.bufferSize) {
debugI_P(PSTR("Increasing RX buffer to %d bytes"), bufferSize * 64);
meterConfig.bufferSize = bufferSize;
config.setMeterConfig(meterConfig);
}
}
maxDetectPayloadDetectDone = true;
}
Expand Down Expand Up @@ -1035,6 +1045,9 @@ void setupHanPort(GpioConfig& gpioConfig, uint32_t baud, uint8_t parityOrdinal,
break;
}

#if defined(ESP8266)
if(meterConfig.bufferSize > 2) meterConfig.bufferSize = 2;
#endif
swSerial->begin(baud, serialConfig, pin, -1, invert, meterConfig.bufferSize * 64);
hanSerial = swSerial;

Expand Down

0 comments on commit f04e15f

Please sign in to comment.