Skip to content

Commit 9c9c3e7

Browse files
paulvhaandreagilardoni
authored andcommitted
add ping
1 parent 1169821 commit 9c9c3e7

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Web ICMP Ping
3+
4+
This sketch pings a device based on the IP address or the hostname
5+
using the WiFi module. By default the attempt is performed 5 times, but can
6+
be changed to max. 255
7+
8+
It requires the latest USB Wifi bridge firmware level and WiFiS3 library.
9+
10+
This example is written for a network using WPA encryption. For
11+
WEP or WPA, change the WiFi.begin() call accordingly.
12+
13+
created 14 February 2024
14+
by paulvha
15+
16+
*/
17+
18+
#include "WiFiS3.h"
19+
#include "arduino_secrets.h"
20+
21+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
22+
char ssid[] = SECRET_SSID; // your network SSID (name)
23+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
24+
int keyIndex = 0; // your network key index number (needed only for WEP)
25+
26+
int status = WL_IDLE_STATUS;
27+
28+
/* -------------------------------------------------------------------------- */
29+
void setup() {
30+
/* -------------------------------------------------------------------------- */
31+
//Initialize serial and wait for port to open:
32+
Serial.begin(115200);
33+
while (!Serial) {
34+
; // wait for serial port to connect. Needed for native USB port only
35+
}
36+
37+
// check for the WiFi module:
38+
if (WiFi.status() == WL_NO_MODULE) {
39+
Serial.println("Communication with WiFi module failed. freeze !");
40+
// don't continue
41+
while (true);
42+
}
43+
44+
String fv = WiFi.firmwareVersion();
45+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
46+
Serial.println("Please upgrade to the WiFi USB bridge firmware. freeze !");
47+
// don't continue
48+
while (true);
49+
}
50+
51+
// attempt to connect to WiFi network:
52+
while (status != WL_CONNECTED) {
53+
Serial.print("Attempting to connect to SSID: ");
54+
Serial.println(ssid);
55+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
56+
status = WiFi.begin(ssid, pass);
57+
58+
// wait 10 seconds for connection:
59+
delay(10000);
60+
}
61+
62+
printWifiStatus();
63+
}
64+
65+
/* -------------------------------------------------------------------------- */
66+
void loop() {
67+
/* -------------------------------------------------------------------------- */
68+
69+
// Ping IP
70+
const IPAddress remote_ip(140,82,121,4);
71+
Serial.print("Trying to ping github.com on IP: ");
72+
Serial.println(remote_ip);
73+
74+
// using default ping count of 5
75+
float res = WiFi.ping(remote_ip);
76+
77+
if (res != 0) {
78+
Serial.print("Pin average response time: ");
79+
Serial.print(res);
80+
Serial.println(" ms");
81+
}
82+
else {
83+
Serial.println("Timeout on IP!");
84+
}
85+
86+
// Ping Host
87+
const char* remote_host = "www.google.com";
88+
Serial.print("Trying to ping host: ");
89+
Serial.println(remote_host);
90+
91+
// setting ping count to 10 instead of default 5
92+
float res1 = WiFi.ping(remote_host,10);
93+
94+
if (res1 != 0) {
95+
Serial.print("Pin average response time: ");
96+
Serial.print(res1);
97+
Serial.println(" ms");
98+
}
99+
else {
100+
Serial.println("Timeout on host!");
101+
}
102+
103+
Serial.println();
104+
delay(1000);
105+
}
106+
107+
/* -------------------------------------------------------------------------- */
108+
void printWifiStatus() {
109+
/* -------------------------------------------------------------------------- */
110+
// print the SSID of the network you're attached to:
111+
Serial.print("SSID: ");
112+
Serial.println(WiFi.SSID());
113+
114+
// print your board's IP address:
115+
IPAddress ip = WiFi.localIP();
116+
Serial.print("IP Address: ");
117+
Serial.println(ip);
118+
119+
// print the received signal strength:
120+
long rssi = WiFi.RSSI();
121+
Serial.print("signal strength (RSSI):");
122+
Serial.print(rssi);
123+
Serial.println(" dBm");
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

libraries/WiFiS3/src/WiFi.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,30 @@ void CWifi::setTimeout(unsigned long timeout) {
558558
_timeout = timeout;
559559
}
560560

561+
/* -------------------------------------------------------------------------- */
562+
float CWifi::ping(IPAddress ip, unsigned int count) {
563+
/* -------------------------------------------------------------------------- */
564+
modem.begin();
565+
string res = "";
566+
567+
if(modem.write(string(PROMPT(_PINGIP)),res, "%s,%s,%d\r\n", CMD_WRITE(_PINGIP), ip.toString().c_str(), count )) {
568+
String rsl = res.c_str();
569+
return rsl.toFloat();
570+
}
571+
return 0;
572+
}
573+
574+
/* -------------------------------------------------------------------------- */
575+
float CWifi::ping(const char* host, unsigned int count) {
576+
/* -------------------------------------------------------------------------- */
577+
modem.begin();
578+
string res = "";
579+
if (modem.write(string(PROMPT(_PINGNAME)),res, "%s,%s,%d\r\n", CMD_WRITE(_PINGNAME), host, count)) {
580+
String rsl = res.c_str();
581+
return rsl.toFloat();
582+
}
583+
return 0;
584+
}
561585

562586
CWifi WiFi;
563587

libraries/WiFiS3/src/WiFi.h

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class CWifi {
5757
*/
5858
static const char* firmwareVersion();
5959

60+
/*
61+
* PING
62+
*/
63+
float ping(IPAddress ip, unsigned int count = 5);
64+
float ping(const char* host, unsigned int count = 5);
6065

6166
/*
6267
* Start WiFi connection for OPEN networks

0 commit comments

Comments
 (0)