mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-04-26 04:07:57 +00:00
160mhz esp32 and some error handling in loop()
This commit is contained in:
@@ -17,11 +17,12 @@ extra_scripts =
|
|||||||
scripts/makeweb.py
|
scripts/makeweb.py
|
||||||
build_flags =
|
build_flags =
|
||||||
-D WEBSOCKET_DISABLED=1
|
-D WEBSOCKET_DISABLED=1
|
||||||
|
build_unflags = -fno-exceptions
|
||||||
|
|
||||||
|
|
||||||
[env:esp32]
|
[env:esp32]
|
||||||
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
|
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
|
||||||
board = esp32dev
|
board = esp32doit-devkit-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps = ${common.lib_deps}
|
lib_deps = ${common.lib_deps}
|
||||||
lib_ignore = ${common.lib_ignore}
|
lib_ignore = ${common.lib_ignore}
|
||||||
@@ -30,3 +31,4 @@ extra_scripts =
|
|||||||
scripts/makeweb.py
|
scripts/makeweb.py
|
||||||
build_flags =
|
build_flags =
|
||||||
-D WEBSOCKET_DISABLED=1
|
-D WEBSOCKET_DISABLED=1
|
||||||
|
board_build.f_cpu = 160000000L
|
||||||
|
|||||||
@@ -333,7 +333,11 @@ unsigned long lastErrorBlink = 0;
|
|||||||
int lastError = 0;
|
int lastError = 0;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Debug.handle();
|
try {
|
||||||
|
Debug.handle();
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
Serial.printf("Exception in Debug loop (%s)\n", e.what());
|
||||||
|
}
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
if(gpioConfig.apPin != 0xFF) {
|
if(gpioConfig.apPin != 0xFF) {
|
||||||
if (digitalRead(gpioConfig.apPin) == LOW) {
|
if (digitalRead(gpioConfig.apPin) == LOW) {
|
||||||
@@ -363,7 +367,11 @@ void loop() {
|
|||||||
if (WiFi.status() != WL_CONNECTED) {
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
wifiConnected = false;
|
wifiConnected = false;
|
||||||
Debug.stop();
|
Debug.stop();
|
||||||
WiFi_connect();
|
try {
|
||||||
|
WiFi_connect();
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception in WiFi connect (%s)", e.what());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
wifiReconnectCount = 0;
|
wifiReconnectCount = 0;
|
||||||
if(!wifiConnected) {
|
if(!wifiConnected) {
|
||||||
@@ -377,10 +385,14 @@ void loop() {
|
|||||||
}
|
}
|
||||||
DebugConfig debug;
|
DebugConfig debug;
|
||||||
if(config.getDebugConfig(debug)) {
|
if(config.getDebugConfig(debug)) {
|
||||||
Debug.begin(wifi.hostname, (uint8_t) debug.level);
|
try {
|
||||||
Debug.setSerialEnabled(debug.serial);
|
Debug.begin(wifi.hostname, (uint8_t) debug.level);
|
||||||
if(!debug.telnet) {
|
Debug.setSerialEnabled(debug.serial);
|
||||||
Debug.stop();
|
if(!debug.telnet) {
|
||||||
|
Debug.stop();
|
||||||
|
}
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
Serial.printf("Exception in Debug setup (%s)\n", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(Debug.isActive(RemoteDebug::INFO)) {
|
if(Debug.isActive(RemoteDebug::INFO)) {
|
||||||
@@ -439,33 +451,45 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
if(eapi != NULL && ntpEnabled) {
|
try {
|
||||||
if(eapi->loop() && mqtt != NULL && mqttHandler != NULL && mqtt->connected()) {
|
if(eapi != NULL && ntpEnabled) {
|
||||||
mqttHandler->publishPrices(eapi);
|
if(eapi->loop() && mqtt != NULL && mqttHandler != NULL && mqtt->connected()) {
|
||||||
}
|
mqttHandler->publishPrices(eapi);
|
||||||
}
|
|
||||||
|
|
||||||
if(config.isEntsoeChanged()) {
|
|
||||||
EntsoeConfig entsoe;
|
|
||||||
if(config.getEntsoeConfig(entsoe) && strlen(entsoe.token) > 0) {
|
|
||||||
if(eapi == NULL) {
|
|
||||||
eapi = new EntsoeApi(&Debug);
|
|
||||||
ws.setEntsoeApi(eapi);
|
|
||||||
}
|
}
|
||||||
eapi->setup(entsoe);
|
|
||||||
} else if(eapi != NULL) {
|
|
||||||
delete eapi;
|
|
||||||
eapi = NULL;
|
|
||||||
ws.setEntsoeApi(NULL);
|
|
||||||
}
|
}
|
||||||
config.ackEntsoeChange();
|
|
||||||
|
if(config.isEntsoeChanged()) {
|
||||||
|
EntsoeConfig entsoe;
|
||||||
|
if(config.getEntsoeConfig(entsoe) && strlen(entsoe.token) > 0) {
|
||||||
|
if(eapi == NULL) {
|
||||||
|
eapi = new EntsoeApi(&Debug);
|
||||||
|
ws.setEntsoeApi(eapi);
|
||||||
|
}
|
||||||
|
eapi->setup(entsoe);
|
||||||
|
} else if(eapi != NULL) {
|
||||||
|
delete eapi;
|
||||||
|
eapi = NULL;
|
||||||
|
ws.setEntsoeApi(NULL);
|
||||||
|
}
|
||||||
|
config.ackEntsoeChange();
|
||||||
|
}
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception in ENTSO-E loop (%s)", e.what());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
ws.loop();
|
try {
|
||||||
|
ws.loop();
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception in Web server loop (%s)", e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(mqtt != NULL) { // Run loop regardless, to let MQTT do its work.
|
if(mqtt != NULL) {
|
||||||
mqtt->loop();
|
try {
|
||||||
delay(10); // Needed to preserve power. After adding this, the voltage is super smooth on a HAN powered device
|
mqtt->loop();
|
||||||
|
delay(10); // Needed to preserve power. After adding this, the voltage is super smooth on a HAN powered device
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception in MQTT loop (%s)", e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(dnsServer != NULL) {
|
if(dnsServer != NULL) {
|
||||||
@@ -488,17 +512,29 @@ void loop() {
|
|||||||
hc = NULL;
|
hc = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(readHanPort() || now - meterState.getLastUpdateMillis() > 30000) {
|
try {
|
||||||
if(now - lastTemperatureRead > 15000) {
|
if(readHanPort() || now - meterState.getLastUpdateMillis() > 30000) {
|
||||||
unsigned long start = millis();
|
if(now - lastTemperatureRead > 15000) {
|
||||||
hw.updateTemperatures();
|
try {
|
||||||
lastTemperatureRead = now;
|
unsigned long start = millis();
|
||||||
|
hw.updateTemperatures();
|
||||||
|
lastTemperatureRead = now;
|
||||||
|
|
||||||
if(mqtt != NULL && mqttHandler != NULL && WiFi.getMode() != WIFI_AP && WiFi.status() == WL_CONNECTED && mqtt->connected() && !topic.isEmpty()) {
|
try {
|
||||||
mqttHandler->publishTemperatures(&config, &hw);
|
if(mqtt != NULL && mqttHandler != NULL && WiFi.getMode() != WIFI_AP && WiFi.status() == WL_CONNECTED && mqtt->connected() && !topic.isEmpty()) {
|
||||||
|
mqttHandler->publishTemperatures(&config, &hw);
|
||||||
|
}
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while publishing temperatures to MQTT (%s)", e.what());
|
||||||
|
}
|
||||||
|
debugD("Used %d ms to update temperature", millis()-start);
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while updating temperatures (%s)", e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
debugD("Used %d ms to update temperature", millis()-start);
|
|
||||||
}
|
}
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception in readHanPort (%s)", e.what());
|
||||||
}
|
}
|
||||||
delay(1); // Needed for auto modem sleep
|
delay(1); // Needed for auto modem sleep
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
@@ -706,7 +742,11 @@ bool readHanPort() {
|
|||||||
int pos = HDLC_FRAME_INCOMPLETE;
|
int pos = HDLC_FRAME_INCOMPLETE;
|
||||||
while(hanSerial->available() && pos == HDLC_FRAME_INCOMPLETE) {
|
while(hanSerial->available() && pos == HDLC_FRAME_INCOMPLETE) {
|
||||||
buf[len++] = hanSerial->read();
|
buf[len++] = hanSerial->read();
|
||||||
pos = HDLC_validate((uint8_t *) buf, len, hc, ×tamp);
|
try {
|
||||||
|
pos = HDLC_validate((uint8_t *) buf, len, hc, ×tamp);
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while parsing validating HDLC (%s)", e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(len > 0) {
|
if(len > 0) {
|
||||||
if(len >= BUF_SIZE) {
|
if(len >= BUF_SIZE) {
|
||||||
@@ -770,7 +810,11 @@ bool readHanPort() {
|
|||||||
while(hanSerial->available()) hanSerial->read();
|
while(hanSerial->available()) hanSerial->read();
|
||||||
if(pos > 0) {
|
if(pos > 0) {
|
||||||
debugD("Valid data, start at byte %d", pos);
|
debugD("Valid data, start at byte %d", pos);
|
||||||
data = IEC6205675(((char *) (buf)) + pos, meterState.getMeterType(), meterConfig.distributionSystem, timestamp, hc);
|
try {
|
||||||
|
data = IEC6205675(((char *) (buf)) + pos, meterState.getMeterType(), meterConfig.distributionSystem, timestamp, hc);
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while parsing IEC62056-7-5 (%s)", e.what());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if(Debug.isActive(RemoteDebug::WARNING)) {
|
if(Debug.isActive(RemoteDebug::WARNING)) {
|
||||||
switch(pos) {
|
switch(pos) {
|
||||||
@@ -823,7 +867,11 @@ bool readHanPort() {
|
|||||||
}
|
}
|
||||||
} else if(currentMeterType == 2) {
|
} else if(currentMeterType == 2) {
|
||||||
String payload = hanSerial->readString();
|
String payload = hanSerial->readString();
|
||||||
data = IEC6205621(payload);
|
try {
|
||||||
|
data = IEC6205621(payload);
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while parsing IEC62056-21 (%s)", e.what());
|
||||||
|
}
|
||||||
if(data.getListType() == 0) {
|
if(data.getListType() == 0) {
|
||||||
currentMeterType = 1;
|
currentMeterType = 1;
|
||||||
return false;
|
return false;
|
||||||
@@ -839,17 +887,29 @@ bool readHanPort() {
|
|||||||
if(!hw.ledBlink(LED_GREEN, 1))
|
if(!hw.ledBlink(LED_GREEN, 1))
|
||||||
hw.ledBlink(LED_INTERNAL, 1);
|
hw.ledBlink(LED_INTERNAL, 1);
|
||||||
if(mqttEnabled && mqttHandler != NULL && mqtt != NULL) {
|
if(mqttEnabled && mqttHandler != NULL && mqtt != NULL) {
|
||||||
if(mqttHandler->publish(&data, &meterState)) {
|
try {
|
||||||
if(data.getListType() == 3 && eapi != NULL) {
|
if(mqttHandler->publish(&data, &meterState)) {
|
||||||
mqttHandler->publishPrices(eapi);
|
if(data.getListType() == 3 && eapi != NULL) {
|
||||||
|
try {
|
||||||
|
mqttHandler->publishPrices(eapi);
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while publishing prices to MQTT (%s)", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(data.getListType() >= 2) {
|
||||||
|
try {
|
||||||
|
mqttHandler->publishSystem(&hw);
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while publishing system info to MQTT (%s)", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(data.getListType() >= 2) {
|
if(mqtt != NULL) {
|
||||||
mqttHandler->publishSystem(&hw);
|
mqtt->loop();
|
||||||
|
delay(10);
|
||||||
}
|
}
|
||||||
}
|
} catch(const std::exception& e) {
|
||||||
if(mqtt != NULL) {
|
debugE("Exception while publishing AMS data to MQTT (%s)", e.what());
|
||||||
mqtt->loop();
|
|
||||||
delay(10);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1123,7 +1183,11 @@ void MQTT_connect() {
|
|||||||
config.ackMqttChange();
|
config.ackMqttChange();
|
||||||
|
|
||||||
if(mqttHandler != NULL) {
|
if(mqttHandler != NULL) {
|
||||||
mqttHandler->publishSystem(&hw);
|
try {
|
||||||
|
mqttHandler->publishSystem(&hw);
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
debugE("Exception while publishing system info to MQTT (%s)", e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Debug.isActive(RemoteDebug::ERROR)) {
|
if (Debug.isActive(RemoteDebug::ERROR)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user