Separate logic further into functions, allowing reconnection to WiFi
This commit is contained in:
parent
47892039b7
commit
6b8535d211
1 changed files with 24 additions and 15 deletions
39
src/main.cpp
39
src/main.cpp
|
@ -19,32 +19,41 @@ bool testHTTP() {
|
||||||
bool uplinkWorks() { return testHTTP(); }
|
bool uplinkWorks() { return testHTTP(); }
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
Serial.print("Connecting to WiFi");
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
Serial.print("Connected, IP address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
|
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
pinMode(RELAY, OUTPUT);
|
pinMode(RELAY, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void testInternet() {
|
||||||
if (uplinkWorks()) {
|
if (uplinkWorks()) {
|
||||||
Serial.println("Network is up");
|
Serial.println("Internet is up");
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
digitalWrite(RELAY, HIGH);
|
digitalWrite(RELAY, HIGH);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Network is down");
|
Serial.println("Internet is down");
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
digitalWrite(RELAY, 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(".");
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
Serial.print("Connected, IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void loop() {
|
||||||
|
ensureWiFiConnection();
|
||||||
|
testInternet();
|
||||||
delay(5000); // TODO: cleaner interval between tests
|
delay(5000); // TODO: cleaner interval between tests
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue