[转载] 树莓派读取温湿度传感器DHT11
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[转载] 树莓派读取温湿度传感器DHT11,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2660字,纯文字阅读大概需要4分钟。
内容图文
原文地址: http://blog.csdn.net/liang890319/article/details/8739683
硬件:
树莓派 2.0
DHT模块 接树莓派5V GND GPIO1
功能:读取传感器数据并打印出来
// // 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 datauint32 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 highfor(i=0;i<32;i++) { while(digitalRead(pinNumber)); //data clock startwhile(!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 startwhile(!digitalRead(pinNumber)); //data start delayMicroseconds(HIGH_TIME); crc*=2; if(digitalRead(pinNumber)==1) //1 { crc++; } } return1; } else { return0; } } int main (void) { printf("Use GPIO1 to read data!\n"); if (-1 == wiringPiSetup()) { printf("Setup wiringPi failed!"); return1; } 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; } } return0; }
wiringpi是通过C语言控制树莓派GPIO口的头文件。在C语言中包含这个头文件之后可以很简单的调用已经封装好的方法来控制树莓派GPIO口。程序需要先安装。
wiringpi官网:http://wiringpi.com/
wiringpi下载和安装:http://wiringpi.com/download-and-install/
wiringpi文档:http://wiringpi.com/reference/
编译和运行:
写好C文件后,通过如下命令进行编译:
$ gcc -Wall -o dt11_test dt11-.c -lwiringPi #连接动态库
gcc是编译器,-Wall是在编译时显示警告信息,-o executefilename cfilename.c是将cfilename.c文件编译成文件名为executefilename的可执行文件,-lwiringPi是将wiringPi头文件包含在可执行文件中。
编译完之后会生成文件名为executefilename的文件,使用root权限执行如下命令即可运行:
运行程序:
$ sudo ./dt_11_test
#wiringPi 源代码,我已经同步到bitbucket.org了。
https://bitbucket.org/sndnvaps/wiringpi/
编译wiringpi 动态库
使用git clone git@bitbucket.org:sndnvaps/wiringpi.git #下载源代码
cd wiringpi
./build
原文:http://www.cnblogs.com/sn-dnv-aps/p/4390899.html
内容总结
以上是互联网集市为您收集整理的[转载] 树莓派读取温湿度传感器DHT11全部内容,希望文章能够帮你解决[转载] 树莓派读取温湿度传感器DHT11所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。