From e6c28f8ed9f8be73469adbc9f91a090c6dc2434b Mon Sep 17 00:00:00 2001 From: Thijs Raymakers Date: Thu, 20 Jun 2024 00:56:07 +0200 Subject: [PATCH] Connect to WiFi and draft of uptime testing Co-authored-by: x --- .gitignore | 1 + platformio.ini | 1 - src/main.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 70f8402..53c140e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .pio compile_commands.json +.vscode \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index be27263..ddf2d98 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,6 @@ platform=espressif8266 board=esp12e framework=arduino upload_speed=921600 -lib_deps = https://github.com/tzapu/WiFiManager.git [env:esp12e] build_flags=-D RELEASE diff --git a/src/main.cpp b/src/main.cpp index 974114b..6a747ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,57 @@ #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; +} + +bool testHTTP() { + return true; +} + +// Returns true if Internet works +bool uplinkWorks() { + return testDNS() || testHTTP(); +} void setup() { + WiFi.begin(WIFI_NAME, WIFI_PASSWORD); Serial.begin(9600); + + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println(); + + Serial.print("Connected, IP address: "); + Serial.println(WiFi.localIP()); + pinMode(LED_BUILTIN, OUTPUT); pinMode(RELAY, OUTPUT); } void loop() { - digitalWrite(LED_BUILTIN, HIGH); - digitalWrite(RELAY, HIGH); - delay(1000); - digitalWrite(LED_BUILTIN, LOW); - digitalWrite(RELAY, LOW); - delay(1000); + if(uplinkWorks()) { + Serial.println("Network is up"); + digitalWrite(LED_BUILTIN, HIGH); + digitalWrite(RELAY, HIGH); + } else { + Serial.println("Network is down"); + digitalWrite(LED_BUILTIN, LOW); + digitalWrite(RELAY, LOW); + } + delay(1000); // TODO: cleaner interval between tests }