-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpm-series.py
executable file
·125 lines (102 loc) · 3.48 KB
/
hpm-series.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
#!/usr/bin/python
# coding=utf-8
#
# Copyright © 2018 UnravelTEC
# Michael Maier <[email protected]>
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# If you want to relicense this code under another license, please contact [email protected].
from __future__ import print_function
import serial, sys, time
import datetime, time
import os
import signal
LOGFILE = "/run/hpm"
ser = serial.Serial()
ser.port = "/dev/serial0"
ser.baudrate = 9600
ser.timeout = 1
ser.open()
def exit_gracefully(a,b):
print("set sleep")
sendSimpleCommand("\x68\x01\x02\x95", "Stop Particle Measurement")
ser.close()
os.path.isfile(LOGFILE) and os.access(LOGFILE, os.W_OK) and os.remove(LOGFILE)
print("exit")
exit(0)
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGTERM, exit_gracefully)
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def sendSimpleCommand(cmd, description):
for tried in range(5):
try:
ser.write(cmd)
ret = ser.read(size=2)
except:
eprint("serial comm error")
exit(1)
if len(ret) < 2:
eprint("Error: only " + str(len(ret)) + " bytes received")
continue
if ord(ret[0]) != 0xA5 or ord(ret[1]) != 0xA5:
print(description + ": ret should be 0xA5 0xA5, is", hex(ord(ret[0])), hex(ord(ret[1])))
else:
return
eprint(description, "unsuccessful, exit")
exit(1)
def startMeasurement():
sendSimpleCommand("\x68\x01\x01\x96", "start measurement")
def stopAutoSend():
sendSimpleCommand("\x68\x01\x20\x77", "stop auto send")
def readMeasurement():
try:
ser.write("\x68\x01\x04\x93")
ret = ser.read(size=8)
except:
exit(1)
if len(ret) <8:
eprint("Error: only " + str(len(ret)) + " bytes received")
exit(1)
if ord(ret[0]) != 0x40 or ord(ret[1]) != 0x5 or ord(ret[2]) != 0x4:
eprint("header NOK\n0x40 0x05 0x04")
for i in range(len(ret)):
eprint(hex(ord(ret[i])) + ' ',end='')
eprint('')
pm25 = ord(ret[3]) * 256 + ord(ret[4])
pm10 = ord(ret[5]) * 256 + ord(ret[6])
output_string = 'particulate_matter_ugpm3{{size="pm2.5",sensor="HPM"}} {0}\n'.format(pm25)
output_string += 'particulate_matter_ugpm3{{size="pm10",sensor="HPM"}} {0}\n'.format(pm10)
return(output_string)
if __name__ == "__main__":
print("resetting sensor...")
ser.flushInput()
sendSimpleCommand("\x68\x01\x02\x95", "Stop Particle Measurement")
time.sleep(2)
stopAutoSend()
print("starting measurement...")
startMeasurement()
stopAutoSend()
for i in range(15): # throw away first measurements because of internal running average over 10s and fan speed up
output_string = readMeasurement()
print(output_string, end='')
time.sleep(1)
print("starting logging.")
while True:
output_string = readMeasurement()
logfilehandle = open(LOGFILE, "w",1)
logfilehandle.write(output_string)
logfilehandle.close()
time.sleep(1)