-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
maplin2emoncms.py
35 lines (30 loc) · 1.15 KB
/
maplin2emoncms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Monitoring electricity use with a Maplin monitor, Raspberry Pi and emoncms
# James Singleton 2016 - https://unop.uk - https://github.com/jpsingleton
# MIT licensed
import serial
import struct
import datetime
import time
import urllib2
# Replace this with your emoncms Write API Key from the user account page
writeApiKey = 'WRITE API KEY GOES HERE'
# Change this if you use a different host or SSL (a must if not on a LAN)
baseAddress = 'http://localhost/emoncms'
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
s2 = 'aa0200ad'
h2 = s2.decode('hex')
maxValue = 25000
while True:
try:
ser.write(h2)
data = ser.read(200)
watts = struct.unpack('<H', data[13:15])[0]
if (watts > 0 and watts < maxValue):
url = baseAddress + '/input/post.json?node=1&json={power:' + str(watts) + '}&apikey=' + writeApiKey
request = urllib2.Request(url)
response = urllib2.urlopen(request, timeout=6)
except Exception:
import traceback
print traceback.format_exc()
pass
time.sleep(7)