Connect to WiFi and draft of uptime testing
Co-authored-by: x <xbr@c3l.lu>
This commit is contained in:
parent
e2862bc220
commit
e6c28f8ed9
3 changed files with 46 additions and 7 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
.pio
|
.pio
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
|
.vscode
|
|
@ -13,7 +13,6 @@ platform=espressif8266
|
||||||
board=esp12e
|
board=esp12e
|
||||||
framework=arduino
|
framework=arduino
|
||||||
upload_speed=921600
|
upload_speed=921600
|
||||||
lib_deps = https://github.com/tzapu/WiFiManager.git
|
|
||||||
|
|
||||||
[env:esp12e]
|
[env:esp12e]
|
||||||
build_flags=-D RELEASE
|
build_flags=-D RELEASE
|
||||||
|
|
51
src/main.cpp
51
src/main.cpp
|
@ -1,18 +1,57 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
|
||||||
#define RELAY 4 // GPIO4
|
#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() {
|
void setup() {
|
||||||
|
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
|
||||||
Serial.begin(9600);
|
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(LED_BUILTIN, OUTPUT);
|
||||||
pinMode(RELAY, OUTPUT);
|
pinMode(RELAY, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
if(uplinkWorks()) {
|
||||||
digitalWrite(RELAY, HIGH);
|
Serial.println("Network is up");
|
||||||
delay(1000);
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(RELAY, HIGH);
|
||||||
digitalWrite(RELAY, LOW);
|
} else {
|
||||||
delay(1000);
|
Serial.println("Network is down");
|
||||||
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
digitalWrite(RELAY, LOW);
|
||||||
|
}
|
||||||
|
delay(1000); // TODO: cleaner interval between tests
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue