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

Appearance settings

Commit b83b15c

Browse filesBrowse files
authored
Merge pull request #424 from fabik111/add-ping-command
Ping command for Portenta C33
2 parents cbac52a + 8094f71 commit b83b15c
Copy full SHA for b83b15c

File tree

12 files changed

+383
-7
lines changed
Filter options

12 files changed

+383
-7
lines changed

‎extras/net/lwipopts.h

Copy file name to clipboardExpand all lines: extras/net/lwipopts.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@
277277
* (requires the LWIP_RAW option)
278278
*/
279279
#ifndef MEMP_NUM_RAW_PCB
280-
#define MEMP_NUM_RAW_PCB 0
280+
#define MEMP_NUM_RAW_PCB 1
281281
#endif
282282

283283
/**
@@ -642,7 +642,7 @@
642642
* LWIP_RAW==1: Enable application layer to hook into the IP layer itself.
643643
*/
644644
#ifndef LWIP_RAW
645-
#define LWIP_RAW 0
645+
#define LWIP_RAW 1
646646
#endif
647647

648648
/*
+83Lines changed: 83 additions & 0 deletions
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+
}

‎libraries/Ethernet/src/Ethernet.cpp

Copy file name to clipboardExpand all lines: libraries/Ethernet/src/Ethernet.cpp
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,27 @@ IPAddress CEthernet::dnsServerIP() {
223223
return CLwipIf::getInstance().getDns();
224224
}
225225

226+
/* -------------------------------------------------------------------------- */
227+
int CEthernet::ping(IPAddress ip, uint8_t ttl) {
228+
/* -------------------------------------------------------------------------- */
229+
return CLwipIf::getInstance().ping(ip, ttl);
230+
}
231+
232+
/* -------------------------------------------------------------------------- */
233+
int CEthernet::ping(const String &hostname, uint8_t ttl)
234+
/* -------------------------------------------------------------------------- */
235+
{
236+
return ping(hostname.c_str(), ttl);
237+
}
238+
239+
/* -------------------------------------------------------------------------- */
240+
int CEthernet::ping(const char* host, uint8_t ttl) {
241+
/* -------------------------------------------------------------------------- */
242+
IPAddress ip;
243+
if(CLwipIf::getInstance().getHostByName(host,ip)) {
244+
return CLwipIf::getInstance().ping(ip, ttl);
245+
}
246+
return -1;
247+
}
248+
226249
CEthernet Ethernet;

‎libraries/Ethernet/src/EthernetC33.h

Copy file name to clipboardExpand all lines: libraries/Ethernet/src/EthernetC33.h
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ class CEthernet {
6969
IPAddress gatewayIP();
7070
IPAddress dnsServerIP();
7171

72+
/*
73+
* PING
74+
*/
75+
int ping(IPAddress ip, uint8_t ttl = 128);
76+
int ping(const String &hostname, uint8_t ttl = 128);
77+
int ping(const char* host, uint8_t ttl = 128);
7278

7379
friend class EthernetClient;
7480
friend class EthernetServer;
+120Lines changed: 120 additions & 0 deletions
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+
}
+2Lines 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/WiFi/src/WiFi.cpp

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFi.cpp
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,33 @@ unsigned long CWifi::getTime() {
321321
return 0;
322322
}
323323

324-
325-
326324
void CWifi::setTimeout(unsigned long timeout) {
327325
(void)(timeout);
328326
}
329327

328+
/* -------------------------------------------------------------------------- */
329+
int CWifi::ping(IPAddress ip, uint8_t ttl) {
330+
/* -------------------------------------------------------------------------- */
331+
return CLwipIf::getInstance().ping(ip, ttl);
332+
}
333+
334+
/* -------------------------------------------------------------------------- */
335+
int CWifi::ping(const String &hostname, uint8_t ttl)
336+
/* -------------------------------------------------------------------------- */
337+
{
338+
return ping(hostname.c_str(), ttl);
339+
}
340+
341+
/* -------------------------------------------------------------------------- */
342+
int CWifi::ping(const char* host, uint8_t ttl) {
343+
/* -------------------------------------------------------------------------- */
344+
IPAddress ip;
345+
if(hostByName(host,ip)) {
346+
return CLwipIf::getInstance().ping(ip, ttl);
347+
}
348+
return -1;
349+
}
350+
330351

331352
CWifi WiFi;
332353

‎libraries/WiFi/src/WiFiC3.h

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFiC3.h
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,12 @@ class CWifi {
254254

255255

256256
void setTimeout(unsigned long timeout);
257-
257+
/*
258+
* PING
259+
*/
260+
int ping(IPAddress ip, uint8_t ttl = 128);
261+
int ping(const String &hostname, uint8_t ttl = 128);
262+
int ping(const char* host, uint8_t ttl = 128);
258263

259264
};
260265

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.