-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
raspberry-pi-electricity-monitor.py
56 lines (46 loc) · 1.57 KB
/
raspberry-pi-electricity-monitor.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Software for monitoring the electricity consumption of a home with a Raspberry Pi
# James Singleton 2013 - 2024 - https://unop.uk - https://github.com/jpsingleton
# MIT licensed
import datetime
import struct
import time
# sudo apt-get install python-serial
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
s2 = 'aa0200ad'
h2 = s2.decode('hex')
fixedThreshold = 15
percentThreshold = 10
fRaw = open('/var/www/meterLogRaw.csv', 'a')
fFixed = open('/var/www/meterLogFixedThreshold.csv', 'a')
fPercent = open('/var/www/meterLogPercentageThreshold.csv', 'a')
lastValueF = 0
lastValueP = 0
maxValue = 25000
dateString = '%Y-%m-%d %H:%M:%S'
while True:
try:
ser.write(h2)
data = ser.read(200)
watts = struct.unpack('<H', data[13:15])[0]
if (watts > 0 and watts < maxValue):
print >> fRaw, datetime.datetime.now().strftime(dateString), ',', watts
fRaw.flush()
deltaF = watts - lastValueF
if (deltaF > fixedThreshold or deltaF < -fixedThreshold):
print >> fFixed, datetime.datetime.now().strftime(dateString), ',', lastValueF
time.sleep(1)
print >> fFixed, datetime.datetime.now().strftime(dateString), ',', watts
fFixed.flush()
lastValueF = watts
deltaP = watts - lastValueP
threshold = lastValueP / percentThreshold
if (deltaP > threshold or deltaP < -threshold):
print >> fPercent, datetime.datetime.now().strftime(dateString), ',', lastValueP
time.sleep(1)
print >> fPercent, datetime.datetime.now().strftime(dateString), ',', watts
fPercent.flush()
lastValueP = watts
except:
pass
time.sleep(7)