Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// CloudPing
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
#include <Adafruit_NeoPixel.h>
#define PIN D1
#define NUM_LEDS 1
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// fill in your wifi's credentials
const char* ssid = "SSID";
const char* password = "YOURWIFIPASSWORD";
// either an IP or a hostname. comment the other one out
//const IPAddress remote_ip(192, 168, 0, 1);
const char* remote_ip("yourhost.name");
int pingIPcount = 0;
int pingIPfails = 0;
int pingIPfailmax = 3;
int retryTime = 60000;
int retryTimeMins = retryTime / 60000; // time in minutes
WiFiClient espClient;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.begin(115200);
setup_wifi();
}
void setup_wifi() {
delay(10);
// connect to wifi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.hostname("PingCloud");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
while(pingIPfails < pingIPfailmax){
delay(retryTime);
Serial.print("Pinging Server: ");
Serial.println(remote_ip);
if(Ping.ping(remote_ip)){
Serial.print("Success! Will try ping again in ");
Serial.print(retryTimeMins);
Serial.println(" minutes");
Serial.println("LED set to blue. :)");
strip.setPixelColor(0, 100, 100, 110);
strip.show();
}
else {
Serial.println("PING FAILED!");
Serial.print("Incrementing pingIPfail count to ");
++pingIPfails; // Increment pingIPfails counter
Serial.println(pingIPfails);
}
}
Serial.println("LED set to RED!");
strip.setPixelColor(0, 250, 0, 0);
strip.show();
pingIPfails = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.