Faster response + MDNS

This commit is contained in:
x 2024-06-20 22:35:45 +02:00
parent 9ebca3698d
commit ad2c64c63a

View file

@ -2,6 +2,7 @@
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#define RELAY 4 // GPIO4
#define WIFI_NAME "TechInc"
@ -18,6 +19,7 @@ boolean automatic = true;
// ----- //
void ensureWiFiConnection();
void testInternet();
void relayOn();
void relayOff();
@ -73,9 +75,12 @@ void webSetup() {
server.on("/auto", []() {
server.send(200, "text/plain", "automatic mode");
automatic = true;
ensureWiFiConnection();
testInternet();
});
server.begin();
if (MDNS.begin("internot")) { Serial.println("MDNS responder started"); }
}
void setup()
@ -83,9 +88,6 @@ void setup()
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY, OUTPUT);
relayOn();
delay(2000);
relayOff();
ensureWiFiConnection();
setupOTA();
webSetup();
@ -119,7 +121,7 @@ void ensureWiFiConnection()
if(status == WL_CONNECTED)
return;
else {
relayOff();
if(!automatic) relayOff();
Serial.print("WiFi is disconnected, now connecting");
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED) {
@ -162,9 +164,10 @@ void loop()
{
ensureWiFiConnection();
testInternet();
for(size_t i = 0; i < 5; i++) {
for(size_t i = 0; i < 20; i++) {
ArduinoOTA.handle();
delay(1000);
delay(250);
server.handleClient();
MDNS.update();
}
}