Edge / IoT

Off-The-Shelf Hacker: Machine Vision Meets MQTT Messaging

3 Aug 2019 6:00am, by

In the past few weeks, we’ve covered recognizing objects with the JeVois sensor and sending the data to text-to-speech Processing scripts. I started thinking that passing the recognized object name and location data to other machines and systems, using the MQTT protocol, opens up a lot of additional automation possibilities. MQTT has become a common message-exchanging standard in the Internet of Things (IoT) and Industrial Internet of Things (IIoT) worlds.

Integrating MQTT messaging into our Processing scripts is fairly straightforward with a few additional lines of code. Posting MQTT messages to a networked broker makes the data readily available for logging purposes, announcements, controlling physical actions and other uses.

Machine Vision Plus MQTT

We’ll need an MQTT broker to be able to send messages back and forth. JakeMakes has a pretty good installation procedure. I also covered MQTT installation in a past wearables story.

I installed the Mosquitto MQTT broker and clients on my Raspberry Pi-based talking skull. It runs automatically at boot-up, so is available whenever it needs to handle messages to or from clients. I also installed the MQTT contributed library through the Processing IDE. The modded text-to-speech Processing script takes the recognized object name, passes it into an MQTT publish function and makes it available through the broker. I also installed the standard MQTT broker/client package using the Synaptic application manager on my ASUS Linux notebook to be able to test the new Processing script on another networked machine. I could examine the messages (recognized name and location data) using the following command line, once software changes were made to the Processing script.

Here’s the command line I used on the Linux notebook to see the MQTT messages from the JeVois sensor.

The following procedure adds the MQTT library through the Processing IDE. Start Processing on the Raspberry Pi. Under the “Sketch” tab, click “Import Library,” then “Add Library.” Type “MQTT” into the Contributing Manager search box and then hit “Enter.” Click the “MQTT library for Processing based on the Eclipse Paho project” from Joel Gaehwiler. Push the “Install” button at the lower right-hand corner to install the library and then close the window upon installation completion. I’m using version 3.5.3 of Processing, both on the ASUS Linux notebook and Hedley’s Raspberry Pi. The MQTT Processing library version on both machines was 1.7.1. The Mosquitto broker and clients on the Linux notebook are at version 1.4.15-2ubuntu0.18.04.3.

Modding Last Week’s Code

One of the coolest things about prototyping physical computing projects today is that you can plug hardware modules together, then radically change a gadget’s behavior by changing some code. Point to a newly installed library here, add a few lines of code there and poof…new functionality. Processing has a lot of interesting libraries you might find useful. Cousin, Arduino IDE has similar libraries.

Here’s the Processing code.

The MQTT library and client references are initialized at the beginning of the script. The client and connection are then set up with the IP address of the broker. Be sure to include the “mqtt://” part of the address. You can also use an external MQTT broker. Mosquitto has a public test broker that is free to use. Insert “mqtt://test.mosquitto.org” in place of the IP address, as shown in the commented outline.

The actual message going to the broker is handled by the client.publish() function. In our case, “mqtt5” is the topic. The variable q[1] refers to the recognized name. q[2] and q[3] correspond to the x and y coordinates of the recognized object, within Hedley’s field of view. Could you use object location data, in your machine vision/MQTT project?

One odd situation I had was a “RuntimeException: MQTT] Callback not found” error. Not sure about the cause. The “messageReceived()” function appeared in one of the example programs under the MQTT library, so I just included it in the code and it got rid of the error.

Next Steps

There are lots of possible use cases for the JeVois sensor data and MQTT.

Maybe we could connect the JeVois sensor to a NodeMCU board and use MQTT messaging to notify us when someone is at the front door. The Arduino IDE has a number of MQTT libraries. Or, how about using a Processing script on the Linux notebook for remote text-to-speech audio object recognition? Or maybe a networked lamp, using a NodeMCU board would turn on when the front door sensor detects a car pulling up in the driveway? Hey, how about turning on a sprinkler valve, using MQTT, to discourage stray animals in the yard? Making data available on a network is simple and fast with MQTT.

What else could we do with this tech?

Catch Dr. Torq’s Off-The-Shelf Hacker column, each Saturday, only on The New Stack! Contact him directly for consulting, speaking appearances and commissioned projects at doc@drtorq.com or 407-718-3274.

A newsletter digest of the week’s most important stories & analyses.