|
本帖最后由 wdb_9955 于 2014-4-10 16:41 编辑
#include <LiquidCrystal.h>
#include <EtherCard.h>
#define OUT
#define ONE_WIRE_BUS A5
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
double Kelvin(double celsius)
{
return celsius + 273.15;
}
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);
return (241.88 * T) / (17.558-T);
}
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;
}
#include <Wire.h>
#include <math.h>
#include <dht11.h>
#define BMP085_ADDRESS 0x77
LiquidCrystal lcd(12, 11, 5, 4, 9, 2);
const unsigned char OSS = 0;
int BH1750address = 0x23;
byte buff[2];
dht11 DHT11;
#define DHT11PIN 3
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
long b5;
short temperature;
long pressure;
#define REQUEST_RATE 15000
static byte mymac[] = {
0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = {
192,168,1,178 };
static byte gwip[] = {
192,168,1,1 };
static byte dnsip[] = {
114,114,114,114 };
int mode_id=0;
char website[] PROGMEM = "api.yeelink.net";
char urlBuf[] PROGMEM = "/v1.0/device/wdb_9955/sensor/wdb_9955/datapoints";
char apiKey[] PROGMEM = "U-ApiKey: cf3f5249dc67ec0cf31ced45fa190e0e";
char urlBuf1[] PROGMEM = "/v1.0/device/8388/sensor/13519/datapoints";
char urlBuf2[] PROGMEM = "/v1.0/device/8388/sensor/13715/datapoints";
byte Ethernet::buffer[400];
static long timer;
static void my_result_cb (byte status, word off, word len) {
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
}
void setup()
{ pinMode(6,INPUT);
Serial.begin(9600);
Wire.begin();
bmp085Calibration();
Serial.println("\n[getStaticIP]");
if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0)
Serial.println( "Failed to access Ethernet controller");
ether.staticSetup(myip, gwip, dnsip);
while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());
Serial.println("Gateway found");
timer = - REQUEST_RATE;
}
void loop()
{
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
Serial.print("Temperature: ");
Serial.print(temperature, DEC);
Serial.println(" *0.1 deg C");
Serial.print("Pressure: ");
Serial.print(pressure, DEC);
Serial.println(" Pa");
Serial.print("guangzhao: ");
Serial.print( BH1750() );
Serial.println("[lx]");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
Serial.print("Temperature (oF): ");
Serial.println(Fahrenheit(DHT11.temperature), 2);
Serial.print("Temperature (K): ");
Serial.println(Kelvin(DHT11.temperature), 2);
Serial.print("Dew Point (oC): ");
Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast (oC): ");
Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
Serial.println();
ether.packetLoop(ether.packetReceive());
if (millis() > timer + REQUEST_RATE)
{
timer = millis();
Serial.println("\n>>> REQ");
static char buf[20],buf1[20];
get_send_string(buf,buf1);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
switch(mode_id)
{
case 1:
Serial.println("\n>>> temperature");
ether.httpPost (urlBuf1, website, apiKey, buf, my_result_cb);
mode_id--;
break;
case 0:
Serial.println("\n>>> humidity");
ether.httpPost (urlBuf2, website, apiKey, buf1, my_result_cb);
mode_id++;
break;
default:
mode_id=0;
break;
}
lcdxianshi();
}
delay(3000);
}
void lcdxianshi()
{ int up=digitalRead(6);
if(up==HIGH)
{lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity:");
lcd.print((float)DHT11.humidity, 2);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.print((float)DHT11.temperature, 2);
lcd.print("C");
}
else
{lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pressure:");
lcd.print(pressure, DEC);
lcd.print("pa");
lcd.setCursor(0,1);
lcd.print("guangzhao: ");
lcd.print( BH1750() );
lcd.print("[lx]");
}
delay(100);
}
void bmp085Calibration()
{
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
}
short bmp085GetTemperature(unsigned int ut)
{
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
return ((b5 + 8)>>4);
}
long bmp085GetPressure(unsigned long up)
{
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
return p;
}
char bmp085Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 1);
while(!Wire.available()) ;
return Wire.read();
}
int bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2);
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
unsigned int bmp085ReadUT()
{
unsigned int ut;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
delay(5);
ut = bmp085ReadInt(0xF6);
return ut;
}
unsigned long bmp085ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (OSS<<6));
Wire.endTransmission();
delay(2 + (3<<OSS));
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 3);
while(Wire.available() < 3);
msb = Wire.read();
lsb = Wire.read();
xlsb = Wire.read();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}
double BH1750()
{
int i=0;
double val=0;
Wire.beginTransmission(BH1750address);
Wire.write(0x10);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(BH1750address);
Wire.requestFrom(BH1750address, 2);
while(Wire.available())
{
buff = Wire.read();
i++;
}
Wire.endTransmission();
if(2==i)
{
val=((buff[0]<<8)|buff[1])/1.2;
}
return val;
}
void get_send_string(OUT char *p ,OUT char *p1){
int chk = DHT11.read(DHT11PIN);
int Tc_100 =(float) DHT11.temperature*10;
int whole, fract;
whole = Tc_100/10 ;
fract = Tc_100 % 10;
sprintf(p,"{\"value\":%d.%d}",whole,fract);
int Tc_200 =(float) DHT11.humidity*10;
whole = Tc_200/10 ;
fract = Tc_200 % 10;
sprintf(p1,"{\"value\":%d.%d}",whole,fract);
Serial.println(p);
Serial.println(p1);
}
红色标记部分的代码以添加上去就出现图中显示,删除红色部分代码就好了, 求大神教教我怎么处理。我是新手,代码从网上抄来拼凑了,相当乱,大神费神看下吧 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|