Local MQTT Broker and Bridge

As I mentioned in the Interactive WiFi Christmas Tree introduction, I am waiting for parts to arrive from element14. Meanwhile I started to set up my MQTT infrastructure. My initial plan was to use the sandboxed MQTT Broker available at iot.eclipse.org at port 1883. Then I decided to explore more about the MQTT Broker and wanted to setup a local MQTT Broker on my PC and use it as the MQTT gateway for all my needs. After further research i ended up choosing the mosquitto broker. Since the Mosquitto broker allows bridging, it should be possible for me to selectively choose “topics” to forward to one or more of sandboxes available on the internet.

Local MQTT Broker Installation

The sandboxed MQTT broker above is running the Open Source Mosquitto broker in its version 1.3.1. So i got the broker for windows downloaded from http://mosquitto.org/download/ and installed it. After the installation is complete, I could see the Mosquitto Broker services in the control panel. I found that the service is not started by default so i started the service.

I also went ahead and installed the Mosquitto broker on my Mac. This way i can test the bridge functionalities locally. In addition this allows me to make both my PCs as MQTT Publishers and Subscribers. The audio elements in my project will run from my Mac or PC by subscribing to the appropriate MQTT topic.

When i get my Arduino Yun, it should be possible for me to run the Mosquitto broker on the Yun as well since a OpenWrt version is available at the same download url.

Verifying the Install

In the install directory, there were two handy utilities.

  • mosquitto_sub  — A command line utility for subscribing to Topics
  • mosquitto_pub  — A command line utility for publishing simple messages one at a time

On Mac these were available at:
/usr/local/opt/mosquitto/bin

On PC these were available at:

C:\Program Files (x86)\mosquitto

Running mosquitto_sub

First i ran the mosquitto_sub with the wild card “#” for the topic name in one terminal window. This allowed me to watch every message that is being sent to my gateway. This will be very useful if i run into issues later in the project and need to debug.

mosquitto_sub -h localhost -v -t "#"

-v is for making the output verbose. This will print the Topic name as well along with the actual message payload.

Running mosquitto_pub

Then i opened another terminal window and ran the mosquitto_pub to send some test messages out.
mosquitto_pub -t WiFiTree/RGB -m "210,230,0"
mosquitto_pub -t WiFiTree/RGB -m "0,0,0"
mosquitto_pub -t WiFiTree/Sound -m "0.2"

I could see that the setup is working correctly when the outputs started appearing in the subscriber window. Here are the screen shots for both Subscriber and publisher.

Local MQTT Broker on mac
Local MQTT Broker on mac
Local MQTT Broker on PC
Local MQTT Broker on PC

Local MQTT Broker Bridge on Mac to forward to MQTT Bridge on PC

Before setting up the bridge to sandbox brokers on the net, i wanted to set up the bridge between my broker on Mac and PC. This would allow me to bring these machines also in to my holiday project infrastructure to play sound and other things that i have not thought of yet. There isn’t any specific need for me to run multiple brokers. But i just wanted to test out the capability because some day it might come handy.

On Mac i modified the well documented mosquitto configuration file at /usr/local/etc/mosquitto/mosquitto.conf and added the following:
connection mywindowsmqttbroker
address 192.168.1.217
topic WiFiTree/ForWindowsPC/Test

This configuration should enable the MQTT broker on Mac to forward only the MQTT messages with the topic  “WiFiTree/ForWindowsPC/Test” to my windows PC.

After the configuration change I restarted my mosquito demon and sent out some test messages from my mac:
mosquitto_pub -t WiFiTree/ForWindowsPC/Test -m "Test MQTT bridge message for Windows From Mac"

Success!!!. I was able to see that my Windows PC was receiving only those topics from mac.

Local MQTT Broker Bridge PC
Local MQTT Broker Bridge PC

Now that my MQTT brokers are set up the way i want, each of the modules of my WiFi Christmas Tree project can be individually tested and put together.

Some thought for future exploration

As many of you might have already done, the routers can be modified to run OpenWrt. Since a openWrt version of MQTT broker is available, the router might be the right place to run a home specific MQTT broker. I hope someday router manufacturers by default will put a MQTT broker on their products making it easier for deploying connected devices. My router is not modified to run openWrt. One day i will get around to do that.

6 thoughts on “Local MQTT Broker and Bridge”

Leave a Reply

Your email address will not be published. Required fields are marked *