Micropython Webserver

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Posts: 9
Joined: Mon Sep 23, 2019 10:11 am

Micropython Webserver

Post by DavidRKnowles » Wed Oct 09, 2019 3:11 am


A month ago I got extremely frustrated in writing C/C++ for my home IOT projects, I am not a native C coder and dealing with strings and memory often had me gouging out my eyes. Then I came across a microphone video and while I have never used python I found it extremely easy to do programming in.

I started to pick up where I left off on my C project of building a micropython base framework that works both on ESP32 and ESP8266, the core of the module includes: Settings Configuration, Wifi Connectivity, Mqtt, WebServer.
I am then building modules for things like Relays, US sensors, Voltage, Current, this can be just swapped out depending on what is required for a particular project.

The big issue I am having is finding a webserver module that can load the routes set in my modules. I have played with MicroWebSrv, but without using the templating I cant load an html file and dynamically populate it from the module variables.

I played a little with uasyncio and using uasyncio.start_server but this would require for me to rewrite my modules to suit uasyncio loop.

Currently, for MQTT I have routes loaded that are call upon when the callback fires, these then load the appropriate modules method, If the reply is instant like a relay switching on/off it relay's in the callback method, if its a delayed response say getting sensor data that takes time, to be non blocking the callback finishes and the reply is added to a command object in the loop, once complete it passes through MQTT for transmission.

I was going to do something similar with the HTTP requests, but not sure what library provides the best socket capability.

I would be really interested in what others have done or advice in this situation.

User avatar
Posts: 4152
Joined: Fri Jul 18, 2014 8:01 am
Location: UK

Re: Micropython Webserver

Post by pythoncoder » Wed Oct 09, 2019 6:33 am

I think you'll find that uasyncio is hard to avoid: it is by far the most efficient way of achieving concurrency in MicroPython. Any webserver needs to handle multiple concurrent clients. The principal ways to do this are via uasyncio or threading. The latter is RAM intensive: I don't think the ESP8266 supports it. If I'm wrong and it does, there would be little room for your code.

There are abundant reasons to use cooperative rather than pre-emptive scheduling. So my advice is to get on board with uasyncio. There are resources here including a tutorial.

There are various webserver packages. I've used PicoWeb but this requires a minor mod to work with official firmware. An older version which does work may be found here. Note that PicoWeb is based on uasyncio.
Peter Hinch

Posts: 695
Joined: Sat Feb 03, 2018 7:02 pm

Re: Micropython Webserver

Post by kevinkk525 » Wed Oct 09, 2019 6:33 am

I can't tell you anything about webservers but you should really think about implementing everything with uasyncio. You are already using workarounds for non-blocking callbacks that are similar to uasyncio.

I created a similar framework using uasyncio but without a webserver: https://github.com/kevinkk525/pysmartnode

Edit: Looks like Peter beat me to it :D
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Posts: 9
Joined: Mon Sep 23, 2019 10:11 am

Re: Micropython Webserver

Post by DavidRKnowles » Fri Oct 11, 2019 4:14 am

Thanks guys, Yeah its inevitable isn't it lol.

I always aim to make things as efficient as possible, and when I came across uasyncio I thought damn, this does everything I have hacked together and more, but it does it better.

I have my framework working alright after some time playing around last night, but the big problem is getting a reliable MQTT client that isn't too big to fit on the ESP8266. currently, I have an API for MQTT built in to update any settings and do logging on the ESP8266 and not worrying to much about a web interface (I just have to remember my list of commands lol), I am also building easy Repl access in case I just want to access and change settings that way.

I am extremely impressed on how easy Micropython is though, can't believe it's taken me so long to be won over. I have a sensor setup that needed a load of analog pins and I thought I would use a Nano connected over serial to the ESP32 with a Max485 connection (its for 8 moisture sensors in the garden). I got a refresh how easy it is to use Python vs C when making the API for the serial connection, Strings are just painful in C.

Post Reply