
Mosquitto is a lightweight, small-scale open source MQTT broker. MQTT (Message Queuing Telemetry Transport) is an open message protocol for communication between machines. Allows you to exchange messages between IoT (Internet of Things) devices.
If we need an MQTT server for our home projects, the small size of the Mosquitto is the perfect choice e.g. on the Raspberry Pi.
CanaKit Raspberry Pi 4 8GB Extreme Kit – 128GB Edition (8GB RAM)
Includes Raspberry Pi 4 8GB Model B with 1.5GHz 64-bit quad-core CPU (8GB RAM), 128GB Samsung EVO+ Micro SD Card (Class 10), USB MicroSD Card Reader, Premium High-Gloss Raspberry Pi 4 Case with Integrated Fan, CanaKit 3.5A USB-C Raspberry Pi 4 Power Supply, Heat Sinks, 2 Micro HDMI to HDMI Cables
ad
Update your system first. Type the following command into the terminal or SSH console.
sudo apt-get update
sudo apt-get upgrade
The Mosquitto MQTT broker can be easily installed from Debian package sources.
sudo apt-get install mosquitto
Install the Mosquitto client as well.
sudo apt-get install mosquitto-clients
At the end of the installation, the service is enabled.
sudo systemctl enable mosquitto
Once we have that, we can use the MQTT broker. The Mosquitto server starts automatically after installation.
The Mosquitto server registers itself as a system service and starts automatically at system startup.
Use the following commands to start, stop, or restart the Mosquitto MQTT Broker service.
sudo systemctl start mosquitto
sudo systemctl stop mosquitto
sudo systemctl restart mosquitto
You can disable or enable autorun.
sudo systemctl disable mosquitto
sudo systemctl enable mosquitto
Let’s see if our server is working. Open two Terminals or SSH windows. On the first Terminal, subscribe to the “/test/topic” topic. Here you can receive messages.
mosquitto_sub -h localhost -t /test/topic
At the second terminal, we can send a message to the Mosquitto MQTT broker. Enter the following command and if everything is OK, this message should appear in the first terminal window.
mosquitto_pub -h localhost -t /test/topic -m "Hello Mosquitto!"
If we wish, we can password protect our Mqtt Broker. Mosquitto includes a service called mosquitto_passwd to create a password file. Copy the following command and run it on the terminal. Enter the password.
sudo mosquitto_passwd -c /etc/mosquitto/pwd myusername
The username “myusername” and the corresponding password are created in the password file “/etc/mosquitto/pwd”.
Then create the configuration file for Mosquitto.
sudo nano /etc/mosquitto/conf.d/default.conf
Add these two lines and save.
allow_anonymous false
password_file /etc/mosquitto/pwd
Restart the Mosquitto
systemctl restart mosquitto
Let’s test it.
pi@raspberrypi:~ $ mosquitto_pub -h localhost -t /test/topic -m "Hello Mosquitto!"
Connection Refused: not authorised.
It doesn’t work without a password, we get an error message. Complete the command with the username and password pair as follows.
mosquitto_sub -h localhost -t /test/button -u myusername -P mypassword
mosquitto_pub -h localhost -t /test/topic -m "Hello Mosquitto!" -u myusername -P mypassword
We now have a small MQTT server that can run energy efficiently e.g. on a Raspberry Pi.
With this simple little MQTT client app we can connect our Android phone to the MQTT server

Good luck for that.
AMD Ryzen 7 5800H Processor,Mini Computer with 16G DDR4 RAM/500GB M.2 NVMe 2280 SSD,4K FPS/Three-Screen Display/WiFi 6/BT5.2/Support Auto Power On.
Ideal for Smart Home, Mosquitto, ioBrokers or media servers.

