-
Notifications
You must be signed in to change notification settings - Fork 6
/
aht10.py
79 lines (67 loc) · 2.47 KB
/
aht10.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
# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from machine import I2C
import utime as time
from usr.htsensor import HtSensor
class Aht10(HtSensor):
'''
AHT10类
'''
# Initialization command
AHT10_CALIBRATION_CMD = 0xE1
# Trigger measurement
AHT10_START_MEASURMENT_CMD = 0xAC
# reset
AHT10_RESET_CMD = 0xBA
def __init__(self,addre=0x38):
super().__init__(self)
self._aht10_init(addre)
def _aht10_init(self, addre):
'''
initialize the sensor
'''
self.i2c_dev = I2C(I2C.I2C1, I2C.FAST_MODE)
self.i2c_addre = addre
self.init_data = [0x08, 0x00]
# print("sensor init begin.")
super()._write_data([self.AHT10_CALIBRATION_CMD], self.init_data)
time.sleep_ms(300) # at last 300ms
# print("sensor init complete.")
def _aht10_transfor_data(self, data):
'''
Convert humidity and temperature according to the description in the datasheet
'''
r_data = data
# humidity
humidity = (r_data[0] << 12) | (
r_data[1] << 4) | ((r_data[2] & 0xF0) >> 4)
humidity = (humidity/(1 << 20)) * 100.0
# print("current humidity is {0}%".format(humidity))
# temperature
temperature = ((r_data[2] & 0xf) << 16) | (
r_data[3] << 8) | r_data[4]
temperature = (temperature * 200.0 / (1 << 20)) - 50
# print("current temperature is {0}°C".format(temperature))
return (humidity,temperature)
def read(self):
data = super()._trigger_measure()
return self._aht10_transfor_data(data)
def aht10_test(self,count):
for i in range(count):
print("test %d begin." % (i+1))
data = super()._trigger_measure()
self._aht10_transfor_data(data)
print("test %d end." % (i+1))
super().ht_sensor_reset()
time.sleep(1)