In embedded IoT development, the MQTT protocol has become the standard choice for communication between devices.
Why do you need a local MQTT server?
- • Development Testing Isolation: Avoid impacting the production environment and provide a secure testing space.
- • Network Independence: Does not rely on external network connections, ensuring a stable development environment.
- • Debugging Convenience: Allows real-time monitoring of message flows and quick problem identification.
- • Cost Control: Avoid cloud service fees and reduce development costs.
- • Data Security: Sensitive data does not leave the local environment.
Mosquitto
Eclipse Mosquitto is an open-source MQTT message broker maintained by the Eclipse Foundation, featuring the following characteristics:
- • Lightweight: Memory usage is less than 10MB, suitable for resource-constrained environments.
- • Cross-Platform: Supports major operating systems such as Linux, Windows, and macOS.
- • Standard Compliance: Fully supports MQTT 3.1.1 and MQTT 5.0 protocols.
- • Active Community: Has a large user community and abundant documentation resources.
- • Easy to Configure: Configuration files are straightforward, suitable for quick deployment.
Why Choose Mosquitto?
For embedded development, Mosquitto is chosen because:
- 1. Extremely Low Resource Usage: Suitable for long-term operation on development machines.
- 2. Simple Configuration: Installation and configuration can be completed in minutes.
- 3. Complete Functionality: Supports all core MQTT features.
- 4. Rich Documentation: There are numerous tutorials and example codes available.
- 5. Free and Open Source: No usage restrictions.
Installation and Configuration on Windows
Official Installation Package
- 1. Download the Installation Package
# Visit the official download page https://mosquitto.org/download/ # Choose the appropriate version for Windows, 64-bit version recommended - 2. Installation Steps
# 1. Double-click the downloaded installation file # 2. Follow the installation wizard to complete the installation # 3. Default installation path: C:\Program Files\mosquitto # 4. After installation, it will be automatically added to the system PATH - 3. Verify Installation
# Open the command prompt to verify installation mosquitto --version mosquitto_pub --help mosquitto_sub --help
Windows Configuration and Startup
Create Configuration File
# 1. Create a configuration file in the installation directory
# Default path: C:\Program Files\mosquitto\mosquitto.conf
# 2. Create a basic configuration file
# Basic Settings
port 1883
max_connections 1000
max_inflight_messages 20
max_queued_messages 100
# Persistence Settings
persistence true
persistence_location C:\Program Files\mosquitto\data\
autosave_interval 1800
# Log Settings
log_dest file C:\Program Files\mosquitto\log\mosquitto.log
log_type error
log_type warning
log_type notice
log_type information
# Security Settings
allow_anonymous true
# Network Settings
bind_address 0.0.0.0
max_keepalive 65535
# WebSocket Support
listener 9001
protocol websockets
Start Service
# Method 1: Start from command line (debug mode)
cd "C:\Program Files\mosquitto"
mosquitto -c mosquitto.conf -v
# Method 2: Start as a Windows service
# Must be installed as a service first
sc create mosquitto binPath= "C:\Program Files\mosquitto\mosquitto.exe -c C:\Program Files\mosquitto\mosquitto.conf"
sc start mosquitto
Installation and Configuration on Linux
Ubuntu/Debian Systems
# 1. Update package list
sudo apt update
# 2. Install Mosquitto server and client tools
sudo apt install mosquitto mosquitto-clients
# 3. Start and enable the service
sudo systemctl start mosquitto
sudo systemctl enable mosquitto
# 4. Check service status
sudo systemctl status mosquitto
Install from Source Code
# 1. Install dependencies
sudo apt install build-essential libssl-dev libc-ares-dev libwebsockets-dev
# 2. Download source code
wget https://mosquitto.org/files/source/mosquitto-2.0.15.tar.gz
tar -xzf mosquitto-2.0.15.tar.gz
cd mosquitto-2.0.15
# 3. Compile and install
make
sudo make install
# 4. Create user and configuration
sudo useradd -r -s /sbin/nologin mosquitto
sudo mkdir -p /var/lib/mosquitto
sudo chown mosquitto:mosquitto /var/lib/mosquitto
Linux Service Management
# Start service
sudo systemctl start mosquitto
# Stop service
sudo systemctl stop mosquitto
# Restart service
sudo systemctl restart mosquitto
# Check status
sudo systemctl status mosquitto
# View logs
sudo journalctl -u mosquitto -f
# Manual start (debug mode)
mosquitto -c /etc/mosquitto/mosquitto.conf -v
Testing and Verification
Basic Connection Testing
Test Using Command Line Tools
# 1. Start the MQTT server (using Mosquitto as an example)
mosquitto -c /etc/mosquitto/mosquitto.conf -v
# 2. Subscribe to a topic in another terminal
mosquitto_sub -h localhost -t "test/topic" -v
# 3. Publish a message in a third terminal
mosquitto_pub -h localhost -t "test/topic" -m "Hello MQTT!"
# 4. Observe if the subscribed terminal receives the message
Using MQTT Client Tools
Use the following graphical MQTT client tools:
- 1. MQTTX (recommended)
- • Download link: https://mqttx.app/
- • Supports multiple platforms, user-friendly interface
- • Supports MQTT 3.1.1 and 5.0 protocols
- • Download link: http://mqtt-explorer.com/
- • Powerful features, supports topic tree display