-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsmartmeter.py
More file actions
132 lines (101 loc) · 4.31 KB
/
smartmeter.py
File metadata and controls
132 lines (101 loc) · 4.31 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Initial work: Joost Baltissen
# 2016.10.xx: Romain Aviolat -> added argument support
# -> send output to Influxdb
import sys,time,serial,struct,numpy,requests,argparse
from influxdb import InfluxDBClient
class smartmeter():
def __init__(self):
self.ser = serial.Serial(
port = '/dev/ttyUSB0',
baudrate = '300',
parity=serial.PARITY_EVEN,
bytesize=serial.SEVENBITS,
stopbits=serial.STOPBITS_ONE,
xonxoff = 0,
rtscts = 0,
timeout = 20
)
def readData(self, source=None, event=None):
while True:
self.ser.close()
#self.ser.open()
try:
self.ser.open()
except:
sys.exit ("Error while opening serial-port:" % self.ser.port)
request = struct.pack('BBBBB', 0x2F, 0x3F, 0x21, 0x0D, 0x0A)
self.ser.write(request)
time.sleep(.2) #give time to send
self.ser.flushInput()
tdata = self.ser.read() # Wait forever for anything
time.sleep(1) # Sleep (or inWaiting() doesn't give the correct value)
data_left = self.ser.inWaiting() # Get the number of characters ready to be read
tdata += self.ser.read(data_left) # Do the read and combine it with the first character
self.ser.flushInput()
intString = numpy.frombuffer(tdata, numpy.uint8)
Baudrate = tdata[4]
ModID = tdata[6]
intBaudrate = numpy.frombuffer(Baudrate, numpy.int8)
ack= struct.pack('BBBBBB',0x06,0x30,0x30,0x30,0x0D,0x0A)
self.ser.write(ack)
print ack
time.sleep(.02)
self.ser.flushInput()
tdata = self.ser.read() # Wait forever for anything
time.sleep(12) # Sleep (or inWaiting() doesn't give the correct value)
data_left = self.ser.inWaiting() # Get the number of characters ready to be read
tdata += self.ser.read(data_left) # Do the read and combine it with the first character
self.ser.close()
start = tdata.find('1.8.1')+6
end = tdata.find('*', start)
use_high = float(tdata[start:end])
start = tdata.find('1.8.2')+6
end = tdata.find('*', start)
use_low = float(tdata[start:end])
start = tdata.find('2.8.1')+6
end = tdata.find('*', start)
prod_high = float(tdata[start:end])
start = tdata.find('2.8.2')+6
end = tdata.find('*', start)
prod_low = float(tdata[start:end])
start = tdata.find('31.7(')+5
end = tdata.find('*', start)
values={'use_low':use_low,
'use_high':use_high,
'prod_low':prod_low,
'prod_high':prod_high}
stacked_json = []
for val in values:
json_body = {
"measurement": val,
"tags": {
"host": "smartmeter"
},
"fields": {
"value": values[val]
}
}
stacked_json.append(json_body)
print(stacked_json)
client = InfluxDBClient('localhost', 8086, 'user', 'pass', 'smartmeter_db')
client.write_points(stacked_json)
time.sleep(30)
if __name__ == '__main__':
device = smartmeter()
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--device', help="Specify the device ex: /dev/ttyUSB0", required=True)
parser.add_argument('-D', '--daemon', action="store_true", help="Fork and run in background")
args = parser.parse_args()
#device.ser.port = args.device
if args.daemon:
if os.fork()==0:
os.setsid()
sys.stdout=open("/dev/null", 'w')
sys.stdin=open("/dev/null", 'r')
if os.fork()==0:
device.readData()
sys.exit(0)
else:
device.readData()