万博网页版登陆页派论坛

QQ登录

只需一步,快速开始

查看: 3516|回复: 1
收起左侧

DHT11测温度湿度(附图)

[复制链接]
发表于 2014-8-14 22:48:12 | 显示全部楼层 |阅读模式
原器件DHT11
注意正反面
连接方法vcc接3.3v
Dout接GPIO1
NC不接
GND接地
创建脚本mydht11.c
//
//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 =1; //use gpio1 to read data
uint32 databuf;



uint8 readSensorData(void)
{
uint8 crc;
uint8 i;

pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, 0); // output a high level
delay(25);
digitalWrite(pinNumber, 1); // output a low level
pinMode(pinNumber, INPUT); // set mode to input
pullUpDnControl(pinNumber,PUD_UP);

delayMicroseconds(27);
if(digitalRead(pinNumber)==0) //SENSOR ANS
{
while(!digitalRead(pinNumber)); //wait to high

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)
{

printf("Use GPIO1 to read data!\n");

if (-1 == wiringPiSetup()) {
printf("Setup wiringPi failed!");
return 1;
}

pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
printf("Enter OS-------\n");
while(1) {
pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
delay(3000);
if(readSensorData())
{
printf("Congratulations ! Sensor data read ok!\n");
printf("RH:%d.%d\n",(databuf>>24)&0xff,(databuf>>16)&0xff);
printf("TMP:%d.%d\n",(databuf>>8)&0xff,databuf&0xff);
databuf=0;
}
else
{
printf("Sorry! Sensor dosent ans!\n");
databuf=0;
}
}
return 0;
}

编译
gcc -o mydht11 mydht11.c -L/usr/local/lib-lwiringPi
运行
./mydht11
结果
Use GPIO1 to read data!
Enter OS-------
Congratulations ! Sensor data read ok!
RH:35.0
TMP:25.0
1.png
回复

使用道具 举报

发表于 2018-4-26 17:26:25 | 显示全部楼层
pi@raspberrypi:~/Desktop $ ./mydht11
Use GPIO1 to read data!
Enter OS-------
Sorry! Sensor dosent ans!
Congratulations ! Sensor data read ok!
RH:3.73
TMP:1.10
Congratulations ! Sensor data read ok!
RH:6.146
TMP:2.20
Congratulations ! Sensor data read ok!
RH:3.146
TMP:2.20
我的怎么这般低
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|万博网页版登陆页派论坛 ( 粤ICP备15075382号-1  

GMT+8, 2024-11-1 11:30 , Processed in 1.093750 second(s), 26 queries , Gzip On.

Powered by Shumeipai.net! X3.2

© 2001-2015 万博网页版登陆页派论坛安全联盟

快速回复 返回顶部 返回列表