DEV Community

Fernando Tschopp
Fernando Tschopp

Posted on

MQTT Series: Setup broker on Raspberry PI

Setup broker

Before installing the MQTT broker to our Raspberry Pi, we need to update the operating system.
All we need to do to update the system is to run the following two commands.

sudo apt update
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

Once the system has finished updating, we can now install the Mosquitto software.

sudo apt install mosquitto mosquitto-clients
Enter fullscreen mode Exit fullscreen mode

At this point, you will now have the Mosquitto MQTT broker up and running on your device.
You can verify that it is installed and running by using the command below.

sudo systemctl status mosquitto
Enter fullscreen mode Exit fullscreen mode

Testing the installation

Our first task is to start up a subscriber. The subscriber is what will listen to our MQTT broker running on the Raspberry Pi.

mosquitto_sub -h localhost -t "mqtt/mytopic"
Enter fullscreen mode Exit fullscreen mode

Using the “-h” argument, you can specify the hostname you want to connect to. In our case, we are using the local MQTT broker that we installed on our Raspberry Pi.

Next, we use the “-t” argument to tell the Mosquitto subscriber what topic we should listen to from the MQTT broker.

For our example, we are listening to a topic called “mqtt/mytopic“.

Now we need to use the MQTT publisher client that we installed on our Raspberry Pi earlier to publish a message to the topic.

mosquitto_pub -h localhost -t "mqtt/mytopic" -m "Hello world"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)