Reordering functions

This commit is contained in:
x 2024-06-20 21:05:54 +02:00
parent 68914621ee
commit 3886966bf7

View file

@ -8,18 +8,13 @@
#define HTTP_HOST "google.com"
#define HTTP_PORT (80)
// ----- //
// Setup //
// ----- //
void ensureWiFiConnection();
bool testHTTP() {
WiFiClient client;
client.setNoDelay(true);
bool available = client.connect(HTTP_HOST, HTTP_PORT);
client.stop();
return available;
}
// Returns true if Internet works
bool uplinkWorks() { return testHTTP(); }
void relayOn();
void relayOff();
void setupOTA() {
ArduinoOTA.onStart([]() {
@ -60,10 +55,17 @@ void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT);
relayOn();
delay(2000);
relayOff();
ensureWiFiConnection();
setupOTA();
}
// ----- //
// Relay //
// ----- //
void relayOn() {
Serial.println("Lights on");
digitalWrite(LED_BUILTIN, HIGH);
@ -76,15 +78,9 @@ void relayOff() {
digitalWrite(RELAY, LOW);
}
void testInternet() {
if (uplinkWorks()) {
Serial.println("Internet is up");
relayOn();
} else {
Serial.println("Internet is down");
relayOff();
}
}
// --------------- //
// Network Testing //
// --------------- //
void ensureWiFiConnection() {
wl_status_t status = WiFi.status();
@ -104,6 +100,28 @@ void ensureWiFiConnection() {
}
}
bool pingHTTP() {
WiFiClient client;
client.setNoDelay(true);
bool available = client.connect(HTTP_HOST, HTTP_PORT);
client.stop();
return available;
}
// ---- //
// Loop //
// ---- //
void testInternet() {
if (pingHTTP()) {
Serial.println("Internet is up");
relayOn();
} else {
Serial.println("Internet is down");
relayOff();
}
}
void loop() {
ensureWiFiConnection();
testInternet();