import machine
import onewire
import ds18x20
import time
# DS18B20接在GP22
ow = onewire.OneWire(machine.Pin(22))
ds = ds18x20.DS18X20(ow)
# 扫描传感器
roms = ds.scan()
print('找到传感器数量:', len(roms))
while True:
ds.convert_temp()
time.sleep_ms(750) # DS18B20转换需要750ms
for rom in roms:
temp = ds.read_temp(rom)
if temp < 12:
status = '偏低'
elif temp > 31:
status = '偏高'
else:
status = '正常'
print(f'温度: {temp:.1f} °C| 状态: {status}')
print('---')
time.sleep(2)