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

web_handler Can URL params be accessed in the automation? #14

Closed
brendann993 opened this issue Aug 20, 2021 · 4 comments
Closed

web_handler Can URL params be accessed in the automation? #14

brendann993 opened this issue Aug 20, 2021 · 4 comments

Comments

@brendann993
Copy link

Hi Ssieb, Is it possible to access url params e.g /setval?value=100 from within the automation linked to web_handler?
Use case is I have a template sensor that I want set the of via the web server e.g not using Home Assistant.
I've been able to increment the value by 1 each time the URL is called but I'd like to be able to set the value to a specific value if possible.
Thanks

@ssieb
Copy link
Owner

ssieb commented Aug 20, 2021

The AsyncWebServerRequest is available as request in a lambda. See https://github.com/me-no-dev/ESPAsyncWebServer#request-variables for details.

@brendann993
Copy link
Author

brendann993 commented Aug 26, 2021

Thanks that gets me on the right track. Any idea why they below seems to crash the esp? Basically the server response is empty, the log shows "Got to end of req" but the ESP then drops the connection for a few seconds then reconnects.
Ive been able to access the AsyncWebServerRequest request fine in the function but as soon as I try the stream.print I get that issue. Using stream.print in the lambda itself in yaml also appears to work fine. I have the function in the .h file though as I want to re-use some existing logic.
in device.yaml

- path: /setConfigItem
    on_request:
      - lambda: |-
          gotWebReq(request, stream);
          ESP_LOGD("web_req", "Got to end of req");

in includedFile.h

void gotWebReq(AsyncWebServerRequest request, AsyncResponseStream stream)
{
    stream.print("{\"success\": true}");
    return;
}

@brendann993
Copy link
Author

By updating the includedFile.h to use refs with & instead it works. My lack of C++ knowledge doesn't help me understand why.

void gotWebReq(AsyncWebServerRequest &request, AsyncResponseStream &stream)
{
    stream.print("{\"success\": true}");
    return;
}

@brendann993
Copy link
Author

Works perfectly as mentioned

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

2 participants