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_HOST "google.com"
#define HTTP_PORT (80) #define HTTP_PORT (80)
// ----- //
// Setup //
// ----- //
void ensureWiFiConnection(); void ensureWiFiConnection();
void relayOn();
bool testHTTP() { void relayOff();
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 setupOTA() { void setupOTA() {
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
@ -60,10 +55,17 @@ void setup() {
Serial.begin(9600); Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT); pinMode(RELAY, OUTPUT);
relayOn();
delay(2000);
relayOff();
ensureWiFiConnection(); ensureWiFiConnection();
setupOTA(); setupOTA();
} }
// ----- //
// Relay //
// ----- //
void relayOn() { void relayOn() {
Serial.println("Lights on"); Serial.println("Lights on");
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
@ -76,15 +78,9 @@ void relayOff() {
digitalWrite(RELAY, LOW); digitalWrite(RELAY, LOW);
} }
void testInternet() { // --------------- //
if (uplinkWorks()) { // Network Testing //
Serial.println("Internet is up"); // --------------- //
relayOn();
} else {
Serial.println("Internet is down");
relayOff();
}
}
void ensureWiFiConnection() { void ensureWiFiConnection() {
wl_status_t status = WiFi.status(); 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() { void loop() {
ensureWiFiConnection(); ensureWiFiConnection();
testInternet(); testInternet();