chrysn / aiocoap

The Python CoAP library
Other
264 stars 119 forks source link

Micropython Example #287

Open beriberikix opened 1 year ago

beriberikix commented 1 year ago

Hello! Has anyone successfully used aiocoap with Micropython? It seems feasible in theory but I could imagine there could be gotchas with asyncio, DTLS, etc.

chrysn commented 1 year ago

I've tried it back in early 2016, and fixed some compatibility issues, but didn't get to run it in full. But it's been ages, so maybe just try and report issues.

You might want to use the simple6 transport, as the udp6 transport relies on POSIX socket details often unimplemented. There might be better options if your platform exposes its network stack more directly; which platform do you target?

beriberikix commented 1 year ago

The ESP32, as it's the most accessible and comes in a wide array of variants that are supported by uPy.

beriberikix commented 1 year ago

I started to play around with the new uPy package manager mip here. It's far from working but would be the best path forward.

chrysn commented 1 year ago

Any update on this?

In the meantime, aiocoap has gained support for running on pyodide (eg. in Jupyter notebooks), which while still being the regular CPython, shows that there are no weird dependencies on OS level stuff (even though one may need a custom transport).

chrysn commented 1 year ago

IIUC micropython has no full asyncio, and thus won't support the async UDP sockets aiocoap creates; it may be necessary to build an own transport on micropython's socket, possibly based on or inspired by https://github.com/perbu/dgram/blob/master/dgram.py.

That does sound well feasible, but there's some devious details around (such as: how will the system know to which IP address the packet went, if recvmsg is not supported?), but that should suffice to get something running.

From the resource usage perspective, I think it'd be preferable to run an embedded CoAP library and only expose requests and responses -- but that'll be more effort in total, and the building blocks I'm trying to establish on the path there are not quite there yet.

beriberikix commented 1 year ago

Unfortunately no updates on my side. I didn't get too far in my MicroPython experiments. I did notice that the mip package manager is now officially released, which would be good to target. Most of the community seems to hang out in their Discord server, so if there's interest, that might be the best place to ask questions.

chrysn commented 1 year ago

I won't be using Discord, but it's possibly a suitable tool for others exploring this issue further.

I'll leave it open, both to indicate that there are not yet good examples of how (or whether) aiocoap can be used on micropython, and to have a place where others interested in trying it can exchange experiences.