From eeaac9548e33cc323fe1343e08ae285e4d8bd1ec Mon Sep 17 00:00:00 2001 From: tobimai Date: Mon, 10 Mar 2025 23:33:41 +0100 Subject: [PATCH 1/7] Rework logging --- .vscode/settings.json | 3 +- data/update_progress.html | 10 ++--- documentation/old.md | 6 +-- lib/fetchOTA/fetchOTA.cpp | 42 +++++++++--------- platformio.ini | 9 ++-- src/external_interfacing/leds.cpp | 4 +- src/main.cpp | 74 ++++++++++++++++--------------- src/networking/json_builder.cpp | 2 +- src/networking/networking.cpp | 18 ++++---- src/networking/responses.cpp | 2 +- src/sensor/sensor.cpp | 14 +++--- src/tools/tools.cpp | 48 +++++++------------- 12 files changed, 111 insertions(+), 121 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f9a309a..5601ac9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "files.associations": { "iostream": "cpp", - "random": "cpp" + "random": "cpp", + "vector": "cpp" } } \ No newline at end of file diff --git a/data/update_progress.html b/data/update_progress.html index bf06747..bf5ba8a 100644 --- a/data/update_progress.html +++ b/data/update_progress.html @@ -18,13 +18,13 @@ \ No newline at end of file diff --git a/documentation/old.md b/documentation/old.md index ea6f346..d17c7b9 100644 --- a/documentation/old.md +++ b/documentation/old.md @@ -17,7 +17,7 @@ void listNetworkInterfaces() { } void setEthernetAsDefault() { - Log.verbose("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + logger.log(0, DEBUG, "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); listNetworkInterfaces(); // Attempt to locate the Ethernet network interface. // (The name may vary—on some systems it might be "eth0") @@ -31,8 +31,8 @@ void setEthernetAsDefault() { struct netif *eth_netif2 = netif_find("ETH_DEF"); if (netif) { netif_set_default(eth_netif2); - Log.verbose("Ethernet set as default network interface."); + logger.log(0, DEBUG, "Ethernet set as default network interface."); } else { - Log.verbose("Could not find Ethernet netif."); + logger.log(0, DEBUG, "Could not find Ethernet netif."); } } \ No newline at end of file diff --git a/lib/fetchOTA/fetchOTA.cpp b/lib/fetchOTA/fetchOTA.cpp index 1d6cb8a..ccf7421 100644 --- a/lib/fetchOTA/fetchOTA.cpp +++ b/lib/fetchOTA/fetchOTA.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include @@ -22,14 +22,14 @@ Firmware OTA::getLatestVersionOnServer() { HTTPClient http; http.begin(_serverUrl); int httpCode = http.GET(); - Log.verbose("HTTP Code: %d", httpCode); + logger.log(0, DEBUG, "HTTP Code: %d", httpCode); if (httpCode != 200) { return createErrorResponse("HTTP GET request failed with code " + String(httpCode)); } String payload = http.getString(); - Log.verbose("Payload: %s", payload.c_str()); + logger.log(0, DEBUG, "Payload: %s", payload.c_str()); DynamicJsonDocument doc(4096); DeserializationError error = deserializeJson(doc, payload); @@ -54,7 +54,7 @@ Firmware OTA::getLatestVersionOnServer() { deviceConfig }); } else { - Log.verbose("Configuration %s does not match current device configuration %s", deviceConfig.c_str(), _current_device_configuration.c_str()); + logger.log(0, DEBUG, "Configuration %s does not match current device configuration %s", deviceConfig.c_str(), _current_device_configuration.c_str()); } } } @@ -67,7 +67,7 @@ Firmware OTA::getLatestVersionOnServer() { Configuration latest = getLatestConfiguration(configs.data(), configs.size()); if (!isVersionNewer(_currentVersion, latest.version)) { - Log.verbose("No newer version found. Server version: %d.%d.%d", latest.version.major, latest.version.minor, latest.version.patch); + logger.log(0, DEBUG, "No newer version found. Server version: %d.%d.%d", latest.version.major, latest.version.minor, latest.version.patch); } return Firmware{ @@ -88,79 +88,79 @@ Firmware OTA::createErrorResponse(const String& errorMsg) { } void run_ota_update(String url, std::function callback_started, std::function callback_finished, std::function callback_progress, std::function callback_error) { - Log.verbose("Starting OTA upgrade"); + logger.log(0, DEBUG, "Starting OTA upgrade"); HTTPUpdate httpUpdate; httpUpdate.onStart(callback_started); httpUpdate.onEnd(callback_finished); httpUpdate.onProgress(callback_progress); httpUpdate.onError(callback_error); - Log.verbose("Defined callbacks, Starting update now"); + logger.log(0, DEBUG, "Defined callbacks, Starting update now"); t_httpUpdate_return ret; if (url.startsWith("https")) { - Log.verbose("HTTPS URL"); + logger.log(0, DEBUG, "HTTPS URL"); WiFiClientSecure client; client.setInsecure(); ret = httpUpdate.update(client, url); } else if (url.startsWith("http")) { - Log.verbose("HTTP URL"); + logger.log(0, DEBUG, "HTTP URL"); WiFiClient client; ret = httpUpdate.update(client, url); } else { - Log.error("URL is not valid: \n%s", url.c_str()); + logger.log(0, ERROR, "URL is not valid: \n%s", url.c_str()); } switch (ret) { case HTTP_UPDATE_FAILED: - Log.error("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); + logger.log(0, ERROR, "HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); break; case HTTP_UPDATE_NO_UPDATES: - Log.error("HTTP_UPDATE_NO_UPDATES"); + logger.log(0, ERROR, "HTTP_UPDATE_NO_UPDATES"); break; case HTTP_UPDATE_OK: - Log.verbose("Update done"); + logger.log(0, DEBUG, "Update done"); break; } } void run_ota_spiffs_update(String url, std::function callback_started, std::function callback_finished, std::function callback_progress, std::function callback_error) { - Log.verbose("Starting OTA SPIFFS upgrade"); + logger.log(0, DEBUG, "Starting OTA SPIFFS upgrade"); HTTPUpdate httpUpdate; httpUpdate.onStart(callback_started); httpUpdate.onEnd(callback_finished); httpUpdate.onProgress(callback_progress); httpUpdate.onError(callback_error); - Log.verbose("Defined callbacks, Starting update now"); + logger.log(0, DEBUG, "Defined callbacks, Starting update now"); t_httpUpdate_return ret; if (url.startsWith("https")) { - Log.verbose("HTTPS URL"); + logger.log(0, DEBUG, "HTTPS URL"); WiFiClientSecure client; client.setInsecure(); ret = httpUpdate.updateSpiffs(client, url); } else if (url.startsWith("http")) { - Log.verbose("HTTP URL"); + logger.log(0, DEBUG, "HTTP URL"); WiFiClient client; ret = httpUpdate.updateSpiffs(client, url); } else { - Log.error("URL is not valid: \n%s", url.c_str()); + logger.log(0, ERROR, "URL is not valid: \n%s", url.c_str()); } switch (ret) { case HTTP_UPDATE_FAILED: - Log.error("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); + logger.log(0, ERROR, "HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); break; case HTTP_UPDATE_NO_UPDATES: - Log.error("HTTP_UPDATE_NO_UPDATES"); + logger.log(0, ERROR, "HTTP_UPDATE_NO_UPDATES"); break; case HTTP_UPDATE_OK: - Log.verbose("SPIFFS Update done"); + logger.log(0, DEBUG, "SPIFFS Update done"); break; } } diff --git a/platformio.ini b/platformio.ini index 0d10882..9dada1f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,14 +19,14 @@ monitor_speed = 115200 lib_deps = ottowinter/ESPAsyncWebServer-esphome@^3.3.0 bblanchon/ArduinoJson@^6.21.3 - jsc/ArduinoLog fetchOTA - INA322 + INA233 ESP32Ping + https://github.com/x821938/elog board_build.partitions = default.csv upload_protocol = espota upload_port = 192.168.5.180 -build_flags = -Wall -Wextra +build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE [env:ESP32_INA226] platform = espressif32 @@ -39,10 +39,11 @@ lib_deps = bblanchon/ArduinoJson@^6.21.3 jsc/ArduinoLog ESP32Ping + https://github.com/x821938/elog board_build.partitions = default.csv upload_protocol = espota upload_port = 192.168.4.18 -build_flags = -Wall -Wextra -DUSE_INA226 +build_flags = -Wall -Wextra -DUSE_INA226 -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE [env:native] platform = native diff --git a/src/external_interfacing/leds.cpp b/src/external_interfacing/leds.cpp index 7e1de4d..187c6bd 100644 --- a/src/external_interfacing/leds.cpp +++ b/src/external_interfacing/leds.cpp @@ -1,6 +1,6 @@ #include "leds.h" #include -#include +#include #include "../tools/tools.h" extern ActiveErrors active_errors; @@ -72,7 +72,7 @@ void display_task(void* parameter) // We have no error, refresh status display and wait half a second display_percentage(water_data.percentage); } else { - Log.verbose("Error detected"); + logger.log(0, DEBUG, "Error detected"); // We have an error, display error code for 3 seconds and then water level for 3 seconds if (active_errors.voltage_low) { diff --git a/src/main.cpp b/src/main.cpp index 62a2684..753a006 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,7 @@ #include "AsyncJson.h" #include -#include +#include #include @@ -26,6 +26,7 @@ #include #include +#define MYLOG 0 Preferences prefs; @@ -40,19 +41,18 @@ extern "C" int rom_phy_get_vdd33(); Version current_spiffs_version; AsyncWebServer server(80); -AsyncWebSocket ws("/ws"); +AsyncWebSocket webSocket("/webSocket"); #define FORMAT_LITTLEFS_IF_FAILED true void setup() -{ +{ + logger.registerSerial(MYLOG, DEBUG, "tst"); + logger. + logger.provideTime(2025, 3, 10, 23, 28, 00); prefs.begin("waterlevel", false); Serial.begin(115200); - Log.begin(LOG_LEVEL_VERBOSE, &Serial); - Log.setSuffix(printSuffix); - Log.setPrefix(print_prefix); - - Log.verbose("Init LEDs"); + logger.log(0, DEBUG, "Init LEDs"); pinMode(LED_1, OUTPUT); pinMode(LED_2, OUTPUT); pinMode(LED_3, OUTPUT); @@ -64,7 +64,7 @@ void setup() display_error_code(17); init_sensor(); - Log.verbose("Beginning SPIFFS"); + logger.log(0, DEBUG, "Beginning SPIFFS"); SPIFFS.begin(true); // Read the current SPIFFS version from the versions file on the spiffs @@ -73,21 +73,21 @@ void setup() Serial.println("Failed to open version file for reading"); } else { String version = file.readStringUntil('\n'); - Log.verbose("Version: %s", version); + logger.log(0, DEBUG, "Version: %s", version); current_spiffs_version = parseVersion(version.c_str()); - Log.verbose("Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch); + logger.log(0, DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch); } - Log.verbose("SPIFFS initialized"); + logger.log(0, DEBUG, "SPIFFS initialized"); display_error_code(19); - Log.verbose("Begin INA"); + logger.log(0, DEBUG, "Begin INA"); display_error_code(22); /////////////////////////////// ROUTES /////////////////////////////// - Log.verbose("Route Setup"); + logger.log(0, DEBUG, "Route Setup"); // Normal HTML stuff server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/status.html", "text/html", false, processor); }); @@ -106,7 +106,7 @@ void setup() // For settings SSID if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) { - Log.verbose("Updating SSID config"); + logger.log(0, DEBUG, "Updating SSID config"); AsyncWebParameter* ssid_param = request->getParam(ssid_key, true); AsyncWebParameter* password_param = request->getParam(wifi_password_key, true); prefs.putString(ssid_key, ssid_param->value().c_str()); @@ -122,7 +122,7 @@ void setup() int params = request->params(); if (request->hasParam(level_sensor_range_key, true) && request->hasParam(water_level_min_key, true) && request->hasParam(water_level_max_key, true) && request->hasParam(water_volume_key, true)) { - Log.verbose("Updating Sensor config"); + logger.log(0, DEBUG, "Updating Sensor config"); AsyncWebParameter* range_param = request->getParam(level_sensor_range_key, true); AsyncWebParameter* level_min_param = request->getParam(water_level_min_key, true); AsyncWebParameter* level_max_param = request->getParam(water_level_max_key, true); @@ -144,24 +144,24 @@ void setup() // Convert the C string to a float using strtod float value = strtod(paramCStr, &endPtr); - Log.verbose("range_float:%D:", range_float); + logger.log(0, DEBUG, "range_float:%D:", range_float); prefs.putFloat(level_sensor_range_key, range_float); prefs.putFloat(water_level_min_key, level_min_float); prefs.putFloat(water_level_max_key, level_max_float); prefs.putFloat(water_volume_key, liters_float); - Log.verbose("range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0)); + logger.log(0, DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0)); } else { - Log.verbose("!!!! FAIL lo"); + logger.log(0, DEBUG, "!!!! FAIL lo"); for (int i = 0; i < params; i++) { AsyncWebParameter* p = request->getParam(i); if (p->isFile()) { // p->isPost() is also true - Log.verbose("POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + logger.log(0, DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); } else if (p->isPost()) { - Log.verbose("POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + logger.log(0, DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); } else { - Log.verbose("GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); + logger.log(0, DEBUG, "GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); } } request->send(400, "text/plain", "Missing parameters"); // TODO add proper error messages @@ -171,8 +171,8 @@ void setup() setup_api_endpoints(); display_error_code(23); - ws.onEvent(onWsEvent); - server.addHandler(&ws); + webSocket.onEvent(onWsEvent); + server.addHandler(&webSocket); @@ -181,7 +181,7 @@ void setup() display_error_code(24); - Log.verbose("OTA Setup"); + logger.log(0, DEBUG, "OTA Setup"); ArduinoOTA .onStart([]() { String type; @@ -191,16 +191,16 @@ void setup() type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() - Log.verbose("Start updating %s", type); }) - .onEnd([]() { Log.verbose("\nEnd"); }) + logger.log(0, DEBUG, "Start updating %s", type); }) + .onEnd([]() { logger.log(0, DEBUG, "\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }) .onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); - if (error == OTA_AUTH_ERROR) Log.verbose("Auth Failed"); - else if (error == OTA_BEGIN_ERROR) Log.verbose("Begin Failed"); - else if (error == OTA_CONNECT_ERROR) Log.verbose("Connect Failed"); - else if (error == OTA_RECEIVE_ERROR) Log.verbose("Receive Failed"); - else if (error == OTA_END_ERROR) Log.verbose("End Failed"); }); + if (error == OTA_AUTH_ERROR) logger.log(0, DEBUG, "Auth Failed"); + else if (error == OTA_BEGIN_ERROR) logger.log(0, DEBUG, "Begin Failed"); + else if (error == OTA_CONNECT_ERROR) logger.log(0, DEBUG, "Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) logger.log(0, DEBUG, "Receive Failed"); + else if (error == OTA_END_ERROR) logger.log(0, DEBUG, "End Failed"); }); display_error_code(26); digitalWrite(LED_RED, 0); @@ -216,11 +216,13 @@ void setup() if (!started) { xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); } + logger.configureSyslog("192.168.6.11", 5514, "esp32"); + logger.registerSyslog(MYLOG, DEBUG, FAC_LOCAL4, "mylog"); - Log.verbose("Starting webserver"); + logger.log(0, DEBUG, "Starting webserver"); server.begin(); ArduinoOTA.begin(); display_error_code(25); @@ -234,13 +236,13 @@ void setup() if (WiFi.status() == WL_CONNECTED || ETH.localIP()) { int pingResult = Ping.ping("8.8.8.8"); // Use Google's public DNS server as a test IP if (pingResult >= 0) { - Log.verbose("Network connection established"); + logger.log(0, DEBUG, "Network connection established"); break; } else { - Log.verbose("Network not ready, retrying..."); + logger.log(0, DEBUG, "Network not ready, retrying..."); } } else { - Log.verbose("No WiFi or Ethernet connection, retrying..."); + logger.log(0, DEBUG, "No WiFi or Ethernet connection, retrying..."); } delay(1000); // Delay to prevent rapid retry } diff --git a/src/networking/json_builder.cpp b/src/networking/json_builder.cpp index 7390163..309d1d0 100644 --- a/src/networking/json_builder.cpp +++ b/src/networking/json_builder.cpp @@ -1,5 +1,5 @@ #include "json_builder.h" -#include +#include extern WaterData water_data; extern DeviceTelemetry telemetry; diff --git a/src/networking/networking.cpp b/src/networking/networking.cpp index feeb5d0..510b626 100644 --- a/src/networking/networking.cpp +++ b/src/networking/networking.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "../global_data/defines.h" @@ -34,21 +34,21 @@ const char * get_hostname(HostnameType host_type) { void wifi_task(void* parameter) { - Log.verbose("Starting WiFi Task"); + logger.log(0, DEBUG, "Starting WiFi Task"); WiFi.setHostname(get_hostname(Wireless)); while (true) { if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) { wifi_data.link = false; if (failed_connection_attempts > 5) { - Log.verbose("Failed to connecto to currently saved SSID, starting SoftAP"); + logger.log(0, DEBUG, "Failed to connecto to currently saved SSID, starting SoftAP"); } else { - Log.verbose("No SSID saved, starting SoftAP"); + logger.log(0, DEBUG, "No SSID saved, starting SoftAP"); } String ap_ssid = get_hostname(Wireless); WiFi.softAP(ap_ssid, ""); - Log.verbose("[WIFI_TASK] Waiting for SSID now..."); + logger.log(0, DEBUG, "[WIFI_TASK] Waiting for SSID now..."); String old_ssid = prefs.getString(ssid_key, "xxx"); while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) { @@ -64,11 +64,11 @@ void wifi_task(void* parameter) wifi_data.network_name = WiFi.SSID(); wifi_data.ip_address = WiFi.localIP().toString(); - // Log.verbose("RSSI: %F, IP Address, %p, SSID: %s", float(WiFi.RSSI()), WiFi.localIP(), prefs.getString(ssid_key, "NOSSID")); + // logger.log(0, DEBUG, "RSSI: %F, IP Address, %p, SSID: %s", float(WiFi.RSSI()), WiFi.localIP(), prefs.getString(ssid_key, "NOSSID")); // Serial.println(WiFi.channel()); delay(5000); } else { - Log.verbose("Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); + logger.log(0, DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); WiFi.mode(WIFI_STA); WiFi.begin(prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); failed_connection_attempts++; @@ -80,14 +80,14 @@ void wifi_task(void* parameter) void ethernet_task(void* parameter) { - Log.verbose("Connecting Ethernet"); + logger.log(0, DEBUG, "Connecting Ethernet"); ETH.begin(0, 17, 23, 18); ETH.setHostname(get_hostname(Ethernet)); while (true) { ethernet_data.link = ETH.linkUp(); ethernet_data.rssi = ETH.linkSpeed(); ethernet_data.ip_address = ETH.localIP().toString(); - // Log.verbose("Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ethernet_data.ip_address, String(ethernet_data.link)); + // logger.log(0, DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ethernet_data.ip_address, String(ethernet_data.link)); delay(60 * 1000); } } \ No newline at end of file diff --git a/src/networking/responses.cpp b/src/networking/responses.cpp index d053760..ed752ef 100644 --- a/src/networking/responses.cpp +++ b/src/networking/responses.cpp @@ -4,7 +4,7 @@ #include #include "AsyncJson.h" #include -#include +#include #include "json_builder.h" #include "../tools/tools.h" #include diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index 69b813c..eac7236 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -1,6 +1,6 @@ #include "../global_data/defines.h" #include -#include +#include #include "Wire.h" #include "../global_data/global_data.h" @@ -61,8 +61,8 @@ void read_sensor_task(void* parameter) float min_water_level_mA = 4 + min_water_level_mA_over_zero; float max_water_level_mA = 4 + max_water_level_mA_over_zero; - // Log.verbose("max_water_level_mA: %F", max_water_level_mA); - // Log.verbose("min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); + // logger.log(0, DEBUG, "max_water_level_mA: %F", max_water_level_mA); + // logger.log(0, DEBUG, "min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); // Current over the 0 level of the water float shunt_current_over_zero = shunt_current - min_water_level_mA; @@ -82,10 +82,10 @@ void read_sensor_task(void* parameter) active_errors.current_high = shunt_current > 20.2; active_errors.voltage_low = bus_voltage < 23; active_errors.voltage_high = bus_voltage > 25; - // Log.verbose("Shunt current: %F", shunt_current); - // Log.verbose("Shunt voltage: %F", shunt_voltage); - // Log.verbose("Bus voltage: %F", bus_voltage); - // Log.verbose("cm_over_zero: %F", cm_over_zero); + // logger.log(0, DEBUG, "Shunt current: %F", shunt_current); + // logger.log(0, DEBUG, "Shunt voltage: %F", shunt_voltage); + // logger.log(0, DEBUG, "Bus voltage: %F", bus_voltage); + // logger.log(0, DEBUG, "cm_over_zero: %F", cm_over_zero); shunt_data.bus_voltage = bus_voltage; shunt_data.shunt_voltage = shunt_voltage; diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index 738684d..a24b8e2 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -1,6 +1,6 @@ #include #include "tools.h" -#include +#include #include "../global_data/defines.h" #include #include @@ -8,24 +8,10 @@ extern Preferences prefs; extern OTAStatus ota_status; -extern AsyncWebSocket ws; +extern AsyncWebSocket webSocket; extern Version current_spiffs_version; -void printSuffix(Print* _logOutput, int logLevel) -{ - _logOutput->print(CR); -} - -void print_prefix(Print* _logOutput, int logLevel) -{ - _logOutput->print("WATERMETER - TESTLOL - C"); - _logOutput->print(xPortGetCoreID()); - _logOutput->print(" - "); - _logOutput->print(pcTaskGetName(xTaskGetCurrentTaskHandle())); - _logOutput->print(" - "); -} - bool is_error(ActiveErrors active_errors) { return active_errors.current_high || active_errors.current_low || active_errors.level_high || active_errors.level_low || active_errors.voltage_high || active_errors.voltage_low; } @@ -49,28 +35,28 @@ String processor(const String& var) // OTA Callbacks void update_started() { - Log.verbose("OTA Update started"); + logger.log(0, DEBUG, "OTA Update started"); ota_status.update_progress = 0; } void update_finished() { - Log.verbose("OTA Update finished"); + logger.log(0, DEBUG, "OTA Update finished"); ota_status.update_progress = -1; - ws.textAll(String(-1).c_str()); + webSocket.textAll(String(-1).c_str()); } void update_progress(int cur, int total) { ota_status.update_progress = 0; if (cur != 0 ) { ota_status.update_progress = int(float(cur)/float(total)*100); - Log.verbose("OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress); + logger.log(0, DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress); } - ws.textAll(String(ota_status.update_progress).c_str()); - Log.verbose("OTA Update progress: %d/%d", cur, total); + webSocket.textAll(String(ota_status.update_progress).c_str()); + logger.log(0, DEBUG, "OTA Update progress: %d/%d", cur, total); } void update_error(int err) { - Log.error("OTA Update error: %d", err); + logger.log(0, ERROR, "OTA Update error: %d", err); ota_status.update_progress = -2; } @@ -89,20 +75,20 @@ void check_update_task(void* parameter) { // If there is a SPIFSS update it will be ran automatically, as SPIFFS is necessary for the firmware to run Firmware latest_spiff_version = spiffs_fs.getLatestVersionOnServer(); - Log.verbose("Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch); + logger.log(0, DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch); if (latest_spiff_version.valid) { if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) { // If Required SPIFFS version is newer than current version, update - Log.verbose("New SPIFFS version, running update now"); + logger.log(0, DEBUG, "New SPIFFS version, running update now"); run_ota_spiffs_update(latest_spiff_version.url, update_started, update_finished, update_progress, update_error); // Reboot just to be safe ESP.restart(); } else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_spiff_version.version)) { // If Server has new SPIFFS version but it's not required - Log.verbose("New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); + logger.log(0, DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); } else { - Log.verbose("No new SPIFFS version available"); + logger.log(0, DEBUG, "No new SPIFFS version available"); } } @@ -110,11 +96,11 @@ void check_update_task(void* parameter) { while (true) { Firmware fw = ota.getLatestVersionOnServer(); if (fw.valid) { - Log.verbose("New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); + logger.log(0, DEBUG, "New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); ota_status.latest_version = fw.version; ota_status.update_url = fw.url; if (isVersionNewer(current_software_version, fw.version)) { - Log.verbose("Remote version is newer than current version"); + logger.log(0, DEBUG, "Remote version is newer than current version"); ota_status.update_available = true; } else { ota_status.update_available = false; @@ -125,7 +111,7 @@ void check_update_task(void* parameter) { ota_status.update_available = false; } - Log.verbose("No new firmware available"); + logger.log(0, DEBUG, "No new firmware available"); } delay(1000 * 60 * 1); } @@ -135,7 +121,7 @@ void check_update_task(void* parameter) { void run_ota_update_task(void* parameter) { TaskArgs_t *args = (TaskArgs_t *) parameter; - Log.verbose("Running OTA upgrade now with URL: %s", args->ota_status.update_url.c_str()); + logger.log(0, DEBUG, "Running OTA upgrade now with URL: %s", args->ota_status.update_url.c_str()); run_ota_update(args->ota_status.update_url, update_started, update_finished, update_progress, update_error); vTaskDelete(NULL); } From 76e182935b6436ded60929564afa15c6bf5d108b Mon Sep 17 00:00:00 2001 From: tobimai Date: Tue, 18 Mar 2025 22:51:41 +0100 Subject: [PATCH 2/7] Added syslog --- platformio.ini | 2 +- src/external_interfacing/leds.cpp | 6 ++-- src/main.cpp | 53 +++++++++++++++++++++++++------ src/sensor/sensor.cpp | 14 ++++---- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/platformio.ini b/platformio.ini index 9dada1f..608c99e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,7 +25,7 @@ lib_deps = https://github.com/x821938/elog board_build.partitions = default.csv upload_protocol = espota -upload_port = 192.168.5.180 +upload_port = 192.168.5.205 build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE [env:ESP32_INA226] diff --git a/src/external_interfacing/leds.cpp b/src/external_interfacing/leds.cpp index 187c6bd..9dc00b9 100644 --- a/src/external_interfacing/leds.cpp +++ b/src/external_interfacing/leds.cpp @@ -72,20 +72,22 @@ void display_task(void* parameter) // We have no error, refresh status display and wait half a second display_percentage(water_data.percentage); } else { - logger.log(0, DEBUG, "Error detected"); - // We have an error, display error code for 3 seconds and then water level for 3 seconds if (active_errors.voltage_low) { display_error_code(1); + logger.log(0, WARNING, "Sensor Error - Voltage low"); delay(3000); } else if (active_errors.voltage_high) { display_error_code(2); + logger.log(0, WARNING, "Sensor Error - Voltage High"); delay(3000); } else if (active_errors.current_low) { display_error_code(3); + logger.log(0, WARNING, "Sensor Error - Current low"); delay(3000); } else if (active_errors.current_high) { display_error_code(4); + logger.log(0, WARNING, "Sensor Error - Current high"); delay(3000); } else { delay(3000); diff --git a/src/main.cpp b/src/main.cpp index 753a006..2baa92e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ #include "networking/responses.h" #include #include +#include "time.h" #define MYLOG 0 @@ -47,8 +48,6 @@ AsyncWebSocket webSocket("/webSocket"); void setup() { logger.registerSerial(MYLOG, DEBUG, "tst"); - logger. - logger.provideTime(2025, 3, 10, 23, 28, 00); prefs.begin("waterlevel", false); Serial.begin(115200); @@ -205,19 +204,55 @@ void setup() display_error_code(26); digitalWrite(LED_RED, 0); + // Starting bootup sequence + xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL); - delay(1000); - bool started = false; + + // Create Etnernet task and wait a second to see if there is connection + logger.log(0, DEBUG, "Started Ethernet, waiting"); + delay(2000); + if (ETH.linkUp()){ + logger.log(0, DEBUG, "Ethernet connected, starting update checker"); xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); - started = true; - } - xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); - if (!started) { + } else { + logger.log(0, DEBUG, "Ethernet not connected, starting update checker and WiFi Task"); + xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); + delay(2000); } + + logger.log(0, DEBUG, "Getting time now"); + // Configure time with a GMT offset and daylight offset in seconds. + configTime(3600, 0, "pool.ntp.org"); + + // Wait until a valid time is obtained (time > 8 hours in seconds) + time_t now = time(NULL); + struct tm timeinfo; + int retry = 0; + while (now < 8 * 3600 && retry < 10) { + if(!getLocalTime(&timeinfo)){ + Serial.println("Failed to obtain time"); + } + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); + retry++; + } + + + getLocalTime(&timeinfo); + + // Sanity check: Ensure the year is at least 2020. + int currentYear = timeinfo.tm_year + 1900; // tm_year is years since 1900 + if (currentYear < 2020) { + logger.log(0, DEBUG, "Time not set properly: "); + } else { + logger.log(0, DEBUG, "Time is valid: %s", asctime(&timeinfo)); + } + + // Setup syslog logger.configureSyslog("192.168.6.11", 5514, "esp32"); - logger.registerSyslog(MYLOG, DEBUG, FAC_LOCAL4, "mylog"); + logger.registerSyslog(MYLOG, DEBUG, FAC_USER, "waterlevel"); + logger.log(MYLOG, ERROR, "Here is an error message, error code: %d", 17); diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index eac7236..36c6117 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -43,6 +43,8 @@ void read_sensor_task(void* parameter) float bus_voltage = ina_sensor.getBusVoltage(); float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value; + logger.log(0, DEBUG, "RAW Shunt voltage: %F mV", ina_sensor.getShuntVoltage_mV()); + float shunt_current = shunt_voltage / RESISTOR_VALUE; // Get values from storage @@ -61,8 +63,8 @@ void read_sensor_task(void* parameter) float min_water_level_mA = 4 + min_water_level_mA_over_zero; float max_water_level_mA = 4 + max_water_level_mA_over_zero; - // logger.log(0, DEBUG, "max_water_level_mA: %F", max_water_level_mA); - // logger.log(0, DEBUG, "min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); + logger.log(0, DEBUG, "max_water_level_mA: %F", max_water_level_mA); + logger.log(0, DEBUG, "min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); // Current over the 0 level of the water float shunt_current_over_zero = shunt_current - min_water_level_mA; @@ -82,10 +84,10 @@ void read_sensor_task(void* parameter) active_errors.current_high = shunt_current > 20.2; active_errors.voltage_low = bus_voltage < 23; active_errors.voltage_high = bus_voltage > 25; - // logger.log(0, DEBUG, "Shunt current: %F", shunt_current); - // logger.log(0, DEBUG, "Shunt voltage: %F", shunt_voltage); - // logger.log(0, DEBUG, "Bus voltage: %F", bus_voltage); - // logger.log(0, DEBUG, "cm_over_zero: %F", cm_over_zero); + logger.log(0, DEBUG, "Shunt current: %F", shunt_current); + logger.log(0, DEBUG, "Shunt voltage: %F", shunt_voltage); + logger.log(0, DEBUG, "Bus voltage: %F", bus_voltage); + logger.log(0, DEBUG, "cm_over_zero: %F", cm_over_zero); shunt_data.bus_voltage = bus_voltage; shunt_data.shunt_voltage = shunt_voltage; From 6364becbe24fc8fe98714fe8f2b184eaece814f2 Mon Sep 17 00:00:00 2001 From: tobimai Date: Thu, 20 Mar 2025 21:08:30 +0100 Subject: [PATCH 3/7] Fixed library --- platformio.ini | 4 ++-- src/main.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index 608c99e..408d44d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,7 +22,7 @@ lib_deps = fetchOTA INA233 ESP32Ping - https://github.com/x821938/elog + https://github.com/tobimai/elog.git#fix-syslog board_build.partitions = default.csv upload_protocol = espota upload_port = 192.168.5.205 @@ -39,7 +39,7 @@ lib_deps = bblanchon/ArduinoJson@^6.21.3 jsc/ArduinoLog ESP32Ping - https://github.com/x821938/elog + https://github.com/tobimai/elog.git#fix-syslog board_build.partitions = default.csv upload_protocol = espota upload_port = 192.168.4.18 diff --git a/src/main.cpp b/src/main.cpp index 2baa92e..ec8a34c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -223,8 +223,8 @@ void setup() } logger.log(0, DEBUG, "Getting time now"); - // Configure time with a GMT offset and daylight offset in seconds. - configTime(3600, 0, "pool.ntp.org"); + // Configure time to UTC + configTime(0, 0, "pool.ntp.org"); // Wait until a valid time is obtained (time > 8 hours in seconds) time_t now = time(NULL); From f16659c25c3f54af582bac5d7c0776d4542dab7e Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Thu, 20 Mar 2025 21:08:37 +0100 Subject: [PATCH 4/7] Changes --- lib/INA233/INA233.cpp | 27 +++++++++++++++++++++++++++ lib/INA233/INA233.h | 23 ++++++++++++----------- src/sensor/sensor.cpp | 3 +++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/INA233/INA233.cpp b/lib/INA233/INA233.cpp index fcde6db..4f6cb9e 100644 --- a/lib/INA233/INA233.cpp +++ b/lib/INA233/INA233.cpp @@ -23,6 +23,33 @@ uint16_t get_word(uint8_t address, uint8_t reg) { return (data[1] << 8) | data[0]; } +char* INA233::print_device_number() { + char data[6]; + int16_t rawVoltage; + + // Request data from the PMBus device + Wire.beginTransmission(INA233::_address); + Wire.write(0x9A); + Wire.endTransmission(); + Wire.requestFrom(INA233::_address, 6); + + if (Wire.available() == 6) { + data[0] = Wire.read(); + data[1] = Wire.read(); + data[2] = Wire.read(); + data[3] = Wire.read(); + data[4] = Wire.read(); + data[5] = Wire.read(); + } else { + // return 0 on error + data[0] = '\0'; + return data; + } + + // Combine the two bytes into a single 16-bit value + return data; +} + void sendWord(uint8_t deviceAddress, uint8_t registerAddress, uint16_t value) { Wire.beginTransmission(deviceAddress); diff --git a/lib/INA233/INA233.h b/lib/INA233/INA233.h index bb480a5..f9c79c7 100644 --- a/lib/INA233/INA233.h +++ b/lib/INA233/INA233.h @@ -32,20 +32,21 @@ enum ConversionTime { class INA233{ public: - INA233(uint8_t addr, TwoWire *wire = &Wire); - bool begin(const uint8_t sda, const uint8_t scl); + char* print_device_number(); + INA233(uint8_t addr, TwoWire* wire = &Wire); + bool begin(const uint8_t sda, const uint8_t scl); - float getBusVoltage(void); - float getShuntVoltage_mV(void); - float getShuntVoltage(void); + float getBusVoltage(void); + float getShuntVoltage_mV(void); + float getShuntVoltage(void); - void setAveragingMode(AveragingMode); - void setBusVoltageConversionTime(ConversionTime); - void setShuntVoltageConversionTime(ConversionTime); - - uint16_t getConfigRegister(); + void setAveragingMode(AveragingMode); + void setBusVoltageConversionTime(ConversionTime); + void setShuntVoltageConversionTime(ConversionTime); - bool isConnected(void); + uint16_t getConfigRegister(); + + bool isConnected(void); private: float _current_LSB; diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index 36c6117..bdf6e78 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -40,6 +40,9 @@ void read_sensor_task(void* parameter) while (true) { // Get Values from sensor + char* chip_id = ina_sensor.print_device_number(0x40); + logger.log(MYLOG, ERROR, "Chip Model: %s", chip_id); + float bus_voltage = ina_sensor.getBusVoltage(); float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value; From 2fa4b0761b4cdaa77ad18e29e1d9df95e5c131ba Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Thu, 20 Mar 2025 22:33:22 +0100 Subject: [PATCH 5/7] FAR nicer logging --- lib/INA233/INA233.cpp | 20 ++++----- lib/INA233/INA233.h | 2 +- src/external_interfacing/leds.cpp | 9 ++-- src/main.cpp | 71 ++++++++++++++++--------------- src/networking/networking.cpp | 17 ++++---- src/sensor/sensor.cpp | 22 ++++++---- src/tools/log.h | 11 +++++ src/tools/tools.cpp | 27 ++++++------ 8 files changed, 98 insertions(+), 81 deletions(-) create mode 100644 src/tools/log.h diff --git a/lib/INA233/INA233.cpp b/lib/INA233/INA233.cpp index 4f6cb9e..9183374 100644 --- a/lib/INA233/INA233.cpp +++ b/lib/INA233/INA233.cpp @@ -23,31 +23,29 @@ uint16_t get_word(uint8_t address, uint8_t reg) { return (data[1] << 8) | data[0]; } -char* INA233::print_device_number() { - char data[6]; - int16_t rawVoltage; +String INA233::print_device_number() { + char data[7]; // Array size includes space for the null terminator // Request data from the PMBus device Wire.beginTransmission(INA233::_address); - Wire.write(0x9A); + Wire.write(0x9A); // Command to read the manufacturer ID Wire.endTransmission(); - Wire.requestFrom(INA233::_address, 6); + Wire.requestFrom(INA233::_address, 7); - if (Wire.available() == 6) { + if (Wire.available() == 7) { + uint8_t i = Wire.read(); data[0] = Wire.read(); data[1] = Wire.read(); data[2] = Wire.read(); data[3] = Wire.read(); data[4] = Wire.read(); data[5] = Wire.read(); + data[6] = '\0'; // Null-terminate the string } else { - // return 0 on error - data[0] = '\0'; - return data; + return String(""); } - // Combine the two bytes into a single 16-bit value - return data; + return String(data); } diff --git a/lib/INA233/INA233.h b/lib/INA233/INA233.h index f9c79c7..e4931a7 100644 --- a/lib/INA233/INA233.h +++ b/lib/INA233/INA233.h @@ -32,7 +32,7 @@ enum ConversionTime { class INA233{ public: - char* print_device_number(); + String print_device_number(); INA233(uint8_t addr, TwoWire* wire = &Wire); bool begin(const uint8_t sda, const uint8_t scl); diff --git a/src/external_interfacing/leds.cpp b/src/external_interfacing/leds.cpp index 9dc00b9..6f94ff0 100644 --- a/src/external_interfacing/leds.cpp +++ b/src/external_interfacing/leds.cpp @@ -2,6 +2,7 @@ #include #include #include "../tools/tools.h" +#include extern ActiveErrors active_errors; extern WaterData water_data; @@ -75,19 +76,19 @@ void display_task(void* parameter) // We have an error, display error code for 3 seconds and then water level for 3 seconds if (active_errors.voltage_low) { display_error_code(1); - logger.log(0, WARNING, "Sensor Error - Voltage low"); + LOG(WARNING, "Sensor Error - Voltage low"); delay(3000); } else if (active_errors.voltage_high) { display_error_code(2); - logger.log(0, WARNING, "Sensor Error - Voltage High"); + LOG(WARNING, "Sensor Error - Voltage High"); delay(3000); } else if (active_errors.current_low) { display_error_code(3); - logger.log(0, WARNING, "Sensor Error - Current low"); + LOG(WARNING, "Sensor Error - Current low"); delay(3000); } else if (active_errors.current_high) { display_error_code(4); - logger.log(0, WARNING, "Sensor Error - Current high"); + LOG(WARNING, "Sensor Error - Current high"); delay(3000); } else { delay(3000); diff --git a/src/main.cpp b/src/main.cpp index ec8a34c..f21cfe2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,7 @@ #include #include #include "time.h" +#include "tools/log.h" #define MYLOG 0 @@ -51,7 +52,7 @@ void setup() prefs.begin("waterlevel", false); Serial.begin(115200); - logger.log(0, DEBUG, "Init LEDs"); + LOG(DEBUG, "Init LEDs"); pinMode(LED_1, OUTPUT); pinMode(LED_2, OUTPUT); pinMode(LED_3, OUTPUT); @@ -63,7 +64,7 @@ void setup() display_error_code(17); init_sensor(); - logger.log(0, DEBUG, "Beginning SPIFFS"); + LOG(DEBUG, "Beginning SPIFFS"); SPIFFS.begin(true); // Read the current SPIFFS version from the versions file on the spiffs @@ -72,21 +73,21 @@ void setup() Serial.println("Failed to open version file for reading"); } else { String version = file.readStringUntil('\n'); - logger.log(0, DEBUG, "Version: %s", version); + LOG(DEBUG, "Version: %s", version); current_spiffs_version = parseVersion(version.c_str()); - logger.log(0, DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch); + LOG(DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch); } - logger.log(0, DEBUG, "SPIFFS initialized"); + LOG(DEBUG, "SPIFFS initialized"); display_error_code(19); - logger.log(0, DEBUG, "Begin INA"); + LOG(DEBUG, "Begin INA"); display_error_code(22); /////////////////////////////// ROUTES /////////////////////////////// - logger.log(0, DEBUG, "Route Setup"); + LOG(DEBUG, "Route Setup"); // Normal HTML stuff server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/status.html", "text/html", false, processor); }); @@ -105,7 +106,7 @@ void setup() // For settings SSID if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) { - logger.log(0, DEBUG, "Updating SSID config"); + LOG(DEBUG, "Updating SSID config"); AsyncWebParameter* ssid_param = request->getParam(ssid_key, true); AsyncWebParameter* password_param = request->getParam(wifi_password_key, true); prefs.putString(ssid_key, ssid_param->value().c_str()); @@ -121,7 +122,7 @@ void setup() int params = request->params(); if (request->hasParam(level_sensor_range_key, true) && request->hasParam(water_level_min_key, true) && request->hasParam(water_level_max_key, true) && request->hasParam(water_volume_key, true)) { - logger.log(0, DEBUG, "Updating Sensor config"); + LOG(DEBUG, "Updating Sensor config"); AsyncWebParameter* range_param = request->getParam(level_sensor_range_key, true); AsyncWebParameter* level_min_param = request->getParam(water_level_min_key, true); AsyncWebParameter* level_max_param = request->getParam(water_level_max_key, true); @@ -143,24 +144,24 @@ void setup() // Convert the C string to a float using strtod float value = strtod(paramCStr, &endPtr); - logger.log(0, DEBUG, "range_float:%D:", range_float); + LOG(DEBUG, "range_float:%D:", range_float); prefs.putFloat(level_sensor_range_key, range_float); prefs.putFloat(water_level_min_key, level_min_float); prefs.putFloat(water_level_max_key, level_max_float); prefs.putFloat(water_volume_key, liters_float); - logger.log(0, DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0)); + LOG(DEBUG, "range_float_after:%D:", prefs.getFloat(level_sensor_range_key, -1.0)); } else { - logger.log(0, DEBUG, "!!!! FAIL lo"); + LOG(DEBUG, "!!!! FAIL lo"); for (int i = 0; i < params; i++) { AsyncWebParameter* p = request->getParam(i); if (p->isFile()) { // p->isPost() is also true - logger.log(0, DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + LOG(DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); } else if (p->isPost()) { - logger.log(0, DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + LOG(DEBUG, "POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); } else { - logger.log(0, DEBUG, "GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); + LOG(DEBUG, "GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); } } request->send(400, "text/plain", "Missing parameters"); // TODO add proper error messages @@ -180,7 +181,7 @@ void setup() display_error_code(24); - logger.log(0, DEBUG, "OTA Setup"); + LOG(DEBUG, "OTA Setup"); ArduinoOTA .onStart([]() { String type; @@ -190,16 +191,16 @@ void setup() type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() - logger.log(0, DEBUG, "Start updating %s", type); }) - .onEnd([]() { logger.log(0, DEBUG, "\nEnd"); }) + LOG(DEBUG, "Start updating %s", type); }) + .onEnd([]() { LOG(DEBUG, "\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }) .onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); - if (error == OTA_AUTH_ERROR) logger.log(0, DEBUG, "Auth Failed"); - else if (error == OTA_BEGIN_ERROR) logger.log(0, DEBUG, "Begin Failed"); - else if (error == OTA_CONNECT_ERROR) logger.log(0, DEBUG, "Connect Failed"); - else if (error == OTA_RECEIVE_ERROR) logger.log(0, DEBUG, "Receive Failed"); - else if (error == OTA_END_ERROR) logger.log(0, DEBUG, "End Failed"); }); + if (error == OTA_AUTH_ERROR) LOG(DEBUG, "Auth Failed"); + else if (error == OTA_BEGIN_ERROR) LOG(DEBUG, "Begin Failed"); + else if (error == OTA_CONNECT_ERROR) LOG(DEBUG, "Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) LOG(DEBUG, "Receive Failed"); + else if (error == OTA_END_ERROR) LOG(DEBUG, "End Failed"); }); display_error_code(26); digitalWrite(LED_RED, 0); @@ -209,20 +210,20 @@ void setup() xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL); // Create Etnernet task and wait a second to see if there is connection - logger.log(0, DEBUG, "Started Ethernet, waiting"); + LOG(DEBUG, "Started Ethernet, waiting"); delay(2000); if (ETH.linkUp()){ - logger.log(0, DEBUG, "Ethernet connected, starting update checker"); + LOG(DEBUG, "Ethernet connected, starting update checker"); xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); } else { - logger.log(0, DEBUG, "Ethernet not connected, starting update checker and WiFi Task"); + LOG(DEBUG, "Ethernet not connected, starting update checker and WiFi Task"); xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL); xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL); delay(2000); } - logger.log(0, DEBUG, "Getting time now"); + LOG(DEBUG, "Getting time now"); // Configure time to UTC configTime(0, 0, "pool.ntp.org"); @@ -244,26 +245,26 @@ void setup() // Sanity check: Ensure the year is at least 2020. int currentYear = timeinfo.tm_year + 1900; // tm_year is years since 1900 if (currentYear < 2020) { - logger.log(0, DEBUG, "Time not set properly: "); + LOG(DEBUG, "Time not set properly: "); } else { - logger.log(0, DEBUG, "Time is valid: %s", asctime(&timeinfo)); + LOG(DEBUG, "Time is valid: %s", asctime(&timeinfo)); } // Setup syslog logger.configureSyslog("192.168.6.11", 5514, "esp32"); logger.registerSyslog(MYLOG, DEBUG, FAC_USER, "waterlevel"); - logger.log(MYLOG, ERROR, "Here is an error message, error code: %d", 17); + LOG(ERROR, "Here is an error message, error code: %d", 17); - logger.log(0, DEBUG, "Starting webserver"); + LOG(DEBUG, "Starting webserver"); server.begin(); ArduinoOTA.begin(); display_error_code(25); xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL); - xTaskCreate(read_sensor_task, "ReadSensorTask", 2048, NULL, 1, NULL); + xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, NULL, 1, NULL); xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL); // Wait until there is network connection @@ -271,13 +272,13 @@ void setup() if (WiFi.status() == WL_CONNECTED || ETH.localIP()) { int pingResult = Ping.ping("8.8.8.8"); // Use Google's public DNS server as a test IP if (pingResult >= 0) { - logger.log(0, DEBUG, "Network connection established"); + LOG(DEBUG, "Network connection established"); break; } else { - logger.log(0, DEBUG, "Network not ready, retrying..."); + LOG(DEBUG, "Network not ready, retrying..."); } } else { - logger.log(0, DEBUG, "No WiFi or Ethernet connection, retrying..."); + LOG(DEBUG, "No WiFi or Ethernet connection, retrying..."); } delay(1000); // Delay to prevent rapid retry } diff --git a/src/networking/networking.cpp b/src/networking/networking.cpp index 510b626..a4e74a6 100644 --- a/src/networking/networking.cpp +++ b/src/networking/networking.cpp @@ -4,6 +4,7 @@ #include "../global_data/defines.h" #include #include "../global_data/global_data.h" +#include int64_t mac_address = ESP.getEfuseMac(); uint8_t failed_connection_attempts = 0; @@ -34,21 +35,21 @@ const char * get_hostname(HostnameType host_type) { void wifi_task(void* parameter) { - logger.log(0, DEBUG, "Starting WiFi Task"); + LOG(DEBUG, "Starting WiFi Task"); WiFi.setHostname(get_hostname(Wireless)); while (true) { if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) { wifi_data.link = false; if (failed_connection_attempts > 5) { - logger.log(0, DEBUG, "Failed to connecto to currently saved SSID, starting SoftAP"); + LOG(DEBUG, "Failed to connecto to currently saved SSID, starting SoftAP"); } else { - logger.log(0, DEBUG, "No SSID saved, starting SoftAP"); + LOG(DEBUG, "No SSID saved, starting SoftAP"); } String ap_ssid = get_hostname(Wireless); WiFi.softAP(ap_ssid, ""); - logger.log(0, DEBUG, "[WIFI_TASK] Waiting for SSID now..."); + LOG(DEBUG, "[WIFI_TASK] Waiting for SSID now..."); String old_ssid = prefs.getString(ssid_key, "xxx"); while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) { @@ -64,11 +65,11 @@ void wifi_task(void* parameter) wifi_data.network_name = WiFi.SSID(); wifi_data.ip_address = WiFi.localIP().toString(); - // logger.log(0, DEBUG, "RSSI: %F, IP Address, %p, SSID: %s", float(WiFi.RSSI()), WiFi.localIP(), prefs.getString(ssid_key, "NOSSID")); + // LOG(DEBUG, "RSSI: %F, IP Address, %p, SSID: %s", float(WiFi.RSSI()), WiFi.localIP(), prefs.getString(ssid_key, "NOSSID")); // Serial.println(WiFi.channel()); delay(5000); } else { - logger.log(0, DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); + LOG(DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); WiFi.mode(WIFI_STA); WiFi.begin(prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, "")); failed_connection_attempts++; @@ -80,14 +81,14 @@ void wifi_task(void* parameter) void ethernet_task(void* parameter) { - logger.log(0, DEBUG, "Connecting Ethernet"); + LOG(DEBUG, "Connecting Ethernet"); ETH.begin(0, 17, 23, 18); ETH.setHostname(get_hostname(Ethernet)); while (true) { ethernet_data.link = ETH.linkUp(); ethernet_data.rssi = ETH.linkSpeed(); ethernet_data.ip_address = ETH.localIP().toString(); - // logger.log(0, DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ethernet_data.ip_address, String(ethernet_data.link)); + // LOG(DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ethernet_data.ip_address, String(ethernet_data.link)); delay(60 * 1000); } } \ No newline at end of file diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index bdf6e78..913ecc2 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -12,6 +12,10 @@ INA226 ina_sensor(0x40); #include INA233 ina_sensor(0x40); #endif +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" extern Preferences prefs; extern WaterData water_data; @@ -40,13 +44,13 @@ void read_sensor_task(void* parameter) while (true) { // Get Values from sensor - char* chip_id = ina_sensor.print_device_number(0x40); - logger.log(MYLOG, ERROR, "Chip Model: %s", chip_id); + String chip_id = ina_sensor.print_device_number(); + LOG(DEBUG, "Chip Model: %s", chip_id.c_str()); float bus_voltage = ina_sensor.getBusVoltage(); float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value; - logger.log(0, DEBUG, "RAW Shunt voltage: %F mV", ina_sensor.getShuntVoltage_mV()); + LOG(DEBUG, "RAW Shunt voltage: %F mV", ina_sensor.getShuntVoltage_mV()); float shunt_current = shunt_voltage / RESISTOR_VALUE; @@ -66,8 +70,8 @@ void read_sensor_task(void* parameter) float min_water_level_mA = 4 + min_water_level_mA_over_zero; float max_water_level_mA = 4 + max_water_level_mA_over_zero; - logger.log(0, DEBUG, "max_water_level_mA: %F", max_water_level_mA); - logger.log(0, DEBUG, "min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); + LOG(DEBUG, "max_water_level_mA: %F", max_water_level_mA); + LOG(DEBUG, "min_water_level_mA_over_zero: %F", min_water_level_mA_over_zero); // Current over the 0 level of the water float shunt_current_over_zero = shunt_current - min_water_level_mA; @@ -87,10 +91,10 @@ void read_sensor_task(void* parameter) active_errors.current_high = shunt_current > 20.2; active_errors.voltage_low = bus_voltage < 23; active_errors.voltage_high = bus_voltage > 25; - logger.log(0, DEBUG, "Shunt current: %F", shunt_current); - logger.log(0, DEBUG, "Shunt voltage: %F", shunt_voltage); - logger.log(0, DEBUG, "Bus voltage: %F", bus_voltage); - logger.log(0, DEBUG, "cm_over_zero: %F", cm_over_zero); + LOG(DEBUG, "Shunt current: %F", shunt_current); + LOG(DEBUG, "Shunt voltage: %F", shunt_voltage); + LOG(DEBUG, "Bus voltage: %F", bus_voltage); + LOG(DEBUG, "cm_over_zero: %F", cm_over_zero); shunt_data.bus_voltage = bus_voltage; shunt_data.shunt_voltage = shunt_voltage; diff --git a/src/tools/log.h b/src/tools/log.h new file mode 100644 index 0000000..3f46fef --- /dev/null +++ b/src/tools/log.h @@ -0,0 +1,11 @@ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#define LOG(LEVEL, FMT, ...) \ + do { \ + logger.log(0, LEVEL, "[Core: %i][Task: %s][%s] " FMT, \ + xPortGetCoreID(), \ + pcTaskGetName(xTaskGetCurrentTaskHandle()), \ + __FUNCTION__, \ + ##__VA_ARGS__); \ + } while (0) diff --git a/src/tools/tools.cpp b/src/tools/tools.cpp index a24b8e2..8850a44 100644 --- a/src/tools/tools.cpp +++ b/src/tools/tools.cpp @@ -5,6 +5,7 @@ #include #include #include +#include "log.h" extern Preferences prefs; extern OTAStatus ota_status; @@ -35,12 +36,12 @@ String processor(const String& var) // OTA Callbacks void update_started() { - logger.log(0, DEBUG, "OTA Update started"); + LOG(DEBUG, "OTA Update started"); ota_status.update_progress = 0; } void update_finished() { - logger.log(0, DEBUG, "OTA Update finished"); + LOG(DEBUG, "OTA Update finished"); ota_status.update_progress = -1; webSocket.textAll(String(-1).c_str()); } @@ -49,14 +50,14 @@ void update_progress(int cur, int total) { ota_status.update_progress = 0; if (cur != 0 ) { ota_status.update_progress = int(float(cur)/float(total)*100); - logger.log(0, DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress); + LOG(DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress); } webSocket.textAll(String(ota_status.update_progress).c_str()); - logger.log(0, DEBUG, "OTA Update progress: %d/%d", cur, total); + LOG(DEBUG, "OTA Update progress: %d/%d", cur, total); } void update_error(int err) { - logger.log(0, ERROR, "OTA Update error: %d", err); + LOG(ERROR, "OTA Update error: %d", err); ota_status.update_progress = -2; } @@ -75,20 +76,20 @@ void check_update_task(void* parameter) { // If there is a SPIFSS update it will be ran automatically, as SPIFFS is necessary for the firmware to run Firmware latest_spiff_version = spiffs_fs.getLatestVersionOnServer(); - logger.log(0, DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch); + LOG(DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch); if (latest_spiff_version.valid) { if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) { // If Required SPIFFS version is newer than current version, update - logger.log(0, DEBUG, "New SPIFFS version, running update now"); + LOG(DEBUG, "New SPIFFS version, running update now"); run_ota_spiffs_update(latest_spiff_version.url, update_started, update_finished, update_progress, update_error); // Reboot just to be safe ESP.restart(); } else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_spiff_version.version)) { // If Server has new SPIFFS version but it's not required - logger.log(0, DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); + LOG(DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch); } else { - logger.log(0, DEBUG, "No new SPIFFS version available"); + LOG(DEBUG, "No new SPIFFS version available"); } } @@ -96,11 +97,11 @@ void check_update_task(void* parameter) { while (true) { Firmware fw = ota.getLatestVersionOnServer(); if (fw.valid) { - logger.log(0, DEBUG, "New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); + LOG(DEBUG, "New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch); ota_status.latest_version = fw.version; ota_status.update_url = fw.url; if (isVersionNewer(current_software_version, fw.version)) { - logger.log(0, DEBUG, "Remote version is newer than current version"); + LOG(DEBUG, "Remote version is newer than current version"); ota_status.update_available = true; } else { ota_status.update_available = false; @@ -111,7 +112,7 @@ void check_update_task(void* parameter) { ota_status.update_available = false; } - logger.log(0, DEBUG, "No new firmware available"); + LOG(DEBUG, "No new firmware available"); } delay(1000 * 60 * 1); } @@ -121,7 +122,7 @@ void check_update_task(void* parameter) { void run_ota_update_task(void* parameter) { TaskArgs_t *args = (TaskArgs_t *) parameter; - logger.log(0, DEBUG, "Running OTA upgrade now with URL: %s", args->ota_status.update_url.c_str()); + LOG(DEBUG, "Running OTA upgrade now with URL: %s", args->ota_status.update_url.c_str()); run_ota_update(args->ota_status.update_url, update_started, update_finished, update_progress, update_error); vTaskDelete(NULL); } From 67e9ae1bca07a138274ca34e0c3d2b626744e010 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 23 Mar 2025 17:59:43 +0100 Subject: [PATCH 6/7] Fixed wrong shunt voltage reading --- lib/INA233/INA233.cpp | 27 ++++++++++++++++++++++++--- lib/INA233/INA233.h | 5 ++++- platformio.ini | 2 +- src/global_data/defines.h | 2 +- src/sensor/sensor.cpp | 3 ++- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/INA233/INA233.cpp b/lib/INA233/INA233.cpp index 9183374..df39a15 100644 --- a/lib/INA233/INA233.cpp +++ b/lib/INA233/INA233.cpp @@ -23,7 +23,7 @@ uint16_t get_word(uint8_t address, uint8_t reg) { return (data[1] << 8) | data[0]; } -String INA233::print_device_number() { +String INA233::get_device_model() { char data[7]; // Array size includes space for the null terminator // Request data from the PMBus device @@ -57,6 +57,13 @@ void sendWord(uint8_t deviceAddress, uint8_t registerAddress, uint16_t value) { Wire.endTransmission(); } +void sendByte(uint8_t deviceAddress, uint8_t registerAddress, uint8_t value) { + Wire.beginTransmission(deviceAddress); + Wire.write(registerAddress); // Send the register address + Wire.write((uint8_t)value); // Send the low byte first + Wire.endTransmission(); + } + INA233::INA233(const uint8_t address, TwoWire *wire) { _address = address; @@ -109,8 +116,18 @@ float INA233::getBusVoltage() { } float INA233::getShuntVoltage() { - uint16_t rawVoltage = get_word(INA233::_address, REGISTER_READ_VSHUNT); - float voltage = rawVoltage * pow(10,-4); + int16_t rawVoltage = get_word(INA233::_address, REGISTER_READ_VSHUNT); // Read raw voltage (16-bit register) + + // Convert from two's complement to an integer value + // If the MSB (most significant bit) is set, the number is negative + if (rawVoltage & 0x8000) { // Check if MSB (bit 15) is set + rawVoltage = rawVoltage - 0x10000; // Convert two's complement negative number + } + + // Convert value according to the datasheet + // LSB: 2.5 μV per bit -> Multiply by 2.5e-6 to convert to volts + float voltage = rawVoltage * 2.5e-6; + return voltage; } @@ -174,4 +191,8 @@ void INA233::setShuntVoltageConversionTime(ConversionTime conversion_time) { uint16_t INA233::getConfigRegister() { return get_word(INA233::_address, REGISTER_CONFIGURATION); +} + +void INA233::reset() { + sendByte(INA233::_address, 0x12, 0x00); } \ No newline at end of file diff --git a/lib/INA233/INA233.h b/lib/INA233/INA233.h index e4931a7..962a05d 100644 --- a/lib/INA233/INA233.h +++ b/lib/INA233/INA233.h @@ -7,6 +7,7 @@ #define REGISTER_READ_VIN 0x88 #define REGISTER_READ_VSHUNT 0xD1 #define REGISTER_CONFIGURATION 0xD0 +#define REGISTER_READ_IIN 0x89 enum AveragingMode { averages_1 = B000, @@ -32,7 +33,7 @@ enum ConversionTime { class INA233{ public: - String print_device_number(); + String get_device_model(); INA233(uint8_t addr, TwoWire* wire = &Wire); bool begin(const uint8_t sda, const uint8_t scl); @@ -46,6 +47,8 @@ class INA233{ uint16_t getConfigRegister(); + void reset(); + bool isConnected(void); private: diff --git a/platformio.ini b/platformio.ini index 408d44d..c175f92 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,7 +25,7 @@ lib_deps = https://github.com/tobimai/elog.git#fix-syslog board_build.partitions = default.csv upload_protocol = espota -upload_port = 192.168.5.205 +upload_port = 10.1.10.8 build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE [env:ESP32_INA226] diff --git a/src/global_data/defines.h b/src/global_data/defines.h index e4e92d7..503d17d 100644 --- a/src/global_data/defines.h +++ b/src/global_data/defines.h @@ -7,7 +7,7 @@ #define water_level_min_key "water_level_min" #define water_level_max_key "water_level_max" #define water_volume_key "water_volume" -#define current_software_version Version{1, 0, 1} +#define current_software_version Version{1, 0, 0} #define REQUIRED_SPIFFS_VERSION Version{8, 0, 0} #define RESISTOR_VALUE 4 \ No newline at end of file diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index 913ecc2..55cabd3 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -33,6 +33,7 @@ void init_sensor(){ ina_sensor.setShuntVoltageConversionTime(7); ina_sensor.setAverage(4); #else + ina_sensor.reset(); ina_sensor.setShuntVoltageConversionTime(conversion_time_8244uS); ina_sensor.setBusVoltageConversionTime(conversion_time_8244uS); ina_sensor.setAveragingMode(averages_128); @@ -44,7 +45,7 @@ void read_sensor_task(void* parameter) while (true) { // Get Values from sensor - String chip_id = ina_sensor.print_device_number(); + String chip_id = ina_sensor.get_device_model(); LOG(DEBUG, "Chip Model: %s", chip_id.c_str()); float bus_voltage = ina_sensor.getBusVoltage(); From abbb8d918b2c7e622bb65038c63c5e3527cac3e0 Mon Sep 17 00:00:00 2001 From: tobimai Date: Sun, 23 Mar 2025 18:04:14 +0100 Subject: [PATCH 7/7] fix --- platformio.ini | 2 +- src/sensor/sensor.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index c175f92..33ce51f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,7 +25,7 @@ lib_deps = https://github.com/tobimai/elog.git#fix-syslog board_build.partitions = default.csv upload_protocol = espota -upload_port = 10.1.10.8 +upload_port = 10.1.60.43 build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE [env:ESP32_INA226] diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index 55cabd3..8a95059 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -44,9 +44,10 @@ void read_sensor_task(void* parameter) { while (true) { // Get Values from sensor - + #ifndef USE_INA226 String chip_id = ina_sensor.get_device_model(); LOG(DEBUG, "Chip Model: %s", chip_id.c_str()); + #endif float bus_voltage = ina_sensor.getBusVoltage(); float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value;