Week 2: Flask and MQTT

Week 2: Flask and MQTT

Journal of Learning Embedded Programming

·

4 min read

Introduction

Continuing with Practical Python Programming for IoT, this week I read and programmed along with pages 80 to 160. Here's a brief summary.

Controlling a LED with Flask

Following the tutorials, I implemented a server using Flask-RESTful. It allowed me to control a LED through the REST API. In the tutorial, control of the LED was made accessible over the local network only. To make it accessible over the web would require the configuration of my firewall and/or router.

The html page provided by the book's repository did not work properly. It could be my fault, of course. I did not debug why, as the focus of my learning is on embedded development, not HTML and JavaScript. However, using curl in the terminal, I was able to change the brightness of the LED, which means the Python-Flask script worked.

In the script, gpiozero.PWMLED was used instead of gpiozero.LED because this allows one to set a specific brightness of the LED. With gpiozero.LED, we can only turn it on and off. I'll talk more about this next week.

The next tutorial was about implementing the same features using Flask-SocketIO. I need to read up on sockets - the book suggests two resources for learning about web socket basics: this one and this one.

Controlling a LED with MQTT

MQTT in the Terminal

MQTT was both fun and interesting to work with. I can't wait to learn more about it. The book recommended this ten-part blog series for further studies.

MQTT stands for Message Queue Telemetry Transport

I installed mosquitto and started it with sudo systemctl start mosquitto.

Configurations were specified in mosquitto_pyiot.conf, then copied it to another location:

$ sudo cp mosquitto_pyiot.conf /etc/mosquitto/conf.d/

Then, after restarting with systemctl, the html/js files in the http_dir, which was defined in the config, could be viewed in the browser on localhost:<port>.

MQTT is a publish/subscription messaging protocol. The MQTT broker handles the publishing and subscription. Example: A sensor might publish its measurements to the broker. Then, if a client subscribes to this, it will receive the measurements. A client can simultaneously be a publisher and a subscriber. Perhaps it publishes commands to be executed when sensor values reach a certain point. A client only interacts with the broker and is unaware of other clients.

There were multiple exercises about creating listeners and publishers. In one terminal window, I made the listener with the command:

$ mosquitto_sub -v -h localhost -t 'pyiot'

-t is the topic you want to subscribe to and listen to, here "pyiot".

In another terminal, I published messages on the "pyiot" topic like this:

$ mosquitto_pub -h localhost -t ‘pyiot’ -m ‘hello, there’

The message appeared in the other terminal where I was listening. The message format is <topic> <payload>; in this case, pyiot hello, there.

You publish to a specific topic. You can subscribe to a range of topics using wildcard characters. ‘#’ on its own subscribes to all topics.

With QoS (Quality of Service) we can specify how many times messages are delivered. There are three levels (0, 1, and 2). Some take more resources than others because of more attempts.

Adding the -q flag to the above commands, allows us to specify the QoS level. E.g., -q 2.

Adding -d enables debugging.

With the -r flag, you can set a retained message. A subscriber will receive the retained message when logging on.

Set it like this:

$ mosquitto_pub -r -q 2 -h localhost -t 'pyiot' -m 'Welcome to pyiot, my dear!'

The retained message can be cleared by publishing an empty retained message:

$ mosquitto_pub -r -q 2 -h localhost -t 'pyiot' -m ''

Durable connections: The client subscribing can provide an ID and a -c (--disable-clean-session) flag to receive messages that are published even when it is offline. The missed messages will appear the next time the client logs on to start listening again. Both subscriber and publisher must use QoS level 1 or 2 for this to work. The ID can be specified with the -i flag, e.g. -i myid1.

Publishers and subscribers can define a “will”, so that if they suddenly die or disconnect from the broker, the broker will send the “will” message to the subscribers.

MQTT in Python

In Python, we used the paho-mqtt library.

Following the book, I made a script to control the LED through MQTT commands. It also worked in the browser by the provided HTML and js files. Additionally, the terminal and browser stayed in sync when changing the LED values. Very cool.

Summary

It was interesting to see different ways to implement the same LED control. I am so excited about MQTT, and I can't wait to begin implementing it in personal projects (when I have a speck of spare time). Yey!