Micropython question using uasyncio with ESP8266 webserver

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
Hakau
Posts: 1
Joined: Thu Oct 22, 2020 9:47 am
Location: Santa Clara, California

Micropython question using uasyncio with ESP8266 webserver

Post by Hakau » Tue Oct 27, 2020 4:53 pm

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?
Last edited by Hakau on Wed Nov 11, 2020 2:44 pm, edited 1 time in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Micropython question using uasyncio with ESP8266 webserver

Post by jimmo » Wed Oct 28, 2020 12:56 am

Hakau wrote:
Tue Oct 27, 2020 4:53 pm
Is uasyncio the right library for this? Or am I completely going down the wrong path?
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).

User avatar
Ventran
Posts: 24
Joined: Sun Jun 21, 2020 4:28 pm
Location: Poland, Europe

Re: Micropython question using uasyncio with ESP8266 webserver

Post by Ventran » Wed Nov 04, 2020 7:10 pm

Maby try https://github.com/swvincent/mp-web-non-blocking. This solution help me resolve my porblem with blocking socket in system.


User avatar
Ventran
Posts: 24
Joined: Sun Jun 21, 2020 4:28 pm
Location: Poland, Europe

Re: Micropython question using uasyncio with ESP8266 webserver

Post by Ventran » Sat Dec 05, 2020 3:23 pm

I think so. This is only draft but work for me. Currently I have three task one of them is web server.

Post Reply