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,7 +16,8 @@ void ensureWiFiConnection();
void relayOn();
void relayOff();
void setupOTA() {
void setupOTA()
{
ArduinoOTA.onStart([]() {
String type;
if(ArduinoOTA.getCommand() == U_FLASH) {
@ -28,12 +29,9 @@ ArduinoOTA.onStart([]() {
// 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.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) {
@ -51,7 +49,8 @@ ArduinoOTA.onStart([]() {
ArduinoOTA.begin();
}
void setup() {
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT);
@ -66,13 +65,15 @@ void setup() {
// 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);
@ -82,9 +83,11 @@ void relayOff() {
// 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");
@ -100,7 +103,8 @@ void ensureWiFiConnection() {
}
}
bool pingHTTP() {
bool pingHTTP()
{
WiFiClient client;
client.setNoDelay(true);
bool available = client.connect(HTTP_HOST, HTTP_PORT);
@ -112,7 +116,8 @@ bool pingHTTP() {
// Loop //
// ---- //
void testInternet() {
void testInternet()
{
if(pingHTTP()) {
Serial.println("Internet is up");
relayOn();
@ -122,7 +127,8 @@ void testInternet() {
}
}
void loop() {
void loop()
{
ensureWiFiConnection();
testInternet();
for(size_t i = 0; i < 5; i++) {