forked from yyongpil/wattsup
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwattsup.py
executable file
·346 lines (295 loc) · 11.1 KB
/
wattsup.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/python2.7 -tt
##!/usr/bin/python3
"""Watts Up? Pro/.Net meter logger (https://www.wattsupmeters.com).
This script reads meter values and saves the read data as CSV files.
Output format: CSV (comma separated values)
YYYY-MM-DD HH:MM:SS.ssssss, W, V, A, WH, Cost, WH/Mo, Cost/Mo, Wmax, Vmax, Amax, Wmin, Vmin, Amin, PF, DC, PC, HZ, VA
* Meters must be connected using USB and data can be collected and logged
from multiple meters.
* Tested on Mac OS X and Linux using Python 2.7 using Watts Up? .Net meters.
* FTDI drivers may need to be installed. (http://www.ftdichip.com/Drivers/VCP.htm)
* Usage: "wattsup.py -h" for help.
Author: Yongpil Yoon
Copyright: Yongpil Yoon 2015
Version: 0.1.1
License: GPLv3
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version. A copy of the GPL
version 3 license can be found in the file COPYING or at
<http://www.gnu.org/licenses/>.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
import argparse
import time
import datetime
import fnmatch
import os
import sys
import serial
from platform import uname
import multiprocessing
class WattsUp(object):
"""WattsUp meter class"""
def __init__(self, port, interval, duration):
"""__init__
Keyword arguments:
port -- the serial port (e.g. /dev/ttyUSB0)
interval -- the sampling interval in seconds
duration -- the sampling duration in minutes
"""
self.serialPort = serial.Serial(port, 115200)
self.interval = interval
self.duration = int(float(duration) * 60) # duration in seconds
self.logfile = None
self.name = port[5:] #
def getRawLine(self):
"""Return the read values as a list"""
fields = self.serialPort.readline().split(',')
while len(fields) < 21:
fields = self.serialPort.readline().split(',')
return fields
def getFormattedLine(self):
"""Return the read values in appropriate formats as a list"""
fields = self.getRawLine()
#format
fields[1] = self.name
fields[2] = datetime.datetime.now()
fields[3] = str(int(fields[3]) / 10.0)
fields[4] = str(int(fields[4]) / 10.0)
fields[5] = str(int(fields[5]) / 1000.0)
fields[6] = str(int(fields[6]) / 10.0)
fields[7] = str(int(fields[7]) / 1000.0)
fields[8] = str(int(fields[8]) / 10.0)
fields[9] = str(int(fields[9]) / 1000.0)
fields[10] = str(int(fields[10]) / 10.0)
fields[11] = str(int(fields[11]) / 10.0)
fields[12] = str(int(fields[12]) / 1000.0)
fields[13] = str(int(fields[13]) / 10.0)
fields[14] = str(int(fields[14]) / 10.0)
fields[15] = str(int(fields[15]) / 1000.0)
# fields[16] = fields[16]
# fields[17] = fields[17]
# fields[18] = fields[18]
fields[19] = str(int(fields[19]) / 10.0)
if len(fields) is 21:
try:
fields[20] = str(int(fields[20].replace(';\r\n', '')))
except:
print fields[20]
fields[20] = 0
#fields[20] = str(int(fields[20].replace(';', '')))
#fields[20] = str(int(fields[20].replace(';', '').strip('\0')))
elif len(fields) is 22:
fields[21] = str(int(fields[21].replace(';\r\n', '')))
return fields[1:]
def log(self, rawOutput, logfilePrefix):
"""Prints the read valuse on stdout and saves as CSV files
Keyword arguments:
rawOutput -- (bool) saves raw output if True
logfilePrefix -- (str) prefix of logfile name
"""
try:
self.serialPort.write('#L,W,3,E,,%d;' % self.interval)
elapsedTime = 0
logfile = open(logfilePrefix + '-' + self.name + '.csv', 'w')
logfile.write('Meter, Time, W, V, A, WH, Cost, WH/Mo, Cost/Mo, Wmax, Vmax, Amax, Wmin, Vmin, Amin, PF, DC, PC, HZ, VA\n')
sys.stdout.write('Meter, Time, W, V, A, WH, Cost, WH/Mo, Cost/Mo, Wmax, Vmax, Amax, Wmin, Vmin, Amin, PF, DC, PC, HZ, VA\n')
while True:
if elapsedTime > self.duration:
break
if rawOutput:
fields = self.getRawLine()
else:
fields = self.getFormattedLine()
if len(fields) < 20:
continue
for field in fields:
sys.stdout.write(str(field) + ', ')
logfile.write('%s, ' % field)
sys.stdout.write('\n')
sys.stdout.flush()
logfile.write('\n')
elapsedTime += self.interval
try:
logfile.close()
except:
pass
self.serialPort.close()
except KeyboardInterrupt:
try:
logfile.close()
except:
pass
self.serialPort.close()
print '\nKeyboardInterrupt in logger, saving log file for: ', logfile.name
# close file
def clear(self):
"""Clears the internal memory of meters and starts logging to the internal memory
"""
#clear internal memory
self.serialPort.write('#R,W,0;')
#start internal logging
self.serialPort.write('#L,W,3,I,,1;')
self.serialPort.close()
print 'Memory cleared! :', self.name
def fetch(self, rawOutput, logfilePrefix):
"""Fetches the saved data from the internal memory of meters
and saves as CSV files
Keyword arguments:
rawOutput -- (bool) saves raw output if True
logfilePrefix -- (str) prefix of logfile name
"""
currentTime = datetime.datetime.now()
#fetch logged data
self.serialPort.write('#D,R,0;')
logfile = open(logfilePrefix + '-' + self.name + '.csv', 'w')
count = 0
while count == 0:
fields = self.serialPort.readline().split(',')
for field in fields:
if '#n' in field:
count = int(fields[-1].split(';')[0])
break
diffSecs = datetime.timedelta(seconds = count * self.interval)
timeIncrement = datetime.timedelta(seconds = self.interval)
startTime = datetime.datetime.now() - diffSecs
for i in range(count):
fields = []
if rawOutput:
fields = self.getRawLine()
else:
fields = self.getFormattedLine()
fields[1] = startTime
for field in fields:
logfile.write('%s, ' % field)
logfile.write('\n')
sys.stdout.write('\rSaving (%s): %d out of %d lines' % (logfile.name, int(i + 1), count))
sys.stdout.flush()
startTime = startTime + timeIncrement
sys.stdout.write('\nDone!\n')
logfile.close()
self.serialPort.close()
def scanPorts():
"""Return (list of str) paths to serial port device files"""
system = uname()[0]
fileName = 'tty.usbserial-*' if system == 'Darwin' else ('ttyUSB*' if system == 'Linux' else '')
if fileName == '':
print '\nThis operating system is not supported!'
exit(1)
ports = []
for file in os.listdir('/dev'):
if fnmatch.fnmatch(file, fileName):
ports.append('/dev/' + file)
if len(ports) == 0:
print '\nWatts Up? meter not found!'
print '* Please connect your meters.'
print '* FTDI drivers have to be installed. (http://www.ftdichip.com/Drivers/VCP.htm)'
print '* Run', str(__file__).split('/')[-1], '--help (-h) for help\n'
exit(1)
return ports
def checkPorts(ports):
"""Return (bool) True if the given ports exist
Keyword arguments:
ports - (list of str) paths to serial port device files
"""
result = True
for port in ports:
if not os.path.exists(port):
print 'Error: Port does not exist: ' + port
result = False
return result
def main(args, parser):
"""main function
Starts logging according to the given options (command line arguments).
Keyword arguments:
parser -- argparse parser
"""
# check ports
if type(args.ports) is not list:
args.ports = scanPorts()
elif not checkPorts(args.ports):
sys.exit(1)
if args.clear and args.fetch:
sys.stdout.write('--clear (-c) and --fetch (-f) cannot be used together!\n\n')
parser.print_help()
sys.exit(1)
meters = []
for port in args.ports:
meters.append(WattsUp(port, args.interval, args.duration))
if args.clear:
for meter in meters:
if not args.yes:
sys.stdout.write('Clear the internal memory of ' + meter.name + ' (Y/n)? ')
selection = raw_input()
if selection.lower() == 'n':
sys.stdout.write('Cancelled.\n')
continue
else:
meter.clear()
else:
sys.stdout.write('Clearing the internal memory of all meters...\n')
meter.clear()
sys.exit(0)
if args.fetch:
for meter in meters:
if not args.yes:
sys.stdout.write('Fetch logged data from ' + meter.name + ' (Y/n)? ')
selection = raw_input()
if selection.lower() == 'n':
sys.stdout.write('Cancelled.\n')
continue
else:
meter.fetch(args.raw, args.prefix)
else:
sys.stdout.write('Fetching the data from all meters...\n')
meter.fetch(args.raw, args.prefix)
sys.exit(0)
if not args.yes:
# Confirm if the options are right
print 'Meters:', args.ports
print 'Logging duration:', args.duration, 'minute(s)'
print 'Reading interval:', args.interval, 'second(s)'
print 'Logfilename prefix:', args.prefix
print 'Log raw data:', args.raw
sys.stdout.write('Are the above options correct (Y/n)? ')
selection = raw_input()
if selection.lower() == 'n':
print ''
parser.print_help()
sys.exit(0)
# Create WattsUp objects and processes (logger)
loggers = []
for meter in meters:
logger = multiprocessing.Process(target=meter.log, args=(args.raw, args.prefix,))
loggers.append(logger)
# logger.start()
# Start logger processes
for logger in loggers:
logger.start()
# KeyboardInterrupt
try:
for logger in loggers:
logger.join()
except KeyboardInterrupt:
print 'Quitting...(main)'
if __name__ == '__main__':
# Parse command line arguments
parser = argparse.ArgumentParser(description='Get readings from Watts Up? meters in real time (through USB) and save them to files.')
parser.add_argument('-p', '--ports', dest='ports', nargs='+', type=str, help='USB port(s) e.g. /dev/ttyUSB0 /dev/ttyUSB1 ..')
parser.add_argument('-t', '--time', dest='duration', default=0.5, help='Duration of logging in minutes (default 0.5 minute)')
parser.add_argument('-i', '--interval', dest='interval', default=1, type=int, help='Reading interval in seconds (default 1 second)')
parser.add_argument('-o', '--outfile-prefix', dest='prefix', default='WU-' + datetime.date.today().isoformat(), help='Prefix of output files')
parser.add_argument('-r', '--raw', dest='raw', action='store_true', default=False, help='Save raw data')
parser.add_argument('-y', '--yes', dest='yes', action='store_true', default=False, help='Yes to all confirmations')
parser.add_argument('-c', '--clear', dest='clear', action='store_true', default=False, help='Clear the internal memory')
parser.add_argument('-f', '--fetch', dest='fetch', action='store_true', default=False, help='Fetch all data logged in the internal memory')
args = parser.parse_args()
try:
main(args, parser)
except KeyboardInterrupt:
print '\nQuitting...'