How to Create a Local Self-Hosted MQTT Mosquitto Broker on a Raspberry

I have a bunch of stuff that I’ve slowly been installing in the house that I’m realizing that I need a cohesive and coherant way to control and use. I have Alexa PhotoShow5, and a Amazon Dot, some Ubiquiti cameras, HUE lights, smart TVs, WiFi enabled thermostats, smart laundry, and who knows what else? I think I need a way to start to get all of these devices under one roof (of control that is!)

I’m going to install an MQTT broker called Mosquitto. MQTT stands for Message Queuing Telemetry Transport. MQTT is a simple messaging protocol, designed for constrained devices with low bandwidth. So, it’s the perfect solution to exchange data between multiple IoT devices.

MQTT communication works as a publish and subscribe system. Devices publish messages on a specific topic. All devices that are subscribed to that topic receive the message.

The MQTT broker is responsible for receiving all messages, filtering the messages, deciding who is interested in them, and then publishing the message to all subscribed clients.

To start, setup a Raspberry in the usual way. Get it on the network and changes the passwords. Run “apt update” and “apt -y upgrade” Once you’ve got it set up, change the hostname. I’m calling mine “mosquitto”. Next step is to install both the mosquitto broker (server) and a client:

apt install mosquitto mosquitto-clients

Now that it’s installed, let’s ensure that it’s running:

systemctl status mosquitto

Looks good! Let’s start a new subscription called “home” and see if we can receive a message:

mosquitto_sub -h localhost -t "mqtt/home"

Now, open another ssh session into the same Raspberry and send a message to that topic which we just subscribed to in the other session:

mosquitto_pub -h localhost -t "mqtt/home" -m "We're doin' it!"

You should see the message appear in the other ssh session that’s subscribed to this topic “mqtt/home”! OK! Proof of Concept complete! Let’s get some of these things subscibed to our new topic!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.