Mosquitto - gangely/espp GitHub Wiki

Installing and starting Mosquitto

Table of Contents

Info

Mosquitto is the MQTT broker.
It can run on any machine inside a LAN; a good choice for a host is a server running 24/7.

Doc: man pages

man mosquitto
man mosquitto_sub
man mosquitto_pub

Installation and service

  1. install the mosquitto package found in your distro

  2. start the mosquitto service; if using systemd:
    systemclt enable --now mosquitto

Configuration

  • the default configuration should work in most cases

  • when sending commands, a configuration with persistence is necessary if the destination device can not be always connected (as the ESP32 in deepsleep)

Adding Persistence

to activate persistence:

  1. add to /etc/mosquitto/mosquitto.conf

    ### my setting for espp ###
    persistence true
    persistence_file mosquitto.db
    persistence_location /var/lib/mosquitto/
    ###########################
  2. create the folder /var/lib/mosquitto/, owned by mosquitto user

    mkdir /var/lib/mosquitto
    chown mosquitto:mosquitto /var/lib/mosquitto

    check later that the file mosquitto.db is well created in this folder

Verification and test

Port 1883

  • verify if port 1883 is open

    nmap -p1883 <brokerhostname>
  • example:

    ~/ nmap -p1883 hc1
    
    Starting Nmap 7.60 ( https://nmap.org ) at 2018-03-15 11:37 CET
    Nmap scan report for hc1 (192.168.0.10)
    Host is up (0.00068s latency).
    rDNS record for 192.168.0.10: nas
    
    PORT     STATE SERVICE
    1883/tcp open  mqtt

MQTT publication and subscription

open two terminals:

  • in first terminal:

    mosquitto_sub -d -t 'test/topic' -v
  • in second terminal:

    mosquitto_pub -d -t 'test/topic' -m "Hello World"

example of output:

  • in first terminal:

    ~/ mosquitto_sub -d -t 'test/topic' -v
    Client mosqsub|3338-pp249 sending CONNECT
    Client mosqsub|3338-pp249 received CONNACK
    Client mosqsub|3338-pp249 sending SUBSCRIBE (Mid: 1, Topic: test/topic, QoS: 0)
    Client mosqsub|3338-pp249 received SUBACK
    Subscribed (mid: 1): 0
    Client mosqsub|3338-pp249 sending PINGREQ
    Client mosqsub|3338-pp249 received PINGRESP
    Client mosqsub|3338-pp249 sending PINGREQ
    Client mosqsub|3338-pp249 received PINGRESP
    [.. waiting publish ..]
    Client mosqsub|3338-pp249 received PUBLISH (d0, q0, r0, m0, 'test/topic', ... (11 bytes))
    test/topic Hello World
    Client mosqsub|3338-pp249 sending PINGREQ
    Client mosqsub|3338-pp249 received PINGRESP
    [.. waiting publish ..]
  • in second terminal:

    ~/ mosquitto_pub -d -t 'test/topic' -m "Hello World"
    Client mosqpub|3359-pp249 sending CONNECT
    Client mosqpub|3359-pp249 received CONNACK
    Client mosqpub|3359-pp249 sending PUBLISH (d0, q0, r0, m1, 'test/topic', ... (11 bytes))
    Client mosqpub|3359-pp249 sending DISCONNECT
⚠️ **GitHub.com Fallback** ⚠️