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 2f38268

Browse files
paulvhapennam
authored andcommitted
add ping
1 parent 9006eab commit 2f38268

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
Lines changed: 124 additions & 0 deletions
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+
}
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,23 @@ void CWifi::setTimeout(unsigned long timeout) {
561561
_timeout = timeout;
562562
}
563563

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

565582
CWifi WiFi;
566583

‎libraries/WiFiS3/src/WiFi.h‎

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

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

6267
/*
6368
* Start WiFi connection for OPEN networks

0 commit comments

Comments
(0)

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