Mosquitto is a popular and open-source MQTT broker written in Python. It's very easy to install and configure and runs well on small compute modules like the Raspberry Pi or Intel Edison. One of its more powerful features is the ability to form bridge connections between the local broker and a remote broker. In this tutorial, we're going to cover how to bridge a local Mosquitto broker to Losant's cloud broker.
For this example, if a device publishes to a Losant-specific topic on the local broker, Mosquitto will bridge that publish to the Losant broker. All other communication will stay local.
You can download Mosquitto from its website. They have installers for most major platforms. Once downloaded, you can follow their configuration instructions for the basic MQTT broker setup. We won't be covering these steps, since there are many useful tutorials that already exist. This article will only focus on the bridge configuration.
In order to use Mosquitto as a bridge, you will need to create a gateway device and one or more peripheral devices in Losant. Mosquitto will connect to Losant as the gateway and use its connection to send peripheral state and receive peripheral commands.
When creating a device, you will have the ability to set its type. When choosing peripheral, you must also select the previously created gateway that will have permission to report on its behalf.
The gateway may or may not have any device attributes. The peripherals should be configured with the attributes you'll be reporting. For example, if this is a bluetooth peripheral, like a LightBlueBean, you may have attributes for temperature and acceleration.
Once the devices are successfully created, we'll use their IDs in the following section to configure the Mosquitto bridge.
I'll start by posting the entire configuration and I'll go through each line after.
The last two lines will have to be repeated for each unique peripheral device you have connected to the local Mosquitto broker.
Now when you start Mosquitto it will open a bridge connection to Losant and subscribe to each of the losant/id/command topics you have configured. Whenever Losant publishes a command, Mosquitto will forward it to the local device. Whenever a local device publishes to /losant/id/state Mosquitto will forward it to Losant.
This is a basic bridge setup. Mosquitto's topic configuration can be quite powerful and will allow you to bridge existing topics to Losant-specific topics if needed. Check out the bridge configuration documentation for more details.
That's all there is to it. If you have any questions, please leave them below or in our forums.