$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: Receiving a ping

DFRobot Mar 11 2019 1391

In this esp32 tutorial we will check how to send a ping to a ESP32 connected to a WiFi network. 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 ESP32 connected to a WiFi network.

We don’t need to write any code in order to explicitly treat the ping request. The ESP32 will natively answer to the ping as long as it is connected to the same network of the device that is pinging it.

This feature is very useful for debugging, since we can confirm that our ESP32 is reachable just by sending a simple ping command from a computer.

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

If you prefer a video tutorial, please check my YouTube channel below.

The code

We will start our code by including the WiFi.h library, so we can connect the ESP32 to a WiFi network.

#include "WiFi.h"

After this, we will declare the credentials of the WiFi network. We will need the name of the network (SSID) and the password.

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

Moving on to the setup function, we start by opening a serial connection, so we can output the IP assigned to the ESP32 on the WiFi network, which will be needed to perform the ping from another machine.

Serial.begin(115200);

Then, we will call the begin method on the WiFi extern variable that becomes accessible to us when we include the WiFi.h library.

This method receives as first input the network name and as second input its password, which we both declared before as global variables. After calling this method, the ESP32 will start connecting to the WiFi network asynchronously.

WiFi.begin(ssid, password);

Since we need to wait for the connection to finish before we print the IP assigned to the ESP32, we will simply periodically poll the connection status with a call to the status method on the WiFi extern variable.

So, we will do it in a loop which will break when the status method returns a value equal to the enum WL_CONNECTED.

Note that polling should usually be avoided due to its inefficiency but, in cases such as this where we simply don’t have any additional computation to perform while the connection is not finished, then it is acceptable.

while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
}

To finalize, we will print to the serial port the IP assigned to the ESP32 on the WiFi network to which it connected.

Serial.println(WiFi.localIP());

The final complete code can be seen below.

#include "WiFi.h"
 
const char* ssid = "yourNetworkName";
const char* password =  "yourPassword";
 
void setup(){
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  } 
 
  Serial.println(WiFi.localIP());
}
 
void loop(){}

Testing the code

To test the code, first compile it and upload it to your ESP32, using the Arduino IDE. Once the procedure finishes, open the IDE serial monitor and wait for the WiFi connection to be established.

After that, the program should output the IP address assigned to the ESP32 on the network, as highlighted in figure 1. Copy that IP, since we will need it to reach the device.

Then, on a computer connected to the same network, open a command line and send the following command, changing ESP_IP by the ESP32 IP you just copied:

ping ESP_IP

Your computer should then ping the ESP32 and you should get a result similar to figure 2. As can be seen, the ESP32 was reached with success and returned back the answer to the ping.

https://techtutorialsx.com/2019/01/11/esp32-arduino-receiving-a-ping/

REVIEW