-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
56 lines (52 loc) · 1.46 KB
/
script.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
import serial
import json
from time import gmtime, strftime
import statistics
import csv
if []:
print "haha"
else:
print "hoho"
def line2json(line):
try:
data = json.loads(line)
data['time'] = strftime("%H:%M:%S", gmtime())
return data
except:
return None
timestr = strftime("%H:%M:%S", gmtime())
datestr = strftime("%d.%m.%Y", gmtime())
with serial.Serial('/dev/ttyUSB0', 9800, timeout=5) as ser:
with open("test.csv", "a") as file:
lsthum = []
lsttmp = []
lstco2 = []
lstindextmp = []
curmin = strftime("%M", gmtime())
while True:
line = ser.readline() # read a '\n' terminated line
jsonob = line2json(line)
if jsonob:
lsthum.append(float(jsonob['humidity']))
lsttmp.append(float(jsonob['temperature']))
lstco2.append(float(jsonob['co2']))
lstindextmp.append(float(jsonob['heatindex']))
print curmin
if curmin != strftime("%M", gmtime()):
curmin = strftime("%M", gmtime())
row = ""
row = row + unicode(strftime("%d.%m.%Y", gmtime())) + ";"
row = row + unicode(strftime("%H:%M:%S", gmtime())) + ";"
row = row + unicode(statistics.median(lsthum)) + ";"
row = row + unicode(statistics.median(lsttmp)) + ";"
row = row + unicode(statistics.median(lstindextmp)) + ";"
row = row + unicode(statistics.median(lstco2)) + "\n\r"
print row
file.write(row)
file.flush()
lsthum = []
lsttmp = []
lstindextmp = []
lstco2 = []
ser.close()
file.close()