OK please be patient, I'm just learning about OOP!
The object of my code is to ping a set of URLs and flash leds to show each performance.
I'm using the Leoncini ESP8266-ping library, and developing from the only example program.
It all works, but I cant see how I can get EITHER the individual ping time - or better still the average ping time as a parameter in my program to control an LED.
I've tried setting a parameter inside the pinger.OnReceive or pinger.OnEnd functions, (bold)
to get the value in my program (in CASE ONE ) but it does not work - reports a zero.
Here is my program. The state machine works fine; the serial print is just for debugging.
BTW I know its clunky, when it works I'm hoping to put all the code to send the 5 pings, get the average, and manage the associated LED into a function. So any help there would be appreciated.
#include <Pinger.h>
#include <ESP8266WiFi.h>
extern "C"
{
#include <lwip/icmp.h> // needed for icmp packet definitions
}
const char* ssid = "XXXXXXXXXXXXXX";
const char* password = "XXXXXXXXXXX";
int pingTime, pingAvg;
// Set global to avoid object removing after setup() routine
Pinger pinger;
void setup()
{
// Begin serial connection at 9600 baud
Serial.begin(115200);
delay(1000); // serial comms is disturbed during a restart
// Connect to WiFi access point
bool stationConnected = WiFi.begin(ssid, password);
delay(200); //allow time to connect
// Check if connection errors
if (!stationConnected)
{
Serial.println("Error, unable to connect specified WiFi network.");
}
// Wait until connection completed
Serial.print("Connecting to AP...");
while (WiFi.status() != WL_CONNECTED)
{
delay(200);
Serial.print(".");
}
Serial.print("Ok\n");
//Next bits from sketch_jun27b edited
pinger.OnReceive([](const PingerResponse & response)
{
if (response.ReceivedResponse)
{
Serial.print("ping response time is ");
Serial.print(response.ResponseTime);
Serial.println(" msec");
}
else
{
Serial.printf("Request timed out.\n");
}
// Return true to continue the ping sequence.
// If current event returns false, the ping sequence is interrupted.
return true;
});
pinger.OnEnd([](const PingerResponse & response)
{
// Evaluate lost packet percentage
float loss = 100;
if (response.TotalReceivedResponses > 0)
{
loss = (response.TotalSentRequests - response.TotalReceivedResponses) * 100 / response.TotalSentRequests;
}
// Print packet trip data
Serial.printf(
"Ping statistics for %s:\n",
response.DestIPAddress.toString().c_str());
Serial.printf(
" Packets: Sent = %lu, Received = %lu, Lost = %lu (%.2f%% loss),\n",
response.TotalSentRequests,
response.TotalReceivedResponses,
response.TotalSentRequests - response.TotalReceivedResponses,
loss);
// Print time information
if (response.TotalReceivedResponses > 0)
{
Serial.printf("Approximate round trip times in milli-seconds:\n");
Serial.printf(
" Minimum = %lums, Maximum = %lums, Average = %.2fms\n",
response.MinResponseTime,
response.MaxResponseTime,
response.AvgResponseTime);
}
// Print host data
Serial.printf("Destination host data:\n");
Serial.printf(
" IP address: %s\n",
response.DestIPAddress.toString().c_str());
if (response.DestMacAddress != nullptr)
{
Serial.printf(
" MAC address: " MACSTR "\n",
MAC2STR(response.DestMacAddress->addr));
}
if (response.DestHostname != "")
{
Serial.printf(
" DNS name: %s\n",
response.DestHostname.c_str());
}
[b] pingAvg = response.AvgResponseTime;[/b]
return true;
});
}
void loop()
{
static unsigned long timeReady, timeNow, timeLost = 0, timeASM = 0; //recording millis() times
enum target { ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX }; //different targets to ping
static byte target = ZERO;
static bool ASMStatus = 0;
int pingAvg = 0;
switch ( target ) {
case ZERO: {
Serial.println("zero:");
target = ONE;
timeASM = millis();
}
case ONE: {
timeReady = millis() - timeASM;
if (timeReady >= 10000 )
{
// Ping default gateway
Serial.printf("\n\nPinging default gateway with IP %s\n",
WiFi.gatewayIP().toString().c_str());
if (pinger.Ping(WiFi.gatewayIP(), 5, 200) == false)
{
Serial.println("Error during last ping command.");
}
Serial.print("Average ping is ");
Serial.println(pingAvg);
// then I'd have some code to use the parameter to control LED1.
//done this ping, get ready for next
target = TWO;
timeASM = millis();
}
break;
}
case TWO: {
timeReady = millis() - timeASM;
if (timeReady >= 6000 )
{
// Ping RYZEN5
Serial.printf("\n\nPinging RYZEN5 on ip 192.168.1.11\n");
if (pinger.Ping(IPAddress(192, 168, 1, 11), 5, 200) == false)
{
Serial.println("Error during ping command.");
}
Serial.print("Average ping is ");
Serial.println(pingAvg);
// then I'd have some code to use the parameter to control LED2.
//done this ping, get ready for next
target = THREE;
timeASM = millis();
}
break;
}
case THREE: {
timeReady = millis() - timeASM;
if (timeReady >= 3000 )
{
// Ping network printer
Serial.printf("\n\nPinging network printer on ip 192.168.1.22\n");
if (pinger.Ping(IPAddress(192, 168, 1, 22), 5, 200) == false)
{
Serial.println("Error during ping command.");
}
Serial.print("Average ping is ");
Serial.println(pingAvg);
//done this ping, get ready for next
target = FOUR;
timeASM = millis();
}
break;
}
case FOUR: {
timeReady = millis() - timeASM;
if (timeReady >= 3000 )
{
// Ping technologytourist.com
Serial.printf("\n\nPinging technologytourist.com\n");
if (pinger.Ping("technologytourist.com", 5, 200) == false)
{
Serial.println("Error during ping command.");
}
Serial.print("Average ping is ");
Serial.println(pingAvg);
//done this ping, get ready for next
target = FIVE;
timeASM = millis();
}
break;
}
case FIVE: {
timeReady = millis() - timeASM;
if (timeReady >= 3000 )
{
// Ping NAS box
Serial.printf("\n\nPinging NAS box on ip 192.168.1.24\n");
if (pinger.Ping(IPAddress(192, 168, 1, 24), 5, 200) == false)
{
Serial.println("Error during ping command.");
}
Serial.print("Average ping is ");
Serial.println(pingAvg);
//done this ping, get ready for next
target = SIX;
timeASM = millis();
}
break;
}
case SIX: {
timeReady = millis() - timeASM;
if (timeReady >= 2000 )
{
// Ping GEOBRDG
Serial.printf("\n\nPinging GEOBRDG on ip 192.168.1.14\n");
if (pinger.Ping(IPAddress(192, 168, 1, 14), 5, 200) == false)
{
Serial.println("Error during ping command.");
}
Serial.print("Average ping is ");
Serial.println(pingAvg);
Serial.println("Completed one set of pings to all targets");
target = ZERO;
timeASM = millis();
}
}
}//switch
}
Output on serial monitor (case 6) follows:
Pinging GEOBRDG on ip 192.168.1.14
Average ping is 0
ping response time is 72 msec
Request timed out.
ping response time is 151 msec
ping response time is 109 msec
ping response time is 68 msec
Ping statistics for 192.168.1.14:
Packets: Sent = 5, Received = 4, Lost = 1 (20.00% loss),
Approximate round trip times in milli-seconds:
Minimum = 68ms, Maximum = 151ms, Average = 100.00ms