Skip to content

Commit 7f6ad1e

Browse files
fabik111pennam
authored andcommitted
add ping examples
1 parent 489d637 commit 7f6ad1e

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Web ICMP Ping
3+
4+
This sketch pings a device based on the IP address or the hostname
5+
using the Ethernet module.
6+
7+
created 14 February 2024
8+
by paulvha
9+
10+
updated 27 February 2025
11+
by Fabik111
12+
13+
*/
14+
15+
#include "EthernetC33.h"
16+
17+
int status = WL_IDLE_STATUS;
18+
19+
// Set the static IP address to use if the DHCP fails to assign
20+
IPAddress ip(10, 130, 22, 84);
21+
22+
/* -------------------------------------------------------------------------- */
23+
void setup() {
24+
/* -------------------------------------------------------------------------- */
25+
//Initialize serial and wait for port to open:
26+
Serial.begin(115200);
27+
while (!Serial) {
28+
; // wait for serial port to connect. Needed for native USB port only
29+
}
30+
31+
// start the Ethernet connection:
32+
if (Ethernet.begin() == 0) {
33+
Serial.println("Failed to configure Ethernet using DHCP");
34+
// try to configure using IP address instead of DHCP:
35+
// IN THAT CASE YOU SHOULD CONFIGURE manually THE DNS or USE the IPAddress Server variable above
36+
// that is what is automatically done here...
37+
Ethernet.begin(ip);
38+
}
39+
// give the Ethernet shield a second to initialize:
40+
delay(2000);
41+
}
42+
43+
/* -------------------------------------------------------------------------- */
44+
void loop() {
45+
/* -------------------------------------------------------------------------- */
46+
47+
// Ping IP
48+
const IPAddress remote_ip(140,82,121,4);
49+
Serial.print("Trying to ping github.com on IP: ");
50+
Serial.println(remote_ip);
51+
52+
// using default ping count of 1
53+
int res = Ethernet.ping(remote_ip);
54+
55+
if (res > 0) {
56+
Serial.print("Ping response time: ");
57+
Serial.print(res);
58+
Serial.println(" ms");
59+
}
60+
else {
61+
Serial.println("Timeout on IP!");
62+
}
63+
64+
// Ping Host
65+
const char* remote_host = "www.google.com";
66+
Serial.print("Trying to ping host: ");
67+
Serial.println(remote_host);
68+
69+
// setting ttl to 128 and ping count to 10
70+
int res1 = Ethernet.ping(remote_host);
71+
72+
if (res1 > 0) {
73+
Serial.print("Ping average response time: ");
74+
Serial.print(res1);
75+
Serial.println(" ms");
76+
}
77+
else {
78+
Serial.println("Timeout on host!");
79+
}
80+
81+
Serial.println();
82+
delay(1000);
83+
}

Diff for: libraries/WiFi/examples/WiFiPing/WiFiPing.ino

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
updated 27 February 2025
17+
by Fabik111
18+
19+
*/
20+
21+
#include "WiFiC3.h"
22+
#include "arduino_secrets.h"
23+
24+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
25+
char ssid[] = SECRET_SSID; // your network SSID (name)
26+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
27+
int keyIndex = 0; // your network key index number (needed only for WEP)
28+
29+
int status = WL_IDLE_STATUS;
30+
31+
/* -------------------------------------------------------------------------- */
32+
void setup() {
33+
/* -------------------------------------------------------------------------- */
34+
//Initialize serial and wait for port to open:
35+
Serial.begin(115200);
36+
while (!Serial) {
37+
; // wait for serial port to connect. Needed for native USB port only
38+
}
39+
40+
// check for the WiFi module:
41+
if (WiFi.status() == WL_NO_MODULE) {
42+
Serial.println("Communication with WiFi module failed.");
43+
// don't continue
44+
while (true);
45+
}
46+
47+
// attempt to connect to WiFi network:
48+
while (status != WL_CONNECTED) {
49+
Serial.print("Attempting to connect to SSID: ");
50+
Serial.println(ssid);
51+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
52+
status = WiFi.begin(ssid, pass);
53+
54+
// wait 10 seconds for connection:
55+
delay(10000);
56+
}
57+
58+
printWifiStatus();
59+
}
60+
61+
/* -------------------------------------------------------------------------- */
62+
void loop() {
63+
/* -------------------------------------------------------------------------- */
64+
65+
// Ping IP
66+
const IPAddress remote_ip(140,82,121,4);
67+
Serial.print("Trying to ping github.com on IP: ");
68+
Serial.println(remote_ip);
69+
70+
// using default ping count of 1
71+
int res = WiFi.ping(remote_ip);
72+
73+
if (res > 0) {
74+
Serial.print("Ping response time: ");
75+
Serial.print(res);
76+
Serial.println(" ms");
77+
}
78+
else {
79+
Serial.println("Timeout on IP!");
80+
}
81+
82+
// Ping Host
83+
const char* remote_host = "www.google.com";
84+
Serial.print("Trying to ping host: ");
85+
Serial.println(remote_host);
86+
87+
// setting ttl to 128 and ping count to 10
88+
int res1 = WiFi.ping(remote_host);
89+
90+
if (res1 > 0) {
91+
Serial.print("Ping average response time: ");
92+
Serial.print(res1);
93+
Serial.println(" ms");
94+
}
95+
else {
96+
Serial.println("Timeout on host!");
97+
}
98+
99+
Serial.println();
100+
delay(1000);
101+
}
102+
103+
/* -------------------------------------------------------------------------- */
104+
void printWifiStatus() {
105+
/* -------------------------------------------------------------------------- */
106+
// print the SSID of the network you're attached to:
107+
Serial.print("SSID: ");
108+
Serial.println(WiFi.SSID());
109+
110+
// print your board's IP address:
111+
IPAddress ip = WiFi.localIP();
112+
Serial.print("IP Address: ");
113+
Serial.println(ip);
114+
115+
// print the received signal strength:
116+
long rssi = WiFi.RSSI();
117+
Serial.print("signal strength (RSSI):");
118+
Serial.print(rssi);
119+
Serial.println(" dBm");
120+
}

Diff for: libraries/WiFi/examples/WiFiPing/arduino_secrets.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

0 commit comments

Comments
 (0)