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