-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread-raw-serial.py
executable file
·129 lines (113 loc) · 4.25 KB
/
read-raw-serial.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
#!/usr/bin/python
# coding: utf-8
# Python application for reading raw (comma-delimited) data from BlueSensor via serial console (/dev/ttyUSB0).
import sys, os
import traceback
import datetime, time, pytz
from pprint import pprint
import serial
import json
import random
tz = pytz.timezone('Europe/Ljubljana')
va = [None]*6
def print_exc(f_name, msg=''):
exc_type, exc_obj, exc_tb = sys.exc_info()
exc = traceback.format_exception_only(exc_type, exc_obj)
err = '{}({}): {}'.format(f_name, exc_tb.tb_lineno, msg) + exc[-1].strip()
sys.stderr.write(err + '\n')
sys.stderr.flush()
def time_now_ms():
tt = datetime.datetime.now(tz).timetuple()
now = time.mktime(tt) + 3600 # WTF?!
if tt.tm_isdst: now += 3600
return int(now)*1000
def sim_value(n, max, fluc):
global va
plus = random.random() > 0.5
diff = random.random()*(max*fluc/100)
if va[n] is None: va[n] = max/2
if plus: va[n] += diff
else: va[n] -= diff
if va[n] > max: va[n] -= 2*diff
elif va[n] < 0: va[n] += 2*diff
return va[n]
# Reopen stdout and stderr with buffer size 0 (unbuffered) - only in python 2.7
#sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
#sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
if len(sys.argv) == 1:
sys.stderr.write('This application must be called with parameter specifying USB port.\n')
sys.stderr.write('Use 0 for /dev/ttyUSB0, 1 for /dev/ttyUSB1, etc.\n')
sys.stderr.flush()
sys.exit()
if sys.platform.startswith('win'):
DEVNAME = 'COM'
elif sys.platform.startswith('darwin'):
DEVNAME = '/dev/tty.'
else: # linux
DEVNAME = '/dev/ttyUSB'
USBPORT = DEVNAME + sys.argv[1] # can be 'usbserialxxx' on Mac
simulate = len(sys.argv) > 2
if not simulate:
try:
ser = serial.Serial(port=USBPORT, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
except:
print_exc(sys._getframe().f_code.co_name)
sys.exit(1)
sys.stderr.write('Reading RAW data from ' + USBPORT + '...\n')
sys.stderr.flush()
else:
ser = None
sys.stderr.write('Reading RAW data from simulated ' + USBPORT + '...\n')
sys.stderr.flush()
while True:
try:
if not simulate:
sensordata = ser.readline().rstrip()
values = [str(i) for i in sensordata.split(',')]
else:
values = []
values.append('Raw sensor')
values.append('RAW1')
values.append('IJS')
values.append('Gas 1'); values.append(sim_value(0, 10, 20))
values.append('Gas 2'); values.append(sim_value(1, 10, 20))
values.append('Hum 1'); values.append(sim_value(2, 100, 5))
values.append('Temp 1'); values.append(sim_value(3, 50, 2))
values.append('Temp 2'); values.append(sim_value(4, 50, 2))
values.append('Temp 3'); values.append(sim_value(5, 50, 2))
t = time_now_ms()
data = {
"metadata": {
"device_name": str(values[0]),
"device_id": str(values[1]),
"device_location": str(values[2]),
"sensors": {
"gas1": [str(values[3]), "Gas 1", "raw value", "black"],
"gas2": [str(values[5]), "Gas 2", "raw value", "green"],
"humidity": [str(values[7]), "DHT-22", "%", "blue"],
"temp1": [str(values[9]), "DHT-22", "°C", "orange"],
"temp2": [str(values[11]), "DS18B20", "°C", "red"],
"temp3": [str(values[13]), "DS18B20", "°C", "yellow"]
}
},
"time": t,
"data": {
"gas1": round(values[4], 2),
"gas2": round(values[6], 2),
"humidity": round(values[8], 2),
"temp1": round(values[10], 2),
"temp2": round(values[12], 2),
"temp3": round(values[14], 2)
}
}
data_s = json.dumps(data)
sys.stdout.write(data_s + '\n')
sys.stdout.flush()
time.sleep(1) # wait 1 second
except KeyboardInterrupt:
sys.stderr.write('Quit!\n')
sys.stderr.flush()
sys.exit(0)
except:
print_exc(sys._getframe().f_code.co_name)
time.sleep(1) # wait 1 second