From 47892039b7b5dceebcc06f6960089284dbef39f7 Mon Sep 17 00:00:00 2001 From: Thijs Raymakers Date: Thu, 20 Jun 2024 01:48:08 +0200 Subject: [PATCH] Test the internet by TCP connecting to Google --- src/main.cpp | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6a747ed..7fe6603 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,36 +1,29 @@ #include #include -#include #define RELAY 4 // GPIO4 #define WIFI_NAME "TechInc" #define WIFI_PASSWORD "itoldyoualready" -#define ROOT_DNS_IP "193.0.14.129" - -const IPAddress root_dns_ip(193, 0, 14, 129); - -bool testDNS() { - WiFiUDP udp; - udp.beginPacket(root_dns_ip, 53); - return true; -} +#define HTTP_HOST "google.com" +#define HTTP_PORT (80) bool testHTTP() { - return true; + WiFiClient client; + client.setNoDelay(true); + bool available = client.connect(HTTP_HOST, HTTP_PORT); + client.stop(); + return available; } // Returns true if Internet works -bool uplinkWorks() { - return testDNS() || testHTTP(); -} +bool uplinkWorks() { return testHTTP(); } void setup() { WiFi.begin(WIFI_NAME, WIFI_PASSWORD); Serial.begin(9600); Serial.print("Connecting to WiFi"); - while (WiFi.status() != WL_CONNECTED) - { + while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } @@ -44,7 +37,7 @@ void setup() { } void loop() { - if(uplinkWorks()) { + if (uplinkWorks()) { Serial.println("Network is up"); digitalWrite(LED_BUILTIN, HIGH); digitalWrite(RELAY, HIGH); @@ -53,5 +46,5 @@ void loop() { digitalWrite(LED_BUILTIN, LOW); digitalWrite(RELAY, LOW); } - delay(1000); // TODO: cleaner interval between tests + delay(5000); // TODO: cleaner interval between tests }