Mosquitto - gangely/espp GitHub Wiki
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
-
install the
mosquitto
package found in your distro -
start the
mosquitto
service; if usingsystemd
:
systemclt enable --now mosquitto
-
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)
see the note Persistent and non-persistent MQTT servers
to activate persistence:
-
add to
/etc/mosquitto/mosquitto.conf
### my setting for espp ### persistence true persistence_file mosquitto.db persistence_location /var/lib/mosquitto/ ###########################
-
create the folder
/var/lib/mosquitto/
, owned bymosquitto
usermkdir /var/lib/mosquitto chown mosquitto:mosquitto /var/lib/mosquitto
check later that the file
mosquitto.db
is well created in this folder
-
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
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