/***************************************************************************** Arduino library handling ping messages for the esp8266 platform MIT License Copyright (c) 2018 Alessio Leoncini Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *****************************************************************************/ #include #include extern "C" { #include // needed for icmp packet definitions } // Set global to avoid object removing after setup() routine Pinger pinger; const int ping_count = 20; // no of ping to be initiated for each IP // IPs and Information for which ping to be initiated const IPAddress remote_ip1(8, 8, 8, 8); String IPInfo1 = "googleDNS"; TO DELETE const IPAddress remote_ip2(8, 8, 4, 4); String IPInfo2 = "GoogleDNS"; TO DELETE const IPAddress remote_ip3(139, 130, 4, 5); String IPInfo3 = "telstra"; TO DELETE const IPAddress remote_ip4(124, 108, 103, 104); String IPInfo4 = "yahooONE"; const IPAddress remote_ip5(91, 198, 174, 192); String IPInfo5 = "wikipedia"; const IPAddress remote_ip6(205, 210, 42, 205); String IPInfo6 = "DNSRes"; const IPAddress remote_ip7(64, 68, 200, 200); String IPInfo7 = "EasyDNS"; const IPAddress remote_ip8(1, 1, 1, 1); String IPInfo8 = "DNSPer"; const IPAddress remote_ip9(52, 219, 40, 90); String IPInfo9 = "HS"; const IPAddress remote_ip10(172, 217, 31, 36); String IPInfo10 = "Google"; const IPAddress remote_ip11(106, 10, 249, 79); String IPInfo11 = "yahooTWO"; void setup() { // Begin serial connection at 115200 baud Serial.begin(115200); // Connect to WiFi access point bool stationConnected = WiFi.begin( "SSID", "Password"); // Check if connection errors if(!stationConnected) { Serial.println("Error, unable to connect specified WiFi network."); } // Wait connection completed Serial.print("Connecting to AP..."); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print("Ok\n"); pinger.OnReceive([](const PingerResponse& response) { if (response.ReceivedResponse) { Serial.printf( "Reply from %s: icmp_seq=%d bytes=%d time=%lums TTL=%d\n", response.DestIPAddress.toString().c_str(), response.SequenceNumber, response.EchoMessageSize - sizeof(struct icmp_echo_hdr), response.ResponseTime, response.TimeToLive); } 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()); } return true; }); /* // Ping default gateway Serial.printf( "\n\nPinging default gateway with IP %s\n", WiFi.gatewayIP().toString().c_str()); if(pinger.Ping(WiFi.gatewayIP()) == false) { Serial.println("Error during last ping command."); } delay(10000); */ // Ping IP1 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo1); if(pinger.Ping(IPAddress(remote_ip1), 9) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP2 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo2); if(pinger.Ping(IPAddress(remote_ip2), 9) == false) { Serial.println("Error during ping command."); } delay(10000); /* // Ping IP3 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo3); if(pinger.Ping(IPAddress(remote_ip3), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP4 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo4); if(pinger.Ping(IPAddress(remote_ip4), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP5 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo5); if(pinger.Ping(IPAddress(remote_ip5), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP6 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo6); if(pinger.Ping(IPAddress(remote_ip6), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP7 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo7); if(pinger.Ping(IPAddress(remote_ip7), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP8 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo8); if(pinger.Ping(IPAddress(remote_ip8), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP9 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo9); if(pinger.Ping(IPAddress(remote_ip9), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP10 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo10); if(pinger.Ping(IPAddress(remote_ip10), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); // Ping IP11 // Serial.println("#######################################"); Serial.printf("Pinging "); Serial.println(IPInfo11); if(pinger.Ping(IPAddress(remote_ip11), ping_count) == false) { Serial.println("Error during ping command."); } delay(10000); /* // Ping undefinedname Serial.printf("\n\nPinging undefinedname\n"); if(pinger.Ping("undefinedname") == false) { Serial.println("Error during ping command."); } delay(10000); // Ping invalid ip Serial.printf("\n\nPinging invalid ip 1.2.3.4\n"); if(pinger.Ping(IPAddress(1,2,3,4)) == false) { Serial.println("Error during ping command."); } */ } void loop() { }