极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 25672|回复: 13

1602显示ffffffff 求大神解决,附图和代码

[复制链接]
发表于 2014-4-10 16:41:18 | 显示全部楼层 |阅读模式
本帖最后由 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
回复

使用道具 举报

发表于 2014-4-10 19:41:41 | 显示全部楼层
本帖最后由 弘毅 于 2014-4-10 19:45 编辑

LiquidCrystal lcd(12, 11, 5, 4, 9, 2);

1602的液晶引脚定义和网络扩展板的SPI口冲突了,你要避免1602使用13,12,11,10这四个端口。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-11 09:11:27 | 显示全部楼层
弘毅 发表于 2014-4-10 19:41
LiquidCrystal lcd(12, 11, 5, 4, 9, 2);

1602的液晶引脚定义和网络扩展板的SPI口冲突了,你要避免1602使 ...

不胜感激!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-11 10:16:31 | 显示全部楼层
弘毅 发表于 2014-4-10 19:41
LiquidCrystal lcd(12, 11, 5, 4, 9, 2);

1602的液晶引脚定义和网络扩展板的SPI口冲突了,你要避免1602使 ...

引脚改了后,又出来个问题,只显示一行信息了,该怎么解决?先谢谢了
回复 支持 反对

使用道具 举报

发表于 2014-4-11 11:17:17 | 显示全部楼层
wdb_9955 发表于 2014-4-11 10:16
引脚改了后,又出来个问题,只显示一行信息了,该怎么解决?先谢谢了

这种问题应该是有线没有接好,引脚悬空导致。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-11 11:43:55 | 显示全部楼层
弘毅 发表于 2014-4-11 11:17
这种问题应该是有线没有接好,引脚悬空导致。

我很仔细的检查了好几遍,接线不存在问题。可能还有其他问题吗?谢谢
回复 支持 反对

使用道具 举报

发表于 2014-4-11 12:07:41 | 显示全部楼层
wdb_9955 发表于 2014-4-11 11:43
我很仔细的检查了好几遍,接线不存在问题。可能还有其他问题吗?谢谢

这种问题google搜索了下,貌似遇到的人都是gnd什么的接触不良导致。其他的就不知道了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-11 13:18:29 | 显示全部楼层
弘毅 发表于 2014-4-11 12:07
这种问题google搜索了下,貌似遇到的人都是gnd什么的接触不良导致。其他的就不知道了。

原来是自己没有初始化,加条lcd.begin(16, 2)就可以了, 谢谢你。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-11 15:11:24 | 显示全部楼层
弘毅 发表于 2014-4-11 12:07
这种问题google搜索了下,貌似遇到的人都是gnd什么的接触不良导致。其他的就不知道了。

兄弟能在请教个问题吗?上面的程序只能把温度和湿度上传到yeelink, 我想把气压和光照全传上去,但不知道代码怎么写,上面写的代码也是网上抄来的,涉及到上传yeelink的代码看不明白,能帮我把上面的代码中关于着方面的代码后面加个注释吗?帮助我理解下。先谢谢了。
回复 支持 反对

使用道具 举报

发表于 2014-4-11 15:34:20 | 显示全部楼层
wdb_9955 发表于 2014-4-11 15:11
兄弟能在请教个问题吗?上面的程序只能把温度和湿度上传到yeelink, 我想把气压和光照全传上去,但不知道代 ...

这个。。。yeelinlk还没用过ing~~
回复 支持 反对

使用道具 举报

发表于 2014-4-11 16:41:47 | 显示全部楼层
wdb_9955 发表于 2014-4-11 15:11
兄弟能在请教个问题吗?上面的程序只能把温度和湿度上传到yeelink, 我想把气压和光照全传上去,但不知道代 ...

到yeelink  看api的使用
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-14 09:31:10 | 显示全部楼层
弘毅 发表于 2014-4-11 15:34
这个。。。yeelinlk还没用过ing~~

没有呀,我现在是从一个个实例去理解一些东西,好幸苦呀
回复 支持 反对

使用道具 举报

发表于 2014-9-13 11:46:13 | 显示全部楼层
问下math.h在哪里能下载,谢谢!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-14 15:56:32 | 显示全部楼层
zjbhm 发表于 2014-9-13 11:46
问下math.h在哪里能下载,谢谢!

这个不用下载吧,安装程序里都用吧。我也是新手
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-5-16 03:19 , Processed in 0.043875 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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