Favorite Social Media Timesink
When you take a break from work, where are you going?
Video clips on TikTok/YouTube
X, Bluesky, Mastodon et al...
Web surfing
I do not get distracted by petty amusements
Edge Computing / Open Source / Tech Culture

Off-The-Shelf Hacker: Run MQTT on a Wearable Device

Jan 24th, 2018 3:01am by
Featued image for: Off-The-Shelf Hacker: Run MQTT on a Wearable Device

Last week, we looked at reading an ultrasonic range finder and displaying the distance on a nice on-screen gauge. I demonstrated how you could use a gauge on a wearable device, such as for a steampunk conference badge.

Since the badge is “connected,” which is to say, hooked up to a local area network over WiFi, we could also use it as a data collection device. Practical? Maybe. The concept could be transferred over to a less dramatic gadget like a small box you’d clip to your belt, put on a table or integrate into a product.

Reliably transferring data from one device to another is a job for a sweet data transfer protocol called MQTT. It’s a subscription-publication server model and can run on the badge or any number of other machines, including notebooks and servers. In MQTT-speak a server is commonly known as a broker. There’s even a client that works on an ESP8266. The server itself is pretty light, so installing it on the conference badge isn’t a big burden, even when equipped with the Raspberry Pi model 2 board.

Today we’ll install MQTT, capture data from the range finder and then publish it to the server, right there on the badge. I used an external HDMI monitor and keyboard for testing, although they are certainly optional once everything is working properly. We’ll also see the data appear on a networked Linux notebook, using a readily available MQTT client. Data could just as easily be captured from buttons, a photocell, a temperature sensor or other sensing devices. You’ll need to install the python-rpi.gpio software to use those devices with the Pi.

In an upcoming story, we’ll look at mating the MQTT server-sensor concept on the badge, with the slick on-screen gauge that we built last week, running remotely on the notebook. MQTT works so much better than a home-grown protocol.

Install MQTT on the Badge

Install the MQTT server and client software either with apt-get or a package manager like synaptic. Here’s a quick rundown on Raspberry Pi MQTT installation.

Testing is pretty straightforward.

First, open a terminal (on the Pi) and execute the mosquitto subscription command. The terminal will then just sit there waiting for data to come into the “rangeval” topic.

In another terminal, publish some “test” data to the rangeval topic.

You should see the word “test” show up in the subscription terminal window, each time you run the publish command.

Python usually is included in versions of Linux, running on the Raspberry Pi. If not, it’s easy to add.

You might also need to install pip, Python’s built-in application installation tool.

Next, open the browser, go to GitHub and download the Paho Python-MQTT library zip file to the Raspberry Pi 2, on the badge. Unzip the file and cd into the resultant directory.

Look for the file and run it using Python

With everything installed, it’s time to move on to publishing the rangefinder data, using a Python script and the MQTT libraries.

Send Real Data Through MQTT

Below is a simple Python script to read serial data and publish it to the badge’s MQTT server. Recall that there’s an Arduino on the badge that manages the rangefinder and sends the data to the Raspberry Pi over the serial port. Serial ports are sometimes flaky, so be sure to run the stty command first.

Copy the following code into a text file then execute it with Python.

If the subscription terminal is still up, you should see rangefinder data immediately scrolling by in the window. Otherwise run the subscription command again.

MQTT on the Linux Notebook

Installing MQTT on a Linux notebook is no big deal either. Use apt-get or your favorite package manager. Be sure to include the MQTT client applications.

Be sure the badge is properly connected to the local network and that the Python script is still streaming data to the server.

You’ll need to know the IP address of the MQTT server, on the badge, to use the MQTT client remotely on the notebook.

Execute the ifconfig command, in a terminal, on the Pi to get the IP address. Look for the “inet” line associated with your WiFi card, typically marked as wlan0.

Make sure the notebook is on the same network and execute the MQTT subscription command in a terminal window.

The -h option is simply the Pi’s IP address and the -t represents the MQTT topic of rangeval. You should see the rangefinder data streaming by in the terminal. Look back at the subscription window on the Pi and you’ll see the exact same data there, too.

That’s the beauty of the MQTT one-to-many and many-to-one publication-subscription model. You could have tens, hundreds or thousands of MQTT clients consuming this data if you wanted. Likewise, you could also publish vast amounts of data, going the other direction, just as easily.

What’s Next

As soon as I work out the bugs with adapting the Processing steampunk on-screen gauge program, using MQTT, on the notebook, I’ll write up a little story covering the topic. Keep in mind that we aren’t necessarily limited to displaying rangefinder data on a remote gauge. We could also use the Python-MQTT client scripts to feed graphs or do analysis or any number of other jobs.

My challenge for physical computing is to identify and hammer out practical uses for this awesome technology. The tools are there. Leveraging the capabilities in productive ways is what Off-The-Shelf Hackers do.

Feature image via Pixabay.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Shelf, Real.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.