MQTT Broker and Client on Raspberry Pi

A project log for MQTT Porchlight is an Intranet of Thing

Controlling a Wemo light switch with MQTT and Raspberry Pi

Mike SzczysMike Szczys 12/09/2019 at 20:500 Comments

Although I've know about MQTT for years, this is the first I've done beyond a "hello world" style commandline exploration of it. The lightweight protocol runs on a ton of devices and is just a simple text delivery protcol based on a publish/subscribe model.

Follow Elliot's Tutorial:

This process was super-easy for me because Elliot wrote a 4-part series on MQTT (I only needed 2 parts of it for this project) that everyone should check out:

I have a Raspberry Pi on my network so installing a broker using the "mosquitto" package was a snap. One gotcha I ran into was the Raspbian had masked the broker service so I had to use the "systemctl unmask mosquitto.service" before enabling it and starting it with same.

MQTT Python Client:

Like I said, MQTT is just a simple text protocol so it's not going to do anything for you. The nice thing is that this makes it very flexible. I wanted three things to start: On,Off,Status -- so I just had to write functions that would listen for those incoming messages and react accordingly.

I'm not using GPIO on the Pi but this guide is what I followed to get my MQTT up and running:

This uses the paho-mqtt package which can be installed with pip:

pip3 install paho-mqtt

The code I came up with is found on my repo: