MQTT and mosquitto

To install sudo apt install mosquitto mosquitto-clients

sudo systemctl status mosquitto to see if it is running

sudo cat /var/log/mosquitto/mosquitto.log to see if it is happy

/etc/mosquitto/mosquitto.conf for the configuration

As https://mosquitto.org/documentation/authentication-methods/ says authentication options must be chosen explicitly

password authentication for mosquitto

One way is using hashed passwords and user names.

sudo mosquitto_passwd -c <e.g. /etc/mosquitto/passwd><username> to create a password file

sudo mosquitto_passwd -D <password file><username> remove user

sudo mosquitto_passwd <password file><username><password> add/update

and create a /etc/mosquitto/conf.d/password.conf file containing

listener 1883 0.0.0.0
allow_anonymous false
password_file /etc/mosquitto/passwd

Important

MQTT sends user name and password as plain text in the Ethernet frames. For a admin only or single user system, it is ok not using it and use anonymous access. If there are multiple users than it is worth to use password authentication as a hurdle to not mess up the system. If the network is not trusted, use encryption as wireguard or tls.

Don't use $ character in the password

anonymous access to mosquitto

Unauthenticated access is ok when mqtt runs in a closed trusted local network. It is enabled by creating /etc/mosquitto/conf.d/anonymous.conf:

listener 1883
allow_anonymous true

Test mosquitto

Test mosquitto in two consoles

mosquitto_sub -h localhost -t "test/topic" -u <username> -P <password>

mosquitto_pub -h localhost -t "test/topic" -m "Hello" -u <username> -P <password>

The topics is like a directory tree structure. It allows to use wildcards to adress more than one topic:

  • home/+/temperature matches home/livingroom/temperature home/kitchen/temperature but not home/upstairs/bedroom/temperature and home/temperature

  • home/# matches home/livingroom/temperature home/kitchen/humidity home/garage/light/status home

MQTT is known to be robust for devices that appear and disappear. This however has to be told to mosquitto. QoS (Quality of Service) are options that deal with such nodes. In QoS=1 at least once, it is assured that the message appears but it can appear multiple times. In QoS=2 it is assured that the message appears just one time. In both options mosquitto needs to store the message, QoS=2 requires more work and network traffic. The default behavior is QoS=0 meaning messages are sent just once without taking care if they arrive. QoS is passed with the command line option -q or --qos. The publisher as well as the subscriber can set those options based on network quality and importance of the pay load. Shelly's seem to use fix QoS=1

man mqtt

man mosquito_sub

man mosquito_pub

If mosquitto is installed in a container called mosquitto run

docker exec -it mosquitto mosquitto_sub -t "elevator/#" -v

MQTT from Shelly

The next steps is knowing how to communicate to the Shelly's

mosquitto_sub -h 192.168.1.5 -p 1883 -t shelly1pmminig3-5432044f7414/status -u <username> -P <password> subscribe the status or

mosquitto_sub -h 192.168.1.5 -t "shelly1pmminig3-5432044f7414/#" -u <username> -P <password> to get everything from this device

and then

mosquitto_pub -h 192.168.1.5 -p 1883 -t shelly1pmminig3-5432044f7414/command -m status_update -u <username> -P <password> to ask for the status

mosquitto_pub -h 192.168.1.5 -p 1883 -t shelly1pmminig3-5432044f7414/rpc -m '{"id":"Shelly.GetStatus", "src":"devices/shelly1pmminig3-5432044f7414/messages/events", "method":"Shelly.GetStatus"}' -u <username> -P <password> new way to get the status

MQTT to Shelly

mosquitto_pub -h 192.168.1.5 -t "shelly1pmminig3-5432044f7414/rpc" -m '{"id": 1, "src": "whatever", "method":"Switch.Set", "params":{"id":0,"on":true} }' -u <username> -P <password> to turn on the switch

mosquitto_pub -h 192.168.1.5 -t "shelly1pmminig3-5432044f7414/rpc" -m '{"id": 1, "src": "whatever", "method":"Switch.Set", "params":{"id":0,"on":false} }' -u <username> -P <password> to turn off the switch

Important

src might not be optional, so without src the Shelly might ignore the command. It is an indicator from where the command comes and can have nonsense data as "whatever"


Linurs startpage