-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsps30.py
264 lines (233 loc) · 9.06 KB
/
sps30.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import sys
from time import sleep
from datetime import datetime
from i2c.i2c import I2C
# I2C commands
CMD_START_MEASUREMENT = [0x00, 0x10]
CMD_START_MEASUREMENT_IEE = [0x00, 0x10, 0x03, 0x00] #IEEE754_float
CMD_START_MEASUREMENT_INT = [0x00, 0x10, 0x05, 0x00] #unsigned_16_bit_integer
CMD_STOP_MEASUREMENT = [0x01, 0x04]
CMD_READ_DATA_READY_FLAG = [0x02, 0x02]
CMD_READ_MEASURED_VALUES = [0x03, 0x00]
CMD_SLEEP = [0x10, 0x01]
CMD_WAKEUP = [0x11, 0x03]
CMD_START_FAN_CLEANING = [0x56, 0x07]
CMD_AUTO_CLEANING_INTERVAL = [0x80, 0x04]
CMD_PRODUCT_TYPE = [0xD0, 0x02]
CMD_SERIAL_NUMBER = [0xD0, 0x33]
CMD_FIRMWARE_VERSION = [0xD1, 0x00]
CMD_READ_STATUS_REGISTER = [0xD2, 0x06]
CMD_CLEAR_STATUS_REGISTER = [0xD2, 0x10]
CMD_RESET = [0xD3, 0x04]
# Length of response in bytes
NBYTES_READ_DATA_READY_FLAG = 3
NBYTES_MEASURED_VALUES_FLOAT = 60 # IEEE754 float
NBYTES_MEASURED_VALUES_INTEGER = 30 # unsigned 16 bit integer
NBYTES_AUTO_CLEANING_INTERVAL = 6
NBYTES_PRODUCT_TYPE = 12
NBYTES_SERIAL_NUMBER = 48
NBYTES_FIRMWARE_VERSION = 3
NBYTES_READ_STATUS_REGISTER = 6
# Packet size including checksum byte [data1, data2, checksum]
PACKET_SIZE = 3
# Size of each measurement data packet (PMx) including checksum bytes, in bytes
SIZE_FLOAT = 6 # IEEE754 float
SIZE_INTEGER = 3 # unsigned 16 bit integer
# Error value
SPS_DATA_ERR = [0xC2,0xFE,0x00,0x00] #-127.0
#[0xBF,0x80,0x00,0x00] #-1.0
class SPS30:
# Init I2C BUS
def __init__(self, bus: int = 1, address: int = 0x69):
self.cleaning = 0
self.cleaning_ts = 0
self.i2c = I2C(bus, address)
self.type = self.product_type()
self.sn = self.serial_number()
self.fw = self.firmware_version()
# I2C commands BEGIN
def serial_number(self) -> str:
self.i2c.write(CMD_SERIAL_NUMBER)
data = self.i2c.read(NBYTES_SERIAL_NUMBER)
result = ""
for i in range(0, NBYTES_SERIAL_NUMBER, PACKET_SIZE):
if self.crc_calc(data[i:i+2]) != data[i+2]:
return "CRC mismatch"
if(data[i:i+2] != [0x00, 0x00]):
result += "".join(map(chr, data[i:i+2]))
return str(result)
def firmware_version(self) -> str:
self.i2c.write(CMD_FIRMWARE_VERSION)
data = self.i2c.read(NBYTES_FIRMWARE_VERSION)
if self.crc_calc(data[:2]) != data[2]:
return "CRC mismatch"
return ".".join(map(str, data[:2]))
def product_type(self) -> str:
self.i2c.write(CMD_PRODUCT_TYPE)
data = self.i2c.read(NBYTES_PRODUCT_TYPE)
result = ""
for i in range(0, NBYTES_PRODUCT_TYPE, 3):
if self.crc_calc(data[i:i+2]) != data[i+2]:
return "CRC mismatch"
if(data[i:i+2] != [0x00, 0x00]):
result += "".join(map(chr, data[i:i+2]))
return str(result)
def read_status_register(self) -> dict:
self.i2c.write(CMD_READ_STATUS_REGISTER)
data = self.i2c.read(NBYTES_READ_STATUS_REGISTER)
status = []
for i in range(0, NBYTES_READ_STATUS_REGISTER, PACKET_SIZE):
if self.crc_calc(data[i:i+2]) != data[i+2]:
return "CRC mismatch"
status.extend(data[i:i+2])
binary = '{:032b}'.format(
status[0] << 24 | status[1] << 16 | status[2] << 8 | status[3])
speed_status = "high/low" if int(binary[10]) == 1 else "ok"
laser_status = "outofrange" if int(binary[26]) == 1 else "ok"
fan_status = "0rpm" if int(binary[27]) == 1 else "ok"
sps_status = (int(binary[10]) | int(binary[26]) | int(binary[27]))
return {
"speed": speed_status,
"laser": laser_status,
"fan": fan_status,
"status":sps_status
}
def clear_status_register(self) -> None:
self.i2c.write(CMD_CLEAR_STATUS_REGISTER)
def read_data_ready_flag(self) -> bool:
self.i2c.write(CMD_READ_DATA_READY_FLAG)
data = self.i2c.read(NBYTES_READ_DATA_READY_FLAG)
if self.crc_calc(data[:2]) != data[2]:
return False
return True if data[1] == 1 else False
def sleep(self) -> None:
self.i2c.write(CMD_SLEEP)
def wakeup(self) -> None:
self.i2c.write(CMD_WAKEUP)
def start_fan_cleaning(self) -> None:
self.cleaning = 1
self.i2c.write(CMD_START_FAN_CLEANING)
sleep(12)
self.cleaning = 0
self.cleaning_ts = int(datetime.now().timestamp())
def read_auto_cleaning_interval(self, unit: str = 's') -> int:
dividier = {
'd' : 86400,
'm' : 3600,
'h' : 60,
's' : 1
}
self.i2c.write(CMD_AUTO_CLEANING_INTERVAL)
data = self.i2c.read(NBYTES_AUTO_CLEANING_INTERVAL)
interval = []
for i in range(0, NBYTES_AUTO_CLEANING_INTERVAL, 3):
if self.crc_calc(data[i:i+2]) != data[i+2]:
return "CRC mismatch"
interval.extend(data[i:i+2])
ret = (interval[0] << 24 | interval[1] << 16 | interval[2] << 8 | interval[3])
return ret / dividier[unit]
def write_auto_cleaning_interval_days(self, days: int) -> int:
seconds = days * 86400
interval = []
interval.append((seconds & 0xff000000) >> 24)
interval.append((seconds & 0x00ff0000) >> 16)
interval.append((seconds & 0x0000ff00) >> 8)
interval.append(seconds & 0x000000ff)
data = CMD_AUTO_CLEANING_INTERVAL
data.extend([interval[0], interval[1]])
data.append(self.crc_calc(data[2:4]))
data.extend([interval[2], interval[3]])
data.append(self.crc_calc(data[5:7]))
self.i2c.write(data)
sleep(0.05)
return self.read_auto_cleaning_interval()
def reset(self) -> None:
self.i2c.write(CMD_RESET)
def stop_measurement_and_close(self) -> None:
self.i2c.write(CMD_STOP_MEASUREMENT)
self.i2c.close()
def stop_measurement(self) -> None:
self.i2c.write(CMD_STOP_MEASUREMENT)
def start_measurement(self) -> None:
data = CMD_START_MEASUREMENT_IEE
data.append(self.crc_calc(data[2:4]))
self.i2c.write(data)
sleep(0.05)
def read_measurement(self) -> list:
if not self.read_data_ready_flag():
sleep(1)
self.i2c.write(CMD_READ_MEASURED_VALUES)
data = self.i2c.read(NBYTES_MEASURED_VALUES_FLOAT)
return data
# I2C commands END
#
# Helper functions BEGIN
def crc_calc(self, data: list) -> int:
crc = 0xFF
for i in range(2):
crc ^= data[i]
for _ in range(8, 0, -1):
if crc & 0x80:
crc = (crc << 1) ^ 0x31
else:
crc = crc << 1
return (crc & 0x0000FF)
def ieee754_number_conversion(self, data: int) -> float:
binary = "{:032b}".format(data)
sign = int(binary[0:1])
exp = int(binary[1:9], 2) - 127
exp = 0 if exp < 0 else exp
mantissa = binary[9:]
real = int(('1' + mantissa[:exp]), 2)
decimal = mantissa[exp:]
dec = 0.0
for i in range(len(decimal)):
dec += int(decimal[i]) / (2**(i+1))
return round((((-1)**(sign) * real) + dec), 3)
def is_cleaning(self) -> int:
return self.cleaning
# Helper functions END
#
# Main functions
def values_to_list(self, data: list) -> dict:
values = ["pm1", "pm2", "pm4", "pm10", "nc0", "nc1", "nc2", "nc4", "nc10", "tps"]
assoc = {
"pm1": 0.0,
"pm2": 0.0,
"pm4": 0.0,
"pm10": 0.0,
"nc0": 0.0,
"nc1": 0.0,
"nc2": 0.0,
"nc4": 0.0,
"nc10": 0.0,
"tps": 0.0
}
for block, (idx) in enumerate(values):
sensor_data = []
for i in range(0, SIZE_FLOAT, PACKET_SIZE):
offset = (block * SIZE_FLOAT) + i
if self.crc_calc(data[offset:offset+2]) != data[offset+2]:
sensor_data.extend(SPS_DATA_ERR)
else:
sensor_data.extend(data[offset:offset+2])
assoc[idx] = self.ieee754_number_conversion(
sensor_data[0] << 24 | sensor_data[1] << 16 | sensor_data[2] << 8 | sensor_data[3])
return assoc
def status_to_str(self, sps_status_register: dict) -> str:
sps_info = ""
if(sps_status_register['status'] != 0):
for key in sps_status_register:
sps_info = sps_info + "/" + key + "-" + sps_status_register[key]
sps_info = sps_info + "]"
else:
sps_info = "SPS-OK]"
return sps_info
def read_values(self) -> dict:
return self.values_to_list(self.read_measurement())
def read_status(self) -> dict:
sps_status_register = self.read_status_register()
return {
"text": self.status_to_str(sps_status_register),
"stat": str(sps_status_register['status']) + "0000000"
}