Driving LEDs on a Raspberry Pi via MQTT

MQTT is essentially an Internet of Things protocol which is designed to pass single short messages between a set of devices publishing messages to a set of devices which are subscribed to that topic.

A fantastic example of where you might use MQTT is an Internet-connected light switch plus light bulbs. You would first define a topic for your room (i.e. home/livingroom/lights/ceiling) and then configure all the appropriate light bulbs to subscribe to that topic and listen for any messages published to it. You could then configure a light switch on the wall to publish "ON" when you flip it up, and "OFF" when you flip it down. Flip the switch up, it publishes the message to the MQTT broker, which forwards it to all the lightbulbs subscribed to the home/livingroom/lights/ceiling topic, which would all respond accordingly. BOOM! IoT paradise! What could possibly go wrong?!

I've been meaning to do something with MQTT for several years, but the recent (outstanding) series on MQTT published on HaD has finally kicked me into action. This post is documenting the low-voltage half of a project I'm working on to upgrade my status lights at work.
Stack lights, or Andons, are traditionally used in industrial settings to alert tool operators/management to issues on the factory floor. I instead have one mounted on my cubicle at work which I use to show when I'm in the office, in the lab, off-site, etc. It is... a little conspicuous in the middle of a cube farm, but my coworkers have learned to appreciate its utility.

Until now, I've controlled mine via three toggle switches on my desk, but why use toggle switches where you can use technology?

Video:


On the Raspberry Pi, I'm running a few dozen lines of Python to subscribe to the MQTT topic using the Paho-MQTT library and write to the GPIO pins using the RPi.GPIO library (which comes pre-installed in Raspbian)

To set up the Raspberry Pi, install Raspbian, then install paho-mqtt by running this in a terminal:

sudo pip install paho-mqtt

The source code consists of defining a call-back function for the MQTT library to call when it connects to the server, and a second call-back function to call to process each incoming message. Once the callbacks are defined, the GPIO pins are configured as outputs and control is handed off to the MQTT library to process messages forever.


Running the script would look like this:

wget https://gist.github.com/PhirePhly/5eda4214788429e9d09c060cca10971f/raw/8696da1520b76fee9c2213503a4e125d9ce1bfdd/mqtt_led_client.py
chmod +x mqtt_led_client.py
./mqtt_led_client.py

There are a dozen different ways to have programs automatically run on boot in Linux systems, but my tool of choice is to add a line to my cron table (via running crontab -e):
@reboot /home/pi/mqtt_led_client.py

All that's left to do at this point is have the Pi's GPIO's drive transistors instead of LEDs to light up the 24V stack light I managed to find in a debris pile at work and button the whole project up enough to sit on my desk at work.

Popular Posts