0% found this document useful (0 votes)
81 views17 pages

Installing The Mosquitto MQTT Server To The Raspberry Pi - Pi My Life Up

Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

Uploaded by

faszomjoska23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views17 pages

Installing The Mosquitto MQTT Server To The Raspberry Pi - Pi My Life Up

Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

Uploaded by

faszomjoska23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

Raspberry Pi PLC controllers


PLCs/Gateways with Raspberry Pi 4, open source
software solution.

Unipi technology Learn More

Installing the Mosquitto MQTT Server to the


Raspberry Pi
by Emmet  Updated Feb 17, 2022  Beginner  IoT  Servers

      Save as PDF

If you buy through our links, we may earn an affiliate commission. Learn More.

This Raspberry Pi project, will show you how to install the open-source MQTT Server, Mosquitto.

MQTT stands for Message Queuing Telemetry Transport and is a network messaging protocol commonly
used for messaging between IoT devices.

To get our Raspberry Pi to support the MQTT protocol, we will be using a server software called Mosquitto.

Mosquitto is a message broker that implements several versions of the MQTT protocol, including the latest
5.0 revision.

×
Raspberry Pi PLC controllers Learn More

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 1/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

LATEST VIDEOS

Build an AI Assistant on the Raspberry Pi with OpenAI's ChatGPT


In this video, I will show you how to use the ChatGPT API on your Raspberry Pi.

It is also a relatively lightweight software, making Mosquitto the perfect choice for dealing with the MQTT
protocol on our Raspberry Pi.

Raspberry Pi PLC controllers

The MQTT protocol works by having clients act as publishers and subscribers. The publishers send the
messages through to a broker that serves as the middle man.

Subscribers connect to the MQTT broker and read messages that are being broadcast under a specific topic.

You can use MQTT for having multiple sensors to send their data to your Raspberry Pi’s MQTT broker, which
client devices can then receive that data.

If you want to learn more about the MQTT protocol and why it is excellent for IoT devices such as the
Raspberry Pi, be sure to check out the official MQTT website.

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 2/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

Raspberry Pi PLC controllers

Search … Search

How to Setup a Raspberry Pi Running Plant-it on the Setting up the NGINX Proxy How to Install and Run
AFP Server Raspberry Pi Manager on the Raspberry Pi Plexamp on the Raspberry Pi

Equipment

Below you can view the list of equipment we used when installing the MQTT broker to our Raspberry Pi.

Recommended

× Raspberry Pi ( Amazon | SunFounder )

 Micro SD Card ( Amazon | SunFounder ) (8GB+)

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 3/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

 Ethernet Cable ( Amazon ) or Wi-Fi ( Amazon | SunFounder )

Optional

 Raspberry Pi Case ( Amazon | SunFounder )

 USB Keyboard ( Amazon | SunFounder )

 USB Mouse ( Amazon | SunFounder )

We tested this tutorial on a Pi 400 running the latest version of Raspberry Pi OS Buster.

Installing Mosquitto to the Raspberry Pi

In this section, we will show you how to install the Mosquitto broker to your Raspberry Pi.

Before proceeding, it may be worth setting your Raspberry Pi up with a static IP address.

A static IP address provides you with an easy point of connection that should never change upon multiple
×
reboots.

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 4/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

1. 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.

Terminal $  Copy

sudo apt update


sudo apt upgrade

2. Once the system has finished updating, we can now install the Mosquitto software.
Luckily for us, the Mosquitto MQTT broker is available as part of the Raspbian repository, so installing the
software is simple.

Run the following command to install Mosquitto alongside its client software.

Terminal $  Copy

sudo apt install mosquitto mosquitto-clients

The client software will allow us to interact with and test that our MQTT broker is running correctly on our
Raspberry Pi.

During the installation process, the package manager will automatically configure the Mosquitto server to
start on boot.

3. 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.

Terminal $  Copy

sudo systemctl status mosquitto

×
This command will return the status of the “ mosquitto ” service.

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 5/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

You should see the text “ active (running) ” if the service has started up properly.

Testing the Mosquitto Installation on the Raspberry Pi

Our next step will be to test that the service works as it should be and is now acting as an MQTT broker on
our Raspberry Pi.

To do this, we will be making use of the Mosquitto client that we installed earlier in this guide.

For this section, you will need to open up two terminal sessions to your Raspberry Pi (Either locally or over
SSH).

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

We can use the Mosquitto client for subscribers that we installed earlier to do this.

In our example below, we connect to a localhost connection and wait for messages from the broker on the
“ mqtt/pimylifeup ” topic.

Terminal $  Copy

mosquitto_sub -h localhost -t "mqtt/pimylifeup"

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 6/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

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/pimylifeup “.

2. Now that we have a client loaded up and listening for messages, let us try publishing one to it.
We need to use the MQTT publisher client that we installed on our Raspberry Pi earlier to publish a message
to the topic.

Run the following command to publish the message “ Hello World ” to our localhost server under the “
mqtt/pimylifeup ” topic.

Terminal $  Copy

mosquitto_pub -h localhost -t "mqtt/pimylifeup" -m "Hello world"

Two of the arguments are the same as the previous command, with “ -h ” specifying the server to connect to
and “ -t ” specifying the topic to publish to.

The one additional argument that we are using here is the “ -m ” argument. This argument allows you to
specify the message you want to send to the Raspberry Pi MQTT broker.

3. Back in the terminal session where you started the Mosquitto publisher, you should now see your
message appear.

×So if you were following our example, you should see the following text appear in the command line.

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 7/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

Example Mosquitto Message

Hello world

Conclusion

At this point, you should now have an MQTT server up and running on your Raspberry Pi.

We achieved this by making use of the free and open-source software called Mosquitto.

You will also have done a quick test to verify that everything is working as it is intended.

If you are using this alongside a firewall such as UFW, make sure that you open port 1883 .

Please be sure to check out our other Raspberry Pi IoT projects and our other server ideas.

If you have run into any issues, feel free to leave a comment below.

    

Weekly Updates Straight To Your Inbox


Receive our Raspberry Pi projects, coding tutorials, Linux guides and more!

Email*

Subscribe

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 8/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

Recommended

Raspberry Pi Kiosk using How to Run Steam on a Running Xbox Cloud Gaming on
Chromium Raspberry Pi the Raspberry Pi

How to Install and Run Plexamp Raspberry Pi Wireless Access Raspberry Pi IRC Server: Setup
on the Raspberry Pi Point your own Chat Network

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 9/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

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

Comment

Name * Email *

Post Comment

 3 Comments

John Galt on October 30, 2023 at 1:18 am

I am having a tough time understanding what the topic is here, “ mqtt/pimylifeup”. Do we copy the exact
topic you put there or is the topic unique to us? Or do we need to create the topic somehow?

Reply

Emmet on October 30, 2023 at 2:16 pm


 Editor

Hi John,

The topic can be whatever you set it to. The topic is the way you sort MQTT messages. Typically you
× would name the topic relevant to what you are sending.

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 10/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

For example, if you wanted to track your living room temperature you might use the topic name “
LivingRoom/Temperature “. Then you would send and receive the temperature through that topic.

You don’t have to manually create topics, they are automatically handled when you tell MQTT to
subscribe to that particular topic. Additionally, MQTT topics are case sensitive, so “
livingRoom/Temperature ” is different to “ LivingRoom/Temperature “.

Please let me know if you need this to be explained further.

Kind Regards,
Emmet

Reply

John Ellis on March 16, 2022 at 7:17 am

Great Mosquitto installation and test guide, thanks

Reply

Raspberry Pi PLC
controllers
Unipi technology

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 11/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 12/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 13/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 14/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 15/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 16/17
9/13/24, 3:56 PM Installing the Mosquitto MQTT Server to the Raspberry Pi - Pi My Life Up

↑ Back to Top

© 2024 Pi My Life Up |     

 Switch to Light Theme

Disclaimer & Privacy Policy | About Us | Contact

Update Privacy Preferences

A RAPTIVE PARTNER SITE

https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/ 17/17

You might also like