MQTT library and thingsboard

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
ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

MQTT library and thingsboard

Post by ajie_dirgantara » Wed Aug 22, 2018 7:20 am

Hi,

I am using mqtt library for micropython, and when connecting to thingsboard via mqtt, it required to supply username with authentication token and password set to "" (no password). Using no password, mqtt libray is failed to connect, while if I connect to another mqtt broker with password, it work just okay. I think it has something to do with the library. It doesn't support "" (empty) password.

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

Re: MQTT library and thingsboard

Post by pythoncoder » Wed Aug 22, 2018 10:52 am

It should work if you don't supply a password to the constructor, or pass None. The default is None. This is different to "".
Peter Hinch
Index to my micropython libraries.

ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

Re: MQTT library and thingsboard

Post by ajie_dirgantara » Fri Aug 24, 2018 6:51 pm

pythoncoder wrote:
Wed Aug 22, 2018 10:52 am
It should work if you don't supply a password to the constructor, or pass None. The default is None. This is different to "".

if I don't supply any password, it will raise exception TypeError 'NoneType' has no length at simple.py line 60

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: MQTT library and thingsboard

Post by SpotlightKid » Fri Aug 24, 2018 10:26 pm

Yep, looks like a bug to me(it's actually line 68, AFAICS). Code doesn't check whether self.pswd is None or an empty string and sets the password flag in the connect flags regardless. It inccorrectly assumes that if the user is set, the password is not none.

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

Re: MQTT library and thingsboard

Post by pythoncoder » Sat Aug 25, 2018 4:31 am

Well spotted: in my testing neither was supplied. I suggest you raise an issue or PR. I guess you need something like:

Code: Select all

sz += 2 + len(self.user) + 2 + (len(self.pswd) if self.pswd is not None else 0)
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: MQTT library and thingsboard

Post by SpotlightKid » Sat Aug 25, 2018 8:20 am

The broker may handle an empty password differently than no password being supplied. I think the code should rather be something like:

Code: Select all

if self.user is not None:
    sz += 2 + len(self.user) 
    msg[6] |= 1 << 7
    if self.pswd is not None:
        sz += 2 + len(self.pswd)
        msg[6] |= 1 << 6
And below at line 95:

Code: Select all

if self.user is not None:
    self._send_str(self.user)
    if self.pswd is not None:
        self._send_str(self.pswd)

BTW, where does one submit PRs to micropython-lib these days? At https://github.com/micropython/micropython-lib/ or https://github.com/pfalcon/micropython-lib ? The former hasn't seen a commit since february.

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

Re: MQTT library and thingsboard

Post by pythoncoder » Sun Aug 26, 2018 6:13 am

My understanding is that the official library is https://github.com/micropython/micropython-lib.git - ref this post from Damien.

Since the unfortunate loss of @pfalcon as a maintainer, library PR's are not always handled in a timely fashion. This is why I (reluctantly) maintain a fork of uasyncio. I'd hope that a bugfix PR will achieve a higher priority response than my efforts at enhancements.
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 library and thingsboard

Post by pythoncoder » Mon Aug 27, 2018 6:40 am

Looks good to me.
Peter Hinch
Index to my micropython libraries.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: MQTT library and thingsboard

Post by pfalcon » Sun Sep 23, 2018 7:28 pm

BTW, where does one submit PRs to micropython-lib these days? At https://github.com/micropython/micropython-lib/ or https://github.com/pfalcon/micropython-lib ? The former hasn't seen a commit since february.
The official project for micropython-lib by its author is at https://github.com/pfalcon/micropython-lib . Bugreports/pull requests are welcome there.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply