diff --git a/examples/Ping/Ping.ino b/examples/Ping/Ping.ino new file mode 100644 index 0000000..f59f500 --- /dev/null +++ b/examples/Ping/Ping.ino @@ -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 +#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); +} + diff --git a/examples/Ping/arduino_secrets.h b/examples/Ping/arduino_secrets.h new file mode 100644 index 0000000..93fcdbc --- /dev/null +++ b/examples/Ping/arduino_secrets.h @@ -0,0 +1 @@ +#define SECRET_PINNUMBER "" \ No newline at end of file diff --git a/src/GPRS.cpp b/src/GPRS.cpp index 94223eb..a94860f 100644 --- a/src/GPRS.cpp +++ b/src/GPRS.cpp @@ -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; + } + } + } +} diff --git a/src/GPRS.h b/src/GPRS.h index a8e3bfa..9b6477a 100644 --- a/src/GPRS.h +++ b/src/GPRS.h @@ -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: @@ -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;