dancol90 / ESP8266Ping

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

Async ping (non-blocking) #47

Open maiksicks opened 11 months ago

maiksicks commented 11 months ago

Hey :-)

I've added a few things to support async ping. It shouldn't change any existing functionality. A third param disables the while-loop with the delay that waits for the _done flag to become true. Then we can just check for the results with the Ping.hasResult() method and optionally clear the result flag using Ping.hasResult(true).

When the async flag is set to true, the return value of Ping.ping() indicates if the start of the pinging was successful or not. It will return false if theres another ping currently active.

void setup() {
  // ...

  bool ret = Ping.ping("www.google.com", 5, true); // async = true
  if (ret) {
    // ping started successfully
  }
}

void loop() {

  if (Ping.hasResult(true)) {
    // ping is done, get the result
    bool success = Ping.hasSuccess();

    // do something with the result
  }

  // do other stuff here
  delay(200);
}