当前位置:网站首页>ESP32 485空气温湿度测试
ESP32 485空气温湿度测试
2022-07-20 17:21:00 【咸鱼箘】
气象站基础呀
RS-485仅是一个电气标准,描述了接口的物理层,像协议、时序、串行或并行数据以及链路全部由设计者或更高层协议定义。
RS-485定义的是使用平衡(也称作差分)多点传输线的驱动器(driver)和接收器(receiver)的电气特性。
材料及接线
ESP32
单片机TTL转RS485模块
接线说明
开发板 | TTL转RS485模块 |
---|---|
22 | TXD |
23 | RXD |
5v | VCC |
GND | GND |
485温湿度传感器模块
采用工业级芯片,高精度进口SHT20温湿度传感器。采用RS485硬件接口,协议层兼容标准的工业Modbus-Rtu协议。
接线说明
TTL转RS485模块 | 485温湿度传感器 | 开发板 |
---|---|---|
A+ | TXD_A 黄色线 | |
B- | RXD_B 白色线 | |
5v | 5V 红色线 | |
GND | GND 黑色线 |
PS:基本485的接线都一样了
示例程序
获取温度
main.py
from machine import UART
#ESP32可以映射引脚,初始化的时候设置就行,串口号一般设置成2(0和1尽量别用。)
import time
uart= UART(2, baudrate=9600, bits=8, parity=None, rx=22,tx=23, stop=1, timeout=100)
barray = bytearray([0x01,0x04,0x00,0x01,0x00, 0x01 ,0x60 ,0x0A]) #问询帧
while True:
uart.write(barray)
time.sleep(1)
if(uart.any()):
u2by=uart.read() # 返回的是答应帧的内容
aa=u2by
wendux = int.from_bytes(aa[3:5],'big') # 土壤湿度
wendu=wendux/10
print(wendu,"度")
注意这个485传感器带的外壳,对测的温度有影响,可以减掉2~3度,就是实际的温度。或者把外壳打开。
获取湿度
from machine import UART
#ESP32可以映射引脚,初始化的时候设置就行,串口号一般设置成2(0和1尽量别用。)
import time
uart= UART(2, baudrate=9600, bits=8, parity=None, rx=22,tx=23, stop=1, timeout=100)
barray = bytearray([0x01,0x04,0x00,0x02,0x00, 0x01 ,0x90 ,0x0A]) #问询帧
while True:
uart.write(barray)
time.sleep(1)
if(uart.any()):
u2by=uart.read() # 返回的是答应帧的内容
aa=u2by
shidux = int.from_bytes(aa[3:5],'big') # 土壤湿度
shidu=shidux/10
print(shidu,"%")
修改地址
RS485有地址,我们可以用一个串口去连接,然后按照地址来获取数据,省地方。所以要学会修改地址哦。
>>> from machine import UART
>>> uart= UART(2, baudrate=9600, bits=8, parity=None, rx=22,tx=23, stop=1, timeout=100)
>>> barray = bytearray([0x01,0x06,0x01,0x01,0x00, 0x08 ,0xD8 ,0x30])
>>> uart.write(barray)
8
>>> u2by=uart.read()
>>> print(u2by)
b'\x01\x06\x01\x01\x00\x08\xd80'
读取地址
(在已知的情况下去验证)
from machine import UART
#ESP32可以映射引脚,初始化的时候设置就行,串口号一般设置成2(0和1尽量别用。)
import time
uart= UART(2, baudrate=9600, bits=8, parity=None, rx=22,tx=23, stop=1, timeout=100)
barray = bytearray([0x08,0x03,0x01,0x01,0x00, 0x01 ,0xd4 ,0xaf]) #问询帧
while True:
uart.write(barray)
time.sleep(1)
if(uart.any()):
u2by=uart.read() # 返回的是答应帧的内容
print(u2by)
PS:把改地址的485传感器做好标记!!!!!!
使用新地址去获取温度
气象站有很多485传感器,传感器出厂设置地址可能会有重复,导致多个485传感器输出传不回来,所以要学会修改地址。
修改地址后的485传感器在访问的时候别忘了修改barray的后两位的CRC校验码
from machine import UART
#ESP32可以映射引脚,初始化的时候设置就行,串口号一般设置成2(0和1尽量别用。)
import time
uart= UART(2, baudrate=9600, bits=8, parity=None, rx=22,tx=23, stop=1, timeout=100)
barray = bytearray([0x08,0x04,0x00,0x01,0x00, 0x01 ,0x60 ,0x93]) #问询帧
while True:
uart.write(barray)
time.sleep(1)
if(uart.any()):
u2by=uart.read() # 返回的是答应帧的内容
aa=u2by
wendux = int.from_bytes(aa[3:5],'big') # 土壤湿度
wendu=wendux/10
print(wendu,"度")
边栏推荐
猜你喜欢
HBuilderX 格式化 不换行(import)
VGA设计(原理说明。Verilog代码实现,仿真结果)
龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航
Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems
Winform UI界面设计例程——多线程访问UI控件
Systematic thinking and practice of data management of meituan accommodation business
@Configuration和@Bean
記一次曲折的拿靶標記錄
Simulated exhaustive 731 My schedule II
Formatage hbuilderx sans nouvelle ligne (importation)
随机推荐
MySQL 啥时候用表锁,啥时候用行锁?
线段树-区间修改树
类和对象【上】
Combine pyqt5 to develop one click conversion software for office documents, and there is no need to open members to transfer files in the future
MySQL与ORACLE交、并、差集
Is CSDN like this now? No one reviews it?
Introduction to informatization -- Outline of examination -- knowledge points
Cbcgptreectrlex control used by BCG
What is composition and what is its function
Common interview questions for app UI automated testing may be useful~
信息化概论
数据采集与预处理课设——人在回路的气温数据动态处理与可视化
Leetcode outlook 2021
MySQL查询字符串中有多少个相同字符(查询字符串包含特定个数的相同字符)
[thymeleaf] basic objects: base objects
BCG 使用之CBCGPColorDialog控件
Winform UI界面设计例程——多线程访问UI控件
go学习笔记(一)
When does MySQL use table locks and row locks?
科技云报道:云巨头的中场战事:PaaS、SaaS成为关键破局点?