micropython and MQTT

First tests with micropython and MQTT.

On Ubuntu 17.04:

If we want to use our system as a MQTT broker, we need the mosquitto daemon. The mosquitto-clients package include commands to test publishing and subscribing from the command line:

sudo apt-get install mosquitto mosquitto-clients

To install micropython just follow instructions for Debian-based systems.

Then we need to install a MQTT library for micropython. Used ‘umqtt.simple‘:

./micropython -m upip install micropython-umqtt.simple

They have some examples, let’s test ‘example_pub.py‘:

from umqtt.simple import MQTTClient

def main(server="localhost"):
    c = MQTTClient("umqtt_client", server)
    c.connect()
    c.publish(b"foo_topic", b"hello")
    c.disconnect()

if __name__ == "__main__":
    main()

This example publish “hello” message to topic “foo_topic” on a broker on our localhost. If we have installed ‘mosquitto’, it is already started (at least on Ubuntu) so we just need to subscribe for messages under “foo_topic”:

mosquitto_sub -h localhost -t foo_topic

We can now run the example (on another terminal):

./micropython example_pub.py

and we should get:

hello

 

Now let’s use a public MQTT broker. There are several available but since we are already using mosquitto client we will also use mosquitto own public broker (i.e. ‘test.mosquitto.org‘).

Since it is a public broker, it has considerable traffic so we use a less general topic:

from umqtt.simple import MQTTClient

SERVER='test.mosquitto.org'

def main(server=SERVER):
    c = MQTTClient("umqtt_client", server)
    c.connect()
    print("Connected")
    c.publish(b"ev3dev_topic", b"Make my day!")
    c.disconnect()
    print("Sent")

if __name__ == "__main__":
    main()

And we subscribe to it:

mosquitto_sub -h test.mosquitto.org -t ev3dev_topic

and it still works. It’s not instant messaging but it sure takes less than a second (please note that a public broker cannot assure 100% availability or performance; even it the service is available performance can be degraded if lots of publishers and subscribers are connected passing lots of messages).

Now let’s do the opposite – subscribe with micropython. We use another example, ‘example_sub.py’:

 

import time
from umqtt.simple import MQTTClient

# Publish test messages e.g. with:
# mosquitto_pub -t foo_topic -m hello

SERVER = "test.mosquitto.org"

# Received messages from subscriptions will be delivered to this callback
def sub_cb(topic, msg):
    print((topic, msg))

def main(server=SERVER"):
    c = MQTTClient("umqtt_client", server)
    c.set_callback(sub_cb)
    c.connect()
    c.subscribe(b"ev3dev_topic")
    while True:
    if True:
        # Blocking wait for message
        c.wait_msg()
    else:
        # Non-blocking wait for message
        c.check_msg()

        # Then need to sleep to avoid 100% CPU usage (in a real
        # app other useful actions would be performed instead)
        time.sleep(1)

    c.disconnect()

if __name__ == "__main__":
    main()

 

For this script to run we also need ‘time’ library for micropython:

./micropython -m upip install micropython-time

So if we now publish something:

mosquitto_pub -h test.mosquitto.org -t ev3dev_topic -m Yahoo!

we’ll get:

$ ./micropython testsub.py 
(b'ev3dev_topic', b'Yahoo!')

Next: port this to ev3dev.

 

 

 

 

 

 

 

 

 

 

 

Now the first test: