查看: 7541|回复: 5
收起左侧

[项目方案] 树莓派_通过dht11得到温度,发微博

2013-6-17 09:52:34 | 显示全部楼层 |阅读模式
一.连接好 DHT11 温湿度传感器...

接线如图:

786555f64dd83b5b39ab4&690.jpg 786555f64dd83b5adf1b8&690.png
3v3 power连接 vcc || GPIO 1(编程用7)连data || GND随便哪个都成.....

二.利用库wiringPi,写c语言读出数据
wiringPi库的安装:
  1. wget http://project-downloads.drogon.net/files/wiringPi.tgz tar xf wiringPi.tgz cd wiringPi/wiringPi/ make make install
复制代码
sensor.c:(源脚本是读湿度和温度的,但由于我们只要温度,所以就把输出改成只需要温度)

  1. #include <wiringPi.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <stdint.h>  
  5. #define MAX_TIME 85  
  6. #define DHT11PIN 7  
  7. int dht11_val[5]={0,0,0,0,0};  
  8. int errors=0;
  9.   
  10. void dht11_read_val()  
  11. {  
  12.   uint8_t lststate=HIGH;  
  13.   uint8_t counter=0;  
  14.   uint8_t j=0,i;  
  15.   float farenheit;  
  16.   for(i=0;i<5;i++)  
  17.      dht11_val[i]=0;  
  18.   pinMode(DHT11PIN,OUTPUT);  
  19.   digitalWrite(DHT11PIN,LOW);  
  20.   delay(18);  
  21.   digitalWrite(DHT11PIN,HIGH);  
  22.   delayMicroseconds(40);  
  23.   pinMode(DHT11PIN,INPUT);  
  24.   for(i=0;i<MAX_TIME;i++)  
  25.   {  
  26.     counter=0;  
  27.     while(digitalRead(DHT11PIN)==lststate){  
  28.       counter++;  
  29.       delayMicroseconds(1);  
  30.       if(counter==255)  
  31.         break;  
  32.     }  
  33.     lststate=digitalRead(DHT11PIN);  
  34.     if(counter==255)  
  35.        break;  
  36.     // top 3 transistions are ignored  
  37.     if((i>=4)&&(i%2==0)){  
  38.       dht11_val[j/8]<<=1;  
  39.       if(counter>16)  
  40.         dht11_val[j/8]|=1;  
  41.       j++;  
  42.     }  
  43.   }  
  44.   // verify cheksum and print the verified data  
  45.   if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))  
  46.   {  
  47.     //farenheit=dht11_val[2]*9./5.+32;  
  48.    // printf("RH:%d.%d\tTMP:%d.%d\n", dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3]);   
  49.     printf("%d.%d\n",dht11_val[2],dht11_val[3]);
  50.         exit(1);
  51.   }  
  52.   else {
  53.     errors = errors + 1;
  54.     if (errors > 5) {
  55.       printf("0.0\t0.0");
  56.       exit(2);
  57.     }
  58.   }
  59. }  
  60.   
  61. int main(void)  
  62. {  
  63.   if(wiringPiSetup()==-1)  
  64.     exit(1);  
  65.   while(1)  
  66.   {  
  67.      dht11_read_val();  
  68.      delay(3000);  
  69.   }  
  70.   return 0;  
  71. }
复制代码
然后编译
  1. gcc -o sensor sensor.c -L/usr/local/lib -lwiringPi
复制代码
运行下看看结果:
786555f64dd83b5b3e814&690.png
三.写python脚本利用微博发消息:
1.
首先需要申请到一个应用 才有 app key 和 app scret...
网址是:open.weibo.com
然后点击 应用开发—》移动应用 然后根据个人信息填写,之后会收到邮件 里边有app key等.
1.jpg
2.填写授权回调页(重要的一步)
进入应用 点击 左侧的应用信息 然后 在右侧找到完善应用信息
2.jpg
然后点击进入 找到“实际应用地址”,然后改写成为自己想要的...作为之后的授权回调页
3.jpg
3.安装 新浪微博sdk
sudo apt-get install python-pip
sudo pip install sinaweibopy

4.写python脚本:
testblog.py:
  1. #!/usr/bin/python
  2. import webbrowser
  3. import os
  4. import string
  5. import time

  6. from weibo import *

  7. def press_sina_weibo():
  8.         APP_KEY = '- - - - - - - - - -' #your app key
  9.         APP_SECRET = '- - - - - - - - ' # your app secret

  10.         CALLBACK_URL = '- - - - - - - -' #your CALLBACK_URL

  11.         client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
  12.         print client.get_authorize_url()
  13.         r = client.request_access_token(raw_input("input code:").strip())
  14.         client.set_access_token(r.access_token, r.expires_in)   
  15.         tboard = raw_input('Enter the tempreture limited>')
  16.         tlimit = string.atof(tboard)
  17.         text2 = 'warning!!the tem is '
  18.         a = 1
  19.         while True:
  20.                 p = os.popen('sudo ./sensor','r')
  21.                 line = p.readline();
  22.                 tnow = string.atof(line)         
  23.                 if  tnow > tlimit:               
  24.                         line = text2+line
  25.                         if a%2==0:
  26.                                 line += '!!'
  27.                         line += '\n'
  28.                         utext = unicode(line,"UTF-8")
  29.                         client.post.statuses__update(status=utext)
  30.                 a = a+1
  31.                 time.sleep(2)   
  32. if __name__ == '__main__':
  33.         press_sina_weibo()
复制代码
运行说明:
将编译好的sensor和py脚本放在一个文件夹下
python testblog.py
然后会出现让你输入code
如下图:
4.png
只需要把上面https......一串复制到浏览器,然后打开就会跳转到另一个网页

然后把code=后面那一串复制进input code然后确定就可运行脚本
之后会输入温度阀值:
然后输入阀值就可以了,只要温度超过这一个阀值,脚本会发送信息到微博上.
5.png
实验结果:
6.png
为了显示效果比较好,我把阀值调低成10
7.jpg
然后微博上:
8.png

转载自:http://blog.sina.com.cn/s/blog_786555f6010180ji.html





回复

使用道具 举报

2013-6-17 10:11:24 | 显示全部楼层
mark一下
回复 支持 反对

使用道具 举报

2013-6-17 13:45:26 | 显示全部楼层
有意思啊。
回复 支持 反对

使用道具 举报

2013-6-17 22:29:22 来自手机 | 显示全部楼层
如果采集点多的话成本稍高,考虑arduino+树莓
回复 支持 反对

使用道具 举报

2013-6-17 22:57:11 | 显示全部楼层
标记备用
回复 支持 反对

使用道具 举报

2013-6-18 21:53:11 | 显示全部楼层
有没有试过数据线能有多长?一般长了的话,信号失真得厉害
回复 支持 反对

使用道具 举报

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

本版积分规则

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部