How to ping remote host using ESP32 server

Hi,
I am working with ESP32 and making it a server.
I want ESP32 to ping my remote host with a given IP address and using library ESP32Ping fromGithub.
I am encountering with problem:
When i am trying to ping my PC on another network using ESP connected on another network, it is still pinging the PC.
I have attached the code for reference:

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

const char* ssid     = "Smart_Move";
const char* password = "addverb@123";

const IPAddress remote_ip(192,168,225,5);

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

  // We start by connecting to a WiFi network

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

  Serial.println();
  Serial.print("WiFi connected with ip ");  
  Serial.println(WiFi.localIP());

  Serial.print("Pinging ip ");
  Serial.println(remote_ip);

  if(Ping.ping(remote_ip)) {
    Serial.println("Success!!");
  } else {
    Serial.println("Error :(");
  }
}

void loop() { }

It is always pinging remote host irrespective on network on remote device.
Please help me to resolve this
Regards
Akansha

Can you provide the serial debug output? What is printed for 'remote_ip'?

I am attaching my Serial output in image form.
Regards
Akansha

Did you try turning off the PC to make sure it's not the target? What is the IP address of the other ESP32?

Sir,
I wanted to ping my laptop using ESP32.
and remote IP is my laptop IP address.
Sorry, i didn't get why to turn off laptop?
Problem i am encountering here is that ESP32 is always pinging irrespective of IP and connectivity of laptop i.e, showing success always.
But as per my understanding i got to know it should not ping a remote device if both are not on same connectivity.
Please correct me if i am wrong
Regards
Akansha

That is exactly why I want you to turn off your laptop, to make sure there are not duplicate devices. After all, with a ping you only know the existence of a device on a certain IP, you can't know what the device is. You may not actually be pinging your laptop, it might be something else on the same network you're on.

Thanks for explaining!
You are right that it may pinging any other device on the same network.
When i am assigning IP of my laptop then it is pinging that , this is fine.
but when i am disconnecting my laptop from the network.
Then also ESP is still pinging device on that same IP.
Actually i am creating WiFi from mobile phone so i know how many device are connected with mobile. When my laptop is not connected with my mobile WiFi still ESP32 is pinging on the same IP address.
Why is it so?
Regards
Akansha

"my laptop is not connected with my mobile WiFi still ESP32 is pinging on the same IP address."

Have you logged into your network/router to see all the clients connected to it? With your laptop disconnected are you still getting a success with ping times listed?

zoomkat:
Have you logged into your network/router to see all the clients connected to it? With your laptop disconnected are you still getting a success with ping times listed?

Hi,
I have created WiFi through my mobile phone, from which i can confirm how many devices are connected to this.
When laptop is not connected to WiFi network and only ESP32 is connected to that network, Still ESP32 is pinging laptop on that IP address.
Regards
Akansha

"Still ESP32 is pinging laptop on that IP address."

Note that you can ping IP addresses that that are not on line. What is returned from the pings? Do you get ping timeouts, or do you get returned values similar to below?

Microsoft Windows [Version 10.0.18362.720]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\zoomkat>ping 8.8.8.8

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=31ms TTL=52
Reply from 8.8.8.8: bytes=32 time=28ms TTL=52
Reply from 8.8.8.8: bytes=32 time=25ms TTL=52
Reply from 8.8.8.8: bytes=32 time=27ms TTL=52

Ping statistics for 8.8.8.8:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 25ms, Maximum = 31ms, Average = 27ms

C:\Users\zoomkat>

Hi,
Thanks for replying!
I got the return status of Ping.ping(remote_ip) as 1.
and also Ping (average_time) is also equal to 1.

I am attaching my Serial Output:
15:32:23.267 -> Pinging ip 192.168.43.197
15:32:43.297 -> 1
15:32:43.297 -> 1
15:32:43.297 -> Success!!

And part of code corresponding to above output:

  int ret = Ping.ping(remote_ip,10 );
  Serial.println(ret);
  int avg_time_ms = Ping.averageTime();
  Serial.println(avg_time_ms);
    if(ret) {
    Serial.println("Success!!");
  } else {
    Serial.println("Error :(");
  }

Regards
Akansha