Skip to content

Instantly share code, notes, and snippets.

@chazcheadle
Last active August 31, 2017 04:42
Show Gist options
  • Save chazcheadle/bb6f3f9a12dd19774229ddb532fc78d3 to your computer and use it in GitHub Desktop.
Save chazcheadle/bb6f3f9a12dd19774229ddb532fc78d3 to your computer and use it in GitHub Desktop.
Electric Power Meter Service written in Python with Adafruit.io logging
#!/usr/bin/env python3
## Download your adafruit.io json data and load it.
## UTC_HOURS_OFFSET - Set to match your local offset.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_json(ADAFRUITIO_JSON_DATAFILE)
# Change index to be usable by TimeGrouper().
df.set_index('created_at', inplace=True)
# Adjust for UTC Offset.
df.index = df.index - pd.DateOffset(hours=UTC_HOURS_OFFSET)
# Resample and interpolate data
df.resample('5T', closed='right').max().interpolate(method='linear').groupby(pd.TimeGrouper(freq='15Min'))['value'].agg({'value':{'use': lambda x: max(x) - min(x)}}).plot()
plt.show()
#!/usr/bin/env python3
## Requires:
# USB SDR dongle or equivalent.
# https://github.com/bemasher/rtlamr
# Adafruit.io account
# FILTERID = Your power meter's ID
import json
from subprocess import Popen, PIPE, CalledProcessError
from Adafruit_IO import *
aio = Client('ADAFRUITIO_KEY')
cmd=["rtlamr", "-format=json", "-quiet", "-filterid=FILTERID"]
with Popen(cmd, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
data = json.loads(line)
if isinstance(data, dict):
if 'Message' in data:
cur_kWh = data['Message']['Consumption']
aio.send('ADAFRUITIO_FEEDID', cur_kWh * .01)
if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment