雨轩 发表于 2015-6-22 12:43:47

环境数据采集

先上图片{:soso_e113:}
后面会在显示屏的空白处加入气压,高度及指南针方位指示,代码如下:



#include <UTFT.h>
#include <dht11.h>
#include "SR04.h"
#include <DS3231.h>
#include <Wire.h>
#include <math.h>
int BH1750address = 0x23;
int DS3231address = 0x68;
byte buff;

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

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



dht11 DHT11;
int ledpin=(5,6);
#define DHT11PIN 2
#define TRIG_PIN 3
#define ECHO_PIN 4

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

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

SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

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

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

// 露点(点在此温度时,空气饱和并产生露珠)
// 参考: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
      double A0= 373.15/(273.15 + celsius);
      double SUM = -7.90298 * (A0-1);
      SUM += 5.02808 * log10(A0);
      SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
      SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
      SUM += log10(1013.246);
      double VP = pow(10, SUM-3) * humidity;
      double T = log(VP/0.61078);   // temp var
      return (241.88 * T) / (17.558-T);
}

// 快速计算露点,速度是5倍dewPoint()
// 参考: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
      double a = 17.271;
      double b = 237.7;
      double temp = (a * celsius) / (b + celsius) + log(humidity/100);
      double Td = (b * temp) / (a - temp);
      return Td;
}
double BH1750()
{
int i=0;
doubleval=0;
Wire.beginTransmission(BH1750address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
delay(200);
Wire.beginTransmission(BH1750address);
Wire.requestFrom(BH1750address, 2);
while(Wire.available()) //
{
    buff = Wire.read();// receive one byte
    i++;
}
Wire.endTransmission();
if(2==i)
{
    val=((buff<<8)|buff)/1.2;
}
return val;
}
void setup()
{
randomSeed(analogRead(0));
myGLCD.InitLCD();
myGLCD.clrScr();
pinMode(ledpin,OUTPUT);
Wire.begin();
      Clock.setSecond(50);//Set the second
      Clock.setMinute(20);//Set the minute
      Clock.setHour(17);//Set the hour
      Clock.setDoW(5);    //Set the day of the week
      Clock.setDate(19);//Set the date of the month
      Clock.setMonth(6);//Set the month of the year
      Clock.setYear(15);//Set the year (Last two
}
void ReadDS3231()
{
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
}

void loop()
{
int buf;
int x, x2;
int y, y2;
int r;
int i;
uint16_t val=0;
ReadDS3231();
int chk = DHT11.read(DHT11PIN);

   myGLCD.setBackColor(0, 0, 0);
   myGLCD.setFont(BigFont);
   myGLCD.setColor(0, 255, 0);
   myGLCD.print("20", 2, 6);
   myGLCD.printNumI(Clock.getYear(), 35, 6);
   myGLCD.print("/", 65, 6);
   myGLCD.setColor(150, 150, 0);
   myGLCD.printNumI(Clock.getMonth(Century), 95, 6);
   myGLCD.print("/", 115, 6);
   myGLCD.setColor(200, 200, 200);
   myGLCD.printNumI(Clock.getDate(), 135, 6);
   
   

   myGLCD.setColor(VGA_FUCHSIA);
   myGLCD.printNumI(Clock.getHour(h12, PM), 30, 33);
   myGLCD.print(":", 63, 33);
   myGLCD.setColor(VGA_TEAL);
   myGLCD.printNumI(Clock.getMinute(), 75, 33);
   myGLCD.print(":", 105, 33);
   
   myGLCD.setFont(SevenSegNumFont);
   myGLCD.setColor(VGA_YELLOW);
   myGLCD.printNumI(Clock.getDoW(), 184, 2);

   myGLCD.setFont(SmallFont);
   myGLCD.setColor(150, 10, 0);
   myGLCD.printNumI(Clock.getSecond(), 121, 37);
   
   
   myGLCD.setColor(200, 100, 20);
   myGLCD.print("Illuminance", 2, 102);
   myGLCD.printNumI(val, 103, 102);
   myGLCD.setColor(30, 10, 102);
   myGLCD.print("Lx", 118, 102);
   
   a=sr04.Distance();
   myGLCD.setColor(255, 255, 0);
   myGLCD.print("Distance", 2, 122);   
   myGLCD.printNumI(a, 103, 122);
   myGLCD.setColor(255, 0, 0);
   myGLCD.print("cm", 118, 122);
   
myGLCD.setColor(0, 255, 0);
myGLCD.print("Humidity", 2, 142);
myGLCD.printNumI(DHT11.humidity, 103, 142);
myGLCD.setColor(0, 0, 255);
myGLCD.print("%", 118, 142);
if ( DHT11.humidity<=40)
{
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("(Dry)", 160, 142);
}
else if( DHT11.humidity<=60)
{
    myGLCD.setColor(200, 200, 0);
    myGLCD.print("(Comefort)", 137, 142);
}
else
{
    myGLCD.setColor(0, 0, 200);
    myGLCD.print("(Moist)", 160, 142);
}

myGLCD.setColor(255, 0, 255);
myGLCD.print("Temperature", 2, 162);
myGLCD.printNumI(DHT11.temperature, 103, 162);
myGLCD.setColor(200, 255, 50);
myGLCD.print("oC", 118, 162);
if ( DHT11.temperature<=25)
{
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    myGLCD.setColor(0, 0, 255);
    myGLCD.print("(Cold)", 160, 162);
}
else if( DHT11.temperature<=30)
{
    myGLCD.setColor(200, 200, 0);
    myGLCD.print("(Comfort)", 137, 162);
}
else
{
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("(Hot)", 160, 162);
}

myGLCD.setColor(0, 0, 255);
myGLCD.drawRect(0, 0, 218, 174); //外框
myGLCD.drawLine(90, 95, 90, 172); //传感器名称右侧竖线
myGLCD.drawLine(136, 95, 136, 172); //传感器显示数值右侧竖线

myGLCD.drawLine(180, 2, 180, 52); //星期左侧竖线
myGLCD.drawLine(180, 52, 219, 52); //星期下面横线
myGLCD.drawLine(1, 28, 181, 28); //日期下面横线
myGLCD.drawLine(1, 52, 181, 52); //时间下面横线

myGLCD.drawLine(1, 95, 219, 95); //光照度上面横线
myGLCD.drawLine(1, 118, 219, 118); //距离上面横线
myGLCD.drawLine(1, 138, 219, 138); //湿度上面横线
myGLCD.drawLine(1, 158, 219, 158); //温度上面横线


delay (1000);

}


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

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

mc.six 发表于 2015-6-22 15:19:16

不错,学习了

雨轩 发表于 2015-6-22 18:55:02

mc.six 发表于 2015-6-22 15:19 static/image/common/back.gif
不错,学习了

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

mc.six 发表于 2015-6-22 19:48:39

我也没搞明白呢,不好意思

mc.six 发表于 2015-6-22 19:48:58

我也没搞明白呢,不好意思

suoma 发表于 2015-6-23 18:34:37

         可能电源问题

雨轩 发表于 2015-6-23 20:47:17

suoma 发表于 2015-6-23 18:34 static/image/common/back.gif
可能电源问题

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

suoma 发表于 2015-6-24 11:18:40

雨轩 发表于 2015-6-23 20:47 static/image/common/back.gif
我用USB供电的,DS3231和BH1705均使用3.3供电,应该不会有问题吧?应该还是I2C程序有问题,可能是地址分配 ...

如果多个传感器,需要独立供电

雨轩 发表于 2015-6-26 23:07:53

suoma 发表于 2015-6-24 11:18 static/image/common/back.gif
如果多个传感器,需要独立供电

试过了,不是电源问题。还是I2C代码有问题。
页: [1]
查看完整版本: 环境数据采集