极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10373|回复: 8

环境数据采集

[复制链接]
发表于 2015-6-22 12:43:47 | 显示全部楼层 |阅读模式
先上图片{:soso_e113:}
后面会在显示屏的空白处加入气压,高度及指南针方位指示,代码如下:



  1. #include <UTFT.h>
  2. #include <dht11.h>
  3. #include "SR04.h"
  4. #include <DS3231.h>
  5. #include <Wire.h>
  6. #include <math.h>
  7. int BH1750address = 0x23;
  8. int DS3231address = 0x68;
  9. byte buff[2];

  10. extern uint8_t SmallFont[];
  11. extern uint8_t BigFont[];
  12. extern uint8_t SevenSegNumFont[];

  13. UTFT myGLCD(QD220A,A2,A1,A5,A4,A3);   // Remember to change the model parameter to suit your display module!



  14. dht11 DHT11;
  15. int ledpin=(5,6);
  16. #define DHT11PIN 2
  17. #define TRIG_PIN 3
  18. #define ECHO_PIN 4

  19. DS3231 Clock;
  20. bool Century=false;
  21. bool h12;
  22. bool PM;
  23. byte ADay, AHour, AMinute, ASecond, ABits;
  24. bool ADy, A12h, Apm;

  25. byte year, month, date, DoW, hour, minute, second;

  26. SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
  27. long a;

  28. double Fahrenheit(double celsius)
  29. {
  30.         return 1.8 * celsius + 32;
  31. }    //摄氏温度度转化为华氏温度

  32. double Kelvin(double celsius)
  33. {
  34.         return celsius + 273.15;
  35. }     //摄氏温度转化为开氏温度

  36. // 露点(点在此温度时,空气饱和并产生露珠)
  37. // 参考: [url]http://wahiduddin.net/calc/density_algorithms.htm[/url]
  38. double dewPoint(double celsius, double humidity)
  39. {
  40.         double A0= 373.15/(273.15 + celsius);
  41.         double SUM = -7.90298 * (A0-1);
  42.         SUM += 5.02808 * log10(A0);
  43.         SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
  44.         SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
  45.         SUM += log10(1013.246);
  46.         double VP = pow(10, SUM-3) * humidity;
  47.         double T = log(VP/0.61078);   // temp var
  48.         return (241.88 * T) / (17.558-T);
  49. }

  50. // 快速计算露点,速度是5倍dewPoint()
  51. // 参考: [url]http://en.wikipedia.org/wiki/Dew_point[/url]
  52. double dewPointFast(double celsius, double humidity)
  53. {
  54.         double a = 17.271;
  55.         double b = 237.7;
  56.         double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  57.         double Td = (b * temp) / (a - temp);
  58.         return Td;
  59. }
  60.   double BH1750()
  61. {
  62.   int i=0;
  63.   double  val=0;
  64.   Wire.beginTransmission(BH1750address);
  65.   Wire.write(0x10);//1lx reolution 120ms
  66.   Wire.endTransmission();  
  67.   delay(200);
  68.   Wire.beginTransmission(BH1750address);
  69.   Wire.requestFrom(BH1750address, 2);
  70.   while(Wire.available()) //
  71.   {
  72.     buff[i] = Wire.read();  // receive one byte
  73.     i++;
  74.   }
  75.   Wire.endTransmission();
  76.   if(2==i)
  77.   {
  78.     val=((buff[0]<<8)|buff[1])/1.2;
  79.   }
  80.   return val;
  81. }
  82. void setup()
  83. {
  84.   randomSeed(analogRead(0));
  85.   myGLCD.InitLCD();
  86.   myGLCD.clrScr();
  87.   pinMode(ledpin,OUTPUT);
  88.   Wire.begin();
  89.         Clock.setSecond(50);//Set the second
  90.         Clock.setMinute(20);//Set the minute
  91.         Clock.setHour(17);  //Set the hour
  92.         Clock.setDoW(5);    //Set the day of the week
  93.         Clock.setDate(19);  //Set the date of the month
  94.         Clock.setMonth(6);  //Set the month of the year
  95.         Clock.setYear(15);  //Set the year (Last two
  96. }
  97. void ReadDS3231()
  98. {
  99.   int second,minute,hour,date,month,year,temperature;
  100.   second=Clock.getSecond();
  101.   minute=Clock.getMinute();
  102.   hour=Clock.getHour(h12, PM);
  103.   date=Clock.getDate();
  104.   month=Clock.getMonth(Century);
  105.   year=Clock.getYear();
  106. }

  107. void loop()
  108. {
  109.   int buf[218];
  110.   int x, x2;
  111.   int y, y2;
  112.   int r;
  113.   int i;
  114.   uint16_t val=0;
  115.   ReadDS3231();
  116.   int chk = DHT11.read(DHT11PIN);
  117.   
  118.    myGLCD.setBackColor(0, 0, 0);
  119.    myGLCD.setFont(BigFont);
  120.    myGLCD.setColor(0, 255, 0);
  121.    myGLCD.print("20", 2, 6);  
  122.    myGLCD.printNumI(Clock.getYear(), 35, 6);
  123.    myGLCD.print("/", 65, 6);
  124.    myGLCD.setColor(150, 150, 0);  
  125.    myGLCD.printNumI(Clock.getMonth(Century), 95, 6);
  126.    myGLCD.print("/", 115, 6);
  127.    myGLCD.setColor(200, 200, 200);
  128.    myGLCD.printNumI(Clock.getDate(), 135, 6);
  129.    
  130.    
  131.   
  132.    myGLCD.setColor(VGA_FUCHSIA);
  133.    myGLCD.printNumI(Clock.getHour(h12, PM), 30, 33);
  134.    myGLCD.print(":", 63, 33);
  135.    myGLCD.setColor(VGA_TEAL);
  136.    myGLCD.printNumI(Clock.getMinute(), 75, 33);
  137.    myGLCD.print(":", 105, 33);
  138.    
  139.    myGLCD.setFont(SevenSegNumFont);
  140.    myGLCD.setColor(VGA_YELLOW);
  141.    myGLCD.printNumI(Clock.getDoW(), 184, 2);

  142.    myGLCD.setFont(SmallFont);
  143.    myGLCD.setColor(150, 10, 0);
  144.    myGLCD.printNumI(Clock.getSecond(), 121, 37);
  145.    
  146.    
  147.      myGLCD.setColor(200, 100, 20);
  148.      myGLCD.print("Illuminance", 2, 102);  
  149.      myGLCD.printNumI(val, 103, 102);
  150.      myGLCD.setColor(30, 10, 102);
  151.      myGLCD.print("Lx", 118, 102);
  152.    
  153.    a=sr04.Distance();
  154.    myGLCD.setColor(255, 255, 0);
  155.    myGLCD.print("Distance", 2, 122);   
  156.    myGLCD.printNumI(a, 103, 122);
  157.    myGLCD.setColor(255, 0, 0);
  158.    myGLCD.print("cm", 118, 122);
  159.    
  160.   myGLCD.setColor(0, 255, 0);
  161.   myGLCD.print("Humidity", 2, 142);
  162.   myGLCD.printNumI(DHT11.humidity, 103, 142);
  163.   myGLCD.setColor(0, 0, 255);
  164.   myGLCD.print("%", 118, 142);
  165.   if ( DHT11.humidity<=40)
  166.   {
  167.     myGLCD.setColor(255, 0, 0);
  168.     myGLCD.print("(Dry)", 160, 142);
  169.   }
  170.   else if( DHT11.humidity<=60)
  171.   {
  172.     myGLCD.setColor(200, 200, 0);
  173.     myGLCD.print("(Comefort)", 137, 142);
  174.   }
  175.   else
  176.   {
  177.     myGLCD.setColor(0, 0, 200);  
  178.     myGLCD.print("(Moist)", 160, 142);
  179.   }
  180.   
  181.   myGLCD.setColor(255, 0, 255);  
  182.   myGLCD.print("Temperature", 2, 162);
  183.   myGLCD.printNumI(DHT11.temperature, 103, 162);
  184.   myGLCD.setColor(200, 255, 50);
  185.   myGLCD.print("oC", 118, 162);
  186.   if ( DHT11.temperature<=25)
  187.   {
  188.     digitalWrite(5,HIGH);
  189.     digitalWrite(6,LOW);
  190.     myGLCD.setColor(0, 0, 255);
  191.     myGLCD.print("(Cold)", 160, 162);
  192.   }
  193.   else if( DHT11.temperature<=30)
  194.   {
  195.     myGLCD.setColor(200, 200, 0);
  196.     myGLCD.print("(Comfort)", 137, 162);
  197.   }
  198.   else
  199.   {
  200.     digitalWrite(5,LOW);
  201.     digitalWrite(6,HIGH);
  202.     myGLCD.setColor(255, 0, 0);  
  203.     myGLCD.print("(Hot)", 160, 162);
  204.   }

  205.   myGLCD.setColor(0, 0, 255);
  206.   myGLCD.drawRect(0, 0, 218, 174); //外框
  207.   myGLCD.drawLine(90, 95, 90, 172); //传感器名称右侧竖线
  208.   myGLCD.drawLine(136, 95, 136, 172); //传感器显示数值右侧竖线
  209.   
  210.   myGLCD.drawLine(180, 2, 180, 52); //星期左侧竖线
  211.   myGLCD.drawLine(180, 52, 219, 52); //星期下面横线
  212.   myGLCD.drawLine(1, 28, 181, 28); //日期下面横线
  213.   myGLCD.drawLine(1, 52, 181, 52); //时间下面横线
  214.   
  215.   myGLCD.drawLine(1, 95, 219, 95); //光照度上面横线
  216.   myGLCD.drawLine(1, 118, 219, 118); //距离上面横线
  217.   myGLCD.drawLine(1, 138, 219, 138); //湿度上面横线
  218.   myGLCD.drawLine(1, 158, 219, 158); //温度上面横线
  219.   
  220.   
  221.   delay (1000);
  222.   
  223. }
复制代码


好吧。。。。我的BH1750不能与DS3231同时运行,同时挂在I2C上面屏幕就白屏,具体原因嘛,我I2C程序不会写。哪位大虾帮我改一下呗{:soso_e149:}

顺便贴几张我做的自平衡车

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2015-6-22 15:19:16 | 显示全部楼层
不错,学习了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-6-22 18:55:02 | 显示全部楼层
mc.six 发表于 2015-6-22 15:19
不错,学习了

能不能帮我改一下那个代码,BH1750和DS3231同时挂一起显示屏就白屏
回复 支持 反对

使用道具 举报

发表于 2015-6-22 19:48:39 | 显示全部楼层
我也没搞明白呢,不好意思
回复 支持 反对

使用道具 举报

发表于 2015-6-22 19:48:58 | 显示全部楼层
我也没搞明白呢,不好意思
回复 支持 反对

使用道具 举报

发表于 2015-6-23 18:34:37 | 显示全部楼层
         可能电源问题
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-6-23 20:47:17 | 显示全部楼层
suoma 发表于 2015-6-23 18:34
可能电源问题

我用USB供电的,DS3231和BH1705均使用3.3供电,应该不会有问题吧?应该还是I2C程序有问题,可能是地址分配或读写有问题
回复 支持 反对

使用道具 举报

发表于 2015-6-24 11:18:40 | 显示全部楼层
雨轩 发表于 2015-6-23 20:47
我用USB供电的,DS3231和BH1705均使用3.3供电,应该不会有问题吧?应该还是I2C程序有问题,可能是地址分配 ...

如果多个传感器,需要独立供电
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-6-26 23:07:53 | 显示全部楼层
suoma 发表于 2015-6-24 11:18
如果多个传感器,需要独立供电

试过了,不是电源问题。还是I2C代码有问题。
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 08:41 , Processed in 0.052069 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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