简 介: 通过测试ESP32 MicroPython中的1-Wire功能,可以看到对于DS18B20可以很好地读取其中的温度,但需要对于DS18B20增加对应的电源。
对于AM2302,直接使用ESP32 1-wire的功能无法直接读取对应的数据。需要重新进行开发。关键词
: 1-wire,ESP32,MicroPython,DS18b20,as2302
One-Wire 是 Maxim 全资子公司 Dallas 的一项专有技术与目前多数标准串行数据通信方式,如 SPI/I2C/MICROWIRE不同,它采用单根信号线,既传输时钟又传输数据,而且数据传输是双向的。它具有节省 I/O口线资源、结构简单、成本低廉、便于总线扩展和维护等诸多优点。为了专注于通信本身,文章只介绍命令序列和信号方式(信号类型和时序)。
1-Wire Devices & Products 中给出了一些基于1-Wire 的器件:
ESP32 的MicroPython中具有 OneWire Driver ,并对于ds18x20系列的传感器有专门的驱动函数。
from machine import Pin
import onewire
ow = onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12
ow.scan() # return a list of devices on the bus
ow.reset() # reset the bus
ow.readbyte() # read a byte
ow.writebyte(0x12) # write a byte on the bus
ow.write('123') # write bytes on the bus
ow.select_rom(b'12345678') # select a specific device by its ROM code
import time, ds18x20
ds = ds18x20.DS18X20(ow)
roms = ds.scan()
ds.convert_temp()
time.sleep_ms(750)
for rom in roms:
print(ds.read_temp(rom))
选择 ESP32-S模块转接板设计与实现 中的ESP32 MicroPython 实验模块测试 1-wire 读写协议。
选择GPIO2作为1-wire 完成读写协议。
手边有 AM2302 , 下面通过ESP32对其进行实验。
from machine import Pin
import onewire
ow = onewire.OneWire(Pin(2))
ret = ow.scan()
print(ret)
输出: 【】,没有扫描到任何期间。
from machine import Pin
import onewire
import time
ow = onewire.OneWire(Pin(2))
ow.reset()
buf = bytearray(5)
for i in range(5):
buf[i] = ow.readbyte()
def buf2am2302(buf):
humidity = buf[0]
humidity = humidity * 256 + buf[1]
temperature = buf[2]
temperature = temperature * 256 + buf[3]
check = buf[0]+buf[1]+buf[2]+buf[3]
check = check&0xff
print(humidity)
print(temperature)
print(check, buf[4])
return humidity, temperature
buf2am2302(buf)
读出的数据出现错误
这说明ESP32的one-wire驱动程序不太适应AM2302的读取协议。
在 利用中断在 ESP32 MicroPython 程序中读取AM2302 测试了利用 ESP32 MicroPython 端口中断完成对于AM2302的数据读取方法。
#!/usr/local/bin/python
# -*- coding: gbk -*-
#******************************
# EXEC2.PY -- by Dr. ZhuoQing 2021-09-21
#
# Note:
#******************************
from machine import Pin
import time
import ds18x20
import onewire
ow = onewire.OneWire(Pin(2))
ds=ds18x20.DS18X20(ow)
roms = ds.scan()
print(roms)
while True:
ds.convert_temp()
time.sleep_ms(750)
print(ds.read_temp(roms[0]))
#------------------------------------------------------------
# END OF FILE : EXEC2.PY
#******************************
读出的结果具有很大的跳跃。
78.375
85.0
78.375
78.375
85.0
78.3125
78.375
78.4375
85.0
78.375
78.3125
78.4375
85.0
将DS18B20 的VCC增加 3.3V之后,读出的数值就非常正常量。
23.5
23.5
23.5
23.5
23.5
23.5625
24.4375
25.75
26.6875
27.3125
27.875
27.9375
27.75
27.5625
27.3125
27.0625
26.875
使用手指安捏DS18B20之后引起的问题上升与下降。
将六个DS18B20串联在一起,进行读取温度数据。
from machine import Pin
import time
import ds18x20
import onewire
ow = onewire.OneWire(Pin(2))
ds=ds18x20.DS18X20(ow)
roms = ds.scan()
print(len(roms))
while True:
ds.convert_temp()
time.sleep_ms(750)
tempdim = []
for r in roms:
tempdim.append(ds.read_temp(r))
print(tempdim)
通过测试ESP32 MicroPython中的1-Wire功能,可以看到对于DS18B20可以很好地读取其中的温度,但需要对于DS18B20增加对应的电源。
对于AM2302,直接使用ESP32 1-wire的功能无法直接读取对应的数据。需要重新进行开发。
■ 相关文献链接:
● 相关图表链接:
★ 本文来自于CSDN文献:ESP32 One-Wire驱动功能 。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。