RS485 温湿度模块和 DS18B20 传感器

任务

树莓派使用 RS485 温湿度模块和 DS18B20 传感器读取温湿度

参考资料

温湿度测试软件.zip

温湿度采集模块使用手册.pdf

https://www.jianshu.com/p/66fdac917aab(致敬大佬)

https://blog.csdn.net/qq_46476163/article/details/116534840(致敬大佬)

流程

硬件连接

RS485 温湿度模块

RS485 模块一共四个引脚,分别为 VCC、GND、A、B

使用 RS485 转 USB 模块将温湿度模块和树莓派相连,只需要注意 A、B 引脚对应相连即可

4624d27220ad2a2e52dc4222a15fb96

41fc0d5d794da9c509c4f402ca90b5d

DS18B20 传感器

电源线————3.3V
地线————GND
信号线————GPIO04(BCM)

d79c1881f396757c78589c1607c035d

软件配置

安装依赖库

在终端执行

pip install pyserial

配置单总线(1-wire)

1.进入树莓派软件配置工具

sudo raspi-config

2.选择

Interface Options

image

3.激活单总线设置

1-Wire

image

重启树莓派。重启后,路径 /sys/bus/ 下才会出现 w1 文件夹

连接温度传感器 DS18B20

首先使用单总线的默认接口 GPIO04(BCM),若想更换自己选择的端口,则需要在 /boot/config.txt 文件的最后那行中加上 “,gpiopin=你想要的端口”

测试(在终端):

cd /sys/bus/w1/devices/
ls

此时,一般显示的地址为 28-xxxxxxxxxxxx

image

继续在终端

cd 28-xxxxxxxxxxxx#编号根据自己的情况改
cat w1_slave

image

最后的 “ t=27875 ” 为当前的温度(摄氏度) X 100 后的结果,所以除以 100 才是真正的温度(27.875℃)

代码

#!/usr/bin/python3
import serial
import time
import struct

# DS18B20总线配置,这个路径要自己去确认,编号一般不一样
device_file = '/sys/bus/w1/devices/28-000000623a3e/w1_slave'

# 串口配置
ser = serial.Serial(
    port='/dev/ttyUSB0',      # 根据你的实际情况修改串口号
    baudrate=9600,
    bytesize=8,
    parity='N',       # 'N'表示无奇偶校验
    stopbits=1,
    timeout=1
)

def read_temp_raw():
    # 读取 DS18B20 温度传感器的原始数据
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    # 解析 DS18B20 温度传感器的数据
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
    return temp_c

def parse_temperature_and_humidity(data):
    # 解析 RS485 传感器的温度和湿度值
    # 读取第二个寄存器,得到 16 位无符号整数
    temperature_register_value = struct.unpack('>H', data[3:5])[0]

    if temperature_register_value < 10000:
        temperature = round(temperature_register_value * 0.1, 1)
    else:
        temperature = -1 * round((temperature_register_value - 10000) * 0.1, 1)

    # 读取第三个寄存器,得到 16 位无符号整数
    humidity_register_value = struct.unpack('>H', data[5:7])[0]
    humidity = round(humidity_register_value * 0.1, 1)

    return temperature, humidity

# 发送Modbus RTU指令的函数
def send_modbus_command(command):
    ser.write(command)
    time.sleep(0.1)  # 可能需要适当的延时以确保设备有足够的时间处理指令

# 读取Modbus RTU响应的函数
def read_modbus_response():
    return ser.read(ser.in_waiting)  # 读取所有可用的字节

# 示例Modbus RTU读取温湿度的命令
modbus_read_command = b'\x01\x04\x00\x00\x00\x02\x71\xCB'

while 1:
    # 发送命令并读取响应
    send_modbus_command(modbus_read_command)
    response_data = read_modbus_response()
    # 解析温度和湿度值
    temperature_value, humidity_value = parse_temperature_and_humidity(response_data)
    DS18B20 = read_temp()

    print(f'RS485 Temperature: {temperature_value} °C')
    print(f'RS485 Humidity: {humidity_value} %RH')
    print(f'DS18B20 Temperature: {DS18B20} °C')
    time.sleep(0.5)

# 关闭串口连接
ser.close()

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...