Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f0dc38

Browse files
authored
Merge pull request #395 from andreagilardoni/ping
Add WiFi.ping()
2 parents 9006eab + 3a81b22 commit 9f0dc38

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 at least version 0.5.0 of USB Wifi bridge firmware 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.");
40+
// don't continue
41+
while (true);
42+
}
43+
44+
// attempt to connect to WiFi network:
45+
while (status != WL_CONNECTED) {
46+
Serial.print("Attempting to connect to SSID: ");
47+
Serial.println(ssid);
48+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
49+
status = WiFi.begin(ssid, pass);
50+
51+
// wait 10 seconds for connection:
52+
delay(10000);
53+
}
54+
55+
printWifiStatus();
56+
}
57+
58+
/* -------------------------------------------------------------------------- */
59+
void loop() {
60+
/* -------------------------------------------------------------------------- */
61+
62+
// Ping IP
63+
const IPAddress remote_ip(140,82,121,4);
64+
Serial.print("Trying to ping github.com on IP: ");
65+
Serial.println(remote_ip);
66+
67+
// using default ping count of 1
68+
int res = WiFi.ping(remote_ip);
69+
70+
if (res > 0) {
71+
Serial.print("Ping response time: ");
72+
Serial.print(res);
73+
Serial.println(" ms");
74+
}
75+
else {
76+
Serial.println("Timeout on IP!");
77+
Serial.println("Make sure your WiFi firmware version is greater than 0.5.0");
78+
}
79+
80+
// Ping Host
81+
const char* remote_host = "www.google.com";
82+
Serial.print("Trying to ping host: ");
83+
Serial.println(remote_host);
84+
85+
// setting ttl to 128 and ping count to 10
86+
int res1 = WiFi.ping(remote_host, 128, 10);
87+
88+
if (res1 > 0) {
89+
Serial.print("Ping average response time: ");
90+
Serial.print(res1);
91+
Serial.println(" ms");
92+
}
93+
else {
94+
Serial.println("Timeout on host!");
95+
Serial.println("Make sure your WiFi firmware version is greater than 0.5.0");
96+
}
97+
98+
Serial.println();
99+
delay(1000);
100+
}
101+
102+
/* -------------------------------------------------------------------------- */
103+
void printWifiStatus() {
104+
/* -------------------------------------------------------------------------- */
105+
// print the SSID of the network you're attached to:
106+
Serial.print("SSID: ");
107+
Serial.println(WiFi.SSID());
108+
109+
// print your board's IP address:
110+
IPAddress ip = WiFi.localIP();
111+
Serial.print("IP Address: ");
112+
Serial.println(ip);
113+
114+
// print the received signal strength:
115+
long rssi = WiFi.RSSI();
116+
Serial.print("signal strength (RSSI):");
117+
Serial.print(rssi);
118+
Serial.println(" dBm");
119+
}
Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,33 @@ void CWifi::setTimeout(unsigned long timeout) {
561561
_timeout = timeout;
562562
}
563563

564+
/* -------------------------------------------------------------------------- */
565+
int CWifi::ping(IPAddress ip, uint8_t ttl, uint8_t count) {
566+
/* -------------------------------------------------------------------------- */
567+
return ping(ip.toString().c_str(), ttl, count);
568+
}
569+
570+
/* -------------------------------------------------------------------------- */
571+
int CWifi::ping(const String &hostname, uint8_t ttl, uint8_t count)
572+
/* -------------------------------------------------------------------------- */
573+
{
574+
return ping(hostname.c_str(), ttl);
575+
}
576+
577+
/* -------------------------------------------------------------------------- */
578+
int CWifi::ping(const char* host, uint8_t ttl, uint8_t count) {
579+
/* -------------------------------------------------------------------------- */
580+
int ret = WL_PING_ERROR;
581+
modem.begin();
582+
/* ping timeout is 1s and interval another 1s */
583+
modem.timeout((count * 2000) + MODEM_TIMEOUT);
584+
string res = "";
585+
if (modem.write(string(PROMPT(_PING)), res, "%s,%s,%d,%d\r\n", CMD_WRITE(_PING), host, ttl, count)) {
586+
ret = atoi(res.c_str());
587+
}
588+
modem.timeout(MODEM_TIMEOUT);
589+
return ret;
590+
}
564591

565592
CWifi WiFi;
566593

‎libraries/WiFiS3/src/WiFi.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class CWifi {
5858
*/
5959
static const char* firmwareVersion();
6060

61+
/*
62+
* PING
63+
*/
64+
int ping(IPAddress ip, uint8_t ttl = 128, uint8_t count = 1);
65+
int ping(const String &hostname, uint8_t ttl = 128, uint8_t count = 1);
66+
int ping(const char* host, uint8_t ttl = 128, uint8_t count = 1);
6167

6268
/*
6369
* Start WiFi connection for OPEN networks

‎libraries/WiFiS3/src/WiFiTypes.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ enum wl_enc_type {
3131
ENC_TYPE_UNKNOWN = 255
3232
};
3333

34+
typedef enum {
35+
WL_PING_DEST_UNREACHABLE = -1,
36+
WL_PING_TIMEOUT = -2,
37+
WL_PING_UNKNOWN_HOST = -3,
38+
WL_PING_ERROR = -4
39+
} wl_ping_result_t;
40+
3441
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /