The power monitoring plan is coming together

As I may want to use power consumption data (and other information, such as data from the weather station) in a number of different applications, I’ve come to the conclusion that MQTT may be a way forward, allowing those applications that way the data to subscribe to the relevant topics to get it. Specifically, I’m looking at using Mosquitto. I already have a server running all the time for MythTV, so that seems like a logical place to run it.

I’m a little behind the curve with containers and related technology so I thought I’d explore the possibility of using Docker for some of the components I need to get everything up and running. I’m thinking I’ll do that for anything that doesn’t need direct access to hardware.

Happily there’s an existing Docker image for Mosquitto that I can use which makes things fairly straightforward, though I did have one little niggle getting it to run initially. The documentation on the linked page suggests running it using:

docker run -it -p 1883:1883 -p 9001:9001 -v mosquitto.conf:/mosquitto/config/mosquitto.conf -v /mosquitto/data -v /mosquitto/log eclipse-mosquitto

but when I did that I was getting errors about mosquitto.conf not being a directory, so I checked to see if there were any other files in the image’s /mosquitto/config directory (there weren’t) and decided to change the command to:

docker run -it -p 1883:1883 -p 9001:9001 -v /etc/mosquitto:/mosquitto/config -v /mosquitto/data -v /mosquitto/log eclipse-mosquitto

That allowed me to have a copy of the config file on the host system that I could work with, but also allowed me to add acl files and password files in the same place.

Then I realised that to test from the host system (currently Ubuntu 20.04) I also needed to install the mosquitto package which wants to put its config files in the same place, so I moved mine to /etc/mosquitto.docker and updated the docker command accordingly:

/usr/bin/docker run --rm -t -p 1883:1883 -p 9001:9001 -v /etc/mosquitto.docker:/mosquitto/config -v /mosquitto/data -v /mosquitto/log eclipse-mosquitto

then installed the mosquitto and mosquitto-clients packages.

Testing looked good, so the final stage was to get the container up and running at boot time using systemd. As I may wish to move the container elsewhere at some point in the future I bound a spare IP address to the host server’s network interface and came up with the following configuration which I put in /etc/systemd/system/docker-mosquitto.service

[Unit]
Description=Mosquitto Container
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker stop eclipse-mosquitto
ExecStartPre=-/usr/bin/docker rm eclipse-mosquitto
ExecStartPre=/usr/bin/docker pull eclipse-mosquitto
ExecStart=/usr/bin/docker run --rm -t -p <ip-address>:1883:1883 -p <ip-address>:9001:9001 -v /etc/mosquitto.docker:/mosquitto/config -v /mosquitto/data -v /mosquitto/log eclipse-mosquitto

[Install]
WantedBy=multi-user.target

In truth this probably isn’t perfect as it may not restart properly should the instance fail, but right now I’m more concerned about having it start cleanly at boot time than what happens if something breaks.

sudo systemctl daemon-reload
sudo systemctl start docker-mosquitto

started everything and testing again pointed to everything being good, so I think for the time being I’m done.

I’m looking at using emoncms from the Open Energy Monitor project to display power data as it appears to be able to grab inputs from MQTT, so getting that up and running together and probably having to write some code (the horror!) to read and submit the data are probably the next steps. It looks as though there’s a project to create a Docker image for that too, though it seems to be tied up with using MySQL in a second image which I don’t want to do as I already have MySQL running on the host server for MythTV and I’d prefer to use that for my database.

This entry was posted in Computing and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *