Page 1 of 2

Official MicroPython MQTT client

Posted: Sat Jun 11, 2016 10:54 pm
by pfalcon
During May, while we resolved issues with ESP8266 port initial release and technical debt in MicroPython code in general, there was also work on MQTT client implementation, which is a first ESP8266 port Kickstarter campaign stretch goals. We briefly considered included code we had at that time in 1.8.1 binary release to the backers, but decided against it, as it was largely in flux, and releasing unfinished design and code could do more harm than good.

However, after 1.8.1 release, I immediately finished most of the remaining work items for initial working version, and pondering about it during this week, didn't find something which requires further big changes. So, I'm going to proceed to the next stage of testing and optimization, and wanted to share this progress report.

If you're interested to play with a still unfinished client, or waited for MQTT support all this time and want to give it a try, you may find related info and share your findings at https://github.com/micropython/micropython/issues/2055 .

Just to clarify again, this is not a release of MQTT client, but a progress report and an invitation for developers and advanced MQTT users to give it a try and report results. It's not yet suitable for end users, but we are definitely going to include it in the next release, by which time it should be much better tested and optimized.

Thanks again to all the Kickstarter campaign backers which made this work possible!

Re: Official MicroPython MQTT client

Posted: Sun Jun 12, 2016 7:46 am
by deshipu
I tried to look at the code, but couldn't find anything. The only link to a branch in that issue is a 404, and there are no mqtt-related commits in either micropython or miropython-lib repo.

Are you *sure* you want us to look at it?

Re: Official MicroPython MQTT client

Posted: Sun Jun 12, 2016 8:14 am
by deshipu
I found this branch in micropython-lib: https://github.com/micropython/micropyt ... /tree/mqtt

It seems to have some mqtt-related work in it. Is this the code you are talking about, or some earlier experiment?

Re: Official MicroPython MQTT client

Posted: Thu Jun 16, 2016 9:17 pm
by patvdleer
Let me know how I can help, I do use MQTT but on a very(!!!) limited class I made based on some work I found around here on the forum. Would be nice to have a client which supports login and optionally ssl/tls.

Code: Select all

import socket
import time
try:
    import binascii
except ImportError:
    import ubinascii as binascii

import config


class MQTT(object):
    s = None
    addr = None
    prefix = ""

    def __init__(self, host, port=1883, prefix=config.MQTT_PREF):
        self.addr = socket.getaddrinfo(host, port)[0][4]
        self.prefix = prefix
        self.reset_socket()

    def reset_socket(self):
        self.s = socket.socket()

    def connect(self):
        address = self.addr
        self.s.connect(address)
        self.s.send(self.mtpConnect(config.DEVICE_NAME))

    def disconnect(self):
        self.s.send(self.mtpDisconnect())
        self.s.close()

    def publish(self, topic, data, sleep=1):
        self.s.send(self.mtpPub(self.prefix+topic, bytes(str(data), 'ascii')))
        if sleep:
            time.sleep(sleep)

    def recv(self, length=4096):
        return binascii.hexlify(self.s.recv(length))

    @staticmethod
    def mtStr(s):
        return bytes([len(s) >> 8, len(s) & 255]) + s.encode('utf-8')

    @staticmethod
    def mtPacket(cmd, variable, payload):
        return bytes([cmd, len(variable) + len(payload)]) + variable + payload

    def mtpConnect(self, name):
        return self.mtPacket(
            0b00010000,
            self.mtStr("MQTT") +  # protocol name
            b'\x04' +  # protocol level
            b'\x00' +  # connect flag
            b'\xFF\xFF',  # keepalive
            self.mtStr(name)
        )

    @staticmethod
    def mtpDisconnect():
        return bytes([0b11100000, 0b00000000])

    def mtpPub(self, topic, data):
        return self.mtPacket(0b00110001, self.mtStr(topic), data)

Re: Official MicroPython MQTT client

Posted: Tue Jun 21, 2016 11:29 am
by platforma
deshipu wrote:I found this branch in micropython-lib: https://github.com/micropython/micropyt ... /tree/mqtt

It seems to have some mqtt-related work in it. Is this the code you are talking about, or some earlier experiment?
I do think that's what he was referring to, I was actually looking at that branch when the commits appeared and then the post followed :)

Re: Official MicroPython MQTT client

Posted: Sat Jun 25, 2016 11:42 am
by digimatic
am I correct in assuming where TLS support currently exists it is only TLSv1 and not TLSv2 ?

Examples like this https://www.pycom.io/connecting-to-microsoft-azure/ only mention v1, has the capability been expanded since then ?

Re: Official MicroPython MQTT client

Posted: Wed Jul 13, 2016 8:37 pm
by pfalcon
umqtt.simple module was officially "released": https://pypi.python.org/pypi/micropython-umqtt.simple . It's also included in ESP8266 official binary release as sent to the backers of the Kickstarter campaign. Documentation and examples are available at https://github.com/micropython/micropyt ... qtt.simple . The module will be of course developed further - it's only v1.0.1 now ;-).

Re: Official MicroPython MQTT client

Posted: Sat Jul 30, 2016 9:14 am
by danielm
I would like to ask - what does value returned by connect() mean?

Re: Official MicroPython MQTT client

Posted: Sat Jul 30, 2016 11:48 am
by pfalcon
what does value returned by connect() mean?
Now described in the documentation https://github.com/micropython/micropyt ... README.rst

Re: Official MicroPython MQTT client

Posted: Sat Jul 30, 2016 11:49 am
by pfalcon
A version of MQTT client with auto-reconnect on errors support, umqtt.robust, was merged to master: https://github.com/micropython/micropyt ... qtt.robust . Docs are coming, in the meantime, see comments in an example: https://github.com/micropython/micropyt ... _robust.py