-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCD30.py
More file actions
78 lines (62 loc) · 2.11 KB
/
Copy pathSCD30.py
File metadata and controls
78 lines (62 loc) · 2.11 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
# https://github.com/feuerrot/esp32-sensor/blob/master/scd30.py
import ustruct
import utime
CMD_START = b"\x00\x10"
CMD_STOP = b"\x01\x04"
CMD_INTERVAL = b"\x46\x00"
CMD_READY = b"\x02\x02"
CMD_READ = b"\x03\x00"
CMD_ASC = b"\x53\x06"
CMD_FRC = b"\x52\x04"
CMD_TEMP_OFFSET = b"\x54\x03"
CMD_ALTITUDE = b"\x51\x02"
CMD_FIRMWARE = b"\xD1\x00"
CMD_RESET = b"\xD3\x04"
class SCD30:
def __init__(self, i2c, address=0x61):
self.i2c = i2c
self.address = address
self.value = {}
def _read(self, location, length):
self.i2c.writeto(self.address, location)
return self.i2c.readfrom(self.address, length)
def _write(self, command, data=None):
if data:
pass
self.i2c.writeto(self.address, command)
def _crc8(self, data):
crc = 0xFF
for elem in data:
crc ^= elem
for shift in range(8):
if (crc & 0x80):
crc = ((crc << 1) ^ 0x31) % 0x100
else:
crc = crc << 1
return crc
def check_crc(self, data):
for offset in range(0, len(data), 3):
if not data[offset+2] == self._crc8(data[offset:offset+2]):
print(offset, data[offset:offset+3])
return False
return True
def start(self):
self._write(CMD_START + b"\x00\x00\x81")
def read(self):
ready_raw = self._read(CMD_READY, 3)
ready_crc = self.check_crc(ready_raw)
ready = ustruct.unpack(">H", ready_raw[0:2])[0]
if not ready and ready_crc:
return False
data_raw = self._read(CMD_READ, 18)
data_crc = self.check_crc(data_raw)
if not data_crc:
return False
self.value["co2"] = ustruct.unpack(">f", data_raw[0:2] + data_raw[3:5])[0]
self.value["temperature"] = ustruct.unpack(">f", data_raw[6:8] + data_raw[9:11])[0]
self.value["humidity"] = ustruct.unpack(">f", data_raw[12:14] + data_raw[15:17])[0]
self.value["timestamp"] = utime.time()
return True
@property
def values(self):
return self.value