Skip to content

Commit 24575bb

Browse files
committed
add pyAI-K210 platform sensors
DS18B20, DHT11, VL53L1X(TOF)
1 parent 137ba44 commit 24575bb

5 files changed

Lines changed: 544 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from modules import onewire
2+
3+
class DS18X20:
4+
_CONVERT = const(0x44)
5+
_RD_SCRATCH = const(0xBE)
6+
_WR_SCRATCH = const(0x4E)
7+
_SKIP_ROM = const(0xCC)
8+
def __init__(self, pin):
9+
self.device = onewire(pin)
10+
self.buf = bytearray(9)
11+
12+
def scan(self):
13+
return self.device.search(65)
14+
15+
def convert_temp(self):
16+
self.device.reset()
17+
self.device.writebyte(_SKIP_ROM)
18+
self.device.writebyte(_CONVERT)
19+
20+
def read_scratch(self, rom):
21+
self.device.reset()
22+
self.convert_temp()
23+
self.device.select(rom)
24+
self.device.writebyte(_RD_SCRATCH)
25+
self.buf = self.device.readbuffer(len(self.buf))
26+
if self.device.crc8(self.buf):
27+
raise Exception("CRC error")
28+
return self.buf
29+
30+
def write_scratch(self, rom, buf):
31+
self.device.reset()
32+
self.device.select(rom)
33+
self.device.writebyte(_WR_SCRATCH)
34+
self.device.writebuffer(buf)
35+
36+
def read_temp(self, rom):
37+
temp = []
38+
for _rom in rom:
39+
buf = self.read_scratch(_rom)
40+
if _rom[0] == 0x10:
41+
if buf[1]:
42+
t = buf[0] >> 1 | 0x80
43+
t = -((~t + 1) & 0xFF)
44+
else:
45+
t = buf[0] >> 1
46+
temp.append(t - 0.25 + (buf[7] - buf[6]) / buf[7])
47+
else:
48+
t = buf[1] << 8 | buf[0]
49+
if t & 0x8000: # sign bit set
50+
t = -((t ^ 0xFFFF) + 1)
51+
temp.append(t / 16)
52+
if len(temp) < 1:
53+
return -1
54+
if len(temp) < 2:
55+
return temp[0]
56+
return tuple(temp)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#实验名称:DS18B20传感器例程
2+
#实验平台:01Studio pyAI-K210开发板
3+
4+
#导入相关模块,onewire库请更新至较新版固件
5+
from fpioa_manager import *
6+
from micropython import const
7+
from board import board_info
8+
from ds18x20 import DS18X20
9+
import time,lcd,image
10+
11+
lcd.init() #初始化 LCD
12+
lcd.clear(lcd.WHITE) #清屏白色
13+
14+
#初始化DS18B20 GPIO
15+
fm.register(10, fm.fpioa.GPIOHS2, force=True)
16+
ds18b20_2 = DS18X20(fm.fpioa.GPIOHS2)
17+
rom = ds18b20_2.scan()
18+
19+
while True:
20+
21+
#采集温度,保留2位小数
22+
temp = str('%.2f'%ds18b20_2.read_temp(rom))
23+
24+
#显示字符
25+
lcd.draw_string(90, 100, "DS18B20 Temp:",lcd.BLACK, lcd.WHITE)
26+
lcd.draw_string(110, 120, temp+ " C",lcd.BLACK, lcd.WHITE)
27+
28+
#串口打印
29+
print(temp+" C")
30+
31+
time.sleep_ms(1000)#每秒采集1次
32+
33+
34+
35+
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import time
2+
from Maix import GPIO
3+
4+
class DHT11Result:
5+
# 'DHT11 sensor result returned by DHT11.read() method'
6+
7+
ERR_NO_ERROR = 0
8+
ERR_MISSING_DATA = 1
9+
ERR_CRC = 2
10+
11+
error_code = ERR_NO_ERROR
12+
temperature = -1
13+
humidity = -1
14+
15+
def __init__(self, error_code, temperature, humidity):
16+
self.error_code = error_code
17+
self.temperature = temperature
18+
self.humidity = humidity
19+
20+
def is_valid(self):
21+
return self.error_code == DHT11Result.ERR_NO_ERROR
22+
23+
24+
class DHT11:
25+
26+
def __init__(self, gpio = None):
27+
# '1DHT11 sensor reader class for MaixPy'
28+
29+
if gpio != None:
30+
self._gpio = gpio
31+
else:
32+
raise Exception("please input a valid gpio")
33+
34+
def read(self):
35+
self._gpio.mode(GPIO.OUT)
36+
37+
# send initial high
38+
self.__send_and_sleep(1, 50)
39+
40+
# pull down to low
41+
self.__send_and_sleep(0, 20)
42+
43+
# change to input using pull up
44+
self._gpio.mode(GPIO.IN)
45+
46+
# collect data into an array
47+
data = self.__collect_input()
48+
49+
# parse lengths of all data pull up periods
50+
pull_up_lengths = self.__parse_data_pull_up_lengths(data)
51+
52+
# if bit count mismatch, return error (4 byte data + 1 byte checksum)
53+
if len(pull_up_lengths) != 40:
54+
return DHT11Result(DHT11Result.ERR_MISSING_DATA, 0, 0)
55+
56+
# calculate bits from lengths of the pull up periods
57+
bits = self.__calculate_bits(pull_up_lengths)
58+
59+
# we have the bits, calculate bytes
60+
the_bytes = self.__bits_to_bytes(bits)
61+
62+
# calculate checksum and check
63+
checksum = self.__calculate_checksum(the_bytes)
64+
if the_bytes[4] != checksum:
65+
return DHT11Result(DHT11Result.ERR_CRC, 0, 0)
66+
67+
# ok, we have valid data
68+
69+
# The meaning of the return sensor values
70+
# the_bytes[0]: humidity int
71+
# the_bytes[1]: humidity decimal
72+
# the_bytes[2]: temperature int
73+
# the_bytes[3]: temperature decimal
74+
75+
temperature = the_bytes[2] + float(the_bytes[3]) / 10
76+
humidity = the_bytes[0] + float(the_bytes[1]) / 10
77+
78+
return DHT11Result(DHT11Result.ERR_NO_ERROR, temperature, humidity)
79+
80+
def __send_and_sleep(self, output, sleep):
81+
self._gpio.value(output)
82+
time.sleep_ms(sleep)
83+
84+
def __collect_input(self):
85+
# collect the data while unchanged found
86+
unchanged_count = 0
87+
88+
# this is used to determine where is the end of the data
89+
max_unchanged_count = 100
90+
91+
last = -1
92+
data = []
93+
while True:
94+
current = self._gpio.value()
95+
data.append(current)
96+
if last != current:
97+
unchanged_count = 0
98+
last = current
99+
else:
100+
unchanged_count += 1
101+
if unchanged_count > max_unchanged_count:
102+
break
103+
104+
return data
105+
106+
def __parse_data_pull_up_lengths(self, data):
107+
STATE_INIT_PULL_DOWN = 1
108+
STATE_INIT_PULL_UP = 2
109+
STATE_DATA_FIRST_PULL_DOWN = 3
110+
STATE_DATA_PULL_UP = 4
111+
STATE_DATA_PULL_DOWN = 5
112+
113+
state = STATE_INIT_PULL_DOWN
114+
115+
lengths = [] # will contain the lengths of data pull up periods
116+
current_length = 0 # will contain the length of the previous period
117+
118+
for i in range(len(data)):
119+
120+
current = data[i]
121+
current_length += 1
122+
123+
if state == STATE_INIT_PULL_DOWN:
124+
if current == 0:
125+
# ok, we got the initial pull down
126+
state = STATE_INIT_PULL_UP
127+
continue
128+
else:
129+
continue
130+
if state == STATE_INIT_PULL_UP:
131+
if current == 1:
132+
# ok, we got the initial pull up
133+
state = STATE_DATA_FIRST_PULL_DOWN
134+
continue
135+
else:
136+
continue
137+
if state == STATE_DATA_FIRST_PULL_DOWN:
138+
if current == 0:
139+
# we have the initial pull down, the next will be the data pull up
140+
state = STATE_DATA_PULL_UP
141+
continue
142+
else:
143+
continue
144+
if state == STATE_DATA_PULL_UP:
145+
if current == 1:
146+
# data pulled up, the length of this pull up will determine whether it is 0 or 1
147+
current_length = 0
148+
state = STATE_DATA_PULL_DOWN
149+
continue
150+
else:
151+
continue
152+
if state == STATE_DATA_PULL_DOWN:
153+
if current == 0:
154+
# pulled down, we store the length of the previous pull up period
155+
lengths.append(current_length)
156+
state = STATE_DATA_PULL_UP
157+
continue
158+
else:
159+
continue
160+
161+
return lengths
162+
163+
def __calculate_bits(self, pull_up_lengths):
164+
# find shortest and longest period
165+
shortest_pull_up = 1000
166+
longest_pull_up = 0
167+
168+
for i in range(0, len(pull_up_lengths)):
169+
length = pull_up_lengths[i]
170+
if length < shortest_pull_up:
171+
shortest_pull_up = length
172+
if length > longest_pull_up:
173+
longest_pull_up = length
174+
175+
# use the halfway to determine whether the period it is long or short
176+
halfway = shortest_pull_up + (longest_pull_up - shortest_pull_up) / 2
177+
bits = []
178+
179+
for i in range(0, len(pull_up_lengths)):
180+
bit = False
181+
if pull_up_lengths[i] > halfway:
182+
bit = True
183+
bits.append(bit)
184+
185+
return bits
186+
187+
def __bits_to_bytes(self, bits):
188+
the_bytes = []
189+
byte = 0
190+
191+
for i in range(0, len(bits)):
192+
byte = byte << 1
193+
if (bits[i]):
194+
byte = byte | 1
195+
else:
196+
byte = byte | 0
197+
if ((i + 1) % 8 == 0):
198+
the_bytes.append(byte)
199+
byte = 0
200+
201+
return the_bytes
202+
203+
def __calculate_checksum(self, the_bytes):
204+
return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# DTH11温湿度传感器
2+
# 实验平台:01Studio pyAI-K210开发板
3+
4+
import time
5+
from fpioa_manager import fm
6+
from Maix import GPIO
7+
from dht11 import DHT11
8+
9+
while True:
10+
11+
# initialize GPIOHS3 using io 11
12+
fm.register(11, fm.fpioa.GPIOHS3, force=True)
13+
gpio = GPIO(GPIO.GPIOHS3, GPIO.OUT)
14+
15+
# read data using gpio
16+
instance = DHT11(gpio)
17+
18+
try:
19+
while True:
20+
result = instance.read()
21+
if result.is_valid():
22+
23+
print("Temperature: %-3.1f C" % result.temperature)
24+
print("Humidity: %-3.1f %%" % result.humidity)
25+
time.sleep_ms(1000)
26+
27+
except Exception as e:
28+
print(e)

0 commit comments

Comments
 (0)