2016-10-12 19:43:41 [显示全部楼层]
5420浏览
查看: 5420|回复: 2

[求助] 大神帮忙看下气象站代码串口无数据

[复制链接]
麻烦群里的大神帮忙看下小弟下面的气站站代码,传感器由DHT11,BMP180,PM2.5组成的
问题:
1接好传感器串口输出的数据显示传感器检测的温度 气压 都是零,请问大神代码哪里有问题
2我想上传乐联网,但是怎么也上传不到乐联上。
麻烦群里大神帮忙修正下代码,感谢。



#include <LeweiClient.h>
#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
#include <SFE_BMP180.h>
#include <Wire.h>

//dht11
dht11 DHT11;
#define DHT11PIN 2//DTH11的引脚
//GP2Y1010AU0F
#define dustPin A0//Sharp Pin 5夏普传感器5号脚
#define ledPower 3//Sharp pin 3夏普传感器3号脚
int dustVal=0,lastDustVal=0;
const int Time280=280, Time40=40, offTime=9680;
float voltage=0, dustDensity = 0,Clow,Chigh;
int Ilow,Ihigh,AQI;
//BMP180
//SCL-A5,SDA-A4
SFE_BMP180 pressure;
double baseline;


//put your api key here,find it in lewei50.com->my account->account setting
#define LW_USERKEY "YOUR_API_KEY_HERE"
//put your gateway number here,01 as default
#define LW_GATEWAY "01"

//数据发送间隔
#define POST_INTERVAL (30*1000)

//IPAddress ip(192,168,1, 15);
//IPAddress mydns(8,8,8,8);
//IPAddress gw(192,168,1,1);
//IPAddress subnet(255,255,255,0);

LeWeiClient *lwc;

//Sharp
int getDustVal(void){
  int Val;
  // ledPower is any digital pin on the Arduino connected to Pin 3 on the sensor
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(Time280);
  Val=analogRead(dustPin); // read the dust value via pin 5 on the sensor
  delayMicroseconds(Time40);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(offTime);

  return Val;
}
//BMP180
double getP()
{
  char status;
  double T,P,p0,a;
  // You must first get a temperature measurement to perform a pressure reading.
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.
  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);
    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Use '&T' to provide the address of T to the function.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.
      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);
        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Use '&P' to provide the address of P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.
        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          return P;
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}

void setup() {
   // start serial port:
   Serial.begin(9600);
   //Sharp
   pinMode(ledPower,OUTPUT);
   //BMP180
  /* pressure.begin();
    Serial.println("BMP180 init success");
   //else
   {
     // 糟糕,气压计出问题了,多半是连线有问题
     Serial.println("BMP180 init fail (disconnected?)\n\n");
     while(1); // 暂停
   }
    baseline = getP();

   lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY);*/
}
void loop()
{
  if (lwc)
  {
    Serial.println("read data ");

    float temp_c;
    float humidity;
    double a,p;

    // Read values from the sensor
    temp_c = DHT11.temperature;
    humidity = DHT11.humidity;

    //BMP180
    p = getP();
    a = pressure.altitude(p,baseline);

    //dht11
    dustVal = int(lastDustVal*0.8 + getDustVal()*0.2);
    lastDustVal = dustVal;
    voltage = dustVal*(5.0/1023);
    dustDensity = 0.17*voltage-0.1;   //
    dustDensity = constrain(dustDensity, 0, 0.5004);     
    //Serial.print(dustVal); Serial.print("\t");
    //Serial.print(voltage,4);  Serial.print("\t");//Serial.print( "V");
    //Serial.print(dustDensity,4);Serial.print("\t");//Serial.print("mg/m^3");
    if(dustDensity >=0 && dustDensity <=0.0154)           { Clow=0;     Chigh =0.0154;Ilow=0;  Ihigh=50; }
    else if(dustDensity >=0.0155 && dustDensity <=0.0404) { Clow=0.0155;Chigh =0.0404;Ilow=51; Ihigh=100;}
    else if(dustDensity >=0.0405 && dustDensity <=0.0654) { Clow=0.0405;Chigh =0.0654;Ilow=101;Ihigh=150;}
    else if(dustDensity >=0.0655 && dustDensity <=0.1504) { Clow=0.0655;Chigh =0.1504;Ilow=151;Ihigh=200;}
    else if(dustDensity >=0.1505 && dustDensity <=0.2504) { Clow=0.1505;Chigh =0.2504;Ilow=201;Ihigh=300;}
    else if(dustDensity >=0.2505 && dustDensity <=0.5004) { Clow=0.2505;Chigh =0.5004;Ilow=301;Ihigh=500;}
    AQI = (dustDensity-Clow)*(Ihigh-Ilow)/(Chigh-Clow) + Ilow;

    //t1,t2.. must using the same name setting on web server.
    lwc->append("temp", temp_c);
    lwc->append("hum", humidity);
    lwc->append("aqi", AQI);
    lwc->append("high",a);
    //Serial.print("*** data send ***");
    lwc->send();
    //Grammar changed by Wei&Anonymous ;)
    Serial.println("*** send completed ***");

   delay(POST_INTERVAL);
   }
}

相关帖子

dsweiliang  初级技神

发表于 2016-10-13 14:04:05

帮顶
回复

使用道具 举报

-stark  中级技师

发表于 2016-10-14 11:22:54

能截图看一下串口输出的初始打印部分吗?(SETUP部分)
乐联网的教程可以参考一下这个帖子:https://www.dfrobot.com.cn/commun ... 0%E8%81%94%E7%BD%91

要注意USERKEY'GATEWAY与乐联网一致
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail