forked from vzaliva/xbee_temp_sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_collector.py
executable file
·184 lines (148 loc) · 5.16 KB
/
data_collector.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
#!/usr/bin/env python2.7
#
#
import atexit
import json
import getopt
import datetime
import httplib
import logging
import serial
import sys
import time
import numbers
import lockfile
import xbee_api
import tmp36
MAIN_LOGFILE = 'data_collector.log'
CFG_FILE="sensors.cfg"
DATA_FILE = 'data_collector.csv'
LOCK_FILE='xbee_sensor_monitor.lock'
SERIAL_PORT= '/dev/ttyUSB0'
log = None
global_lock = lockfile.FileLock(LOCK_FILE)
# Default values for VREF and Battery volage divider. Could be overriden in config
VREF = 3221 # LM 7833
BATTERY_K=4.43/0.892 # Voldate divider from Vcc to ADC1
def cleanup():
global log
if log:
log.info("Stop")
if global_lock.is_locked():
global_lock.release()
def usage():
print """
%s [-s <port>] [-f <cfg file>] [-c] [-d] [-o <csv file>]
-s <port> -- use serial port <port>. Default is' %s'
-f <cfg file> -- config file name. Default is '%s'
-c -- log to console instead of log file
-d -- debug mode, do not update csv (more logging)
-o <csv file> -- CSV file name. Default is '%s'
""" % (sys.argv[0], SERIAL_PORT, CFG_FILE, DATA_FILE)
def read_config(cfg_fname):
log.info("Reading config file %s" % cfg_fname)
f=open(cfg_fname,"r")
try:
return json.load(f)
finally:
f.close()
def get_adc_v(pkt, adc_idx,vref):
"Retruns ADC value in volts"
return float(pkt.get_adc(adc_idx))*vref/(pkt.num_samples * 1023.0)
def get_battery_from_adc(v,k):
return k*v
def main():
global log
try:
try:
opts, args = getopt.getopt(sys.argv[1:], 'cdf:s:o:', [])
except getopt.GetoptError:
usage()
sys.exit(2)
try:
# timeout=0 causes it to raise AlreadyLocked. Any timeout >0
# causes LockTimeout
global_lock.acquire(timeout=0)
except lockfile.AlreadyLocked:
log.error('Another copy of this program is running')
sys.exit(1)
atexit.register(cleanup)
console = False
debug_mode = False
port = SERIAL_PORT;
cfg_fname = CFG_FILE
data_fname = DATA_FILE
for o, a in opts:
if o in ['-s']:
port = a
elif o in ['-f']:
cfg_fname = a
elif o in ['-o']:
data_fname = a
elif o in ['-c']:
console = True
elif o in ['-d']:
debug_mode = True
else:
usage()
sys.exit(1)
log_format = '%(asctime)s %(process)d %(filename)s:%(lineno)d %(levelname)s %(message)s'
if debug_mode:
log_level=logging.DEBUG
else:
log_level=logging.INFO
if console:
logging.basicConfig(level=log_level, format=log_format)
else:
logging.basicConfig(level=log_level, format=log_format,
filename=MAIN_LOGFILE, filemode='a')
log = logging.getLogger('default')
try:
cfg = read_config(cfg_fname)
except Exception, ex:
log.error("Error reading config file %s" % ex)
sys.exit(1)
log.info('Using serial port %s' % port)
s = serial.serialposix.Serial(port=port,
baudrate=9600, bytesize=8, parity='N', stopbits=1,
timeout=120,
rtscts=1)
log.info("Starting collection")
data_file = file(data_fname, 'a')
pkt_reader = xbee_api.read_packet(s)
while True:
pkt = pkt_reader.next()
try:
sensor_address = str(pkt.address)
try:
scfg = cfg[sensor_address]
vref = scfg["vref"]
battery_k = scfg["vccK"]
except KeyError:
log.warning("No config for sensor '%s'. Using defaults" % sensor_address)
vref = VREF
battery_k = BATTERY_K
radc0 = pkt.get_adc(0)
radc1 = pkt.get_adc(1)
adc0 = float(get_adc_v(pkt,0,vref))
adc1 = float(get_adc_v(pkt,1,vref))
battery_V = get_battery_from_adc(adc1,battery_k)/1000.0
temp_C = tmp36.get_t_from_adc(adc0)
log.info('A={0} T={1:.1f}C V={2:.3f}V'.format(
pkt.address,
temp_C, battery_V))
csv_report = '{0},{1},{2},{3},{4:.1f},{5:.3f}\n'.format(
time.time(), pkt.address, radc0, radc1, temp_C, battery_V*1000.0)
if not debug_mode:
try:
data_file.write(csv_report)
data_file.flush()
except IOError, ex:
log.error("Error writing CSV file: %s" % ex)
except IndexError, e:
# I get this from pkt.get_adc() when packet is broken
log.error('Broken XBee packet: "{0}"'.format(pkt))
except serial.SerialException, ex:
log.debug("Serial error %s" % ex)
if __name__ == '__main__':
main()