MQTT Toolcase

Install

Install Mosquitto

sudo apt install mosquitto

Install Mosquitto clients

Useful for testing.

sudo apt install mosquitto-clients

Configuration file

This file is usually placed at: /etc/mosquitto/mosquitto.conf

Baseline configuration

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence false
persistence_location /var/lib/mosquitto/

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

include_dir /etc/mosquitto/conf.d

Access via LAN

To access MQTT from another device via local network, create a configuration file to allow connection via LAN if it does not exist:

Example:

# Simple conf file

listener 1883
allow_anonymous true

Clear stored messages

To clear persistent messages, stop the service, delete the database and start the service:

sudo service mosquitto stop

sudo rm /var/lib/mosquitto/mosquitto.db

sudo service mosquitto start

Manage MQTT service

You can start, stop and restart the service:

sudo service mosquitto status
sudo service mosquitto start
sudo service mosquitto stop
sudo service mosquitto restart

Test service

Note: To test the service you need to install mosquitto-clients.

Subscribe to a topic

mosquitto_sub -h localhost -t livingroom

To subscribe to all topics, you can use #. Don’t forget to escape it if you are using bash:

mosquitto_sub -h localhost -t \#

Publish to a topic

mosquitto_pub -h localhost -t livingroom -m "hello"

References:

Leave a Reply