Skip to content

Commit f514c2a

Browse files
committed
add ping command
1 parent c0ee4ed commit f514c2a

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

examples/Ping/Ping.ino

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Web ICMP Ping
3+
4+
This sketch pings a device based on the IP address or the hostname
5+
using the NB-IoT module of Arduino MKR NB 1500 board.
6+
7+
created 8 Jenuary 2025
8+
by fabik111
9+
10+
*/
11+
12+
#include <MKRNB.h>
13+
#include "arduino_secrets.h"
14+
15+
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
16+
// PIN Number
17+
const char PINNUMBER[] = SECRET_PINNUMBER;
18+
const char APN[] = "mobile.vodafone.it";
19+
// initialize the library instance
20+
GPRS gprs;
21+
NB nbAccess;
22+
23+
/* -------------------------------------------------------------------------- */
24+
void setup() {
25+
/* -------------------------------------------------------------------------- */
26+
//Initialize serial and wait for port to open:
27+
Serial.begin(9600);
28+
while (!Serial) {
29+
; // wait for serial port to connect. Needed for native USB port only
30+
}
31+
32+
while(nbAccess.begin(PINNUMBER, APN) != NB_READY){
33+
Serial.println("Not connected");
34+
delay(1000);
35+
}
36+
37+
while(gprs.attachGPRS() != GPRS_READY){
38+
Serial.println("GPRS not attached");
39+
delay(1000);
40+
}
41+
42+
Serial.println("Connected!");
43+
}
44+
45+
/* -------------------------------------------------------------------------- */
46+
void loop() {
47+
/* -------------------------------------------------------------------------- */
48+
49+
// Ping IP
50+
const IPAddress remote_ip(140,82,121,4);
51+
Serial.print("Trying to ping github.com on IP: ");
52+
Serial.println(remote_ip);
53+
54+
// using default ping count of 1
55+
int res = gprs.ping(remote_ip);
56+
57+
if (res > 0) {
58+
Serial.print("Ping response time: ");
59+
Serial.print(res);
60+
Serial.println(" ms");
61+
}
62+
else {
63+
Serial.println("Timeout on IP!");
64+
}
65+
66+
// Ping Host
67+
const char* remote_host = "www.google.com";
68+
Serial.print("Trying to ping host: ");
69+
Serial.println(remote_host);
70+
71+
int res1 = gprs.ping(remote_host);
72+
73+
if (res1 > 0) {
74+
Serial.print("Ping average response time: ");
75+
Serial.print(res1);
76+
Serial.println(" ms");
77+
}
78+
else {
79+
Serial.println("Timeout on host!");
80+
}
81+
82+
Serial.println();
83+
delay(5000);
84+
}
85+

examples/Ping/arduino_secrets.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define SECRET_PINNUMBER ""

src/GPRS.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,71 @@ NB_NetworkStatus_t GPRS::status()
193193
MODEM.poll();
194194
return _status;
195195
}
196+
197+
int GPRS::ping(const char* hostname, uint8_t ttl)
198+
{
199+
String response;
200+
201+
_pingResult = 0;
202+
203+
MODEM.sendf("AT+UPING=\"%s\",1,32,5000", hostname);
204+
if (MODEM.waitForResponse() != 1) {
205+
return GPRS_PING_ERROR;
206+
};
207+
208+
for (unsigned long start = millis(); (millis() - start) < 5000 && (_pingResult == 0);) {
209+
MODEM.poll();
210+
}
211+
212+
if (_pingResult == 0) {
213+
_pingResult = GPRS_PING_TIMEOUT;
214+
}
215+
216+
return _pingResult;
217+
}
218+
219+
int GPRS::ping(const String &hostname, uint8_t ttl)
220+
{
221+
return ping(hostname.c_str(), ttl);
222+
}
223+
224+
int GPRS::ping(IPAddress ip, uint8_t ttl)
225+
{
226+
String host;
227+
host.reserve(15);
228+
229+
host += ip[0];
230+
host += '.';
231+
host += ip[1];
232+
host += '.';
233+
host += ip[2];
234+
host += '.';
235+
host += ip[3];
236+
237+
return ping(host, ttl);
238+
}
239+
240+
void GPRS::handleUrc(const String &urc)
241+
{
242+
if (urc.startsWith("+UUPINGER: ")) {
243+
if (urc.endsWith("8")) {
244+
_pingResult = GPRS_PING_UNKNOWN_HOST;
245+
} else {
246+
_pingResult = GPRS_PING_ERROR;
247+
}
248+
} else if (urc.startsWith("+UUPING: ")) {
249+
int lastCommaIndex = urc.lastIndexOf(',');
250+
251+
if (lastCommaIndex == -1) {
252+
_pingResult = GPRS_PING_ERROR;
253+
} else {
254+
_pingResult = urc.substring(lastCommaIndex + 1).toInt();
255+
256+
if (_pingResult == -1) {
257+
_pingResult = GPRS_PING_TIMEOUT;
258+
} else if (_pingResult <= 0) {
259+
_pingResult = GPRS_PING_DEST_UNREACHABLE;
260+
}
261+
}
262+
}
263+
}

src/GPRS.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525

2626
#include "Modem.h"
2727

28-
class GPRS {
28+
enum {
29+
GPRS_PING_DEST_UNREACHABLE = -1,
30+
GPRS_PING_TIMEOUT = -2,
31+
GPRS_PING_UNKNOWN_HOST = -3,
32+
GPRS_PING_ERROR = -4
33+
};
34+
35+
class GPRS : public ModemUrcHandler {
2936

3037
public:
3138

@@ -70,6 +77,12 @@ class GPRS {
7077
void setTimeout(unsigned long timeout);
7178
NB_NetworkStatus_t status();
7279

80+
int ping(const char* hostname, uint8_t ttl = 128);
81+
int ping(const String& hostname, uint8_t ttl = 128);
82+
int ping(IPAddress ip, uint8_t ttl = 128);
83+
84+
void handleUrc(const String& urc);
85+
7386
private:
7487
int _state;
7588
NB_NetworkStatus_t _status;

0 commit comments

Comments
 (0)