This commit is contained in:
x 2024-06-20 21:07:24 +02:00
parent 3886966bf7
commit 24fd856584

View file

@ -1,6 +1,6 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#define RELAY 4 // GPIO4
#define WIFI_NAME "TechInc"
@ -16,42 +16,41 @@ void ensureWiFiConnection();
void relayOn();
void relayOff();
void setupOTA() {
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
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)));
// 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) {
if(error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
} else if(error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
} else if(error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
} else if(error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
} else if(error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}
void setup() {
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT);
@ -66,32 +65,36 @@ void setup() {
// Relay //
// ----- //
void relayOn() {
Serial.println("Lights on");
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(RELAY, HIGH);
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 relayOff()
{
Serial.println("Lights off");
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(RELAY, LOW);
}
// --------------- //
// Network Testing //
// --------------- //
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");
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
@ -100,7 +103,8 @@ void ensureWiFiConnection() {
}
}
bool pingHTTP() {
bool pingHTTP()
{
WiFiClient client;
client.setNoDelay(true);
bool available = client.connect(HTTP_HOST, HTTP_PORT);
@ -112,8 +116,9 @@ bool pingHTTP() {
// Loop //
// ---- //
void testInternet() {
if (pingHTTP()) {
void testInternet()
{
if(pingHTTP()) {
Serial.println("Internet is up");
relayOn();
} else {
@ -122,10 +127,11 @@ void testInternet() {
}
}
void loop() {
void loop()
{
ensureWiFiConnection();
testInternet();
for (size_t i = 0; i < 5; i++) {
for(size_t i = 0; i < 5; i++) {
ArduinoOTA.handle();
delay(1000);
}