前言:一时兴起,买了个万博网页版登陆页派2b,信心满满的准备好好研究一番,结果光安装系统就折腾了两天,本人虽然懂些PHP,但是都是在windows下操作,Linux接触很少,C更是一塌糊涂,最终一番折腾,也算是圆满实现,写个教程让大家少走弯路,网上基于万博网页版登陆页派2B的教程较少,很多操作按照教程来做根本无法实现,中间走了很多弯路,本教程适合新手,高手请略过。。。
硬件:万博网页版登陆页派2B DHT22温湿度传感器(买的集成好的模块,已有上拉电阻)
系统:Raspbian Debian Wheezy 2014-02-16
读取操作:基于wiringPi或BCM2835 C Library
接线:/DHT22共有三根引脚(有的是四根):
1–>3.3v 接左边第1个GPIO针脚
2–>GPIO 数据接口,可随意连接1个GPIO针脚(第7个针脚对应的是GPIO Pin #4)
3–>NC 不接(
我的没这个引脚)
3–>GND 接地(第6个针脚)
声明:以下操作均在root权限下进行
一、首先说说基于wiringPi读取:
wiringPi操作相对简单,只要系统安装wiringPi:
- git clone git://git.drogon.net/wiringPi
- cd wiringPi
- git pull origin
- cd wiringPi
- ./build
复制代码到这里wiringPi安装完毕,你可以输入gpio readall看下引脚的定义。
获取操作更简单,直接把代码编译执行就行了,我找了个dht11的读取代码,然后修改了下:
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #define MAX_TIME 100
- #define DHT11PIN 7 //读取数据引脚
- #define ATTEMPTS 5 //retry 5 times when no response
- int dht11_val[5]={0,0,0,0,0};
-
- int dht11_read_val(){
- uint8_t lststate=HIGH; //last state
- uint8_t counter=0;
- uint8_t j=0,i;
- for(i=0;i<5;i++)
- dht11_val[i]=0;
- //host send start signal
- pinMode(DHT11PIN,OUTPUT); //set pin to output
- digitalWrite(DHT11PIN,LOW); //set to low at least 18ms
- delay(18);
- digitalWrite(DHT11PIN,HIGH); //set to high 20-40us
- delayMicroseconds(30);
-
- //start recieve dht response
- pinMode(DHT11PIN,INPUT); //set pin to input
- for(i=0;i<MAX_TIME;i++)
- {
- counter=0;
- while(digitalRead(DHT11PIN)==lststate){ //read pin state to see if dht responsed. if dht always high for 255 + 1 times, break this while circle
- counter++;
- delayMicroseconds(1);
- if(counter==255)
- break;
- }
- lststate=digitalRead(DHT11PIN); //read current state and store as last state.
- if(counter==255) //if dht always high for 255 + 1 times, break this for circle
- break;
- // top 3 transistions are ignored, maybe aim to wait for dht finish response signal
- if((i>=4)&&(i%2==0)){
- dht11_val[j/8]<<=1; //write 1 bit to 0 by moving left (auto add 0)
- if(counter>16) //long mean 1
- dht11_val[j/8]|=1; //write 1 bit to 1
- j++;
- }
- }
- // verify checksum and print the verified data
- if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))){
- float f, h;
- h = dht11_val[0] * 256 + dht11_val[1];
- h /= 10;
- f = (dht11_val[2] & 0x7F)* 256 + dht11_val[3];
- f /= 10.0;
- if (dht11_val[2] & 0x80) f *= -1;
- printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h);
- return 1;
- }
- else
- return 0;
- }
-
- int main(void){
- int attempts=ATTEMPTS;
- if(wiringPiSetup()==-1)
- exit(1);
- while(attempts){ //you have 5 times to retry
- int success = dht11_read_val(); //get result including printing out
- if (success) { //if get result, quit program; if not, retry 5 times then quit
- break;
- }
- attempts--;
- delay(2500);
- }
- return 0;
- }
复制代码保存为test.c编译执行即可看到温度和湿度,很多教程也是到此结束,给个结果的截图,但对于新手来说,怎么编译是个问题,当然包括我在内,百度后知道是gcc -o 可怎么也编译不成功,后来细致的看了下gcc的命令,发现需要加-l参数来指定库名,这个对于老手来说肯定要笑了。
执行编译运行:
- gcc -Wall -o test test.c -lwiringPi
- ./test
复制代码如果不出意外,你就看到了执行结果:
二、再说说基于BCM2835 C Library读取:
这部分是折腾我的地方,本来先是尝试的这种方法,按照网上教程,一路下来,发现只要读取就没反应,什么结果都不出卡在那里,后来觉得是版本问题,改了下代码只执行gpio端口设置为OUT,执行后发现,根本就没变,因为2B是bcm2836的芯片,然后就是各种搜,QQ群求助也是让找2836的,可怎么也找不到,无奈去
http://www.airspayce.com/mikem/bcm2835一看,最新版本为1.42,我下载的是1.36,会不会最新版本支持呢,重新下载编译,尝试读取,成功,下面是操作过程:
首先安装BCM2835 C Library:
- wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.42.tar.gz
- tar xvzf bcm2835-1.42.tar.gz
- cd bcm2835-1.42
- ./configure
- make
- make check
- make install
复制代码 下载dht22读取程序编译读取:
- cd /home/pi
- git clone https://github.com/dohkoos/Raspberry-Pi-Code.git
- cd Raspberry-Pi-Code/DHT22
- make
- ./dht 22 4 #数据接口接在第7针脚,所以这里是4
复制代码来个图: