树莓派驱动DHT温湿度传感器的3种方法

树莓派驱动DHT温湿度传感器的3种方法

一、使用Adafruit_Python_DHT

使用 Adafruit 读取 DHT11 温湿度传感器 | 树莓派实验室树莓派(Raspberry Pi)中文资讯站,提供丰富的树莓派教程和DIY资讯。树莓派驱动DHT温湿度传感器的3种方法https://shumeipai.nxez.com/2018/05/16/dht11-temperature-and-humidity-sensor-raspberry-pi.html

二、使用升级版CircuitPython-DHT

Python Setup | DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging | Adafruit Learning System树莓派驱动DHT温湿度传感器的3种方法https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup

三、经过C语言编译

        上面两种方法都有bug,在获取一次温湿度后,GPIO一直处于高电平,导致下一次读取一直读不到。

        因此可使用直接读取信号线的方式。

        这里用C来编译,也可以python中直接写。

//
//mydht11.c
//
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
 
typedef unsigned char uint8;
typedef unsigned int  uint16;
typedef unsigned long uint32;
 
#define HIGH_TIME 32
 
int pinNumber = 7;  //use gpio1 to read data
uint32 databuf;
 
uint8 readSensorData(void)
{
    uint8 crc; 
    uint8 i;
 
    pinMode(pinNumber,OUTPUT); // set mode to output
    digitalWrite(pinNumber, 1); // output a low level
    delayMicroseconds(4);
    digitalWrite(pinNumber, 0); // output a high level 
    delay(25);
    digitalWrite(pinNumber, 1); // output a low level
    delayMicroseconds(60); 
    pinMode(pinNumber, INPUT); // set mode to input
    pullUpDnControl(pinNumber,PUD_UP);
 
    if(digitalRead(pinNumber)==0) //SENSOR ANS
    {
        while(!digitalRead(pinNumber)); //wait to high
        delayMicroseconds(80);
        for(i=0;i<32;i++)
        {
          while(digitalRead(pinNumber)); //data clock start
          while(!digitalRead(pinNumber)); //data start
          delayMicroseconds(HIGH_TIME);
          databuf*=2;
          if(digitalRead(pinNumber)==1) //1
          {
            databuf++;
          }
        }
 
        for(i=0;i<8;i++)
        {
          while(digitalRead(pinNumber)); //data clock start
          while(!digitalRead(pinNumber)); //data start
          delayMicroseconds(HIGH_TIME);
          crc*=2;  
          if(digitalRead(pinNumber)==1) //1
          {
            crc++;
          }
        }
      return 1;
    }
    else
    {
      return 0;
    }
}
 
int main (void)
{
    if (-1 == wiringPiSetup()) {
      //printf("Setup wiringPi failed!");
      return 1;
    }
 
    pinMode(pinNumber, OUTPUT); // set mode to output
    digitalWrite(pinNumber, 1); // output a high level 
 
    //while(1) 
    //{
    pinMode(pinNumber,OUTPUT); // set mode to output
    digitalWrite(pinNumber, 1); // output a high level 
    //delay(3000);
    if(readSensorData())
    {
      //printf("OK!\n");
      //printf("RH:%d.%d\n",(databuf>>24)&0xff,(databuf>>16)&0xff); 
      //printf("TMP:%d.%d\n",(databuf>>8)&0xff,databuf&0xff);
      printf("{\"RH\":\"%d.%d\", \"TMP\":\"%d.%d\"}",(databuf>>24)&0xff,(databuf>>16)&0xff,(databuf>>8)&0xff,databuf&0xff);
      databuf=0;
    }
    else
    {
      printf("");
      databuf=0;
    }
    //}
  return 0;
}

然后编译(注意这里-I使用了wiringPi):

gcc a.c -o dht11 -lwiringPi

然后就可以调用了:

./dht11 

树莓派驱动DHT温湿度传感器的3种方法

之后再Python中调用:

import json
import subprocess

def updateTempHum():
    res = json.loads(subprocess.check_output('./dht11').decode('utf-8'))
    print(res)
    print(res['RH'])
    print(res['TMP'])

树莓派驱动DHT温湿度传感器的3种方法

 

 

上一篇:嵌入式Linux学习笔记——汇编指令LDR、STR


下一篇:CH579 CH573 CH582 GPIO睡眠唤醒操作