Hi all. I am trying to build an ESP8266 device with micropython that hosts a web server and runs a process. From the hosted web site, I want a start button that triggers a process (in this case a series of events from a series of relays that would take minutes to complete). I would like the web page to have a stop button to halt the events before they complete.
My problem is socket.accept() is blocking. So after starting the relay process, if I listen for the next web request, processing stops while waiting for socket.accept().
I am trying to solve this with something like this:
import uasyncio as asyncio loop = asyncio.get_event_loop() loop.create_task(web_server()) loop.create_task(relay_event_loop()) loop.run_forever()
The web_server and relay_event_loop would communicate with each other via an event_state object. My problem is that in web_server(), socket.accept() is still blocking, even when called from an asyncio task.
I also tried setting the socket to nonblocking (s.setblocking(0)). This solved the blocking, but the page would not render and would fall into an exception state.
Is uasyncio the right library for this? Or am I completely going down the wrong path?
Micropython question using uasyncio with ESP8266 webserver
Micropython question using uasyncio with ESP8266 webserver
Last edited by Hakau on Wed Nov 11, 2020 2:44 pm, edited 1 time in total.
Re: Micropython question using uasyncio with ESP8266 webserver
Yes, uasyncio can do this, but you also need to manage the socket from uasyncio via uayncio.start_server.
However, I strongly recommend using an existing asyncio-based HTTP server library. My recommendation is https://github.com/miguelgrinberg/microdot (which has both async and non-async versions).
Re: Micropython question using uasyncio with ESP8266 webserver
Maby try https://github.com/swvincent/mp-web-non-blocking. This solution help me resolve my porblem with blocking socket in system.
Re: Micropython question using uasyncio with ESP8266 webserver
both https://github.com/miguelgrinberg/microdot and https://github.com/swvincent/mp-web-non-blocking seems not to be in active development, isn't it?
Re: Micropython question using uasyncio with ESP8266 webserver
I think so. This is only draft but work for me. Currently I have three task one of them is web server.