-
Notifications
You must be signed in to change notification settings - Fork 0
/
VZ89TE.py
99 lines (86 loc) · 2.91 KB
/
VZ89TE.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
# VHZ89TE.py
# DM1CR Nov 14, 2020
# Class for CO2 Sensor
# for micropython on a ESP32
#
from machine import Pin, I2C
import utime
VZ89TE_Address = 0x70
VZ89TE_CMD_GETSTATUS = bytes([0xC,0,0,0,0,0xF3])
VZ89TE_CMD_GETREVISION = bytes([0xD,0,0,0,0,0xF2])
VZ89TE_CMD_GETR0 = bytes([0x10,0,0,0,0,0xEF])
VZ_89TE_DATE_CODE = 0x0D
class VZ89TE:
def __init__(self, i2c_bus=None, addr=VZ89TE_Address):
self._i2c = i2c_bus
self._i2c_addr = addr
self._d = bytes([0,0,0,0,0,0,0])
self._crc = 0
self.tVOC = 0
self.CO2 = 0
self.Res = 0
self.ErrStatus = 0
if i2c_bus is None:
raise ValueError('An I2C object is required.')
def calcCrc(self, _d):
_sum = 0
self._crc = 0
for _b in _d[:-1]:
_sum += _b
self._crc = _sum % 256
self._crc += _sum // 0x100
self._crc = 0xff-self._crc
return self._crc
def getAddr(self):
return self._i2c_addr
def cmdGetStatus(self):
self._acks = self._i2c.writeto(self._i2c_addr,VZ89TE_CMD_GETSTATUS)
#print("Acks: ", self._acks)
utime.sleep_ms(100)
self._d = self._i2c.readfrom(self._i2c_addr, 7)
self.calcCrc(self._d)
#print("CRC empfangen: ", self._rd[-1], " CRC berechnet: ", self._crc)
#for b in self._rd:
# print(" ", hex(b))
if(self._d[-1] != self._crc):
raise ValueError('crc error.')
return self._d
def getData(self):
self.cmdGetStatus()
self.tVOC = (self._d[0]-13)*1000/229
self.CO2 = (self._d[1]-13) * 1600/229 + 400
self.Res = 10 *(self._d[4]+256*self._d[3])+65536*self._d[2]
self.ErrStatus = self._d[5]
return { "tVOC": self.tVOC,
"CO2": self.CO2,
"Res": self.Res,
"ErrStatus": self.ErrStatus }
def cmdGetRevision(self):
self._i2c.writeto(self._i2c_addr,VZ89TE_CMD_GETREVISION)
utime.sleep_ms(100)
self._r = self._i2c.readfrom(self._i2c_addr, 7)
self.calcCrc(self._r)
if(self._r[-1] != self._crc):
raise ValueError('crc error.')
return self._r
def getRevision(self):
self.cmdGetRevision()
self.year = self._r[0]
self.month = self._r[1]
self.day = self._r[2]
self.charter = self._r[3]
return { "Year": self.year,
"Month": self.month,
"Day": self.day,
"Charter": self.charter }
def cmdGetR0(self):
self._i2c.writeto(self._i2c_addr,VZ89TE_CMD_GETR0)
utime.sleep_ms(100)
self._r0 = self._i2c.readfrom(self._i2c_addr, 7)
self.calcCrc(self._r0)
if(self._r0[-1] != self._crc):
raise ValueError('crc error.')
return self._r0
def getR0(self):
self.cmdGetR0()
return self._r0[0] + 256 * self._r0[1]