mqtt_as

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
beetle
Posts: 51
Joined: Sat Oct 16, 2021 11:35 am

mqtt_as

Post by beetle » Sat May 14, 2022 11:59 pm

I'm using the excellent mqtt_as library by Peter Hinch @pythoncoder. One issue I am trying to solve is when the program running on an ESP32 automatically starts when the power returns after a power cut. My mqtt broker running on a rpi is not immediately available when the ESP bursts into life as it takes longer to fire up. In this case I get a connection failed message and the program does not appear to retry the connection if the first connection cannot be made.
I have tried the following code:

Code: Select all

# main function - called by asyncio.run 
async def main(client):
    x = 1
    async def connection(x):
        try:
            await client.connect()
            x = 100
            return x
        except OSError as e:
            print('Connection failed.', e)
            x += 1
            return x    
    while x < 10:
        print(f'Connection tries = {x}')
        x = await connection(x) 
    if x < 11:
        globs.run_program = False 
        
 
in the hope I could circle around for 10 times before giving up. However the program will only ever try to connect 2 times before then giving an error of Wifi Internal Error, as per the following:

Trying to connect for 1 time
Connection failed. -1
Trying to connect for 2 time
Connection failed. -1
Trying to connect for 3 time
Connection failed. Wifi Internal Error
Trying to connect for 4 time
Connection failed. Wifi Internal Error
Trying to connect for 5 time
Connection failed. Wifi Internal Error
Trying to connect for 6 time
Connection failed. Wifi Internal Error
Trying to connect for 7 time
Connection failed. Wifi Internal Error
Trying to connect for 8 time
Connection failed. Wifi Internal Error
Trying to connect for 9 time
Connection failed. Wifi Internal Error

Now I tried to search the mqtt_as.py to find this message but I have not located it. In any case I suspect there is a better way to proceed. Any suggestion on a better way of ensuring the mqtt connection is only made once the mqtt broker is actually up and running would be appreciated.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: mqtt_as

Post by davef » Sun May 15, 2022 12:25 am

For the last month or so I have been running this before I connect:

Code: Select all

    if sta_if.isconnected():
        sta_if.disconnect()
        print ('started in the connected state')
        print ('but now it is disconnected')
    else:
        print ('started in the disconnected state')
then proceed with trying to connect.

Next I do:

Code: Select all

    utime.sleep(1)

    if not sta_if.isconnected():
        print('connecting to hotspot...')
        sta_if.active(True)
        sta_if.ifconfig((config.WiFi_device_7bd5, '255.255.255.0', config.gateway_7bd5, '8.8.8.8'))

        try:
            sta_if.connect(config.hotspot_7bd5, config.password_7bd5)
        except OSError as error:
            try:
                with open('errors.txt', 'a') as outfile:
                    outfile.write(str(error) + '\n')
            except OSError:
                pass
and now I don't see any more Wifi Internal Error.

This is for a "normal" WiFi connection, maybe it helps your case.

Good luck!

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

Re: mqtt_as

Post by pythoncoder » Sun May 15, 2022 8:14 am

The module does not retry if the initial connection fails. This is by design: usually such a failure means that something is wrong which needs manual rectification. If it did retry the process would go on forever.

@beetle I'm not sure why your repeated attempts provoke an internal error. I've never seen this, but I've never tried to do what you're doing. One possibility is, in the event of an initial connection failure, to wait a sufficiently long time for the broker to have started. You could then either try again, or issue machine.reset(). I think I prefer the reset option as it presumably won't have a limit to the number of times it might be called.
Peter Hinch
Index to my micropython libraries.

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

Re: mqtt_as

Post by beetle » Sun May 15, 2022 4:19 pm

@davef @pythoncoder
Thanks to both for the responses. Currently I had been testing by just bringing down the mqtt broker leaving the network and wifi working ok but I will bring the whole shebang down for a final bit of testing and the network suggestions of davef on wifi may well prove to be needed.

Thus far I have tried the machine.reset as suggested by pythoncoder and that works perfectly. I have put in a 20 second delay before the reset is triggered not to stress to poor esp32 overly (though I dont suppose it really matters).

A final though on the mqtt_as is that it would be good if it incorporated the use of a direct ethernet link and well as wifi. (I have a Teensy 4.1 running micropython with a direct ethernet port, and I see there are options to use an ethernet connection with an rpi pico and also, I think, the Pybord D. I don't suppose a direct link would really need to be so resilient, but I'm finding I do like to use the async style of programming where I can, and where I can work out how to do it ;) (I'm ploughing through Peter Hinch's tutorials on this but I dont think I will end up an expert), so it would be good to be able to drop in a well known mqtt async library suitable for all cases. I saying this I'm aware that folks (Peter mainly ) will have to put in much time an effort to achieve this so just take it as a throwaway thought and thanks for the current mqtt_as which I find very useful.

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

Re: mqtt_as

Post by pythoncoder » Mon May 16, 2022 9:34 am

The idea of extending mqtt_as to handle wired networks has been discussed and Kevin Köck did some work towards doing this. There is a partially complete PR for this. Unfortunately for personal reasons he has been unable to progress this further.
Peter Hinch
Index to my micropython libraries.

Post Reply