Reordering functions
This commit is contained in:
parent
68914621ee
commit
3886966bf7
1 changed files with 38 additions and 20 deletions
58
src/main.cpp
58
src/main.cpp
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue