极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 33242|回复: 17

使用Arduino及乐联网平台搭建家居环境监测调节系统

[复制链接]
发表于 2013-6-3 20:57:18 | 显示全部楼层 |阅读模式
本帖最后由 小滴 于 2013-6-3 21:13 编辑

使用Arduino及乐联网平台搭建家居环境监测调节系统1.       
功能实现:
    实现了空气温湿度、PM2.5质量浓度的实时监测,本地显示、上传至乐联网,并可以直接自动控制空气净化系统工作,空气质量达标以后,又可以自动断电,节约能源;此外,利用乐联网平台实现了自动推送微博、短信预警、远程控制空气净化器的开关。
    在此过程中,感谢张老师和乐联网刘先生的大力支持,没有两位的帮助,我这个非机电、非软件专业的业余人士不能实现这个系统的搭建。
   P.S.乐联网的自动发送微博、短信功能真的非常便利,安卓APP也很棒~远程控制全靠他了。
  网站指南http://www.lewei50.com/dev/doc/95

2.        硬件列表:
(1)        Arduino UNO 板子60左右,不过由于我是纯新手,买的是一个传感器套装,链接如下:
http://item.taobao.com/item.htm?spm=a1z09.5.0.42.eb1p7A&id=20149064414
(2)        夏普GP2Y1010AU0F_粉尘传感器 在新中发地下一层买的,好像45元吧。


(3)        SHT10温湿度传感器 购买地同上 40元。

(4)        LCD12864 5V 蓝屏 带背光 中文字库 ST7920 提供驱动程序
淘宝购入,42元,链接如下:
http://item.taobao.com/item.htm?spm=a1z09.5.0.40.eDhp4p&id=10495880170

(5)        Enc28j60 独立以太网控制器,远程控制的关键部,28元,新中发地下淘得。

(6)        数据系统平台:乐联网http://www.lewei50.com
3.        硬件连接

(1)        夏普GP2Y1010AU0F_粉尘传感器(按图示方式放置,从左到右分别为1~6脚)
Sharp pin 1 (V-LED) => 5V (串联150Ω电阻,pin 1和电阻之间连220μF电容接地);
Sharp pin 2 (LED-GND) => Arduino GND脚;
Sharp pin 3 (LED) => Arduino 数字口pin 2 ;
Sharp pin 4 (S-GND) => Arduino GND 脚;
Sharp pin 5 (Vo) => Arduino 模拟口A0pin;
Sharp pin 6 (Vcc) => 5V;
(2)        SHT10
SHT10 pin s (时钟)=>Arduino 数字口 pin 9;
SHT10 pin d (数据) =>Arduino 数字口 pin 7;
SHT10 pin + => 5V;
SHT10 pin - => Arduino GND脚;
(3)        LCD12864连接,使用了SPI接法,数据传输只需使用pin 3,9,8
pin1 = Arduino GND;
pin2 = 5V;
rs(CS) = Arduino pin 8;
rw(SID)= Arduino pin 9;
en(CLK) =Arduino pin 3;
psb = Arduino GND脚;
(4)        ENC28J60模块的连接。
Vcc => Arduino Vcc;
Reset => Arduino Reset;
Enc-int =>Arduino 数字口pin 2;
GND => Arduino GND;
Sck => Arduino pin 13;
Miso => Arduino pin 12;
Mosi => Arduino pin 11;
Cs => Arduino pin 10.


  1. /* Interface to Sharp GP2Y1010AU0F Particle Sensor Program by Christopher Nafis Written April 2012 http://www.sparkfun.com/datasheets/Sensors/gp2y1010au_e.pdf http://sensorapp.net/?p=479
  2. Sharp pin 1 (V-LED) => 5V (connected to 150ohm resister)
  3. Sharp pin 2 (LED-GND) => Arduino GND pin
  4. Sharp pin 3 (LED) => Arduino pin 2
  5. Sharp pin 4 (S-GND) => Arduino GND pin
  6. Sharp pin 5 (Vo) => Arduino A0pin
  7. Sharp pin 6 (Vcc) => 5V */

  8. /*
  9. LCD  Arduino
  10. PIN1 = GND
  11. PIN2 = 5V
  12. RS(CS) = 8;
  13. RW(SID)= 9;
  14. EN(CLK) = 3;
  15. PIN15 PSB = GND;
  16. */   

  17. #include <SD.h>
  18. #include "LCD12864RSPI.h"
  19. #include <SHT1x.h>
  20. #define dataPin  7   // DATA
  21. #define clockPin 9   // SCK
  22. #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
  23. SHT1x sht1x(dataPin, clockPin);  
  24. float temp_f;  
  25. float temp_c;
  26. float humidity;  // Read values from the sensor  
  27. float dustVal=0;
  28. float voltage=0;
  29. float dustdensity=20;
  30. float tempdustdensity = 0;
  31. float ppmpercf=0;
  32. float offTime=9680;
  33. float sum1=0;
  34. float sum2=0;
  35. float sum3=0;
  36. float aver=0;
  37. int chipSelect = 4;
  38. int dustPin=0;
  39. int i=10;
  40. int ledPower=2;
  41. int delayTime=280;
  42. int delayTime2=40;
  43. int fanPower=6;
  44. char str1[4]; //定义温度值存储数组,4位,其中3位为数字,1位为小数点
  45. char str2[4];//温度
  46. char str3[4];//湿度
  47. char str4[4];//平均灰尘密度
  48. char s[32]; //灰尘粒子
  49. float temp1=0; //定义中间变量,用于将获取的float型温度值转变为unsigned char数组
  50. float temp2=0;
  51. float temp3=0;
  52. double tempa=0;
  53. double tempb=0;
  54. double tempc=0;
  55. unsigned char show1[]={0x70, 0x00,  0x6D, 0x00,  0x32, 0x00,  0x2E, 0x00,  0x35, 0x00,  0xA3, 0xBA };                    //pm2.5:
  56. unsigned char show2[]={0xCE, 0xC2,0xB6, 0xC8,0xA3, 0xBA};    //温度:
  57. unsigned char show3[]={0xCA, 0xAA,0xB6, 0xC8, 0xA3, 0xBA};  //湿度:
  58. unsigned char Str01[]={0xA6, 0xCC,0x67, 0x00,0x2F, 0x00,0x6D, 0x00,0x33, 0x00 };                    //μg/m3
  59. unsigned char Str02[]={0xA1, 0xE6 };                    //℃
  60. unsigned char Str00[]={0x25, 0x00 };                    //%

  61. void setup()
  62. {
  63.   Serial.begin(9600);
  64.   pinMode(ledPower,OUTPUT);//  make the GP2Y work
  65.   pinMode(7, OUTPUT);
  66.   pinMode(10, OUTPUT);
  67.   pinMode(fanPower,OUTPUT);   
  68.   digitalWrite(fanPower,LOW); //继电器控制脚
  69.   LCDA.Initialise(); // 屏幕初始化
  70.   unsigned char opening[]={
  71.   0xCF, 0xB5,
  72.   0xCD, 0xB3,
  73.   0xB3, 0xF5,
  74.   0xCA, 0xBC,
  75.   0xBB, 0xAF,
  76.   0xA3, 0xAE,
  77.      };                    //系统初始化...
  78.   unsigned char opening2[]={0xA3, 0xAE};
  79.   
  80.   LCDA.DisplayString(1,0,opening,AR_SIZE(opening));
  81.   delay(1000);
  82.    LCDA.DisplayString(1,6,opening2,AR_SIZE(opening2));
  83.   delay(1000);
  84.   LCDA.DisplayString(1,7,opening2,AR_SIZE(opening2));
  85.   delay(5000);
  86.   LCDA.CLEAR();
  87. }
  88. void loop()
  89. {
  90.   LCDA.DisplayString(0,0,show1,AR_SIZE(show1));//第一行第1格开始,显示文字
  91.   LCDA.DisplayString(2,0,show2,AR_SIZE(show2));
  92.   LCDA.DisplayString(3,0,show3,AR_SIZE(show3));
  93.   sum1=0;
  94.   sum2=0;
  95.   sum3=0;
  96.   for(i=20;i>=0;i--)
  97.   {

  98.     digitalWrite(ledPower,LOW); // power on the LED
  99.     delayMicroseconds(delayTime);
  100.     dustVal=analogRead(dustPin);
  101.     voltage = dustVal*0.0049;
  102.     tempdustdensity =(0.172*voltage-0.0999)*2000;//临时灰尘质量浓度
  103.     temp_c = sht1x.readTemperatureC();  //读温度值
  104.     temp_f = sht1x.readHumidity();  // 读湿度值
  105.     dustdensity=tempdustdensity;
  106.     delayMicroseconds(delayTime2);
  107.     digitalWrite(ledPower,HIGH); // turn the LED off
  108.     delayMicroseconds(offTime);
  109.     sum1=sum1+dustdensity;
  110.     sum2=sum2+temp_c;
  111.     sum3=sum3+temp_f;
  112.     delay(300);
  113.   }
  114.     temp1=sum1/20;
  115.     tempa=temp1;
  116.     if(temp1<40)
  117.     {
  118.       digitalWrite(fanPower,LOW); // power off the fan
  119.     }
  120.     if(temp1>40)
  121.     {
  122.       digitalWrite(fanPower,HIGH); // power on the fan
  123.     }
  124.     temp2=sum2/20;
  125.     tempb=temp2;
  126.     temp3=sum3/20;
  127.     tempc=temp3;
  128.     static char tempStr1[10];  
  129.     static char tempStr2[10];  
  130.     static char tempStr3[10];  
  131.     String dataString="";
  132.     floatToString(temp1,tempStr1);
  133.     dtostrf(tempa,4,2,str1);
  134.     dataString += String(tempStr1);
  135.     dataString += ",";
  136.     floatToString(temp2,tempStr2);
  137.     dtostrf(tempb,4,2,str2);
  138.     dataString += String(tempStr2);
  139.     dataString += ",";
  140.     floatToString(temp3,tempStr3);
  141.     dtostrf(tempc,4,2,str3);
  142.     dataString +=String(tempStr3);
  143.     Serial.println(dataString);
  144.     LCDA.DisplayString(1,0,(unsigned char *)str1,sizeof(str1)); //pm2.5
  145.     LCDA.DisplayString(1,3,(unsigned char *)Str01,sizeof(Str01));//pm2.5  danwei
  146.     LCDA.DisplayString(2,3,(unsigned char *)str2,sizeof(str2));//wendu   
  147.     LCDA.DisplayString(2,6,(unsigned char *)Str02,sizeof(Str02));//wendu danwei
  148.     LCDA.DisplayString(3,3,(unsigned char *)str3,sizeof(str3));//shidu
  149.     LCDA.DisplayString(3,5,(unsigned char *)Str00,sizeof(Str00));//shidu danwei
  150.     delay(3000);
  151. }
  152. void floatToString(float in,char* out){
  153.   uint16_t Tc_100 = in*10;
  154.   uint8_t whole, fract;
  155.   whole = Tc_100/10 ;  // separate off the whole and fractional portions
  156.   fract = Tc_100 % 10;
  157.   sprintf(out,"%d.%d",whole,fract);
  158. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2013-6-3 21:00:58 | 显示全部楼层
远程控制代码:来自乐联网http://www.geek-workshop.com/thread-3099-1-1.html
  1. // Simple demo for feeding some random data to Pachube.
  2. // 2011-07-08 <[email][email protected][/email]> http://opensource.org/licenses/mit-license.php
  3. #include <EtherCard.h>
  4. byte Ethernet::buffer[700];
  5. // change these settings to match your own setup
  6. #define FEED    "5942"
  7. #define APIKEY  "b3249xxxxxxxxxe1b0c"

  8. // ethernet interface mac address, must be unique on the LAN
  9. byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

  10. char website[] PROGMEM = "open.lewei50.com";
  11. const int ledPin = 6;

  12. char* on = "ON";
  13. char* off = "OFF";
  14. char* statusLabel;
  15. char* buttonLabel;
  16. char*udpReply="{"deviceName":"01","username":"laoliu1982","port":80,"api":"api","requestType":"get"}";  
  17. #define gPB ether.buffer
  18. #define UDP_DP_HIGH 36
  19. #define UDP_DP_LOW 37
  20. #define DSTPORT_HIHG 0x26
  21. #define DSTPORT_LOW 0xe7  

  22. int x=1;
  23. int sensorValue = 0;
  24. int sensorPin = A0;  
  25. uint32_t timer;
  26. Stash stash;
  27. void setup ()
  28. {  
  29. Serial.begin(9600);
  30. Serial.println("\n[webClient]");  
  31. pinMode(ledPin, OUTPUT);  
  32. digitalWrite(ledPin, LOW);
  33. buttonLabel = "0";  
  34. if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))   
  35. Serial.println( "Failed to access Ethernet controller");  
  36. else  
  37. Serial.println("Ethernet controller initialized");  
  38. if (!ether.dhcpSetup())   
  39. Serial.println("DHCP failed");  
  40. ether.printIp("IP:  ", ether.myip);
  41. ether.printIp("GW:  ", ether.gwip);  
  42. ether.printIp("DNS: ", ether.dnsip);  
  43. if (!ether.dnsLookup(website))   
  44. Serial.println("DNS failed");  
  45. ether.printIp("SRV: ", ether.hisip);   
  46. Serial.println("\n webClient setup end");
  47. }
  48. void loop ()
  49. {
  50. word len = ether.packetReceive();
  51. word pos = ether.packetLoop(len);
  52. if(len)
  53. {     
  54.   if(gPB[IP_PROTO_P]==IP_PROTO_UDP_V)   
  55.   {      
  56.     if((gPB[UDP_DP_HIGH]==DSTPORT_HIHG)&&(gPB[UDP_DP_LOW]==DSTPORT_LOW))
  57.     {         
  58.     Serial.println("---Received an udp 9959 packet---");   
  59.     //  ether.sendUdp(udpReply,strlen(udpReply),60000,(gPB + IP_SRC_P),9960);         
  60.   ether.leweiUdpReply(udpReply,strlen(udpReply),9960);   
  61.   }     
  62.   }   
  63.   }
  64.   if(pos)  
  65.   {   
  66.   Serial.print("len:");
  67.   Serial.println(len);   
  68.   if(strstr((char *)Ethernet::buffer + pos, "&f=getAllSensors") != 0)   
  69.   {
  70.     Serial.println("Received GetAllSensor command");   
  71.     BufferFiller bfill = ether.tcpOffset();   
  72.     bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"   
  73.     "Content-Type: text/html\r\n Pragma: no-cache\r\n\r\n"   
  74.     "{"successful":true,"message":null,"data":[{"id":"1","type":"jdq","name":"s1","value":"$$S","status":"ok"}]}"),buttonLabel);   
  75.   ether.httpServerReply(bfill.position());   
  76.   }
  77.   if(strstr((char *)Ethernet::buffer + pos, "&f=updateSensor&p1=1&p2=0") != 0)     
  78.   {   
  79.     Serial.println("Received OFF command");   
  80.     BufferFiller bfill = ether.tcpOffset();   
  81.     bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"     
  82.     "Content-Type: text/html\r\n Pragma: no-cache\r\n\r\n"   
  83.   "{"data":{"id":"1","type":"jdq","name":"s1","value":"0","status":"ok"},"successful":true,"message":null}"));  
  84.   ether.httpServerReply(bfill.position());     
  85.   digitalWrite(ledPin, LOW);      
  86.   buttonLabel="0";   
  87.   }   
  88.   if(strstr((char *)Ethernet::buffer + pos, "&f=updateSensor&p1=1&p2=1") != 0)   
  89.   {     
  90.   Serial.println("Received ON command");  
  91.   BufferFiller bfill = ether.tcpOffset();  
  92.   bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"  
  93.   "Content-Type: text/html\r\n Pragma: no-cache\r\n\r\n"  
  94.   "{"data":{"id":"1","type":"jdq","name":"s1","value":"1","status":"ok"},"successful":true,"message":null}"));   
  95.   ether.httpServerReply(bfill.position());     
  96.   digitalWrite(ledPin, HIGH);      
  97.   buttonLabel="1";
  98.   }  
  99.   }  
  100.   if (millis() > timer)
  101.   {
  102.     //timer = millis() + 10000;
  103.     timer = millis() + 3000;   
  104.   sensorValue = analogRead(sensorPin);   
  105.   //Serial.println(x);  
  106.   Serial.println(sensorValue);   
  107.   // generate two fake values as payload - by using a separate stash,   
  108.   // we can determine the size of the generated message ahead of time  
  109.   x=x+1;  
  110.   byte sd = stash.create();   
  111.   stash.print("[{"Name":"s1","Value":");   
  112.   //stash.print((word)x);   
  113.   stash.print((word)sensorValue);     
  114.   stash.print("},{"Name":"T1","Value":");      
  115.   stash.print((word)x);   
  116.   stash.println("}]");  
  117.   stash.save();  

  118.   // generate the header with payload - note that the stash size is used,  
  119.   // and that a "stash descriptor" is passed in as argument using "$$H"   

  120.   Stash::prepare(PSTR("POST /api/V1/Gateway/UpdateSensors/01 HTTP/1.1\r\n"  
  121.   "Host: open.lewei50.com\r\n"                    
  122.   "Content-Length: $$D\r\n"                  
  123.   "userkey: $$F\r\n"                    
  124. "\r\n"                    
  125. "$$H"),stash.size(), PSTR(APIKEY),sd);   
  126. // send the packet - this also releases all stash buffers once done  
  127. ether.tcpSend();
  128. }
  129. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-3 21:04:53 | 显示全部楼层
文中想插几个传感器的图没插进去啊
回复 支持 反对

使用道具 举报

发表于 2013-6-3 21:31:16 | 显示全部楼层
沙发板凳都木有了,
楼主的好贴,收藏了找时间好好学习
回复 支持 反对

使用道具 举报

发表于 2013-6-3 21:33:21 | 显示全部楼层
楼主,线路图模糊不清,能否把所有资料打包提供下载给予学习的机会哈?谢谢!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-3 21:34:51 | 显示全部楼层
我可以把我参考过的帖子都发出来,其实基本上在极客工坊就可以搜得到
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-3 21:38:36 | 显示全部楼层
http://www.howmuchsnow.com/arduino/airquality/grovedust/
http://www.guokr.com/article/434130/
http://sensorapp.net/?p=479
http://www.geek-workshop.com/thread-3829-1-1.html
http://www.geek-workshop.com/for ... thread&tid=1747
http://www.geek-workshop.com/for ... thread&tid=3121
http://www.geek-workshop.com/thread-3099-1-1.html
http://www.geek-workshop.com/for ... thread&tid=2260
http://www.howmuchsnow.com/arduino/airquality/grovedust/
回复 支持 反对

使用道具 举报

发表于 2013-6-3 22:09:45 | 显示全部楼层
谢谢楼主小滴
回复 支持 反对

使用道具 举报

发表于 2013-6-3 22:43:35 | 显示全部楼层
准备把我的实验放在论坛上呢,你这么快就发表啦!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-4 12:01:09 | 显示全部楼层
chqiyi 发表于 2013-6-3 22:09
谢谢楼主小滴

不用谢啦···我这么水的···
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-4 12:01:29 | 显示全部楼层
philhoo 发表于 2013-6-3 22:43
准备把我的实验放在论坛上呢,你这么快就发表啦!

您是哪位?
回复 支持 反对

使用道具 举报

发表于 2013-6-5 12:38:54 | 显示全部楼层
{:soso_e183:}
回复 支持 反对

使用道具 举报

发表于 2014-1-12 19:34:31 | 显示全部楼层
lz,求指导啊
回复 支持 反对

使用道具 举报

发表于 2014-3-1 11:23:27 | 显示全部楼层
楼主,图中的u5 sd是什么东西,在材料单里没有啊?
回复 支持 反对

使用道具 举报

发表于 2014-3-1 12:44:56 | 显示全部楼层
楼主,那个电路图里的u5 sd是什么东西,为什么材料清单里面没有?
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-5 19:10 , Processed in 0.048006 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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