Some debugging changes and other optimizations

This commit is contained in:
Gunnar Skjold 2023-04-18 11:50:44 +02:00
parent 7bed5add5d
commit c4005f10a3
12 changed files with 169 additions and 87 deletions

View File

@ -437,8 +437,6 @@ bool AmsDataStorage::load() {
ret = ret && setMonthData(*month);
}
LittleFS.end();
return ret;
}
@ -451,22 +449,22 @@ bool AmsDataStorage::save() {
}
{
File file = LittleFS.open(FILE_DAYPLOT, "w");
char* data = (char*) &day;
for(unsigned long i = 0; i < sizeof(data); i++) {
file.write(data[i]);
char buf[sizeof(day)];
memcpy(buf, &day, sizeof(day));
for(unsigned long i = 0; i < sizeof(day); i++) {
file.write(buf[i]);
}
file.close();
}
{
File file = LittleFS.open(FILE_MONTHPLOT, "w");
char* data = (char*) &month;
for(unsigned long i = 0; i < sizeof(data); i++) {
file.write(data[i]);
char buf[sizeof(month)];
memcpy(buf, &month, sizeof(month));
for(unsigned long i = 0; i < sizeof(month); i++) {
file.write(buf[i]);
}
file.close();
}
LittleFS.end();
return true;
}

View File

@ -438,8 +438,6 @@ bool EnergyAccounting::load() {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("(EnergyAccounting) File not found\n"));
}
LittleFS.end();
return ret;
}
@ -459,8 +457,6 @@ bool EnergyAccounting::save() {
}
file.close();
}
LittleFS.end();
return true;
}

View File

@ -210,7 +210,7 @@ bool EntsoeApi::loop() {
bool EntsoeApi::retrieve(const char* url, Stream* doc) {
#if defined(ESP32)
if(http.begin(url)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Connection established"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Connection established\n"));
#if defined(ESP32)
esp_task_wdt_reset();
@ -227,7 +227,7 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
#endif
if(status == HTTP_CODE_OK) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data\n"));
http.writeToStream(doc);
http.end();
lastError = 0;
@ -357,13 +357,13 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {
#endif
if(status == HTTP_CODE_OK) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data\n"));
data = http.getString();
http.end();
uint8_t* content = (uint8_t*) (data.c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received content for prices:"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received content for prices:\n"));
debugPrint(content, 0, data.length());
}
@ -372,7 +372,7 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {
GCMParser gcm(key, auth);
int8_t gcmRet = gcm.parse(content, ctx);
if(debugger->isActive(RemoteDebug::DEBUG)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Decrypted content for prices:"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Decrypted content for prices:\n"));
debugPrint(content, 0, data.length());
}
if(gcmRet > 0) {

View File

@ -145,7 +145,6 @@ bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwT
);
data->changed = false;
publishTemperatureSensor(i+1, id);
delay(1);
}
}
char* pos = buf+strlen(buf);
@ -339,6 +338,8 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor& sensor)
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
yield();
}
void HomeAssistantMqttHandler::publishList1Sensors() {

View File

@ -318,7 +318,7 @@ bool HwTools::updateTemperatures() {
}
tempSensors[sensorCount++] = data;
}
delay(10);
yield();
}
} else {
if(sensorCount > 0) {

View File

@ -168,7 +168,6 @@ bool JsonMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw)
data->lastRead
);
data->changed = false;
delay(1);
}
}
char* pos = json+strlen(json);

File diff suppressed because one or more lines are too long

View File

@ -109,7 +109,7 @@
{/if}
</div>
<div class="my-2">
Reason: {getResetReason(sysinfo)} ({sysinfo.boot_reason})
Reason: {getResetReason(sysinfo)} ({sysinfo.boot_reason}/{sysinfo.ex_cause})
</div>
{/if}
<div class="my-2">

View File

@ -38,6 +38,7 @@
},
"security": %d,
"boot_reason": %d,
"ex_cause": %d,
"upgrade": {
"x": %d,
"e": %d,

View File

@ -315,8 +315,10 @@ void AmsWebServer::sysinfoJson() {
webConfig.security,
#if defined(ESP32)
rtc_get_reset_reason(0),
0,
#else
ESP.getResetInfoPtr()->reason,
ESP.getResetInfoPtr()->exccause,
#endif
upinfo.exitCode,
upinfo.errorCode,
@ -333,14 +335,15 @@ void AmsWebServer::sysinfoJson() {
server.setContentLength(strlen(buf));
server.send(200, MIME_JSON, buf);
server.handleClient();
delay(250);
if(performRestart || rebootForUpgrade) {
server.handleClient();
delay(250);
if(ds != NULL) {
ds->save();
}
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting"));
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush();
delay(1000);
ESP.restart();
performRestart = false;
@ -723,7 +726,7 @@ void AmsWebServer::temperatureJson() {
conf == NULL || conf->common ? 1 : 0,
data->lastRead
);
delay(10);
yield();
}
char* pos = buf+strlen(buf);
snprintf_P(count == 0 ? pos : pos-1, 8, PSTR("]}"));
@ -964,7 +967,7 @@ void AmsWebServer::configurationJson() {
}
void AmsWebServer::handleSave() {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Handling save method from http"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Handling save method from http\n"));
if(!checkSecurity(1))
return;
@ -1176,7 +1179,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("m")) && server.arg(F("m")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received meter config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received meter config\n"));
config->getMeterConfig(*meterConfig);
meterConfig->baud = server.arg(F("mb")).toInt();
meterConfig->parity = server.arg(F("mp")).toInt();
@ -1215,7 +1218,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("w")) && server.arg(F("w")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received WiFi config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received WiFi config\n"));
WiFiConfig wifi;
config->getWiFiConfig(wifi);
strcpy(wifi.ssid, server.arg(F("ws")).c_str());
@ -1257,7 +1260,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("q")) && server.arg(F("q")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received MQTT config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received MQTT config\n"));
MqttConfig mqtt;
config->getMqttConfig(mqtt);
if(server.hasArg(F("qh")) && !server.arg(F("qh")).isEmpty()) {
@ -1288,7 +1291,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("o")) && server.arg(F("o")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Domoticz config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Domoticz config\n"));
DomoticzConfig domo {
static_cast<uint16_t>(server.arg(F("oe")).toInt()),
static_cast<uint16_t>(server.arg(F("ou1")).toInt()),
@ -1300,7 +1303,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("h")) && server.arg(F("h")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Home-Assistant config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Home-Assistant config\n"));
HomeAssistantConfig haconf;
config->getHomeAssistantConfig(haconf);
strcpy(haconf.discoveryPrefix, server.arg(F("ht")).c_str());
@ -1310,7 +1313,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("g")) && server.arg(F("g")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received web config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received web config\n"));
webConfig.security = server.arg(F("gs")).toInt();
if(webConfig.security > 0) {
strcpy(webConfig.username, server.arg(F("gu")).c_str());
@ -1340,7 +1343,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("i")) && server.arg(F("i")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received GPIO config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received GPIO config\n"));
gpioConfig->hanPin = server.hasArg(F("ihp")) && !server.arg(F("ihp")).isEmpty() ? server.arg(F("ihp")).toInt() : 3;
gpioConfig->hanPinPullup = server.hasArg(F("ihu")) && server.arg(F("ihu")) == F("true");
gpioConfig->ledPin = server.hasArg(F("ilp")) && !server.arg(F("ilp")).isEmpty() ? server.arg(F("ilp")).toInt() : 0xFF;
@ -1359,7 +1362,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("iv")) && server.arg(F("iv")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Vcc config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Vcc config\n"));
gpioConfig->vccOffset = server.hasArg(F("ivo")) && !server.arg(F("ivo")).isEmpty() ? server.arg(F("ivo")).toFloat() * 100 : 0;
gpioConfig->vccMultiplier = server.hasArg(F("ivm")) && !server.arg(F("ivm")).isEmpty() ? server.arg(F("ivm")).toFloat() * 1000 : 1000;
gpioConfig->vccBootLimit = server.hasArg(F("ivb")) && !server.arg(F("ivb")).isEmpty() ? server.arg(F("ivb")).toFloat() * 10 : 0;
@ -1367,7 +1370,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("d")) && server.arg(F("d")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Debug config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received Debug config\n"));
DebugConfig debug;
config->getDebugConfig(debug);
bool active = debug.serial || debug.telnet;
@ -1414,7 +1417,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("p")) && server.arg(F("p")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received price API config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received price API config\n"));
priceRegion = server.arg(F("pr"));
@ -1429,7 +1432,7 @@ void AmsWebServer::handleSave() {
}
if(server.hasArg(F("t")) && server.arg(F("t")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received energy accounting config"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Received energy accounting config\n"));
EnergyAccountingConfig eac;
eac.thresholds[0] = server.arg(F("t0")).toInt();
eac.thresholds[1] = server.arg(F("t1")).toInt();
@ -1444,10 +1447,10 @@ void AmsWebServer::handleSave() {
config->setEnergyAccountingConfig(eac);
}
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Saving configuration now..."));
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Saving configuration now...\n"));
if (config->save()) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully saved."));
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Successfully saved.\n"));
if(config->isWifiChanged() || performRestart) {
performRestart = true;
} else {
@ -1465,14 +1468,15 @@ void AmsWebServer::handleSave() {
server.setContentLength(strlen(buf));
server.send(200, MIME_JSON, buf);
server.handleClient();
delay(250);
if(performRestart || rebootForUpgrade) {
server.handleClient();
delay(250);
if(ds != NULL) {
ds->save();
}
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting"));
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush();
delay(1000);
ESP.restart();
performRestart = false;
@ -1490,7 +1494,8 @@ void AmsWebServer::reboot() {
server.handleClient();
delay(250);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting"));
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush();
delay(1000);
ESP.restart();
performRestart = false;
@ -1578,7 +1583,7 @@ void AmsWebServer::upgradeFromUrl(String url, String nextVersion) {
}
void AmsWebServer::firmwareHtml() {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Serving /firmware.html over http..."));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Serving /firmware.html over http...\n"));
if(!checkSecurity(1))
return;
@ -1592,7 +1597,7 @@ void AmsWebServer::firmwareHtml() {
}
void AmsWebServer::firmwarePost() {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Handling firmware post..."));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Handling firmware post...\n"));
if(!checkSecurity(1))
return;
@ -1602,7 +1607,7 @@ void AmsWebServer::firmwarePost() {
if(server.hasArg(F("url"))) {
String url = server.arg(F("url"));
if(!url.isEmpty() && (url.startsWith(F("http://")) || url.startsWith(F("https://")))) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Custom firmware URL was provided"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Custom firmware URL was provided\n"));
upgradeFromUrl(url, "");
server.send(200, MIME_PLAIN, "OK");
@ -1687,7 +1692,7 @@ HTTPUpload& AmsWebServer::uploadFile(const char* path) {
LittleFS.remove(path);
LittleFS.end();
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("An Error has occurred while writing file"));
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("An Error has occurred while writing file\n"));
snprintf_P(buf, BufferSize, RESPONSE_JSON,
"false",
"File size does not match",
@ -1704,7 +1709,7 @@ HTTPUpload& AmsWebServer::uploadFile(const char* path) {
if(file) {
file.flush();
file.close();
// LittleFS.end();
LittleFS.end();
} else {
debugger->printf_P(PSTR("File was not valid in the end... Write error: %d, \n"), file.getWriteError());
snprintf_P(buf, BufferSize, RESPONSE_JSON,
@ -1728,13 +1733,13 @@ void AmsWebServer::factoryResetPost() {
if(!checkSecurity(1))
return;
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Performing factory reset"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Performing factory reset\n"));
bool success = false;
if(server.hasArg(F("perform")) && server.arg(F("perform")) == F("true")) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Formatting LittleFS"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Formatting LittleFS\n"));
LittleFS.format();
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Clearing configuration"));
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Clearing configuration\n"));
config->clear();
success = true;
@ -1751,7 +1756,8 @@ void AmsWebServer::factoryResetPost() {
server.handleClient();
delay(250);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting"));
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Rebooting\n"));
debugger->flush();
delay(1000);
ESP.restart();
}

View File

@ -221,7 +221,7 @@ void setup() {
}
Debug.setSerialEnabled(true);
delay(1);
yield();
float vcc = hw.getVcc();
@ -254,11 +254,17 @@ void setup() {
debugD_P(PSTR("ESP8266 LittleFS"));
hasFs = LittleFS.begin();
#endif
delay(1);
yield();
if(hasFs) {
#if defined(ESP8266)
LittleFS.gc();
if(!LittleFS.check()) {
debugW_P(PSTR("LittleFS filesystem error"));
if(!LittleFS.format()) {
debugE_P(PSTR("Unable to format broken filesystem"));
}
}
#endif
bool flashed = false;
if(LittleFS.exists(FILE_FIRMWARE)) {
@ -310,11 +316,11 @@ void setup() {
configFileParse();
flashed = true;
}
LittleFS.end();
if(flashed) {
LittleFS.end();
if(Debug.isActive(RemoteDebug::INFO)) {
debugI_P(PSTR("Firmware update complete, restarting"));
Serial.flush();
Debug.flush();
}
delay(250);
ESP.restart();
@ -322,7 +328,7 @@ void setup() {
}
}
LittleFS.end();
delay(1);
yield();
if(config.hasConfig()) {
if(Debug.isActive(RemoteDebug::INFO)) config.print(&Debug);
@ -386,8 +392,13 @@ uint8_t parities[] = { 11, 3, 3, 3 };
bool inverts[] = { false, false, false, true };
void loop() {
Debug.handle();
unsigned long now = millis();
unsigned long start = now;
Debug.handle();
unsigned long end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to handle debug"), millis()-start);
}
if(gpioConfig.apPin != 0xFF) {
if (digitalRead(gpioConfig.apPin) == LOW) {
if (buttonActive == false) {
@ -424,18 +435,22 @@ void loop() {
if(hwSerial->hasOverrun()) {
meterState.setLastError(METER_ERROR_BUFFER);
if(rxBufferSize < MAX_RX_BUFFER_SIZE) {
/*
rxBufferSize += 64;
debugI_P(PSTR("Increasing RX buffer to %d bytes"), rxBufferSize);
config.setMeterChanged();
*/
}
}
#endif
} else if(swSerial != NULL) {
if(swSerial->overflow()) {
meterState.setLastError(METER_ERROR_BUFFER);
/*
rxBufferSize += 64;
debugI_P(PSTR("Increasing RX buffer to %d bytes"), rxBufferSize);
config.setMeterChanged();
*/
}
}
@ -548,8 +563,19 @@ void loop() {
try {
if(eapi != NULL && ntpEnabled) {
start = millis();
if(eapi->loop() && mqtt != NULL && mqttHandler != NULL && mqtt->connected()) {
end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to update prices"), millis()-start);
}
start = millis();
mqttHandler->publishPrices(eapi);
end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to publish prices to MQTT"), millis()-start);
}
}
}
@ -574,11 +600,21 @@ void loop() {
} catch(const std::exception& e) {
debugE_P(PSTR("Exception in ENTSO-E loop (%s)"), e.what());
}
start = millis();
ws.loop();
end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to handle web"), millis()-start);
}
}
if(mqtt != NULL) {
start = millis();
mqtt->loop();
delay(10); // Needed to preserve power. After adding this, the voltage is super smooth on a HAN powered device
end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to handle mqtt"), millis()-start);
}
}
} else {
if(dnsServer != NULL) {
@ -608,23 +644,36 @@ void loop() {
config.ackEnergyAccountingChange();
}
try {
start = millis();
if(readHanPort() || now - meterState.getLastUpdateMillis() > 30000) {
end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to read HAN port"), millis()-start);
}
if(now - lastTemperatureRead > 15000) {
unsigned long start = millis();
start = millis();
if(hw.updateTemperatures()) {
lastTemperatureRead = now;
if(mqtt != NULL && mqttHandler != NULL && WiFi.getMode() != WIFI_AP && WiFi.status() == WL_CONNECTED && mqtt->connected() && !topic.isEmpty()) {
mqttHandler->publishTemperatures(&config, &hw);
}
debugD_P(PSTR("Used %ld ms to update temperature"), millis()-start);
}
end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to update temperature"), millis()-start);
}
}
if(now - lastSysupdate > 60000) {
start = millis();
if(mqtt != NULL && mqttHandler != NULL && WiFi.getMode() != WIFI_AP && WiFi.status() == WL_CONNECTED && mqtt->connected() && !topic.isEmpty()) {
mqttHandler->publishSystem(&hw, eapi, &ea);
}
lastSysupdate = now;
end = millis();
if(end - start > 1000) {
debugW_P(PSTR("Used %dms to send system update to MQTT"), millis()-start);
}
}
}
} catch(const std::exception& e) {
@ -661,6 +710,12 @@ void loop() {
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
yield();
end = millis();
if(end-now > 2000) {
debugW_P(PSTR("loop() used %dms"), end-now);
}
}
#if defined(ESP32)
@ -672,11 +727,13 @@ void rxerr(int err) {
break;
case 2:
debugE_P(PSTR("Serial buffer full"));
/*
if(rxBufferSize < MAX_RX_BUFFER_SIZE) {
rxBufferSize += 64;
debugI_P(PSTR("Increasing RX buffer to %d bytes"), rxBufferSize);
config.setMeterChanged();
}
*/
break;
case 3:
debugE_P(PSTR("Serial FIFO overflow"));
@ -695,7 +752,7 @@ void rxerr(int err) {
void setupHanPort(GpioConfig& gpioConfig, uint32_t baud, uint8_t parityOrdinal, bool invert) {
uint8_t pin = gpioConfig.hanPin;
if(Debug.isActive(RemoteDebug::INFO)) Debug.printf(PSTR("(setupHanPort) Setting up HAN on pin %d with baud %d and parity %d\n")), pin, baud, parityOrdinal;
if(Debug.isActive(RemoteDebug::INFO)) Debug.printf_P(PSTR("(setupHanPort) Setting up HAN on pin %d with baud %d and parity %d\n"), pin, baud, parityOrdinal);
if(baud == 0) {
baud = bauds[meterAutoIndex];
@ -989,6 +1046,7 @@ bool readHanPort() {
if(mqttEnabled && mqtt != NULL && mqttHandler == NULL) {
mqtt->publish(topic.c_str(), toHex(hanBuffer+pos, len));
mqtt->loop();
delay(10);
}
while(hanSerial->available()) hanSerial->read(); // Make sure it is all empty, in case we overflowed buffer above
len = 0;
@ -1008,6 +1066,7 @@ bool readHanPort() {
if(mqttEnabled && mqtt != NULL && mqttHandler == NULL) {
mqtt->publish(topic.c_str(), toHex((byte*) payload, ctx.length));
mqtt->loop();
delay(10);
}
debugV_P(PSTR("Using application data:"));
@ -1088,9 +1147,13 @@ bool readHanPort() {
if(ea.update(&data)) {
debugI_P(PSTR("Saving energy accounting"));
ea.save();
saveData = true; // Trigger LittleFS.end
}
if(saveData) {
LittleFS.end();
}
}
delay(1);
yield();
return true;
}
@ -1174,6 +1237,7 @@ void WiFi_connect() {
if(mqtt != NULL) {
mqtt->disconnect();
mqtt->loop();
delay(10);
yield();
delete mqtt;
mqtt = NULL;
@ -1337,6 +1401,7 @@ int16_t unwrapData(uint8_t *buf, DataParserContext &context) {
if(mqttEnabled && mqtt != NULL && mqttHandler == NULL) {
mqtt->publish(topic.c_str(), toHex(buf, curLen));
mqtt->loop();
delay(10);
}
break;
case DATA_TAG_MBUS:
@ -1345,6 +1410,7 @@ int16_t unwrapData(uint8_t *buf, DataParserContext &context) {
if(mqttEnabled && mqtt != NULL && mqttHandler == NULL) {
mqtt->publish(topic.c_str(), toHex(buf, curLen));
mqtt->loop();
delay(10);
}
break;
case DATA_TAG_GBT:
@ -1364,6 +1430,7 @@ int16_t unwrapData(uint8_t *buf, DataParserContext &context) {
if(mqttEnabled && mqtt != NULL && mqttHandler == NULL) {
mqtt->publish(topic.c_str(), (char*) buf);
mqtt->loop();
delay(10);
}
break;
}
@ -2002,7 +2069,20 @@ void configFileParse() {
debugD_P(PSTR("Deleting config file"));
file.close();
LittleFS.remove(FILE_CFG);
if(!LittleFS.remove(FILE_CFG)) {
debugW_P(PSTR("Unable to remove config file, formatting filesystem"));
if(!sDs) {
ds.load();
sDs = true;
}
if(!sEa) {
ea.load();
sEa = true;
}
if(!LittleFS.format()) {
debugE_P(PSTR("Unable to format broken filesystem"));
}
}
debugI_P(PSTR("Saving configuration now..."));
Serial.flush();
@ -2020,4 +2100,5 @@ void configFileParse() {
if(sDs) ds.save();
if(sEa) ea.save();
config.save();
}
LittleFS.end();
}

View File

@ -19,7 +19,7 @@ LNG::LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, Da
for(uint8_t x = 0; x < h->arrayLength-1; x++) {
ptr = (uint8_t*) &descriptor[1];
descriptor = (LngObisDescriptor*) ptr;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(L&G) OBIS %d.%d.%d with type 0x%02X"), descriptor->obis[2], descriptor->obis[3], descriptor->obis[4], *data);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(L&G) OBIS %d.%d.%d with type 0x%02X\n"), descriptor->obis[2], descriptor->obis[3], descriptor->obis[4], *data);
CosemData* item = (CosemData*) data;
if(descriptor->obis[2] == 1) {
@ -27,22 +27,22 @@ LNG::LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, Da
if(descriptor->obis[4] == 0) {
o170 = getNumber(item);
listType = listType >= 1 ? listType : 1;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o170);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o170);
}
} else if(descriptor->obis[3] == 8) {
if(descriptor->obis[4] == 0) {
o180 = getNumber(item);
listType = listType >= 3 ? listType : 3;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o180);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o180);
activeImportCounter = o180 / 1000.0;
} else if(descriptor->obis[4] == 1) {
o181 = getNumber(item);
listType = listType >= 3 ? listType : 3;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o181);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o181);
} else if(descriptor->obis[4] == 2) {
o182 = getNumber(item);
listType = listType >= 3 ? listType : 3;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o182);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o182);
}
}
} else if(descriptor->obis[2] == 2) {
@ -50,22 +50,22 @@ LNG::LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, Da
if(descriptor->obis[4] == 0) {
o270 = getNumber(item);
listType = listType >= 2 ? listType : 2;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o270);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o270);
}
} else if(descriptor->obis[3] == 8) {
if(descriptor->obis[4] == 0) {
o280 = getNumber(item);
listType = listType >= 3 ? listType : 3;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o280);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o280);
activeExportCounter = o280 / 1000.0;
} else if(descriptor->obis[4] == 1) {
o281 = getNumber(item);
listType = listType >= 3 ? listType : 3;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o281);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o281);
} else if(descriptor->obis[4] == 2) {
o282 = getNumber(item);
listType = listType >= 3 ? listType : 3;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu"), o282);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %lu\n"), o282);
}
}
} else if(descriptor->obis[2] == 96) {
@ -76,14 +76,14 @@ LNG::LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, Da
str[item->oct.length] = '\0';
meterId = String(str);
listType = listType >= 2 ? listType : 2;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %s (oct)"), str);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %s (oct)\n"), str);
} else if(descriptor->obis[4] == 1) {
char str[item->oct.length+1];
memcpy(str, item->oct.data, item->oct.length);
str[item->oct.length] = '\0';
meterModel = String(str);
listType = listType >= 2 ? listType : 2;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %s (oct)"), str);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR(" and value %s (oct)\n"), str);
}
}
}