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 is require 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

To allow remote access

Important

Don't use $ character in the password

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>

Configure mosquitto integration and give as IP 127.0.0.1 with port 1883 and user name and password. The mosquitto integration gui allows to subscribe and publish MQTT topics. As first test it can communicate to console windows.

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

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

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