Implementing MQTT Communication with Mosquitto

ilianaboone

Aug 11, 2023
38
Joined
Aug 11, 2023
Messages
38
Mosquitto is a lightweight open-source MQTT (Message Queuing Telemetry Transport) message broker, widely used for efficient communication in IoT and distributed systems. Below is a structured guide to its deployment and usage


Core Features

  • Lightweight: Optimized for resource-constrained devices (e.g., Raspberry Pi).
  • Cross-Platform: Supports Linux, Windows, macOS.
  • QoS Levels:


    QoS 0 (At most once)
  • QoS 1 (At least once)
  • QoS 2 (Exactly once)

[*]Persistence: Stores messages on disk for session recovery after resta


Installation on Debian 12



  1. Install via APT:

    bash



    Code:
    sudo apt-get update && sudo apt-get install mosquitto
  2. Verify Service Status:
    bash



    Code:
    sudo systemctl status mosqui
Configuration



  1. Edit Config File:

    bash



    Code:
    sudo vim /etc/mosquitto/mosquitto.conf
  2. Key Settings:
    conf










     


    # Default MQTT port (plaintext)




     


    listener 1883




     


    # WebSocket support




     


    listener 9001




     


    protocol websockets




     


    # Allow anonymous connections (disable in production)




     


    allow_anonymous true




     


    # Log file path




     


    log_dest file /var/log/mosquitto/mosquitto.log





  3. Restart Service:
    bash



    Code:
    sudo systemctl restart mosquitt
Security Setup



  1. Create Password File:

    bash



    Code:
    sudo mosquitto_passwd -c /etc/mosquitto/passwd your_username
  2. Enforce Authentication:
    conf










     


    password_file /etc/mosquitto/passwd




     


    allow_anonymous false





  3. Restart Service:
    bash



    Code:
    sudo systemctl restart mosquitt
Testing with MQTTX



  1. Install MQTTX:
    Download from MQTTX官网.
  2. Connect to Broker:


    • Host: mqtt://your_server_ip:1883
    • Username/Password: Set if authentication is enabled.
  3. Publish/Subscribe:


    • Publish Topic: sensor/data
    • Subscribe Topic: device/comma
Serial Server Integration



  1. Hardware: Ebyte NE2-D11 serial server (product page).
  2. Configuration:


    • Set Work Mode to MQTT.
    • Enter broker IP/port and authentication details.
    • Define topics for data publishing/command subscription.
  3. Testing:


    • Use XCOM (serial tool) to send data → Verify reception in MQTTX.
    • Send commands via MQTTX → Confirm receipt in XCOM
Advanced Features



  • TLS/SSL Encryption:
    Uncomment listener 8883 and configure certificates.
  • Client ID Prefix:
    conf



    Code:
    clientid_prefix iot_device_
  • Logging & Debugging:
    Monitor logs with:
    bash



    Code:
    tail -f /var/log/mosquitto/mosquitto.
Best Practices



  1. Avoid Root Permissions: Use sudo for service management.
  2. Disable Anonymous Access in production.
  3. Buffer Day: Restart devices 1 day before returning to normal operation.

 

fussyplink

Apr 1, 2025
2
Joined
Apr 1, 2025
Messages
2
It is quite impressive to combine the MQTT Communication model with Mosquitto. Perhaps you should provide more simulation images to make it easier to understand.

Scratch Games, thanks!

 
Top