mqtt_as connections issues

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
ebolisa
Posts: 55
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

mqtt_as connections issues

Post by ebolisa » Fri Nov 05, 2021 6:56 pm

@pythoncoder
Hi Peter,

I read somewhere you prefer be contacted via this forum regarding any issues with mqtt_as.

As I mentioned on my last post, I'm working on a project where I'm using wifimgr to make the ESP32 module mobile, OTA to update the code and mqtt to communicate with node-red.

So, I've been doing lots of testing in the last few days and I noticed that the function (async def wifi_connect (self)) was causing me lot of problems as it was trying to connect to the network when the module was already connected.

Therefore, I make some changes to get around the problem. The function is now checking for a connection first and then continues otherwise, makes a new connection.

Not sure if the code is technically correct as I'm not a programmer, but it solved the problem, for now.

Code: Select all

async def wifi_connect(self):
        s = self._sta_if
        self.dprint('WIFI connection')
        if not s.isconnected():
            self.dprint('Conn error! raising error 1')
            s.active(True)
            s.connect(self._ssid, self._wifi_pw)      
            await asyncio.sleep(5)
            while s.status() == network.STAT_CONNECTING:  # Break out on fail or success. Check once per sec.
                await asyncio.sleep(1)
            raise OSError
        # Ensure connection stays up for a few secs.
        self.dprint('Checking WiFi integrity.')
        for _ in range(5):
            if not s.isconnected():
                self.dprint('Conn error! raising error 2')
                raise OSError  # in 1st 5 secs
            await asyncio.sleep(1)
        self.dprint('Got reliable connection')

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

Re: mqtt_as connections issues

Post by pythoncoder » Sat Nov 06, 2021 10:35 am

This is a known problem, and @kevinkk525 is working on a PR which fixes this and adds extra functionality.

The library was designed on the assumption that an embedded device would power up and initialise the MQTT connection which would then be kept alive through outages. On this assumption the "already connected" state could never arise.

Evidently people are using it in other ways, which will be supported when the PR is in. In the meantime another option is to deliberately disconnect from WiFi prior to initialising mqtt_as.
Peter Hinch
Index to my micropython libraries.

User avatar
ebolisa
Posts: 55
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

Re: mqtt_as connections issues

Post by ebolisa » Sat Nov 06, 2021 11:11 am

pythoncoder wrote:
Sat Nov 06, 2021 10:35 am
In the meantime another option is to deliberately disconnect from WiFi prior to initialising mqtt_as.
That will definitely do it. So, will do a disconnect after the OTA code has done with its routine and let mqtt_as connect again. This way, I can use the mqtt_as lib as designed.

Thank you!!

Post Reply