ESP32 + uPython + uaiohttpclient
Updated 2022-12-01: Let go this rough uaiohttpclient since it’s kind too simple (just GET, no POST, and lacking almost anything needed) to be integrated to something really useful. So currently I’m using someone’s own stuff (async mod on urequests) that works – from the REPO HERE.
This is a basic test around the async http client of micropython, which is NOT built-in with the official uPy image on download page of offcial site. Anyway it can be easily installed thru the upip – make sure you have sufficient free RAM before the installation though, otherwise it could fail.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
WebREPL connected >>> import gc >>> import upip >>> upip.install("micropython-uaiohttpclient") Installing to: /lib/ Warning: micropython.org SSL certificate is not validated Installing micropython-uaiohttpclient 0.5.1 from https://micropython.org/pi/uaiohttpclient/uaiohttpclient-0.5.1.tar.gz >>>utils.free() '61.11%' >>>gc.collect() >>>utils.free() '93.75%' |
I somehow guess that the reason it’s not included in standard build might be… the asyncio is not quite common among the uPy developers? So people can use the built-in urequests instead.
Well I’m not an async – await Python guy on Mac/Linux either (since we’ve an powerful O/S anyway), but here I consider it might be the only method, AFAIK, to make my ESP32 nodeMCU work with kinda multi-tasking with uPy, non-preemptive, though. If not, then I’m about to be left with no choice and head back to the C stuff (with a traditional RTOS), right?
1 2 3 4 5 6 7 |
>>> import uaiohttpclient as aiohttp >>> import uasyncio as asyncio >>> async def req(url): ... resp = await aiohttp.request("GET", url) ... print(await resp.read()) ... |
Never thought that the 1st time Python coroutine learning could be on a MCU with RAM like less than half mega. It’s overall a fantastic experience except the failure rate of the devkit board after several code update.
Perhaps it’s me who should to change the mind and lower the expectation on quality of some parts – especially the flash chip, considering its price.
1 2 3 4 5 6 7 8 |
>>> asyncio.run(req('http://fisherworks.cn')) b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional… 部分内容省略' >>> utils.free() '54.66%' >>> gc.collect() >>> utils.free() '85.23%' |
It’s always a good thing to have more options, e.g. micropython, lua, which helps eliminating the barrier between rookie developers and the basic knowledge such as “rising edge”, “registers”, “datasheet” and etc, to debug on targets with limited resources like the ESP or Pi pico series.
文章的脚注信息由WordPress的wp-posturl插件自动生成