Test the internet by TCP connecting to Google
This commit is contained in:
parent
e6c28f8ed9
commit
47892039b7
1 changed files with 11 additions and 18 deletions
27
src/main.cpp
27
src/main.cpp
|
@ -1,36 +1,29 @@
|
|||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
#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(".");
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue