MQTT Broker on Brainy Pi
by dhruvarora561 in Circuits > Computers
220 Views, 1 Favorites, 0 Comments
MQTT Broker on Brainy Pi
MQTT is standard messaging protocol for IOT devices. It was designed as an extremely lightweight messaging transport. It is ideal for connecting remote devices with a small footprint. This protocol is used in a wide variety of industries.
Today, we are going to create a MQTT broker using BrainyPi.
A broker is a middle man which allows the MQTT clients to communicate. The broker receives the messages from clients, filters them by topic and then forwards them to the subscribers.
Supplies
- A brainyPI or RaspberryPI
- keyboard
- mouse
Installing the Mosquito Broker
- lets start by updating the operating system and then we will proceed with the installation.
- After installation, run the systemctl command to check the status of the service.
sudo apt update
sudo apt upgrade
sudo apt install mosquitto mosquitto-clients
sudo systemctl status mosquitto
Testing the Installation of MQTT
After running the first command, open up another session and run the following commands
mosquitto_sub -h localhost -t "mqtt/brainypi"
Open up another terminal session and run the commands below
mosquitto_pub -h localhost -t "mqtt/brainypi" -m "Hello world"
In the first window, you should see "Hello World" printed. Here, we first created a subscriber of the topic "mqtt/brainypi" and then we published a message for the same topic.
Enabling Auto Startup
Now, that our setup is tested. We shall enable auto startup of the service so that in case of restart the service should restart automatically.
sudo systemctl enable mosquitto.service
Enabling Remote Connections
4.1 By default, mqtt has stopped allowing remote connections.
4.2 We can change this behavior easily by editing the config file.
4.3 Run the following command to open the file.
sudo nano /etc/mosquitto/mosquitto.conf
4.4 Scroll to the bottom of the file, and add the following.
listener 1883
allow_anonymous true
4.5 This will enable "no authentication" method. This method is generally not recommended
4.6 Restart the service to apply the changes.
sudo systemcl restart mosquitto
Enabling Password Authentication
5.1 We'll start by creating a password for a user.
sudo mosquitto_passwd -c /etc/mosquitto/passwd replace_this_with_your_name
5.2 Now, that we have created a password for the username. We need to tell MQTT to use it as default authentication method.
5.3 We can achieve the above by editing the configuration file
sudo nano /etc/mosquitto/mosquitto.conf
5.4 Add the following lines at the top of the file
per_listener_settings true
5.5 Add the following lines to the end of the file. These will tell Mosquitto where the password file lies.
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd
Adding More Users
We can add more users by running the command below
mosquitto_password passwd <username>
Testing Our Changes
7.1 Last time we tested the configuration was without using username/password authentication.
mosquitto_sub -d -t brainypi -u <username> -P password@23
7.2 Sending the message
mosquitto_pub -d -t testTopic -m "Hello world!" -u dhruv -P password@23
Publishing to Multiple Clients
8.1 MQTT can have multiple clients. To add them just run the commands below.
mosquitto_pub -h IP_ADDRESS_BRAINYPI -d -t testTopic -m "Hello world!" -u dhruv -P password@23
8.2 Remember to change the relevant details like ip address, topic name, username, and password.