fixed time
This commit is contained in:
112
src/main.cpp
112
src/main.cpp
@@ -39,48 +39,41 @@ extern NetworkData ethernet_data;
|
||||
extern SensorData shunt_data;
|
||||
extern ActiveErrors active_errors;
|
||||
|
||||
extern "C" int rom_phy_get_vdd33();
|
||||
|
||||
Version current_spiffs_version;
|
||||
|
||||
AsyncWebServer server(80);
|
||||
// AsyncWebSocket webSocket("/webSocket");
|
||||
AsyncWebSocket webSocket("/webSocket");
|
||||
#define FORMAT_LITTLEFS_IF_FAILED true
|
||||
|
||||
void setup()
|
||||
{
|
||||
Logger.registerSerial(MYLOG, ELOG_LEVEL_DEBUG, "Serial");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Init LEDs");
|
||||
led_setup();
|
||||
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Init Starting prefs and Serial output");
|
||||
prefs.begin("waterlevel", false);
|
||||
Serial.begin(115200);
|
||||
|
||||
|
||||
delay(500);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Init Sensor");
|
||||
init_sensor();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Beginning SPIFFS");
|
||||
//SPIFFS.begin(FORMAT_LITTLEFS_IF_FAILED);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Beginning LittleFS");
|
||||
LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED);
|
||||
|
||||
// Read the current SPIFFS version from the versions file on the spiffs
|
||||
LOG(ELOG_LEVEL_DEBUG, "Reading LittleFS version");
|
||||
File file = LittleFS.open("/version", FILE_READ);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open version file for reading");
|
||||
LOG( ELOG_LEVEL_ERROR, "Failed opening LittleFS version file");
|
||||
} else {
|
||||
String version = file.readStringUntil('\n');
|
||||
LOG(ELOG_LEVEL_DEBUG, "Version: %s", version);
|
||||
current_spiffs_version = parseVersion(version.c_str());
|
||||
LOG(ELOG_LEVEL_DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Current LittleFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch);
|
||||
}
|
||||
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "SPIFFS initialized");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Begin INA");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "LittleFS initialized");
|
||||
|
||||
/////////////////////////////// ROUTES ///////////////////////////////
|
||||
LOG(ELOG_LEVEL_DEBUG, "Route Setup");
|
||||
@@ -98,8 +91,6 @@ void setup()
|
||||
|
||||
// API stuff - internal
|
||||
server.on("/update_wifi_credentials", HTTP_POST, [](AsyncWebServerRequest* request) {
|
||||
int params = request->params();
|
||||
|
||||
// For settings SSID
|
||||
if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Updating SSID config");
|
||||
@@ -134,12 +125,6 @@ void setup()
|
||||
float level_max_float = level_max_str.toFloat();
|
||||
float liters_float = liters_str.toFloat();
|
||||
|
||||
const char* paramCStr = range_str.c_str();
|
||||
char* endPtr;
|
||||
|
||||
// Convert the C string to a float using strtod
|
||||
float value = strtod(paramCStr, &endPtr);
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "range_float:%D:", range_float);
|
||||
|
||||
prefs.putFloat(level_sensor_range_key, range_float);
|
||||
@@ -166,10 +151,8 @@ void setup()
|
||||
});
|
||||
|
||||
setup_api_endpoints();
|
||||
// webSocket.onEvent(onWsEvent);
|
||||
// server.addHandler(&webSocket);
|
||||
|
||||
|
||||
webSocket.onEvent(onWsEvent);
|
||||
server.addHandler(&webSocket);
|
||||
|
||||
server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/chota.css", "text/css", false); });
|
||||
server.on("/gauge.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/gauge.js", "application/javascript", false); });
|
||||
@@ -178,14 +161,14 @@ void setup()
|
||||
LOG(ELOG_LEVEL_DEBUG, "OTA Setup");
|
||||
ArduinoOTA
|
||||
.onStart([]() {
|
||||
String type;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH)
|
||||
type = "sketch";
|
||||
else // U_SPIFFS
|
||||
type = "filesystem";
|
||||
String type;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH)
|
||||
type = "sketch";
|
||||
else // U_SPIFFS
|
||||
type = "filesystem";
|
||||
|
||||
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
||||
LOG(ELOG_LEVEL_DEBUG, "Start updating %s", type); })
|
||||
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
||||
LOG(ELOG_LEVEL_DEBUG, "Start updating %s", type); })
|
||||
.onEnd([]() { LOG(ELOG_LEVEL_DEBUG, "\nEnd"); })
|
||||
.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
|
||||
.onError([](ota_error_t error) {
|
||||
@@ -196,63 +179,20 @@ void setup()
|
||||
else if (error == OTA_RECEIVE_ERROR) LOG(ELOG_LEVEL_DEBUG, "Receive Failed");
|
||||
else if (error == OTA_END_ERROR) LOG(ELOG_LEVEL_DEBUG, "End Failed"); });
|
||||
|
||||
digitalWrite(LED_RED, 0);
|
||||
|
||||
// Starting bootup sequence
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting main tasks");
|
||||
xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL);
|
||||
xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL);
|
||||
|
||||
if (ETH.linkUp()){
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet connected, starting update checker");
|
||||
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet not connected, starting update checker and WiFi Task");
|
||||
// esp_netif_set_default_netif(esp_netif_get_handle_from_ifkey("ETH_DEF"));
|
||||
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Getting time now");
|
||||
// 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);
|
||||
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) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Time not set properly: ");
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Time is valid: %s", asctime(&timeinfo));
|
||||
}
|
||||
|
||||
// Setup syslog
|
||||
LOG(ELOG_LEVEL_ERROR, "Here is an error message, error code: %d", 17);
|
||||
|
||||
|
||||
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
||||
server.begin();
|
||||
ArduinoOTA.begin();
|
||||
|
||||
// xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, NULL, 1, NULL);
|
||||
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL);
|
||||
xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL);
|
||||
xTaskCreate(get_time_task, "GetTimeTask", 1024 * 4, NULL, 1, NULL);
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
||||
server.begin();
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler");
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user