-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsample_2jciebu.py
More file actions
executable file
·130 lines (115 loc) · 5.19 KB
/
sample_2jciebu.py
File metadata and controls
executable file
·130 lines (115 loc) · 5.19 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
import serial
import time
from datetime import datetime
import sys
# LED display rule. Normal Off.
DISPLAY_RULE_NORMALLY_OFF = 0
# LED display rule. Normal On.
DISPLAY_RULE_NORMALLY_ON = 1
def s16(value):
return -(value & 0x8000) | (value & 0x7fff)
def calc_crc(buf, length):
"""
CRC-16 calculation.
"""
crc = 0xFFFF
for i in range(length):
crc = crc ^ buf[i]
for i in range(8):
carrayFlag = crc & 1
crc = crc >> 1
if (carrayFlag == 1):
crc = crc ^ 0xA001
crcH = crc >> 8
crcL = crc & 0x00FF
return (bytearray([crcL, crcH]))
def print_latest_data(data):
"""
print measured latest value.
"""
time_measured = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
temperature = str( s16(int(hex(data[9]) + '{:02x}'.format(data[8], 'x'), 16)) / 100)
relative_humidity = str(int(hex(data[11]) + '{:02x}'.format(data[10], 'x'), 16) / 100)
ambient_light = str(int(hex(data[13]) + '{:02x}'.format(data[12], 'x'), 16))
barometric_pressure = str(int(hex(data[17]) + '{:02x}'.format(data[16], 'x')
+ '{:02x}'.format(data[15], 'x') + '{:02x}'.format(data[14], 'x'), 16) / 1000)
sound_noise = str(int(hex(data[19]) + '{:02x}'.format(data[18], 'x'), 16) / 100)
eTVOC = str(int(hex(data[21]) + '{:02x}'.format(data[20], 'x'), 16))
eCO2 = str(int(hex(data[23]) + '{:02x}'.format(data[22], 'x'), 16))
discomfort_index = str(int(hex(data[25]) + '{:02x}'.format(data[24], 'x'), 16) / 100)
heat_stroke = str(s16(int(hex(data[27]) + '{:02x}'.format(data[26], 'x'), 16)) / 100)
vibration_information = str(int(hex(data[28]), 16))
si_value = str(int(hex(data[30]) + '{:02x}'.format(data[29], 'x'), 16) / 10)
pga = str(int(hex(data[32]) + '{:02x}'.format(data[31], 'x'), 16) / 10)
seismic_intensity = str(int(hex(data[34]) + '{:02x}'.format(data[33], 'x'), 16) / 1000)
temperature_flag = str(int(hex(data[36]) + '{:02x}'.format(data[35], 'x'), 16))
relative_humidity_flag = str(int(hex(data[38]) + '{:02x}'.format(data[37], 'x'), 16))
ambient_light_flag = str(int(hex(data[40]) + '{:02x}'.format(data[39], 'x'), 16))
barometric_pressure_flag = str(int(hex(data[42]) + '{:02x}'.format(data[41], 'x'), 16))
sound_noise_flag = str(int(hex(data[44]) + '{:02x}'.format(data[43], 'x'), 16))
etvoc_flag = str(int(hex(data[46]) + '{:02x}'.format(data[45], 'x'), 16))
eco2_flag = str(int(hex(data[48]) + '{:02x}'.format(data[47], 'x'), 16))
discomfort_index_flag = str(int(hex(data[50]) + '{:02x}'.format(data[49], 'x'), 16))
heat_stroke_flag = str(int(hex(data[52]) + '{:02x}'.format(data[51], 'x'), 16))
si_value_flag = str(int(hex(data[53]), 16))
pga_flag = str(int(hex(data[54]), 16))
seismic_intensity_flag = str(int(hex(data[55]), 16))
print("")
print("Time measured:" + time_measured)
print("Temperature:" + temperature)
print("Relative humidity:" + relative_humidity)
print("Ambient light:" + ambient_light)
print("Barometric pressure:" + barometric_pressure)
print("Sound noise:" + sound_noise)
print("eTVOC:" + eTVOC)
print("eCO2:" + eCO2)
print("Discomfort index:" + discomfort_index)
print("Heat stroke:" + heat_stroke)
print("Vibration information:" + vibration_information)
print("SI value:" + si_value)
print("PGA:" + pga)
print("Seismic intensity:" + seismic_intensity)
print("Temperature flag:" + temperature_flag)
print("Relative humidity flag:" + relative_humidity_flag)
print("Ambient light flag:" + ambient_light_flag)
print("Barometric pressure flag:" + barometric_pressure_flag)
print("Sound noise flag:" + sound_noise_flag)
print("eTVOC flag:" + etvoc_flag)
print("eCO2 flag:" + eco2_flag)
print("Discomfort index flag:" + discomfort_index_flag)
print("Heat stroke flag:" + heat_stroke_flag)
print("SI value flag:" + si_value_flag)
print("PGA flag:" + pga_flag)
print("Seismic intensity flag:" + seismic_intensity_flag)
def now_utc_str():
"""
Get now utc.
"""
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
if __name__ == '__main__':
# Serial.
ser = serial.Serial("/dev/ttyUSB0", 115200, serial.EIGHTBITS, serial.PARITY_NONE)
try:
# LED On. Color of Green.
command = bytearray([0x52, 0x42, 0x0a, 0x00, 0x02, 0x11, 0x51, DISPLAY_RULE_NORMALLY_ON, 0x00, 0, 255, 0])
command = command + calc_crc(command, len(command))
ser.write(command)
time.sleep(0.1)
ret = ser.read(ser.inWaiting())
while ser.isOpen():
# Get Latest data Long.
command = bytearray([0x52, 0x42, 0x05, 0x00, 0x01, 0x21, 0x50])
command = command + calc_crc(command, len(command))
tmp = ser.write(command)
time.sleep(0.1)
data = ser.read(ser.inWaiting())
print_latest_data(data)
time.sleep(1)
except KeyboardInterrupt:
# LED Off.
command = bytearray([0x52, 0x42, 0x0a, 0x00, 0x02, 0x11, 0x51, DISPLAY_RULE_NORMALLY_OFF, 0x00, 0, 0, 0])
command = command + calc_crc(command, len(command))
ser.write(command)
time.sleep(1)
# script finish.
sys.exit