Compare commits
3 commits
f12fbafc47
...
24fd856584
Author | SHA1 | Date | |
---|---|---|---|
24fd856584 | |||
3886966bf7 | |||
68914621ee |
1 changed files with 110 additions and 42 deletions
152
src/main.cpp
152
src/main.cpp
|
@ -1,4 +1,5 @@
|
|||
#include <Arduino.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#define RELAY 4 // GPIO4
|
||||
|
@ -7,7 +8,103 @@
|
|||
#define HTTP_HOST "google.com"
|
||||
#define HTTP_PORT (80)
|
||||
|
||||
bool testHTTP() {
|
||||
// ----- //
|
||||
// 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";
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
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);
|
||||
digitalWrite(RELAY, HIGH);
|
||||
}
|
||||
|
||||
void relayOff()
|
||||
{
|
||||
Serial.println("Lights off");
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
digitalWrite(RELAY, LOW);
|
||||
}
|
||||
|
||||
// --------------- //
|
||||
// Network Testing //
|
||||
// --------------- //
|
||||
|
||||
void ensureWiFiConnection()
|
||||
{
|
||||
wl_status_t status = WiFi.status();
|
||||
if(status == WL_CONNECTED)
|
||||
return;
|
||||
else {
|
||||
relayOff();
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
bool pingHTTP()
|
||||
{
|
||||
WiFiClient client;
|
||||
client.setNoDelay(true);
|
||||
bool available = client.connect(HTTP_HOST, HTTP_PORT);
|
||||
|
@ -15,29 +112,13 @@ bool testHTTP() {
|
|||
return available;
|
||||
}
|
||||
|
||||
// Returns true if Internet works
|
||||
bool uplinkWorks() { return testHTTP(); }
|
||||
// ---- //
|
||||
// Loop //
|
||||
// ---- //
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(RELAY, OUTPUT);
|
||||
}
|
||||
|
||||
void relayOn() {
|
||||
Serial.println("Lights on");
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
digitalWrite(RELAY, HIGH);
|
||||
}
|
||||
|
||||
void relayOff() {
|
||||
Serial.println("Lights off");
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
digitalWrite(RELAY, LOW);
|
||||
}
|
||||
|
||||
void testInternet() {
|
||||
if (uplinkWorks()) {
|
||||
void testInternet()
|
||||
{
|
||||
if(pingHTTP()) {
|
||||
Serial.println("Internet is up");
|
||||
relayOn();
|
||||
} else {
|
||||
|
@ -46,25 +127,12 @@ void testInternet() {
|
|||
}
|
||||
}
|
||||
|
||||
void ensureWiFiConnection() {
|
||||
wl_status_t status = WiFi.status();
|
||||
if(status == WL_CONNECTED) return;
|
||||
else {
|
||||
relayOff();
|
||||
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() {
|
||||
void loop()
|
||||
{
|
||||
ensureWiFiConnection();
|
||||
testInternet();
|
||||
delay(5000);
|
||||
for(size_t i = 0; i < 5; i++) {
|
||||
ArduinoOTA.handle();
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue