mqtt_as Arduino RP2040 Connect

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
sdcmhp
Posts: 2
Joined: Tue Apr 05, 2022 10:49 pm

mqtt_as Arduino RP2040 Connect

Post by sdcmhp » Tue Apr 05, 2022 11:09 pm

been trying to improve MQTT on Arduino RP2040 connect with mqtt_as. To get the device to connect to wifi I have had to change the code from

Code: Select all

       if s.status() == network.STAT_CONNECTING:
to what you see below.

Code: Select all

        if not s.isconnected() and self._ssid is not None and self._wifi_pw is not None:
            s.connect(self._ssid, self._wifi_pw)
            while s.isconnected() == False: # network.STAT_CONNECTING: 
                print(s.status())
                await asyncio.sleep(1)
            print('wifi connect')
If I try to use the same code as the ESP the console shows:

NameError: name 'STAT_CONNECTING' isn't defined

Trying to learn uP so I'm wondering what I am missing here. The WLAN should return the constant but status() just returns True and False.

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

Re: mqtt_as Arduino RP2040 Connect

Post by pythoncoder » Wed Apr 06, 2022 9:49 am

I don't yet have an Arduino RP2040 so I haven't been able to test this. Unfortunately there are firmware variations between ports, so it seems that STAT_CONNECTING is not supported. This may be a consequence of the design of the WiFi hardware module.
[EDIT]
Your solution has the drawback that it could hang forever if connection didn't complete. I would try copying the code used for the LOBO port:

Code: Select all

                i = 0
                while not s.isconnected():
                    await asyncio.sleep(1)
                    i += 1
                    if i >= 10:
                        break
I've ordered a board and will update the driver in due course.

Note, I've added code tags to your post. You will be able to use these yourself when you've had two posts approved.
Peter Hinch
Index to my micropython libraries.

sdcmhp
Posts: 2
Joined: Tue Apr 05, 2022 10:49 pm

Re: mqtt_as Arduino RP2040 Connect

Post by sdcmhp » Wed Apr 06, 2022 6:02 pm

Thank you for the information. I will give it a try.

I have an RP2040 Connect that I can send to you if you would like one for testing.

If you are interested please let me know.

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

Re: mqtt_as Arduino RP2040 Connect

Post by pythoncoder » Thu Apr 07, 2022 9:57 am

sdcmhp wrote:
Wed Apr 06, 2022 6:02 pm
...
I have an RP2040 Connect that I can send to you if you would like one for testing...
Thanks for the offer but I have one on order which should arrive today or tomorrow :)
Peter Hinch
Index to my micropython libraries.

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

Re: mqtt_as Arduino RP2040 Connect

Post by pythoncoder » Thu Apr 07, 2022 4:49 pm

This contraption arrived today; it seems that the firmware paint isn't quite dry. For a start, if I connect with

Code: Select all

import network
sta_if = network.WLAN(network.STA_IF)
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(False)         # deactivate the interface
if not sta_if.isconnected():
    print('connecting to network...')
    sta_if.active(True)
    sta_if.connect('network_name', 'password')
    while not sta_if.isconnected():
        pass
print('network config:', sta_if.ifconfig())
a = sta_if.config('mac')
print('MAC {:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(a[0],a[1],a[2],a[3],a[4]))
I get

Code: Select all

network config: ('0.0.0.0', '75.255.1.0', '1.0.0.0', '113.154.0.16')
which is nonsense. I get different nonsense a few seconds later:

Code: Select all

>>> sta_if.ifconfig()
('0.0.0.0', '3.82.0.16', '0.0.0.0', '0.0.0.0')
Compare with a valid config e.g. ESP32:

Code: Select all

network config: ('192.168.0.36', '255.255.255.0', '192.168.0.1', '208.67.220.220')
Regarding mqtt_as the nonblocking socket produces OSError -110 on occasion. If I modify the code to ignore these errors, mqtt_as runs for about 20s when the WiFi gets dropped. It recovers and resumes operation, but sta_if.isconnected() suddenly returns False after another 20s for no apparent reason. The RSSI is good and an ESP32 in the same room runs fine. The WiFi stays up if I don't actually use it...

I'll see if I can produce a simple test case to raise an issue. Has anyone else observed WiFi issues with this board?

I've moved this thread to the Raspberry Pi subforum where it is more likely to be seen by other users.
Peter Hinch
Index to my micropython libraries.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: mqtt_as Arduino RP2040 Connect

Post by scruss » Thu Apr 07, 2022 6:37 pm

A bunch of the Nano RP2040 Connect boards shipped with wifi firmware that was, well, a bit soggy. It might be worth looking at Upgrading Nano RP2040 Connect NINA Firmware

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

Re: mqtt_as Arduino RP2040 Connect

Post by pythoncoder » Fri Apr 08, 2022 12:59 pm

scruss wrote:
Thu Apr 07, 2022 6:37 pm
A bunch of the Nano RP2040 Connect boards shipped with wifi firmware that was, well, a bit soggy. It might be worth looking at Upgrading Nano RP2040 Connect NINA Firmware
Sadly attempting to do that has confirmed my view that this device isn't ready for primetime as the instructions don't actually work. This seems surprising: my recollections of Arduino are positive.

I went through the procedure first using Arduino Cloud IOT which said "something went wrong".

Using the command line instructions I installed the blink sketch, taking the board back to its delivered state and tried again, with the same result - so presumably MP isn't the problem.

I then tried the command line approach to upgrading with this outcome, repeated forever.

Code: Select all

$ ./arduino-fwuploader firmware flash -b arduino:mbed_nano:nanorp2040connect -a /dev/ttyACM0 -m NINA@1.4.8
error during sketch flashing: exit status 1
rp2040load 1.0.1 - compiled with go1.15.8
.....................
error during sketch flashing: exit status 1
rp2040load 1.0.1 - compiled with go1.15.8
.....................
Note that /dev/ttyACM0 does exist and the board showed some response in the LED's, so the comment in the troublehooting section seemed wrong - the updater clearly did find the board.

Finally I tried the suggestion at the very end of the web page of resetting the bootloader, but that produced the same result - this time inevitably, as /dev/ttyACM0 does not exist after a bootloader reset with GND linked to REC.

Have you any thoughts as to how to make this work?
Peter Hinch
Index to my micropython libraries.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: mqtt_as Arduino RP2040 Connect

Post by scruss » Fri Apr 08, 2022 2:53 pm

It's kind of a sad day when Arduino - aka 'the original and best' - has worse instructions for their $$ hardware than Lolin, who sell direct from AliExpress.

If you're running Linux, the only thing I can think of is that you're missing a udev rule. It seems that the Arduino IDE package updater assumes you're able to update system scripts as root (eek!) and will lose important warning messages during installation. Digging through arduino-fwuploader's issues it suggests using ArduinoCore-mbed/post_install.sh to add this udev rule:

Code: Select all

SUBSYSTEMS=="usb", ATTRS{idVendor}=="2e8a", MODE:="0666"
This is the Vendor ID for Raspberry Pi (Trading) Limited, so you might already have it.

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

Re: mqtt_as Arduino RP2040 Connect

Post by pythoncoder » Sat Apr 09, 2022 8:57 am

Thanks for the tips. And I agree about Arduino - on past experience I started the update thinking "this will be easy"...

I put the udev rule in place, rebooted, and tried again. The online installer failed in the same way ("something went wrong"), but I figured this was because I'd reinstated MP. So I repeated the command line approach, loading the blink sketch before running the firmware installer and it ran to completion.

However the symptoms in MP remain the same, with .ifconfig returning rubbish and umqtt.simple refusing to connect. Perhaps you or someone else might like to try this script (adapting WiFi credentials and MQTT broker address). Or just run the first part of the script to connect to WiFi and for further amusement check sta_if.ifconfig() again after completion. It changes from one ridiculous result to another...

Code: Select all

import network
sta_if = network.WLAN(network.STA_IF)
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(False)         # deactivate the interface
if not sta_if.isconnected():
    print('connecting to network...')
    sta_if.active(True)
    sta_if.connect('NetworkName', 'Password')  # ADAPT
    while not sta_if.isconnected():
        pass
print('network config:', sta_if.ifconfig())  # Prints a pile of ordure
a = sta_if.config('mac')
print('MAC {:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(a[0],a[1],a[2],a[3],a[4]))

from umqtt.simple import MQTTClient
from time import sleep

def main(server="192.168.0.10"):  # ADAPT
    c = MQTTClient("umqtt_client", server)
    c.connect()  # Fails here
    while True:
        c.publish(b"shed", b"hello", qos=1)
        sleep(5)
main()
Peter Hinch
Index to my micropython libraries.

beetle
Posts: 51
Joined: Sat Oct 16, 2021 11:35 am

Re: mqtt_as Arduino RP2040 Connect

Post by beetle » Sat Apr 09, 2022 6:10 pm

Some six months ago I don't think there was a micropython verion available for this board so I put CircuitPython on it. A couple of days ago I saw there were instruction form Arduino on how to put micropython on the board so I followed along. After coping the ARDUINO_NANO_RP2040_CONNECT-20220117-v1.18.uf2 on the board into the Rpi folder, a reboot still bought us the Circuit Python folder (on my mac). So then I though maybe to get rid of all the previous stuff I would use the Arduino IDE and put on a C++ program.
After that, again I tried to put on the micropython.uf2 and boot up Thonny. The blinking serial output of Thonny showed it was still running my C++ program and I could not get a micropython prompt. I ended up putting CircuitPython back on the board and all is well again.

I dont think the micropython version is at all ready for this board yet.

But, Dear Peter Hinch, it would be really great if the mqtt_as could eventually be persuaded to run as I had ideas about running that on my Arduino Nano RP2040 boards. I originally had ideas of running your mqtt_as on a rpi pico with an attached ESP8266 but then found there was no network module in the pico distribution. I have got your other networking examples running on the pico / wemos esp8266 combination though, and very nice it runs too.

Post Reply