Skip to content

Add ping command #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions examples/Ping/Ping.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Web ICMP Ping

This sketch pings a device based on the IP address or the hostname
using the NB-IoT module of Arduino MKR NB 1500 board.

created 8 Jenuary 2025
by fabik111

*/

#include <MKRNB.h>
#include "arduino_secrets.h"

// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
const char APN[] = "mobile.vodafone.it";
// initialize the library instance
GPRS gprs;
NB nbAccess;

/* -------------------------------------------------------------------------- */
void setup() {
/* -------------------------------------------------------------------------- */
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

while(nbAccess.begin(PINNUMBER, APN) != NB_READY){
Serial.println("Not connected");
delay(1000);
}

while(gprs.attachGPRS() != GPRS_READY){
Serial.println("GPRS not attached");
delay(1000);
}

Serial.println("Connected!");
}

/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */

// Ping IP
const IPAddress remote_ip(140,82,121,4);
Serial.print("Trying to ping github.com on IP: ");
Serial.println(remote_ip);

// using default ping count of 1
int res = gprs.ping(remote_ip);

if (res > 0) {
Serial.print("Ping response time: ");
Serial.print(res);
Serial.println(" ms");
}
else {
Serial.println("Timeout on IP!");
}

// Ping Host
const char* remote_host = "www.google.com";
Serial.print("Trying to ping host: ");
Serial.println(remote_host);

int res1 = gprs.ping(remote_host);

if (res1 > 0) {
Serial.print("Ping average response time: ");
Serial.print(res1);
Serial.println(" ms");
}
else {
Serial.println("Timeout on host!");
}

Serial.println();
delay(5000);
}

1 change: 1 addition & 0 deletions examples/Ping/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define SECRET_PINNUMBER ""
68 changes: 68 additions & 0 deletions src/GPRS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,71 @@ NB_NetworkStatus_t GPRS::status()
MODEM.poll();
return _status;
}

int GPRS::ping(const char* hostname, uint8_t ttl)
{
String response;

_pingResult = 0;

MODEM.sendf("AT+UPING=\"%s\",1,32,5000", hostname);
if (MODEM.waitForResponse() != 1) {
return GPRS_PING_ERROR;
};

for (unsigned long start = millis(); (millis() - start) < 5000 && (_pingResult == 0);) {
MODEM.poll();
}

if (_pingResult == 0) {
_pingResult = GPRS_PING_TIMEOUT;
}

return _pingResult;
}

int GPRS::ping(const String &hostname, uint8_t ttl)
{
return ping(hostname.c_str(), ttl);
}

int GPRS::ping(IPAddress ip, uint8_t ttl)
{
String host;
host.reserve(15);

host += ip[0];
host += '.';
host += ip[1];
host += '.';
host += ip[2];
host += '.';
host += ip[3];

return ping(host, ttl);
}

void GPRS::handleUrc(const String &urc)
{
if (urc.startsWith("+UUPINGER: ")) {
if (urc.endsWith("8")) {
_pingResult = GPRS_PING_UNKNOWN_HOST;
} else {
_pingResult = GPRS_PING_ERROR;
}
} else if (urc.startsWith("+UUPING: ")) {
int lastCommaIndex = urc.lastIndexOf(',');

if (lastCommaIndex == -1) {
_pingResult = GPRS_PING_ERROR;
} else {
_pingResult = urc.substring(lastCommaIndex + 1).toInt();

if (_pingResult == -1) {
_pingResult = GPRS_PING_TIMEOUT;
} else if (_pingResult <= 0) {
_pingResult = GPRS_PING_DEST_UNREACHABLE;
}
}
}
}
15 changes: 14 additions & 1 deletion src/GPRS.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@

#include "Modem.h"

class GPRS {
enum {
GPRS_PING_DEST_UNREACHABLE = -1,
GPRS_PING_TIMEOUT = -2,
GPRS_PING_UNKNOWN_HOST = -3,
GPRS_PING_ERROR = -4
};

class GPRS : public ModemUrcHandler {

public:

Expand Down Expand Up @@ -70,6 +77,12 @@ class GPRS {
void setTimeout(unsigned long timeout);
NB_NetworkStatus_t status();

int ping(const char* hostname, uint8_t ttl = 128);
int ping(const String& hostname, uint8_t ttl = 128);
int ping(IPAddress ip, uint8_t ttl = 128);

void handleUrc(const String& urc);

private:
int _state;
NB_NetworkStatus_t _status;
Expand Down
Loading