一.连接好 DHT11 温湿度传感器...
接线如图:
3v3 power连接 vcc || GPIO 1(编程用7)连data || GND随便哪个都成.....
二.利用库wiringPi,写c语言读出数据
wiringPi库的安装:- wget http://project-downloads.drogon.net/files/wiringPi.tgz tar xf wiringPi.tgz cd wiringPi/wiringPi/ make make install
复制代码 sensor.c:(源脚本是读湿度和温度的,但由于我们只要温度,所以就把输出改成只需要温度)
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #define MAX_TIME 85
- #define DHT11PIN 7
- int dht11_val[5]={0,0,0,0,0};
- int errors=0;
-
- void dht11_read_val()
- {
- uint8_t lststate=HIGH;
- uint8_t counter=0;
- uint8_t j=0,i;
- float farenheit;
- for(i=0;i<5;i++)
- dht11_val[i]=0;
- pinMode(DHT11PIN,OUTPUT);
- digitalWrite(DHT11PIN,LOW);
- delay(18);
- digitalWrite(DHT11PIN,HIGH);
- delayMicroseconds(40);
- pinMode(DHT11PIN,INPUT);
- for(i=0;i<MAX_TIME;i++)
- {
- counter=0;
- while(digitalRead(DHT11PIN)==lststate){
- counter++;
- delayMicroseconds(1);
- if(counter==255)
- break;
- }
- lststate=digitalRead(DHT11PIN);
- if(counter==255)
- break;
- // top 3 transistions are ignored
- if((i>=4)&&(i%2==0)){
- dht11_val[j/8]<<=1;
- if(counter>16)
- dht11_val[j/8]|=1;
- j++;
- }
- }
- // verify cheksum and print the verified data
- if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))
- {
- //farenheit=dht11_val[2]*9./5.+32;
- // printf("RH:%d.%d\tTMP:%d.%d\n", dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3]);
- printf("%d.%d\n",dht11_val[2],dht11_val[3]);
- exit(1);
- }
- else {
- errors = errors + 1;
- if (errors > 5) {
- printf("0.0\t0.0");
- exit(2);
- }
- }
- }
-
- int main(void)
- {
- if(wiringPiSetup()==-1)
- exit(1);
- while(1)
- {
- dht11_read_val();
- delay(3000);
- }
- return 0;
- }
复制代码 然后编译- gcc -o sensor sensor.c -L/usr/local/lib -lwiringPi
复制代码 运行下看看结果:
三.写python脚本利用微博发消息:
1.
首先需要申请到一个应用 才有 app key 和 app scret...
网址是:open.weibo.com
然后点击 应用开发—》移动应用 然后根据个人信息填写,之后会收到邮件 里边有app key等.
2.填写授权回调页(重要的一步)
进入应用 点击 左侧的应用信息 然后 在右侧找到完善应用信息
然后点击进入 找到“实际应用地址”,然后改写成为自己想要的...作为之后的授权回调页
3.安装 新浪微博sdk
sudo apt-get install python-pip
sudo pip install sinaweibopy
4.写python脚本:
testblog.py:- #!/usr/bin/python
- import webbrowser
- import os
- import string
- import time
- from weibo import *
- def press_sina_weibo():
- APP_KEY = '- - - - - - - - - -' #your app key
- APP_SECRET = '- - - - - - - - ' # your app secret
- CALLBACK_URL = '- - - - - - - -' #your CALLBACK_URL
- client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
- print client.get_authorize_url()
- r = client.request_access_token(raw_input("input code:").strip())
- client.set_access_token(r.access_token, r.expires_in)
- tboard = raw_input('Enter the tempreture limited>')
- tlimit = string.atof(tboard)
- text2 = 'warning!!the tem is '
- a = 1
- while True:
- p = os.popen('sudo ./sensor','r')
- line = p.readline();
- tnow = string.atof(line)
- if tnow > tlimit:
- line = text2+line
- if a%2==0:
- line += '!!'
- line += '\n'
- utext = unicode(line,"UTF-8")
- client.post.statuses__update(status=utext)
- a = a+1
- time.sleep(2)
- if __name__ == '__main__':
- press_sina_weibo()
复制代码 运行说明:
将编译好的sensor和py脚本放在一个文件夹下
python testblog.py
然后会出现让你输入code
如下图:
只需要把上面https......一串复制到浏览器,然后打开就会跳转到另一个网页
然后把code=后面那一串复制进input code然后确定就可运行脚本
之后会输入温度阀值:
然后输入阀值就可以了,只要温度超过这一个阀值,脚本会发送信息到微博上.
实验结果:
为了显示效果比较好,我把阀值调低成10
然后微博上:
转载自:http://blog.sina.com.cn/s/blog_786555f6010180ji.html
|