In this tutorial, you’re going to test the Mosquitto MQTT Broker and MQTT Client on a Raspberry Pi. You’ll subscribe the MQTT client to an MQTT topic and publish sample messages.
Updated 23 February 2023
Recommended resources:
- You need a Raspberry Pi board – read Best Raspberry Pi Starter Kits
- Install Raspberry Pi OS, Set Up Wi-Fi, Enable and Connect with SSH
- How to Install Mosquitto Broker on Raspberry Pi
- What is MQTT and How It Works
- Getting Started with Node-RED on Raspberry Pi
Testing MQTT Broker Installation
After installing MQTT Broker, I recommend installing an MQTT Client to test the Broker installation and publish sample messages.
Run the following command to install MQTT Mosquitto Client:
sudo apt install -y mosquitto mosquitto-clients
Run Mosquitto in the background as a daemon:
mosquitto -d
Subscribing to testTopic Topic
To subscribe to an MQTT topic with Mosquitto Client open a terminal Window #1 and enter the command:
mosquitto_sub -d -t testTopic
You’re now subscribed to a topic called testTopic.
If you have enabled username and password on your broker, you need to run the command as follows:
mosquitto_sub -d -t testTopic -u user -P pass
Replace user with your username and pass with your password. For example, my username is sara and the password is pass1245, so I need to run the command as follows:
mosquitto_sub -d -t testTopic -u sara -P pass1245
Publishing “Hello World!” Message to testTopic Topic
To publish a sample message to testTopic, open a terminal Window #2 and run the following command:
mosquitto_pub -d -t testTopic -m "Hello world!"
The message “Hello World!” is received in Window #1 as illustrated in the figure above.
If you have enabled username and password on your broker, you need to run the command as follows:
mosquitto_pub -d -t testTopic -m "Hello world!" -u user -P pass
Replace user with your username and pass with your password.
Publishing a Message to Multiple Clients
Having Window #1 still subscribed to topic testTopic, open a new terminal Window #3 and run this command to subscribe to testTopic topic:
mosquitto_sub -d -t testTopic
If you have enabled username and password on your broker, you need to run the command as follows:
mosquitto_sub -d -t testTopic -u user -P pass
Replace user with your username and pass with your password.
On Window #2 publish the “Hello World!” message:
mosquitto_pub -d -t testTopic -m "Hello world!"
If you have enabled username and password on your broker, you need to run the command as follows:
mosquitto_pub -d -t testTopic -m "Hello world!" -u user -P pass
Since two clients are subscribed to testTopic topic, they will both receive the “Hello world!” message.
This simple example shows how MQTT works and how your devices (for example: ESP8266 or ESP32, etc.) can be subscribed to the same topic to receive messages, or a device can publish messages to multiple devices.
Wrapping Up
In this tutorial, you checked that your Mosquitto Broker installed on the Raspberry Pi is running properly. Now, you can experiment with MQTT in your ESP32/ESP8266 IoT projects. We have several MQTT practical examples that you can experiment with. Here are some examples:
- ESP32 MQTT – Publish and Subscribe with Arduino IDE
- ESP8266 and Node-RED with MQTT (Publish and Subscribe)
- ESP32 MQTT – Publish DS18B20 Temperature Readings (Arduino IDE)
- ESP8266 NodeMCU MQTT – Publish DS18B20 Temperature Readings (Arduino IDE)
- All MQTT-related projects…
You can also run Mosquitto MQTT broker on the cloud. Running an MQTT Mosquitto Broker in the cloud allows you to connect several ESP32/ESP8266 boards and other IoT devices from anywhere using different networks as long as they have an Internet connection. Check the tutorial below:
Do you want to learn more about Home Automation? Learn Node-RED and InfluxDB on a Raspberry Pi to build a Home Automation System with the ESP32 and ESP8266. Check out our eBook:
Do you have any questions? Leave a comment down below!
Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing to my blog.
Hi Rui, Odd things happening for me on this one. I am using a Rpi2b, Raspian Pixel, Mosquitto ver 1.4.14. When I type in the second terminal window “mosquitto_pub -d -t testTopic -m “Hello World” the following occurs:
1 I receive an Error: Unknown option ‘world’
2 I noticed when I typed the command again on the first depression of the quote (shift + ‘) the quote did not show. (it does show in text editor).
AND if I type “mosquitto_pub -d -t testTopic -m Hello World (without the quotes) I get the same error, but if I type mosquitto_pub -d -t testTopic -m Hello, the message “Hello” is transmitted. Any thoughts on this?
Put double quotes around “Hello World” and it should work.
I have tried using my PC with a virtual machine and linux installed and it works, Thanks!
awesome tutorial, thanks. So many questions answered in my head, going through it and seeing it work really helps the understanding.
Hi Rui,
This is a very helpful tutorial series for someone starting with MQTT. May I suggest that the flow of the process would be easier to follow if the order of the Terminal window images was changed. The window where the command is entered should appear first, followed by the windows where the message is received.
Also, in the paragraph before Wrapping Up, you mention “a device could publish messages to multiple devices”. This contradicts your earlier explanation that messages are published to TOPICS, and devices must subscribe to topics to receive messages.
I’ve been following RNT for a few years and learned a lot. Keep up the great work.
Hello Michael. Thanks for the helpful feedback!
Both statements are true. Multiple boards can be subscribed to the same topic. So, one board can publish readings to all devices subscribed to that topic.
I’m glad you’ve found our content helpful.
Regards,
Rui
This was helpful, but not real clear how to get mosquitto to respond to a different machine.
To clarify to myself, I did this between two RaspberryPi’s. Then tried to Node-RED to help me understand.
explanation methodology is well:)
Thank yo so much for this instruction I have a raspberry pi 3 so installed raspberry OS as standard then followed your instructions, I then installed Mosquito then opened two terminal windows and followed your instructions for Hello world and job done
it appeared in the other window I was amazed that it all just worked as I want to learn MQTT for my Wemose D1 min and ESP32 and ESP8266 and ESP32-cam that I have and would like to make up some sort of small system
I can then get away from Ewe link and China but I did not want to use HA as it is to big for me and two complicated and all the cloud services are slow the latency takes too long with Ewe Link and this was instant I have tried Telegram tutorial that you provided and that worked for two days then just stopped so gave up with that.
I would like to take this opportunity to thank you for a wonderful YouTube channel and all your hard work you are the best at describing a project and your written word is second to none you go the extra mile to explain it all and make it very easy to follow.
I would like to purchase your book but I am not sure of the best one?
Many thanks Bob in the UK
Hi Bob.
Thank you for your nice words.
We have courses related to ESP32, ESP8266, Web Servers, MicroPython, and more.
You can check all our courses here: https://randomnerdtutorials.com/courses/
If you have any doubts about any of those courses, you can send a message to support, asking for more information: https://randomnerdtutorials.com/support/
Regards,
Sara
Thank you Sara for your reply I will have a look
Bonjour,
Je découvre MQTT pour la première fois. Les explications et les tests me permettent de vraiment mieux comprendre les commandes exécutées pas à pas.
Merci encore de votre implication
Hi. I guess default port number is 1883 for MQTT and 8883 for MQTTS.
Do you know how to change port for daemon.
It starts on 1883 port number even though updating listener to 8883 in mosquitto.conf.
Client (null) sending CONNECT
Client (null) received CONNACK (5)
Connection error: Connection Refused: not authorised.
Client (null) sending DISCONNECT
Hi I’m getting this error when I’m trying to subscribe to the testTopic.
Could you guide me through this.
same, how do you solve it?
Hi.
Check if you’ve enabled remote access. See this tutorial: https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/
Scroll down to the section called “Enable remote access”.
You can set up a username and password or choose not to have one.
If you set up a username and password you need to pass them in the command when trying to publish or subscribe.
That tutorial shows an example for that.
Regards,
Sara
Hey! Thanks for a very good review of an MQTT installation. Was able to test it locally and it worked fine. Having trouble getting it to work against external calls and getting no error messages. have added lines in conf. – file.
How can I test external connections to the server?
Can I see if the server has received a call?
Hi.
Did you follow these instructions and allowed remote access: https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/?
Regards,
Sara
I didnt quite know what yoiu meant by external server but, if you are having trouble sending and recieving from two different pi’s Did you add a line to your publishing code of -h 192.168.0.XXX defining your server? I managed to send between 2 pi’s that way
Hi,
Another Newbie…
I’m looking into MQTT to interact with an Raspberry Pi Process Control System
I understand monitoring and the lamp on / lamp off, but is there any way to change a value, i.e. from now on lamp goes on at 8pm in stead of 9pm
Hi.
I’m not sure that I understood your question…
Are you using Node-RED?
If so, you can use a node called Big Timer. It provides plenty of schedule options.
Regards,
Sara
Hi Peeps
Is there a way to use mosquitto clients from a python script? please tell me you have a book that covers this 😀
I am trying to make a rf doorbell that sends a mqtt message to my node red raspi mosquitto broker and while the tutorial I have found uses paho I cant for the life of me follow the set up of the clients. I can get it all to run as a direct pub/sub using your method, but not from a script.