I am trying to implement a web server in ESP8266 that will handle simple HTTP GET requests by initiating an operation (e.g. move a stepper motor to the requested position) and return a response when the operation is finished (i.e. when the motor has reached the target position). This may take a few seconds, but during that time the main loop should run normally so that both the TCP stack can work and the stepper library can actually move the motor (it works in the background). This means I cannot wait for the operation to complete within the handler. I have to start the operation in the handler, return without sending back a response and when the operation is done (i can poll for that in the main loop) then send the response.

I have been looking at https://github.com/me-no-dev/ESPAsyncWebServer but I'm not sure if such a scenario is supported. Has anynone done anything similar? Is it even possible?

One solution I was thinking was to store the request in a global variable (or queue) before exiting the handler and use it when the operation finishes. But will the request survive outside of the handler? And is it even possible to send the response outside the handler?

  • Without knowing too much about this, it sounds like a bad idea to delay the actual response to the GET request. A client with a too short timeout might think, that the connection was lost. It may be feasible for you to just poll the ESP from the client for the status of the operation, to get the information if it was finished – chrisl Sep 16 '19 at 17:54
  • Granted, this is not a good design, but since this is what the client expects and I have no control of the client, I am looking at how to implement the requirement – P. Kouvarakis Sep 16 '19 at 19:00
  • if you are using a webpage to interact with the server, then put a timer into the webpage for auto refresh ... i think that using ajax may also do the job (I'm not sure though) – jsotola Sep 16 '19 at 22:13
  • 1
    I'm thinking of going a completely different direction. Dump the client and make my own. Then I have full control and I can do any polling or even use websockets to send events from ESP – P. Kouvarakis Sep 16 '19 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.