added support for ota and ntp

This commit is contained in:
Dan Priece
2026-05-19 10:37:51 -04:00
parent a29d0c5cfc
commit 2c5f532717
4 changed files with 138 additions and 26 deletions
+42 -23
View File
@@ -6,6 +6,9 @@
#include "ChronoReadingManager.h"
#include "ChronoWebServer.h"
#include "FileManager.h"
#include <WiFi.h>
#include <time.h>
#include <sys/time.h>
//testing mode variables
static bool testMode=false;
@@ -64,10 +67,13 @@ void setup()
FileManager& files = FileManager::getInstance();
files.init();
//initialize web portal
//initialize web portal (also connects to WiFi)
webServer.init();
webServer.setTestModeCallback(toggleTestMode);
//sync time via NTP after WiFi is connected
setDateTime();
//initialize display
Display::init();
@@ -83,32 +89,45 @@ void setup()
chronoBLE.setSpeedCallback(speedCallback);
}
void setDateTime(){
// Example: Set time to January 1, 2025, 12:00:00
struct tm t;
t.tm_year = 2025 - 1900; // Year - 1900
t.tm_mon = 7; // Month (0-11, where 0 = Jan)
t.tm_mday = 4; // Day of the month (1-31)
t.tm_hour = 14; // Hour (0-23)
t.tm_min = 56; // Minute (0-59)
t.tm_sec = 0; // Second (0-59)
t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown
// NTP server configuration
configTime("EST5EDT,M3.2.0/2,M11.1.0/2", "pool.ntp.org", "time.nist.gov");
time_t epoch_time = mktime(&t);
struct timeval tv;
tv.tv_sec = epoch_time;
tv.tv_usec = 0;
settimeofday(&tv, NULL);
// Verify the set time
// Wait for time to be set
time_t now;
char strftime_buf[64];
struct tm timeinfo;
int retry = 0;
const int maxRetry = 20; // 10 seconds max
time(&now);
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
while (retry < maxRetry) {
time(&now);
localtime_r(&now, &timeinfo);
// Check if we have a valid time (year > 2020)
if (timeinfo.tm_year > 120) { // 120 = year 2020
Serial.println("Time synced via NTP");
break;
}
Serial.print("Waiting for NTP sync... ");
Serial.println(retry);
delay(500);
retry++;
}
// If still not synced, fall back to a default date
if (timeinfo.tm_year <= 120) {
Serial.println("NTP sync failed, using fallback date");
timeinfo.tm_year = 2024 - 1900;
timeinfo.tm_mon = 0; // January
timeinfo.tm_mday = 1;
timeinfo.tm_hour = 0;
timeinfo.tm_min = 0;
timeinfo.tm_sec = 0;
timeinfo.tm_isdst = -1;
now = mktime(&timeinfo);
struct timeval tv = {.tv_sec = now};
settimeofday(&tv, NULL);
}
}
void updateTime(){
time_t now;