ESP32 Arduino: Pinging a remote host

In this tutorial we will check how to send a ping to a remote host using the ESP32 and the Arduino core. We will be using this library. The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this tutorial we will check how to send a ping to a remote host using the ESP32 and the Arduino core. We will be using this library.

To install the library, you just need to download its source code from the GitHub page, unzip it and place it in your Arduino libraries folder. Depending on your installation, it might be located in different paths. In my case, it is located on:

C:\Users\<user name>\Documents\Arduino\libraries

Note that the extracted root folder will be named ESP32Ping-master. You should rename it simply to ESP32Ping before placing it in the Arduino libraries folder.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

We will start our code by the library includes. We will need the WiFi.h library, so we can connect to a WiFi network, and the ESP32Ping.h we have just installed, so we have access to the features that will allow us to ping a host.

#include <WiFi.h>
#include <ESP32Ping.h>

Then we will need to define two variables with the credentials of the WiFi network, so we can connect to it. We will need both the network name (SSID) and the password.

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";

Moving on to the Arduino setup, we will start by opening a serial connection, so we can output the results of our program. After that, we will take care of connecting the ESP32 to the WiFi network, using the credentials defined before.

Serial.begin(115200);

WiFi.begin(ssid, password);
  
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi...");
}

Then, to send a ping to a remote host, we simply need to call the ping method on the Ping extern variable. Note that this variable is an object of class PingClass.

The ping method receives as input a string with the host to which we want to send the ping. We will try to reach google.com.

Note however that the ping method is overloaded and, alternatively, it can receive as input an object of class IPAddress with the address of the host. This is very useful if, for example, we want to ping a device on the same network simply by using its local IP address.

As second optional argument, the ping method receives a count with the number of pings to be sent. If not specified, this value defaults to 5. In our case, to illustrate the use of this parameter, we will set it to 3.

As output, the ping method returns a Boolean indicting if the ping was successful or not. we will use it for error checking.

bool success = Ping.ping("www.google.com", 3);

if(!success){
    Serial.println("Ping failed");
    return;
}

Serial.println("Ping succesful.");

The final source code can be seen below.

#include <WiFi.h>
#include <ESP32Ping.h>

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi...");
  }

  bool success = Ping.ping("www.google.com", 3);

  if(!success){
    Serial.println("Ping failed");
    return;
  }

  Serial.println("Ping succesful.");


}

void loop() { }

Testing the code

To test the code, simply compile it and upload it to your device, using the Arduino IDE. Make sure to replace the WiFi credential placeholders by the ones that apply to your network.

Once the procedure finishes, open the Arduino IDE serial monitor and then wait for the device to connect to the network.

After it finishes the connection, it should then send the ping to the host. After the answer to the ping is received, you should get an output similar to figure 1.

Output of the ESP32 Arduino program that sends a ping to a remote host
Figure 1 – Output of the program, showing the result of the ping.

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading