This is my first project and first post on here. I have tried to create a simple program that would ping the Google DNS server and if this fails a set number of times then it is to switch a relay off, then back on. (the point being to power cycle my modem) Once I get this working the plan was to have 2 pings with the other being to a local IP address on my network and to toggle a second relay if that ping fails.
So far I have the below code but all it does is connect to the network and then turn the relay off after a few seconds. In the serial monitor I don't get any attempt for a ping at all. I'm guessing its to do with my 'while' loop being wrong but I'm far too new to this to see my error. This is going on a NodeMCU (ESP8266) programmed with the Arduino IDE.
Can someone please help me out?
/*
this program checks to see if it can Ping the Google DNS server and if the Ping fails
'pingIP1failmax' times in a row if will turn relay 1 OFF for 10 seconds and then back ON. It will
do a simlar thing with relay 2 if it cant Ping the HassIO server IP.
*/
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
//#include <ESP8266Ping.impl.h>
// Update these with values suitable for your network.
const char* ssid = "SSID";
const char* password = "PASSWORD";
const IPAddress remote_ip1(8, 8, 8, 8);
const IPAddress remote_ip2(192, 168, 0, 13);
int pingIP1count = 0;
int pingIP1fails = 0;
int pingIP1failmax = 0;
int pingIP2count = 0;
int pingIP2fails = 0;
int pingIP2failmax = 0;
WiFiClient espClient;
void setup() {
pinMode(5, OUTPUT); // Initialize the GPIO5 pin as an output
pinMode(4, OUTPUT); // Initialize the GPIO4 pin as an output
digitalWrite(5, LOW); // Turn GPIO5 on (Note that LOW is the voltage level
// but actually the Relay is on; this is because
// it is acive low on the ESP-01)
digitalWrite(4, LOW);
Serial.begin(115200);
setup_wifi();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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(pingIP1fails < pingIP1failmax){
Serial.print("Pinging Google DNS Server ");
Serial.println(remote_ip1);
if(Ping.ping(remote_ip1)){
Serial.println("Success!!");
delay(600);
}
else {
Serial.println("Error :(");
++pingIP1fails; // = ++pingIP2fails;
}
}
digitalWrite(5, HIGH); // Turn GPIO5 off
delay(10);
digitalWrite(5, LOW); // Turn GPIO5 on
pingIP2fails = 0;
}
OK, so I realised I had accidentally left the 'pingIP1failmax' equal to zero so it would drop out of the 'while' loop straight away. I have changed that to 3 as I had originally planned and now the program works... except that it constantly retries the Ping whereas I wanted it to delay for 5 mins before trying again. Why would the delay in the above code not be working?
EDIT: I'm an idiot. I was still thinking of a different programming language where a delay is in seconds, not milliseconds. Changed the delay time and things are looking good.
I have fixed up my code so thought I'd post it here for thread completeness if anyone else is interested later (although I haven't yet implemented the second relay, that's still to be done).
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
//#include <ESP8266Ping.impl.h>
// Update these with values suitable for your network.
const char* ssid = "xxSSIDxx";
const char* password = "xxPASSWORDxx";
const IPAddress remote_ip1(8, 8, 8, 8);
const IPAddress remote_ip2(192, 168, 0, 13);
int pingIP1count = 0;
int pingIP1fails = 0;
int pingIP1failmax = 3;
int pingIP2count = 0;
int pingIP2fails = 0;
int pingIP2failmax = 3;
int retryTime = 300000;
int retryTimeMins = retryTime / 60000; //convert time to minutes
WiFiClient espClient;
void setup() {
pinMode(5, OUTPUT); // Initialize the GPIO5 pin as an output
pinMode(4, OUTPUT); // Initialize the GPIO4 pin as an output
digitalWrite(5, LOW); // Turn GPIO5 on (Note that LOW is the voltage level
// but actually the Relay is on; this is because
// it is acive low on the ESP-01)
digitalWrite(4, LOW);
Serial.begin(115200);
setup_wifi();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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(pingIP1fails < pingIP1failmax){
delay(retryTime);
Serial.print("Pinging Google DNS Server ");
Serial.println(remote_ip1);
if(Ping.ping(remote_ip1)){
Serial.print("Success! Will try ping again in ");
Serial.print(retryTimeMins);
Serial.println(" minutes");
}
else {
Serial.println("PING FAILED!");
Serial.print("Incrementing pingIP1fail count to ");
++pingIP1fails; // Increment pingIP1fails counter
Serial.println(pingIP1fails);
}
}
Serial.println("Switching relay 1 OFF for 10 seconds to reboot router and resetting pingIP1fails to zero");
digitalWrite(5, HIGH); // Turn GPIO5 off
delay(10000); // Delay 10 seconds
digitalWrite(5, LOW); // Turn GPIO5 on
pingIP1fails = 0;
}