Create a new Mosquitto user
mosquitto_passwd -c /etc/mosquitto/passwd publisher1
Create an ACL so that only registered users can publish to topics - anonymous users (web clients viewing the weather page) will only be able to subscribe.
# Allow anonymous access to the sys
topic read $SYS/#
# Allow anonymous to read weather
topic read weather/#
# weewx readwrite to the loop
user publisher1
topic weather/#
Add these modifications to the main config file we created earlier - /etc/mosquitto/conf.d/weewx.conf
allow_anonymous true
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl
Restart Mosquitto
systemctl start mosquitto.service
To test MQTT, open two new SSH sessions to your server, I'll refer to them as Session 1 and Session 2.
In Session 1, subscribe to the weather
topic. The ‘#’ means you want to listen to everything under the weather
topic (use ‘#’ for testing only).
mosquitto_sub -h localhost -t weather/#
In Session 2, publish a message to the weather
topic. You should see the message show up in Session 1 because you are subscribed to the topic being posted to. Additionally, try posting to the topic without authenticating to make sure the ACL is working correctly.
mosquitto_pub -h localhost -t "weather/test" -m "hello, this is a test" -u "publisher1" -P "**************"