Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any online request causing delay on the main loop #16

Open
PawelSzymanski89 opened this issue Jan 4, 2020 · 5 comments
Open

Any online request causing delay on the main loop #16

PawelSzymanski89 opened this issue Jan 4, 2020 · 5 comments

Comments

@PawelSzymanski89
Copy link

PawelSzymanski89 commented Jan 4, 2020

I have one task with simple request to REST api and if i'll start in main esp loop is waiting for response...

class ApiRequest : public Task {
  public:
    void loop() {
      Serial.println("ApiRequest");
      if (WiFi.status() == WL_CONNECTED) {
        client.get("/basementData");
        int statusCode = client.responseStatusCode();
        String resBody = client.responseBody();
        //        Serial.println("-------------------------");
        //        Serial.println(resBody);
        if (statusCode = 200) {
          JsonObject root = doc.as<JsonObject>();
          auto error = deserializeJson(doc, resBody);
          if (!error) {
            //            double stoveTemp = root["stoveTemperature"];
            stoveTemperature = (int)root["stoveTemperature"];
            waterTemperature = (int)root["waterTemperature"];
            timeMills = root["timeStamp"];
          }
        }
      }
    }
} api_task;
@nrwiersma
Copy link
Owner

What is the issue exactly?

@dbuezas
Copy link
Contributor

dbuezas commented Feb 3, 2021

client.get will wait using the global Delay until the response is received and call the global yield, this blocks the tasks.

The only ways around this I can think of are:

  • Implement the get request yourself so you can call the task-specific delay function instead code
  • Expose Scheduler.current->delay globally and wrap the include"HTTPclient" around #define and #undefine to change the calls to global delay and yield to Scheduler.current->delay. (hacky but sounds fun) (didn't get it to work)

@dbuezas
Copy link
Contributor

dbuezas commented Feb 6, 2021

I found a way to make this work:

  1. expose the yield function of the current Task (e.g SchedulerClass::yield())
  2. Make a new class that inherits from HTTPSClient
class HTTPSClientScheduled : public HTTPSClient {
  int available() override {
    SchedulerClass::yield();
    return HTTPSClient::available();
  }
};
  1. instantiate a client from HTTPSClientScheduled instead.

This works because GET makes extensive use of readStringUntil, which in term calls available, which just happens to be overridable. I'm using it with HTTPSRedirect


@nrwiersma would you consider a PR that exposes yield and delay in that way inside the Scheduler class?

@nrwiersma
Copy link
Owner

@dbuezas In light of #20 it seems unlikely.

@dbuezas
Copy link
Contributor

dbuezas commented Feb 7, 2021

I think it can be done (I first have to understand the run groups thing), I'll make a separate PR after #20 gave up on making it work with run groups.
For this functionality (w/o run groups), there's a fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants