- This blog is about how i install and run Mosquitto broker on Linux VirtualMachine and Raspberry Pi (RPi)
Author : Anecha Khumjun 6401012630141
- MQTT is a lightweight protocol that supports the Internet of Things (IoT).An MQTT broker is an intermediary entity that enables MQTT clients to communicate. Specifically, an - MQTT broker receives messages published by clients, filters the messages by topic, and distributes them to subscribers.
- You can choose Linux VM you favorite to install Mosquitto broker this time i will use Raspbian OS . 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
- Once the system has finished updating, we can now install the Mosquitto software.Run the following command to install Mosquitto alongside its client software.
sudo apt install mosquitto mosquitto-clients
- 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
- This command will return the status of the
“mosquitto”
service. - You should see the text
“active (running)”
if the service has started up properly.Like this picture.
- 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
- Once the system has finished updating, we can now install the Mosquitto software.Run the following command to install Mosquitto alongside its client software.
sudo apt install mosquitto mosquitto-clients
- To make Mosquitto auto start when the Raspberry Pi boots, you need to run the following command (this means that the Mosquitto broker will automatically start when the Raspberry Pi starts).
sudo systemctl enable mosquitto.service
- 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
- This command will return the status of the
“mosquitto”
service. You should see the text“active (running)”
if the service has started up properly.
Not done ...
- It have so many GUI-base of MQTT client you can check on this link to list of GUI-based MQTT client but on this contents I will use MQTT Explorer as client on my note book to publish messages or subscribe to a topic. First you need to dowload MQTT Explorer download MQTT Explorer.
- Once the system finished installing, we need to run the mosquitto on (RPI) and set it to broker . This is how to set mosquitto broker for (RPI)
- Run the following command to open the
"mosquitto.conf"
file.
sudo nano /etc/mosquitto/mosquitto.conf
- Move to the end of the file using the arrow keys and paste the following two lines.
listener 1883
allow_anonymous true
- Then, press CTRL-X to exit and save the file. Press Y and Enter.
- Restart Mosquitto for the changes to take effect.
sudo systemctl restart mosquitto
- To subscribe to an MQTT topic with Mosquitto Client open a terminal and enter the command
mosquitto_sub -d -t testTopic
- Now your RPI is ready to subscribe massage testTopic Topic.let Publishing message to testTopic Topic by MQTT Client.
- Open your program and change host to your Raspberry Pi IP address.To use Mosquitto broker in your projects, you’ll need to know the Raspberry Pi IP address. To retrieve your - Raspberry Pi IP address, type the next command in your Pi Terminal window.
hostname -I
- Enter topic name that you want to send massage to for me is "testTopic".and I will send massge "test".
mqtt-sub-msg no-img