Monitoring_with_MQTT - markmac99/ukmon-pitools GitHub Wiki
If you are running OpenHAB, Home Assistant or a similar home automation software you can monitor the camera with MQTT. MQTT is a lightweight messaging protocol widely used in the IoT (Internet of Things) world to communicate between smart devices. For example using MQTT your home automation software can monitor and control smart plugs, switches, weatherstations, doorbells and so forth.
- MQ Broker. I run this on my OpenHAB machine.
- MQ client on the Pi.
- A home automation system such as OpenHAB or Home Assistant.
- A tool for monitoring the Broker during testing. On Windows, you can use MQTT Explorer.
The broker recieves messages from the Pi and makes them available to your HA software. Some HA software comes with a broker preinstalled but if not, you can install one as explained here. I run the broker on the HA server but you can use any machine on your home network that both the Pi and HA server can reach.
I use Mosquitto which is available for most Linux and Windows distributions. If you're running on a Raspberry Pi or Linux, you can install it with
sudo apt-get install mosquitto
Mosquitto supports anonymous connections, connections with username/password and TLS encryption. Setting passwords and TLS up is outside the scope of this document. For testing purposes and on a private network I suggest changing allow_anonymous true in /etc/mosquitto/mosquitto.conf.
Mosquitto installs as a system service and should automatically start. You can check this by typing
sudo systemctl status mosquitto
This should tell you that it is enabled and started. If not, then enable and start it by typing
sudo systemctl enable mosquitto && sudo systemctl start mosquitto
It should now be running and should automatically start at boot.
The client sends messages to the Broker. To install the client software, login as the "pi" user and run the following:
cd ~/source git clone https://github.com/markmac99/rms_mqtt.git cd rms_mqtt pip install -r requirements.txt
Edit config.ini and set the RMSCFG entry to point to your RMS configuration file, then change the value of BROKER and PORT to match the hostname your broker runs on and the port its using. If you've configured your broker to require a username/password you can provide these here. If you've configured your broker to use TLS then supply the full path to the CA.crt file in the CAFILE parameter. Otherwise leave these lines unchanged.
Test the installation by typing python3 sendToMQTT.py test
which will send a test message to your broker under a topic testing.
If you get connection failures make sure you have got the correct details for the broker and port, that the broker is running and that there's no firewall blocking connections. If there are no errors but nothing appears in the broker, this means that the client connected but the broker dropped the message. You will need to check the broker logs to see why this happened.
Now test that real data can be sent by running logPiStats.sh and logMeteorStats.sh. A new topic meteorcams should appear with a child entry named after your camera. Expand this child and you should find entries for cputemp, diskspace, detectioncount and meteorcount.
Assuming your Pi is set to reboot daily, add two cronjobs as shown here:
*/5 * * * * /home/pi/source/rms_mqtt/logPiStats.sh > /dev/null 2>&1 @reboot sleep 300 && /home/pi/source/rms_mqtt/logMeteorStats.sh > /dev/null 2>&1
This will send diskspace and temperature messages every five minutes, and will report a count of meteors and detections five minutes after each reboot. The five minute delay is necessary to allow RMS to complete any update that it is performing.
If your Pi is not rebooting daily, you'll need to decide when is best to run the second job - around 10am is probably safe. Or, you could perhaps monitor the RMS log for some keyword though this is left as an exercise for the Reader.
If refreshing the client from github, do the following:
git stash git pull git stash apply
This will preserve your changes to the config file.
Installing a Home Automation product is beyond the scope of this wiki but you'll find detailed instructions on the maker's websites.
I provide here instructions for two popular HA products, OpenHab and Home Automation.
Login to your OpenHAB GUI as Admin.
- Select Things from the Settings menu.
- Click to create a new Thing.
- If the MQTT Binding isn't available, add it.
- Select MQTT Binding then MQTT Broker.
- Give it a name (label) and fill in the hostname.
- Click Create Thing.
- click Back to return to the Things menu.
- Click on the MQTT Broker Thing to open it.
- Select the Channels tab.
- Click Add Channel.
- Enter a value for the identifier and label (eg uk12345_meteors)
- Select Number value, then scroll down to MQTT State Topic.
- Enter the full topic into this field eg meteorcams/uk123456/meteorcount. You can get the correct value from MQTT Explorer.
- Click Create.
- Repeat for each of the published topics and for each of your cameras.
- Open the MQTT Broker Thing, then select the Channel tab.
- Click to expand a Channel, then select Add Link to Item...
- Select Create a New Item
- Fill in the details. Semantic class should be point, semantic property none.
- Click Link.
- Repeat for each channel.
You can now use this Item in a Widget. For example to display the value you could create a Label Card using this Item. If you set the Action on the card to Analyse, then clicking the card will display a graph of the data. You can also configure the Card's trend line to display a background mini-graph.
Thanks to Stewart Doyle ([email protected]) for this information. I only have instructions for adding by editing the config file - it may also be possible to add via the HA GUI - thats left as an exercise for the reader.
HA seems to come with a broker preinstalled, but configured for a username/password. See note earlier about support for this.
Edit HA's configuration file, configuration.yaml, and add the following, replacgin 006V with your camera ID.
mqtt: sensor: - name: meteorcams_0006V_pi4_cputemp_c unique_id: uniqueid__pi4_cputemp_c icon: mdi:thermometer state_topic: "meteorcams/raspberrypi/cputemp" value_template: "{{ value | round(1) }}" unit_of_measurement: "°C" device_class: temperature - name: meteorcams_0006V_pi4_diskspace unique_id: uniqueid__pi4_diskspace icon: mdi:content-save state_topic: "meteorcams/raspberrypi/diskspace" unit_of_measurement: "%" - name: meteorcams_0006V_detectioncount unique_id: uniqueid__0006V_detectioncount icon: mdi:smoke-detector-variant state_topic: "meteorcams/uk006v/detectioncount" - name: meteorcams_0006V_meteorcount unique_id: uniqueid__0006V_meteorcount icon: mdi:meteor state_topic: "meteorcams/uk006v/meteorcount"
This should make the sensors available in Home Automation once data has been sent at least twice. You can then replicate this for additional sensors for other cameras.