Separate logic further into functions, allowing reconnection to WiFi

This commit is contained in:
x 2024-06-20 12:48:04 +02:00
parent 47892039b7
commit 6b8535d211

View file

@ -19,10 +19,29 @@ bool testHTTP() {
bool uplinkWorks() { return testHTTP(); }
void setup() {
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT);
}
Serial.print("Connecting to WiFi");
void testInternet() {
if (uplinkWorks()) {
Serial.println("Internet is up");
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(RELAY, HIGH);
} else {
Serial.println("Internet is down");
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(RELAY, LOW);
}
}
void ensureWiFiConnection() {
wl_status_t status = WiFi.status();
if(status == WL_CONNECTED) return;
else {
Serial.print("WiFi is disconnected, now connecting");
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
@ -31,20 +50,10 @@ void setup() {
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT);
}
void loop() {
if (uplinkWorks()) {
Serial.println("Network is up");
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(RELAY, HIGH);
} else {
Serial.println("Network is down");
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(RELAY, LOW);
}
}
void loop() {
ensureWiFiConnection();
testInternet();
delay(5000); // TODO: cleaner interval between tests
}