Mosquitto is a lightweight, open-source message broker that adheres to the MQTT (Message Queuing Telemetry Transport) protocol. It is commonly used for message communication between IoT (Internet of Things) devices. Below is a detailed introduction to Mosquitto:
Lightweight: Mosquitto is designed to be resource-efficient, making it suitable for running on small servers and single-board computers.
Cross-platform: It supports multiple platforms, including Linux, Windows, and macOS.
Multi-user support: It can handle multiple client connections, managing a large number of concurrent sessions.
QoS (Quality of Service) Levels: It supports the three MQTT QoS levels, allowing users to choose the message delivery reliability according to their needs:
QoS 0: At most once delivery
QoS 1: At least once delivery
QoS 2: Exactly once delivery
Persistence: Mosquitto supports message persistence, allowing messages to be saved to disk and session states to be restored after a restart.
IoT Applications: Mosquitto is widely used in smart home automation, industrial automation, and environmental monitoring, where devices can efficiently communicate over low-bandwidth networks.
Mobile Applications: It supports low-power devices, reducing battery consumption and enabling real-time data transmission.
Node-to-node Communication: In distributed systems, Mosquitto can be used for asynchronous communication between different nodes.
Broker: The core of Mosquitto, responsible for receiving, filtering, storing, and forwarding messages. All communication is routed through the broker.
Client: An application or device that connects to the broker, which can act as a publisher, subscriber, or both.
Authentication and Authorization: Mosquitto supports user authentication via usernames and passwords, as well as topic access control.
SSL/TLS Encryption: It supports encrypted connections, securing data transmission.
Installation: Mosquitto can be installed via Docker, package managers (like apt or yum), or by compiling from source.
Configuration: Mosquitto can be easily configured using its configuration file, where you can customize connection parameters, authentication settings, and more.
Command-line Tools: Mosquitto provides tools like mosquitto_pub and mosquitto_sub for quickly publishing and subscribing to messages.
Mosquitto has an active developer community, providing abundant documentation and support channels. Developers can access resources, troubleshoot issues, or contribute code through GitHub or the official documentation.
In summary, Mosquitto is a powerful and flexible message broker, ideal for efficient communication in IoT and distributed application scenarios. Its lightweight nature and ease of use make it a popular choice for developers and enterprises.
MQTTX software and a real serial server are used to connect and communicate. The serial server used is a single serial port server by ebyte.
Mosquitto Deployment System: Debian 12
SSH Remote Server for installation (SSH with root privileges or sudo access is required for deployment; direct root access is not recommended for production environments).
Mosquitto Official Website: https://mosquitto.org
Debian Official Website: https://www.debian.org
MQTTX Official Website: https://mqttx.app
Serial Server (ebyte): https://www.ebyte.com/product/2124.html
For usage of the serial server:
Serial Server Tutorial
Start by installing Mosquitto on the server or device. You can find installation methods on the Mosquitto official website. Below, we demonstrate a simple installation on a Debian 12 system. The installation is straightforward, and you can refer to the official guide.
Most open-source systems include Mosquitto in their repositories, so you can install it directly using the following command:
sudo apt-get install mosquitto
Once the installation is complete, open the configuration file and modify it based on your needs:
sudo vim /etc/mosquitto/mosquitto.conf
For detailed configuration information, refer to the official manual. Here is a brief summary:
Port: The port Mosquitto listens on (default is 1883).
TLS/SSL: The port for using TLS/SSL encryption (default is 8883).
WebSocket: Port for using WebSocket protocol (default is 9001).
Allow Anonymous: Whether to allow anonymous connections.
Password File: File path for storing usernames and passwords.
Client ID Prefix: Prefix for client IDs.
Log Destination: Path to the log file.
Example configuration:
# Local configuration in /etc/mosquitto/conf.d/pid_file /run/mosquitto/mosquitto.pidpersistence truepersistence_location /var/lib/mosquitto/allow_anonymous truelog_dest file /var/log/mosquitto/mosquitto.loginclude_dir /etc/mosquitto/conf.dlistener 1883listener 9001protocol websockets
After modifying the configuration, restart the Mosquitto service to apply the changes:
For Linux (using systemd):
sudo systemctl restart mosquitto
For Windows (using the service manager):
net stop mosquittonet start mosquitto
For macOS (using launchctl):
sudo launchctl stop homebrew.mxcl.mosquittosudo launchctl start homebrew.mxcl.mosquitto
After restarting, the server will apply the new configuration
Once the configuration is complete, you can connect devices to the MQTT server. Open NE2-D11 configuration software, select the appropriate link, and switch the operation mode to MQTT.
Enter the actual MQTT server address and port, then set the MQTT parameters:
Choose "MQTT 3.1.1" as the standard version.
Enter the device name, username, and password (for this example, we use an anonymous connection, so you can enter any user information).
Configure subscription and publication settings based on your requirements. We use MQTTX software to simulate data collection and command dispatching from the server.
After completing the setup, save and restart the device. Observe the device’s indicator light to confirm a successful connection to the server
Once connected to the server, communication can begin. Use MQTTX software for simulating communication.
After connecting to MQTTX, configure the subscription and publication topics. You can send data for communication. Use XCOM serial tools to simulate the data collection from a serial port and send it to the server. You should see that MQTTX successfully receives the data sent via the serial port.
Next, use MQTTX to send data to simulate server commands. Similarly, XCOM will receive the data sent by MQTTX.
This concludes the tutorial on Mosquitto MQTT communication. Those interested can continue exploring further
To add username and password authentication in the Mosquitto configuration file, follow these steps:
Create a Password File: Use the mosquitto_passwd command to generate the password file. For example, execute the following command to create a password file named passwd:
mosquitto_passwd -c /etc/mosquitto/passwd <username>
Replace <username> with the desired username. You will be prompted to enter a password, and the password file will be created.
Enable Authentication in the Configuration: Add the following lines to the Mosquitto configuration file to enable username and password authentication:
password_file /etc/mosquitto/passwdallow_anonymous false
Save and Close the configuration file.
Restart Mosquitto: Restart the Mosquitto service to apply the changes. Refer to the previous section for the restart instructions.
Once completed, clients must provide the correct username and password to connect and access the MQTT broker.