Compare commits

..

No commits in common. "24fd856584f4a9cde6de6012f466785ccd7a8c25" and "f12fbafc4774b3dadbcd2ff3915dd141ff70e67c" have entirely different histories.

View file

@ -1,5 +1,4 @@
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#define RELAY 4 // GPIO4
@ -8,86 +7,48 @@
#define HTTP_HOST "google.com"
#define HTTP_PORT (80)
// ----- //
// Setup //
// ----- //
void ensureWiFiConnection();
void relayOn();
void relayOff();
void setupOTA()
{
ArduinoOTA.onStart([]() {
String type;
if(ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
bool testHTTP() {
WiFiClient client;
client.setNoDelay(true);
bool available = client.connect(HTTP_HOST, HTTP_PORT);
client.stop();
return available;
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); });
ArduinoOTA.onProgress(
[](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); });
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if(error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if(error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if(error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if(error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if(error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}
// Returns true if Internet works
bool uplinkWorks() { return testHTTP(); }
void setup()
{
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT);
relayOn();
delay(2000);
relayOff();
ensureWiFiConnection();
setupOTA();
}
// ----- //
// Relay //
// ----- //
void relayOn()
{
void relayOn() {
Serial.println("Lights on");
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(RELAY, HIGH);
}
void relayOff()
{
void relayOff() {
Serial.println("Lights off");
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(RELAY, LOW);
}
// --------------- //
// Network Testing //
// --------------- //
void testInternet() {
if (uplinkWorks()) {
Serial.println("Internet is up");
relayOn();
} else {
Serial.println("Internet is down");
relayOff();
}
}
void ensureWiFiConnection()
{
void ensureWiFiConnection() {
wl_status_t status = WiFi.status();
if(status == WL_CONNECTED)
return;
if(status == WL_CONNECTED) return;
else {
relayOff();
Serial.print("WiFi is disconnected, now connecting");
@ -102,37 +63,8 @@ void ensureWiFiConnection()
Serial.println(WiFi.localIP());
}
}
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();
testInternet();
for(size_t i = 0; i < 5; i++) {
ArduinoOTA.handle();
delay(1000);
}
delay(5000);
}