dancol90 / ESP8266Ping

Ping library for ESP8266 Arduino core
GNU Lesser General Public License v2.1
265 stars 136 forks source link

Suggestion: retrieve ip address if ping to host was successful #8

Closed vossilius closed 7 years ago

vossilius commented 7 years ago

Thanks for the library! Just a thought. Would be nice if one could retrieve the ip address from a ping reply to a host in order to save another dns request. I use the function to select one out of three servers based on their latency and whould like to use the returned ip address straight away instead of accessing the selected server by its host name once again.

dancol90 commented 7 years ago

Hi!

To resolve the ip address the library use the WiFi.hostByName() function, like this:

bool PingClass::ping(const char* host, byte count) {
    IPAddress remote_addr;

    if (WiFi.hostByName(host, remote_addr))
        return ping(remote_addr, count);

    return false;
}

I have to think about a way to return some data in a prettier way, including host IP, average time, success rate, etc.

In the meantime you can mimic the function above in your code, saving the return value of WiFi.hostByName()!

vossilius commented 7 years ago

Thanks for your prompt reply. Yes, that's what I am doing right now. But doesn't that call initiate another dns request?

dancol90 commented 7 years ago

Ping.ping() also accept an IPAddress instead of a host string.

So, if you call hostByName in your code, and then pass the resulting IPAddress to Ping.ping(), only one DNS request will be made.

There's also an example (https://github.com/dancol90/ESP8266Ping/blob/master/examples/SimplePing/SimplePing.ino) that uses directly an IPAddress.

vossilius commented 7 years ago

Ah! Got it. Thanks!