Eclipse Mosquitto is a free and lightweight MQTT broker. It’s widely used to enable communication between devices in IoT (Internet of Things) projects. Follow these steps to install Eclipse Mosquitto on a Windows computer.
Step 1: Download Mosquitto
- Open your web browser.
- Go to the official Mosquitto website: https://mosquitto.org/download/.
- Scroll down to the Windows section.
- Click on the link to download the appropriate installer (e.g., mosquitto-<version>-installer.exe).
Step 2: Install Mosquitto
- Open the downloaded .exe file by double-clicking it.
- The installer will start. Follow the instructions on the screen:
-
- Choose the installation folder (default is fine).
-
- Make sure to select Install Service during installation. This allows Mosquitto to run automatically in the background.
3.Click Next to complete the installation.
Step 3: Add Mosquitto to the System Path
To use Mosquitto in the command prompt, add it to the system path:
- Open the Start Menu, find a Setting , search for “Environment Variables,” and click Edit the system environment variables.
2. In the new window, click Environment Variables.
3. Under System Variables, find Path and click Edit.
4. Click New and add the path to the Mosquitto folder. (By default, it’s C:\Program Files\mosquitto.)
5. Click OK to save.
Step 4: Test Mosquitto Installation
- Open Command Prompt (press Win + R, type cmd, and press Enter)
2. Type the following command to check if Mosquitto is working:
mosquitto -v
3. If the installation is successful, you will see Mosquitto starting and waiting for connections.
Step 5: Start Using Mosquitto
Mosquitto comes with two tools for testing:
- mosquitto_pub: Sends (publishes) messages.
- mosquitto_sub: Receives (subscribes to) messages.
Example: Test Mosquitto Locally
- Open two Command Prompt windows.
- In the first window, subscribe to a topic by typing:
mosquitto_sub -t “test/topic”
This means you’re listening for messages on the topic test/topic.
3. In the second window, publish a message to the same topic:
mosquitto_pub -t “test/topic” -m “Hello, Mosquitto!”
4. The message “Hello, Mosquitto!” will appear in the first window.
Step 6: (Optional) Configure Mosquitto
The Mosquitto configuration file is located in the installation folder (e.g., C:\Program Files\mosquitto\mosquitto.conf).
- You can edit it with a text editor like Notepad to enable security features (e.g., passwords, encryption).
- Restart Mosquitto after making changes.
Detailed Mosquitto Settings for Windows
Once Mosquitto is installed, you can customize its behavior by modifying the configuration file. These settings let you enable authentication, encryption, logging, and more. Follow the steps below to adjust Mosquitto’s settings.
Where is the Configuration File?
The default Mosquitto configuration file is located in the installation directory. On Windows, the typical location is:
C:\Program Files\mosquitto\mosquitto.conf
If the file does not exist, you can create a new one by opening Notepad and saving an empty file as mosquitto.conf in the same folder.
How to Edit the Configuration File
- Open the file mosquitto.conf with a text editor like Notepad or Notepad++.
- Each line in the file represents a specific setting. Lines starting with # are comments and are ignored by Mosquitto.
- After making changes, save the file and restart Mosquitto to apply the settings.
Common Mosquitto Configuration Options
Here are some of the most useful configuration options and how to use them:
1. Set the Listening Port
By default, Mosquitto listens on port 1883 for MQTT connections. To change the port, add this line:
port 1883
To disable the default MQTT port and use a custom port, set:
port 8883
2. Enable Authentication
To restrict access to Mosquitto, enable authentication with a username and password.
Steps:
- Create a password file by opening Command Prompt and typing:
mosquitto_passwd -c password_file username
Replace password_file with the desired file name (e.g., C:\Program Files\mosquitto\passwd) and username with your desired username. You’ll be prompted to enter a password.
2. In the mosquitto.conf file, add:
allow_anonymous false
password_file C:\Program Files\mosquitto\passwd
3. Save the file and restart Mosquitto.
3. Enable SSL/TLS Encryption
To secure connections with encryption, you need SSL certificates.
Steps:
- Obtain or generate SSL certificates (e.g., using Let’s Encrypt).
- In the mosquitto.conf file, add:
listener 8883
cafile C:\path\to\ca.crt
certfile C:\path\to\server.crt
keyfile C:\path\to\server.key
Replace the paths with the actual locations of your certificate files.
3. Restart Mosquitto to apply the changes.
4. Configure Logging
Logging helps you monitor Mosquitto’s activity and troubleshoot issues.
Settings:
- To enable logging, add the following line:
log_dest file C:\Program Files\mosquitto\mosquitto.log
- To display logs in the console, use:
log_dest stdout
Log Levels: You can choose the level of detail in the logs:
log_type error
log_type warning
log_type notice
log_type information
5. Restrict Client Access by IP
To allow only certain IP addresses to connect, use the following setting:
listener 1883
allow_anonymous false
acl_file C:\Program Files\mosquitto\aclfile
Create an aclfile with permissions, for example:
user username
topic readwrite #
6. Use WebSockets
If you want Mosquitto to work with web applications, enable WebSocket support:
listener 8080
protocol websockets
Testing Configuration Changes
After editing the configuration file, restart Mosquitto:
- Open Command Prompt.
- Type:
net stop mosquitto
net start mosquitto
Alternatively, you can restart it from the Services app in Windows.
Tips for Managing Configuration
- Backup Configuration: Always make a backup of mosquitto.conf before making significant changes.
- Check Syntax: If Mosquitto fails to start, check the logs for syntax errors or missing files.
- Start with Simple Settings: Test basic functionality (e.g., ports and authentication) before enabling advanced features like SSL.
Conclusion
By customizing the mosquitto.conf file, you can tailor Mosquitto to meet your needs. Whether you want to secure your broker with encryption, restrict access to authorized users, or enable WebSockets for web applications, these settings give you full control over your MQTT broker.