Official MicroPython MQTT client

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Official MicroPython MQTT client

Post by pfalcon » Sat Jun 11, 2016 10:54 pm

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!
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/

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Official MicroPython MQTT client

Post by deshipu » Sun Jun 12, 2016 7:46 am

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?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Official MicroPython MQTT client

Post by deshipu » Sun Jun 12, 2016 8:14 am

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?

User avatar
patvdleer
Posts: 46
Joined: Mon Jun 13, 2016 11:52 am
Location: Maastricht, NL, Europe
Contact:

Re: Official MicroPython MQTT client

Post by patvdleer » Thu Jun 16, 2016 9:17 pm

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)
NodeMCU v0.9 / V1 / V2 / V3
WeMos D1 Mini
WeMos Lolin32 v1.0.0
WeMos Lolin D32 Pro V2

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

Re: Official MicroPython MQTT client

Post by platforma » Tue Jun 21, 2016 11:29 am

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 :)

digimatic
Posts: 2
Joined: Sat Jun 25, 2016 11:02 am

Re: Official MicroPython MQTT client

Post by digimatic » Sat Jun 25, 2016 11:42 am

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 ?

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

Re: Official MicroPython MQTT client

Post by pfalcon » Wed Jul 13, 2016 8:37 pm

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 ;-).
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/

danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

Re: Official MicroPython MQTT client

Post by danielm » Sat Jul 30, 2016 9:14 am

I would like to ask - what does value returned by connect() mean?

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

Re: Official MicroPython MQTT client

Post by pfalcon » Sat Jul 30, 2016 11:48 am

what does value returned by connect() mean?
Now described in the documentation https://github.com/micropython/micropyt ... README.rst
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/

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

Re: Official MicroPython MQTT client

Post by pfalcon » Sat Jul 30, 2016 11:49 am

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
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