Greatly improved the wifi connection, ntp polling, and irc connection by adding retries and delays for syncing ntp which was causing tls errors when out of sync

This commit is contained in:
Dionysus 2024-05-26 00:57:34 -04:00
parent 9a5379eea2
commit 5df11770d6
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -396,14 +396,20 @@ bool connectToIRC() {
void connectToWiFi() {
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED) {
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 10) { // Try to connect for up to 10 seconds
delay(500);
displayCenteredText("CONNECTING TO " + ssid);
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
displayCenteredText("CONNECTED TO " + ssid);
delay(1000);
updateTimeFromNTP();
} else {
displayCenteredText("WIFI CONNECTION FAILED");
Serial.println("Failed to connect to WiFi.");
}
displayCenteredText("CONNECTED TO " + ssid);
delay(1000);
updateTimeFromNTP();
}
void sendIRC(String command) {
@ -798,11 +804,17 @@ uint16_t getColorFromPercentage(int rssi) {
void updateTimeFromNTP() {
configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
delay(2000); // Wait for NTP to sync
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
Serial.println(&timeinfo, "Time synchronized: %A, %B %d %Y %H:%M:%S");
} else {
Serial.println("Failed to synchronize time");
for (int i = 0; i < 10; ++i) { // Try up to 10 times
delay(2000);
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
Serial.println(&timeinfo, "Time synchronized: %A, %B %d %Y %H:%M:%S");
return;
} else {
Serial.println("Failed to synchronize time, retrying...");
}
}
Serial.println("Failed to synchronize time after multiple attempts.");
}