Maybe a strange request. Is there any easy way to build an ESP32 as a ping responder so I can easily test my ping meter?
So what I'd like is to be able to ping the ESP32 - say at an assigned IP address or using mdns; and to be able to switch it on or off, and perhaps even adjust the response time with a pot.
On testing I see my wifi connected ESP32 gadgets do respond to a ping request, and the speed of response seems to depend on the program activity.
OK thats working now, just turning the wifi on and off; but I'd like to flash an LED when the unit receives a ping (while the wifi is up of course).
I tried using timed loops to slow down the esp32 but figuring out how to occupy both cores became too complicated. SO just turn wifi off and on.
The ping response time is very unpredictable which for my purpose isnt an issue.
the responder locates at responder.local using mdns
/*
*ping responder: switch on 23, LED to ground on 21
*/
#include <WiFi.h>
#include <ESPmDNS.h> //support for multicast to assign name to ip address
const char* ssid = "";
const char* password = "";
constexpr int offOn = 23;
constexpr int yLED = 21;
void setup() {
Serial.begin(115200);
delay(1000);
pinMode(offOn, INPUT_PULLUP);
pinMode(yLED, OUTPUT);
WiFi.mode(WIFI_STA); //Optional
WiFi.begin(ssid, password);
Serial.println("\nConnecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
// Initialize mDNS
if (!MDNS.begin("responder")) { // Set the hostname to "responder.local"
Serial.println("Error setting up MDNS responder!");
}
Serial.println("mDNS responder started");
}
void loop() {
//
int x = digitalRead(offOn);
digitalWrite(yLED, x);
if (!x) {
if (WiFi.status() == WL_CONNECTED) {
WiFi.disconnect();
Serial.println("wifi disconnected");
}
} else if (WiFi.status() != WL_CONNECTED) {
WiFi.reconnect();
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("WiFi reconnected");
// Initialize mDNS
if (!MDNS.begin("responder")) {
Serial.println("Error setting up MDNS responder!");
}
Serial.println("mDNS responder started");
}
delay(100);
}
When you ping a device from your PC, it sends ICMP (Internet Control Message Protocol) echo requests, and the device replies with echo responses. These ICMP packets are how basic ping works.
On the ESP32, if you're using Arduino, these ICMP packets are handled automatically by the internal Wi-Fi stack (LwIP), so you can’t easily access or modify the ping responses manually.
If you want to detect, modify, or manually respond to ping packets, you need to use ESP-IDF instead. ESP-IDF allows access to raw sockets, which means you can work directly with ICMP packets and customize how the ESP32 handles ping requests.
Thakns @AresXT
Its not worth spending a lot of time on, its just a simple gadget to help while testing my internet service logging device
I guess what I have now will suffice.
Would have been nice to have an LED flash when it received a ping.