Skip to content
December 17, 2012 / hrafnkell

Monitoring power usage in my home

download

I recently started a small project monitoring the power usage of my home. I use arduino and a LDR to count blinks on my power meter. It blinks a 1000 times per 1Kwh. The arduino is running a very simple sketch that counts the blinks. A python script running on a model B raspberry pi queries the arduino every 5 minutes and sends the amount of blinks recorded in that period to a carbon server running on a VPS I have. The carbon data is then displayed by graphite, where i can get pretty little graphs like the one above.

The python code for pushing the data to carbon is delightfully simple:

 import serial
 import requests, json
 from carbon import carbon

c = carbon()

API_KEY = "o_"
 FEED_ID = "89441"

ser = serial.Serial("/dev/ttyUSB0", 9600, timeout=2)
 ser.open()
 ser.write('h')
 ser.write('t')
 ser.write('c')
 ser.write('r')
 res = ser.readlines()
 ser.close()

print res

results = {}
 results['version'] = '1.0.0'
 results['datastreams'] = []
 temperature = res[1][res[1].find(' '):].strip()
 humidity = res[0][res[0].find(' '):].strip()
 if temperature > 0 and temperature < 50: results['datastreams'].append({'id':'1','current_value': temperature}) if humidity > 10 and temperature < 80: results['datastreams'].append({'id':'0','current_value': humidity}) blinks = res[2][res[2].find(' '):].strip() if blinks > 0 and blinks < 1000:
 results['datastreams'].append({'id':'2','current_value':blinks})

f = open('power.txt', 'r+')
 v = int(f.readline())
 powertotal = int(blinks) + v
 f.seek(0)
 f.truncate()
 f.write(str(powertotal))
 f.close()
 results['datastreams'].append({'id':'3', 'current_value':str(powertotal)})

c.addData("garage.temperature", temperature)
 c.addData("garage.humidity", humidity)
 c.addData("garage.blinks", blinks)
 if not c.send():
 print "Carbon troubles"

url = 'http://api.cosm.com/v2/feeds/%s' % FEED_ID
 headers = {'X-ApiKey':API_KEY}
 data = json.dumps(results)
 print data

r = requests.put(url, data=data, headers=headers) # Push data to cosm
 print r
 

I also push the data to cosm using this script.

If anyone’s interested i can post a better writeup here… Until then I’ll just make do with this.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: