查看: 3129|回复: 0

关于python驱动gpioDHT11

[复制链接]

2

主题

1

回帖

22

积分

新手上路

Rank: 1

积分
22
发表于 2022-12-14 09:59:29 | 显示全部楼层 |阅读模式
本帖最后由 风火轮用户Zruq4 于 2022-12-14 10:01 编辑

我写了一份代码接收DHT11的数据,设备是rockpi4,不知道哪里写的有问题。返回的温湿度都是0.附代码


# coding:utf-8
import mraa as GPIO
import time
import math

# 使用物理编码为3的引脚做总线
P = 3

print("开始进行DHT11测量数据获取\n")
# 等待1s后再进行逻辑
time.sleep(1)
gpio_1 = GPIO.Gpio(P)

def readData():
    print("readData")
    # 先将总线引脚设置为输出模式
    gpio_1.dir(GPIO.DIR_OUT)
    # 将总线电平拉低,发出开始信号
    gpio_1.outputMode(GPIO.MODE_IN_ACTIVE_LOW)
    # 手册要求至少保持18ms的低电平,我们这里保持20ms
    time.sleep(0.02)
    # 拉高电平
    gpio_1.outputMode(GPIO.MODE_IN_ACTIVE_HIGH)
    # 之后将引脚设为输入模式,等待传感器响应
    gpio_1.dir(GPIO.DIR_IN)

    # 先等待80us的低电平信号
    while gpio_1.read() == GPIO.MODE_IN_ACTIVE_LOW:
        continue
    # 再等待80us的高电平信号
    while gpio_1.read() == GPIO.MODE_IN_ACTIVE_HIGH:
        continue
    # 开始接收数据
    # 循环计数
    i = 0
    # 存放二进制位数据
    data = []
    # 存放中间数据
    kdata = []
    # 每次读取40位数据
    while i < 40:
        j = 0
        # 50us的低电平表示准备传输一位数据
        while gpio_1.read() == GPIO.MODE_IN_ACTIVE_LOW:
            continue
        # 开始检测高电平的时间
        while gpio_1.read() == GPIO.MODE_IN_ACTIVE_HIGH:
            j += 1
            if j > 100:
                return [False, 0, 0]
        kdata.append(j)
        i += 1



    print("--------临时数据----------\n")
    print(kdata)
    print("--------临时数据----------\n")

    # 开始整理数据
    i = 0
    while i < 40:
        tmp = kdata
        if tmp > 7:
            data.append(1)
        else:
            data.append(0)
        i += 1
    print("--------临时数据2----------\n")
    print(data)
    print("--------临时数据2----------\n")
    # 解析湿度整数部分
    t1 = data[0:8]
    c1 = 0
    i = 7
    for n in t1:
        c1 += n * math.pow(2, i)
        i -= 1

    # 解析湿度整数部分
    t2 = data[8:16]
    c2 = 0
    i = 7
    for n in t2:
        c2 += n * math.pow(2, i)
        i -= 1

    # 解析温度整数部分
    t3 = data[16:24]
    c3 = 0
    i = 7
    for n in t3:
        c3 += n * math.pow(2, i)
        i -= 1

    # 解析温度整数部分
    t4 = data[24:32]
    c4 = 0
    i = 7
    for n in t4:
        c4 += n * math.pow(2, i)
        i -= 1

    # 解析校验
    t5 = data[32:40]
    c5 = 0
    i = 7
    for n in t5:
        c5 += n * math.pow(2, i)
        i -= 1

    # 进行校验
    va = True
    print("c1:%d\nc2:%d\nc3:%d\nc4:%d\nc5:%d\n" % (c1, c2, c3, c4, c5))
    if c1 + c2 + c3 + c4 == c5:
        va = True
    else:
        va = False

    return [va, "%d.%d" % (c1, c2), "%d.%d" % (c3, c4)]


while True:
    time.sleep(1)
    result = readData()
    if result[0]:
        hum = result[1]
        temp = result[2]
        print("当前环境湿度: %s %%, 当前环境温度:%s\n" % (hum, temp))
    else:
        print("此次数据无效,已被丢弃\n")
    time.sleep(1)




这是返回数据
1670983261661.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|风火轮WIKI|手机版|小黑屋|深圳风火轮团队 ( 粤ICP备17095099号 )

GMT+8, 2024-3-29 06:27 , Processed in 0.057508 second(s), 22 queries .

快速回复 返回顶部 返回列表
 
【客服1】 商务合作 15289193
【客服2】 业务洽谈 13257599
【客服3】 售前咨询 510313198
【邮箱】
smartfire@smartfire.cn