#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.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() { }