This commit is contained in:
71
src/main.cpp
71
src/main.cpp
@@ -26,6 +26,7 @@
|
||||
#include <fetchOTA.h>
|
||||
#include <ESP32Ping.h>
|
||||
#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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user