Language: EN

como-instalar-mosquitto-mqtt-en-raspberry-pi

How to Use the Mosquitto MQTT Broker on Raspberry Pi

Mosquitto is a popular MQTT (Message Queuing Telemetry Transport) broker, a lightweight and efficient messaging protocol ideal for the Internet of Things (IoT).

Mosquitto is an MQTT broker that facilitates communication between different devices and applications through messages. MQTT is a publish/subscribe messaging protocol that works over TCP/IP, designed to be lightweight and efficient in data transmission.

Some key features of Mosquitto are:

  • Lightweight and Efficient: Ideal for resource-constrained devices like the Raspberry Pi.
  • Scalability: Can handle large volumes of messages and devices.
  • Support for MQTT v3.1 and v3.1.1: Compatible with the most common versions of the MQTT protocol.
  • Ease of Configuration: Allows for easy configuration of communication between devices.

If you don’t know what MQTT is or want to learn more about this widely used protocol in IoT, here’s a link 👇.

Installation of Mosquitto on Raspberry Pi

Before installing Mosquitto, we must ensure that our Raspberry Pi is up to date. We run the following commands to update the packages and the operating system:

sudo apt update
sudo apt upgrade

To install Mosquitto on Raspberry Pi, we simply use the apt package manager. We open a terminal and run the following command:

sudo apt install mosquitto mosquitto-clients

This command will install both the Mosquitto broker and the necessary client tools to interact with it.

You can check the status of the Mosquitto service with:

sudo systemctl status mosquitto

This will provide information on whether the broker is running or if it has encountered any issues.

Basic Configuration

Mosquitto comes with a default configuration that is suitable to get started, but we can adjust it according to our needs. The main configuration file is located at /etc/mosquitto/mosquitto.conf.

To modify the configuration, we open the file with a text editor using the following command:

sudo nano /etc/mosquitto/mosquitto.conf

In this file, we can configure various parameters, such as the port on which the broker will listen (by default, it is port 1883), as well as options for authentication and log files.

Configuring Authentication

If we want to enable authentication for access to the Mosquitto broker, we first need to set up a password file. We start by installing the mosquitto_passwd tool:

sudo apt install mosquitto

Then, we create a password file with the following command:

sudo mosquitto_passwd -c /etc/mosquitto/pwfile username

We replace username with the desired username. We will be prompted to enter and confirm a password.

Once the password file is created, we edit the configuration file /etc/mosquitto/mosquitto.conf to add the following line:

password_file /etc/mosquitto/pwfile

We save and close the file, and then restart Mosquitto to apply the changes with the following command:

sudo systemctl restart mosquitto

Using Mosquitto

Once we have installed and configured Mosquitto, we can start using it to send and receive MQTT messages.

Publish a Message

To publish a message on a specific topic, we use the mosquitto_pub command. For example, to send the message “Hello, MQTT!” to the topic “test/topic”, we execute:

mosquitto_pub -h localhost -t "test/topic" -m "Hello, MQTT!"

Subscribe to a Topic

To subscribe to a topic and receive messages, we use the mosquitto_sub command. For example, to subscribe to the topic “test/topic”, we execute:

mosquitto_sub -h localhost -t "test/topic"

When we publish a message on the topic “test/topic”, we will see the message in the terminal where we are subscribed.