Smart Meter Dashboard: Collecting the Data
Previous Post: Smart Meter Dashboard: Installing and Configuring InfluxDb
Once I had a place to store my power usage data, I needed to start actually collecting it. I already had my radio and software picked out during my initial testing, but now I needed a more permanent setup. With the idea being to lower my power usage, I couldn’t just leave my desktop running all the time. On my first attempt I tried an older Raspberry PI Model B I had laying around, but it couldn’t keep up with the processor load. While it would catch a few entries, for the most part it would drop the data.
A perfect excuse to get a new PI. I went with Raspberry PI 3, although I expect this will work with a Model 2 B.
Installing RTL-SDR
I installed the requirements for RTL-SDR based on Adafruit’s SDR scanner tutorial. I only needed the requirements for rtl-sdr since I’m not using a python library to interact with the radio directly.
Setting rtl-tcp To Start At Boot
Rtlamr uses rtl-tcp to control and read data from the radio. For testing purposes
you can start it on its own terminal by running sudo rtl-tcp
, but I wanted
something that would start automatically on boot. To do this, I created a systemd
unit file at /etc/systemd/system/rtl-tcp.service
that will always restart the
service if it fails.
Once that is created I set it to start on boot with
sudo systemctl enable rtl-tcp.service
.
Installing Go and Rtlamr
The version of Go in the Rasbian repository was high enough to allow rtlamr to
run, so I used it instead of compiling or installing a later ARM Go version:
sudo apt-get install golang
Once that was installed, I went through the standard Go setup procedure to create
my Go path folder and adding the Go environment variables to my .profile
file.
You can reload your .profile
by running . ~/.profile
. With that done, the
installation of Rtlamr was as simple as go get github.com/bemasher/rtlamr
For simplicity, I copied the binary to a dedicated location as root.
Testing
To make sure everything was working I ran rtlamr at the console to make sure it was receiving data. There are quite a few apartments near mine using the same meter type, so it didn’t take long to see a large number of results coming through.
You can filter the results to only include your meter using the --filterid
switch. My meter had the ID printed on it, but it wouldn’t have been hard to
compare my current meter reading to all of the results to find the ID.
Influx Python Upload Script
The final step of the collection was to send the data to InfluxDB. I decided to go with a simple Python script to parse then forward that information on to the database. Rtlmar is capable of outputting its data in JSON format which made reading the data simple line by line. Python also has a nice Influx client library (that I was already planning to use on the API side of things), however the version that is in the Rasbian package is out of date and does not work with the latest Influx version. The one in pip works just fine.
The script itself is pretty simple. It starts rtlamr with a filter for my meter’s ID and a JSON output format. The output is parsed line by line, converted into a format for Influx, and then sent onward. Below is the current state of the script (with placeholders for my db info and my meter ID).
Just like rtl_tcp, I created a systemd unit file to start powermon at boot.
I then enabled it sudo systemctl enable powermon.service
Problems
For now the biggest problem I’m having is the clock on the Raspberry Pi. Any time I lose power (a very infrequent event), the clock gets out of sync. I’ve tried some of the recommended fixes, but so far nothing has seemed to work. I don’t lose any data when this occurs, but the it does throw off the data.
I’m also not a fan of having the 2 minute delay in the script, but that’s a relatively minor problem for now.