raspberry用dht22测试温度与湿度

首先进行软件的安装

由于dht22读取温度时用一个数据线要求速度快

所以用python就显得有些慢了!要用c语言才行的,

要从GitHub下载代码,用的都是现成的代码和程序,非常方便,

就是需要一个能上网的pi然后输入以下的代码进行安装

git clone git://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git

$ cd Adafruit-Raspberry-Pi-Python-Code

$ cd Adafruit_DHT_Driver

现在就安装成功了哦!

然后我们可以测试我的代码哦了

命令是
sudo ./Adafruit_DHT  设备代码    gpio端口

现在我们连接硬件

 raspberry-pi-winingpi-gpio


raspberry-pi-winingpi-gpio

 

我的测试命令是

Adafruit_DHT 2302 3

raspberry-pi-temperature-and-humidity-sensor

raspberry-pi-temperature-and-humidity-sensor

到此结束我的raspberry-pi-temperature-and-humidity-sensor测试成功

如果你对编译好的Adafruit_DHT的输出结果不满意可以自己进行编译,

但是需要由于DHT传感器编译一下代码需要BCM2835 C语言库

这里先安装一下这个BCM2835 的库

$ wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.8.tar.gz

$ tar -zxvf bcm2835-1.8.tar.gz

$ cd bcm2835-1.8

$ ./configure

$ make

$ sudo make install

然后就可以对Adafruit_DHT.c进行编译了!

下面是我用的代码

//  How to access GPIO registers from C-code on the Raspberry-Pi
//  Example program
//  15-January-2012
//  Dom and Gert
//

// Access from ARM Running Linux

#define BCM2708_PERI_BASE        0x20000000
#define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bcm2835.h>
#include <unistd.h>

#define MAXTIMINGS 100

//#define DEBUG

#define DHT11 11
#define DHT22 22
#define AM2302 22

int readDHT(int type, int pin);

int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;

if (argc != 3) {
printf(“usage: %s [11|22|2302] GPIOpin#\n”, argv[0]);
printf(“example: %s 2302 4 – Read from an AM2302 connected to GPIO #4\n”, argv[0]);
return 2;
}
int type = 0;
if (strcmp(argv[1], “11”) == 0) type = DHT11;
if (strcmp(argv[1], “22”) == 0) type = DHT22;
if (strcmp(argv[1], “2302”) == 0) type = AM2302;
if (type == 0) {
printf(“Select 11, 22, 2302 as type!\n”);
return 3;
}

int dhtpin = atoi(argv[2]);

if (dhtpin <= 0) {
printf(“Please select a valid GPIO pin #\n”);
return 3;
}

// printf(“Using pin #%d\n”, dhtpin);
readDHT(type, dhtpin);
return 0;

} // main

int bits[250], data[100];
int bitidx = 0;

int readDHT(int type, int pin) {
int counter = 0;
int laststate = HIGH;
int j=0;

// Set GPIO pin to output
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);

bcm2835_gpio_write(pin, HIGH);
usleep(500000);  // 500 ms
bcm2835_gpio_write(pin, LOW);
usleep(20000);

bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);

data[0] = data[1] = data[2] = data[3] = data[4] = 0;

// wait for pin to drop?
while (bcm2835_gpio_lev(pin) == 1) {
usleep(1);
}

// read data!
for (int i=0; i< MAXTIMINGS; i++) {
counter = 0;
while ( bcm2835_gpio_lev(pin) == laststate) {
counter++;
//nanosleep(1);        // overclocking might change this?
if (counter == 1000)
break;
}
laststate = bcm2835_gpio_lev(pin);
if (counter == 1000) break;
bits[bitidx++] = counter;

if ((i>3) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > 200)
data[j/8] |= 1;
j++;
}
}

#ifdef DEBUG
for (int i=3; i<bitidx; i+=2) {
printf(“bit %d: %d\n”, i-3, bits[i]);
printf(“bit %d: %d (%d)\n”, i-2, bits[i+1], bits[i+1] > 200);
}
#endif

// printf(“Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n”, j, data[0], data[1], data[2], data[3], data[4]);

if ((j >= 39) &&
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
// yay!
if (type == DHT11)
printf(“Temp = %d *C, Hum = %d \%\n”, data[2], data[0]);
if (type == DHT22) {
float f, h;
h = data[0] * 256 + data[1];
h /= 10;

f = (data[2] & 0x7F)* 256 + data[3];
f /= 10.0;
if (data[2] & 0x80)  f *= -1;
printf(”  %.1f   %.1f \n”, f, h);
}
return 1;
}

return 0;
}

用这个命令进行编译:

sudo  gcc Adafruit_DHT.c -l bcm2835 -std=gnu99 -o Adafruit_DHT

编译成功后运行一下看看效果

sudo ./Adafruit_DHT  22 3

raspberry-pi-temperature-and-humidity-sensor

raspberry-pi-temperature-and-humidity-sensor

 

各位需要注意的问题是尽量保持树莓派的供电充足!!否则容易出现读取温度出错!!!

还有一个需要注意的问题是每次读取温度湿度的时间间隔按照dht22的官方说法是连续读取时间间隔是2秒,

我建议大家连续读取温度湿度的时间间隔在5秒以上,最好是10秒以上,否则更容易出错!!!

 

 » 本站地址:http://www.gomoth.com

标签: ,