Skip to content

Configure mosquitto MQTT broker

David Crook edited this page Apr 16, 2021 · 7 revisions

hosts in example

hostname role
rpih1 shairport-sync client
mqttbrokerhost MQTT broker (mosquitto)
flaskhost app.py host
  • shairport-sync and mosquitto can both be running on the same host (i.e., rpih1 == mqttbrokerhost).
  • And the flask app.py can join them.

A single Raspberry Pi Model 2 B or 3 B works great.

This page assumes

mosquitto config

/etc/mosquitto/conf.d/mosquitto.conf

most basic config

Not very secure. Listens on default port, to any client which connects.

# mosquitto.conf

user mosquitto
max_queued_messages 200
message_size_limit 0
allow_duplicate_messages false
connection_messages true
log_timestamp true

# Defaults to error, warning, notice and information
# debug, error, warning, notice, information, subscribe, unsubscribe, websockets, none, all
#log_type all
#log_type debug

#autosave_interval 900
# Set to 10800 seconds (3 hours)
##autosave_interval 10800
##autosave_on_changes false

# `autosave_on_changes` changes `autosave_interval` to mean: number of
#     subscription changes, retained messages received and queued messages
autosave_interval 8000
autosave_on_changes true

########################################
# new in mosquitto 1.5
########################################
per_listener_settings true

# #######################################
# # TCP MQTT listener
# #######################################
listener 1883
allow_anonymous true

Using mosquitto systemd service

# mqttbrokerhost
sudo systemctl status mosquitto
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
sudo systemctl restart mosquitto
journalctl -u mosquitto

# mosquitto log file lives elsewhere, and has restrictive permissions
sudo zless /var/log/mosquitto/mosquitto.log

Looking at other components

# rpih1
sudo systemctl status shairport-sync   

# flaskhost
sudo systemctl status [email protected]
journalctl -u [email protected]

By default, the running flask web app will be visible at http://flaskhost:8080

Testing

This assumes the mosquitto MQTT clients (mosquitto_sub/mosquitto_pub) are installed on your system (package mosquitto-clients on Raspbian)

# subscribe to topics
mosquitto_sub -v -d \
 -t shairport-sync/rpih1/artist \
 -t shairport-sync/rpih1/album  \
 -t shairport-sync/rpih1/title  \
 -t shairport-sync/rpih1/genre  \
 -t shairport-sync/rpih1/format \
 -t shairport-sync/rpih1/songalbum \
 -t shairport-sync/rpih1/volume \
 -t shairport-sync/rpih1/client_ip \
 -t shairport-sync/rpih1/active_start \
 -t shairport-sync/rpih1/active_end \
 -t shairport-sync/rpih1/play_start \
 -t shairport-sync/rpih1/play_end \
 -t shairport-sync/rpih1/play_flush \
 -t shairport-sync/rpih1/play_resume \
    -h mqttbrokerhost \
    -p 1883

# does not subscribe to "cover" (binary data) topic

this example assumes:

  • mqtt.topic in config.secrets.yaml is: shairport-sync/rpih1
  • mqttbrokerhost is configured as the MQTT host
    • may require /etc/hosts entry to work best
  • -p is the well-known TCP port broker is listening on for MQTT (1883)

The command output should inform of the connection to MQTT broker and subscriptions. And if shairport-sync is sending MQTT metadata to broker, the info should be output.

Here's an example snippet of the metadata topics to get an idea of what it looks like:

Client mosq/X8WkLwgIXaseXl8Flu received PUBLISH (d0, q0, r0, m0, 'shairport-sync/rpih1/play_flush', ... (0 bytes))
shairport-sync/rpih1/play_flush (null)
Client mosq/X8WkLwgIXaseXl8Flu received PUBLISH (d0, q0, r0, m0, 'shairport-sync/rpih1/album', ... (37 bytes))
shairport-sync/rpih1/album Sgt. Pepper's Lonely Hearts Club Band
Client mosq/X8WkLwgIXaseXl8Flu received PUBLISH (d0, q0, r0, m0, 'shairport-sync/rpih1/artist', ... (11 bytes))
shairport-sync/rpih1/artist The Beatles