Wifi bridge for RPI Pico

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
kevinkk525
Posts: 868
Joined: Sat Feb 03, 2018 7:02 pm

Wifi bridge for RPI Pico

Post by kevinkk525 » Thu Jan 28, 2021 4:42 pm

The RPI Pico seems like a great device and I received mine today but I need WLAN in all my projects so I was wondering about the possibilities to have a WLAN bridge from an esp8266 to the Pico.
There is the project esp-link https://github.com/jeelabs/esp-link for the esp8266 to provide WLAN over its UART and a client in c++ el-client https://github.com/jeelabs/el-client. With this it would be possible to port the el-client to micropython and get some mqtt functionality (I only need mqtt).
However, the project is 4 years old and I was wondering if there is something more recent or even something already in micropython?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
mattyt
Posts: 329
Joined: Mon Jan 23, 2017 6:39 am

Re: Wifi bridge for RPI Pico

Post by mattyt » Fri Jan 29, 2021 6:47 am

You could try leveraging the work Adafruit have put into their ESP32-based Airlift wifi co-processor.

Basically, run some Arduino-based firmware on the ESP32, connect it to your micro over SPI and then, the tricky part, write a module to route wifi requests over SPI. You could try porting the CircuitPython module to do this or create your own.

kevinkk525
Posts: 868
Joined: Sat Feb 03, 2018 7:02 pm

Re: Wifi bridge for RPI Pico

Post by kevinkk525 » Fri Jan 29, 2021 8:40 am

That actually looks really good. Thanks a lot! I have enough esp32 lying around too, so that's fine.
And the client code seems to be easily portable to micropython. I might give that a try.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 4935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wifi bridge for RPI Pico

Post by pythoncoder » Fri Jan 29, 2021 12:38 pm

If you only need MQTT we have already done this. Our micropython-mqtt repo has a library designed to bring MQTT to the Pyboard 1.x. With the advent of the Pyboard D I regarded it as obsolescent, but it could have a new role with the Pico.

There is a bit of pyb specific stuff there but it might be worth investigating making it cross-platform.
Peter Hinch

kevinkk525
Posts: 868
Joined: Sat Feb 03, 2018 7:02 pm

Re: Wifi bridge for RPI Pico

Post by kevinkk525 » Fri Jan 29, 2021 2:15 pm

oh, thanks a lot! I forgot about that option, I only remembered having a WIFI bridge in the micropython-iot project, which would have required some work around it.
I'll definitely check this out, I only need mqtt on the Pico. Might need to port it to uasyncio V3 first but should be easier to set up than the two previously mentioned options if only the pyboard/pico side needs change.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 4935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wifi bridge for RPI Pico

Post by pythoncoder » Fri Jan 29, 2021 4:06 pm

There is undoubtedly some work required to port to uasyncio V3 - in particular it uses my workrounds for the task cancellation bugs in V2. I didn't port this because I thought that the Pyboard D made it obsolete. However I now think there's a great role for it with the Pico. A Pico with an ESP8266 would be an inexpensive way to build an MQTT setup on a baremetal platform.

I'm busy with graphics coding at the moment so it will be at least a couple of weeks before I'll be able to do much on this. I'd certainly welcome PR's and would consider any other changes you might want to make. I don't think it has many existing users to worry about.

From a quick look the Pyboard-specific stuff is just the RTC and an LED. You're right in saying that the main task is the port to V3, but I don't think you'll have much trouble with it. It's really a matter of getting rid of the asyn module and using standard task cancellation. I've done that with other modules and had no problems. It's a testament to the quality of Damien's code that I've ported so much code to V3 without needing to raise a single issue.

I'd leave the ESP8266 code as it is to begin with - you could even use the firmware image in the repo to get started. We should port that to V3, but it can be done later. If you wish I'd be happy to take this on when I have time.

It uses my "syncom" protocol to communicate between the host and the ESP8266. This leaves the UART free for debugging. It's not fast but it is designed for asynchronous coding. It uses a defined sequence of state changes with no timing dependencies. The syncom module doesn't use any V2 workrounds so should work under V3.

Good luck!
Peter Hinch

kevinkk525
Posts: 868
Joined: Sat Feb 03, 2018 7:02 pm

Re: Wifi bridge for RPI Pico

Post by kevinkk525 » Fri Jan 29, 2021 4:49 pm

Thanks for the detailed answer. I took a look at the implementation and I can see some advantages and disadvantages of your approach.

I like that it is rather platform independent but it makes it slow, an SPI approach like the Adafruit is a lot more performant.

Even though I only need mqtt, I like the Adafruit approach of providing a socket object, which could make it possible to simply tell the mqtt_as library to use that socket class. Then the mqtt_as library wouldn't need to be changed and you could use the same mqtt_as library on a client with or without its own WIFI.

I could live without those 2 points but one approach I'm really concerned about is the changed API on the pyboard side. Publish (and other public methods) are synchronous and have a different inner working than mqtt_as. This would make it impossible to use as a simple replacement library as the behaviour and API is different. I'd have to rewrite large parts of my mqtt handler, in which case I could put that time into programming/porting a different approach.
pythoncoder wrote:
Fri Jan 29, 2021 4:06 pm
However I now think there's a great role for it with the Pico. A Pico with an ESP8266 would be an inexpensive way to build an MQTT setup on a baremetal platform.
Given the concerns I raised, I (in my opinion) wouldn't put any effort into reviving this approach. I'd rather suggest thinking about a more general approach of providing WIFI to the PICO based on the work of Adafruit. A generic socket (over e.g. SPI) and a wifi control class (configure wifi from the Pico) would be very helpful and could be used with any project, not just mqtt.

I might tackle those problems eventually but certainly not within the next 4 weeks due to doctors' appointments and even after that I'm quite unsure about my speed/time investment as I've had much trouble concentrating lately..
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 4935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wifi bridge for RPI Pico

Post by pythoncoder » Sat Jan 30, 2021 9:45 am

Good points. It's probably time to retire that project - bringing WiFi to the Pico has a lot more merit.

A generic socket would be ideal: I briefly considered it at one time but couldn't figure out how to do it. I have a recollection that somebody set out to do this, but nothing came of it.

How would you use SPI? An SPI slave is not easy to write, which I guess is the reason that MP doesn't support it. The slave would have to be interrupt driven - but we both know what the latency on an ESP8266 is like. For performance it might need to be written in C with buffering/DMA.

Even I2C presents challenges, see my effort. This relies on the fact that the Pyboard supports slave mode. If the Pico does not support it, that approach wouldn't work. The ESP8266 does not support slave mode. Further, my approach does involve significant latency.

The easy way would be to use an ESP32 and a UART...
Peter Hinch

kevinkk525
Posts: 868
Joined: Sat Feb 03, 2018 7:02 pm

Re: Wifi bridge for RPI Pico

Post by kevinkk525 » Sat Jan 30, 2021 11:42 am

You can see the generic socket implementation of Adafruit Curcuitpython here: https://github.com/adafruit/Adafruit_Ci ... _socket.py

And their SPI implementation here: https://github.com/adafruit/Adafruit_Ci ... sp32spi.py

Those libraries are MIT so that's good too.

Adafruit says:
All you need is an SPI bus and 2 control pins plus a power supply that can provide up to 250mA during WiFi usage.
I looked at the code and the micropython/pyboard/pico side is the SPI master, while the ESP32 is the SPI slave. So there is no special implementation needed on the micropython side. It should be relatively easy to port that project from circuitpython to micropython and use the existing firmware/project on the esp32. If that works as expected, porting the esp32 side (nina-fw project) to the esp8266 might be an option but it's written in C++ and I haven't checked it out carefully enough to know how difficult it would be to port to the esp8266.
Last edited by kevinkk525 on Sun Jan 31, 2021 4:11 pm, edited 1 time in total.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 4935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Wifi bridge for RPI Pico

Post by pythoncoder » Sun Jan 31, 2021 9:54 am

Thanks for that - I was unaware of that Adafruit project. It looks very useful but two things puzzle me.

Firstly, given that they are using an ESP32, I wonder why they chose SPI rather than a UART? Not that it matters in terms of usability, but a UART receiver is easier to code than an SPI slave; and UARTS are very widely supported. Perhaps it's an Arduino issue? Or does ESP32 have hardware support for SPI slave?

Secondly, unless I'm missing something the socket doesn't seem to support nonblocking operation. How do you plan to use it?
Peter Hinch

Post Reply