ESP8266Ping icon indicating copy to clipboard operation
ESP8266Ping copied to clipboard

Async ping (non-blocking)

Open maiksicks opened this issue 1 year ago • 0 comments

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);
}

maiksicks avatar Oct 29 '23 10:10 maiksicks