a6641 发表于 2016-2-19 16:25:06

为什么我收不到大气压,温度湿度数据,帮我看看

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
#include <Adafruit_BMP085.h>
#include "DHT.h"

int BH1750address = 0x23;
byte buff;

int BMP085address = 0x25;

#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11

// for yeelink api
#define APIKEY         "67091e8255dc2f2d8e589974bc166531" // replace your yeelink api key here
#define deviceid       164872 // replace your device ID
#define sensorid       183023 // replace your sensor ID
#define deviceid       164872 // replace your device ID
#define sensorid       183049 // replace your sensor ID
#define deviceid       164872 // replace your device ID
#define sensorid       183050 // replace your sensor ID
#define deviceid       164872 // replace your device ID
#define sensorid       381365 // replace your sensor ID

// assign a MAC address for the ethernet controller.
byte mac[] = { 0x5A, 0x94, 0x5D, 0x2a, 0x96, 0x9C};
// initialize the library instance:
EthernetClient client;
char server[] = "api.yeelink.net";   // name address for yeelink API

unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;               // state of the connection last time through the main loop
const unsigned long postingInterval = 5*1000; // delay between 2 datapoints, 30s

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;

void setup() {
Wire.begin();
// start serial port:
Serial.begin(57600);
dht.begin();
bmp.begin();
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for(;;)
      ;
}
else {
    Serial.println("Ethernet configuration OK");
}
}

void loop() {
   float h = dht.readHumidity();
Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature = ");
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");
   
    Serial.print("Pressure = ");
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");
// if there's incoming data from the net connection.
// send it out the serial port.This is for debugging
// purposes only:
if (client.available()) {
    char c = client.read();
    Serial.print(c);
}

// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
}

// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    // read sensor data, replace with your code
    int sensorReading = readLightSensor();
    //send data to server
    sendData(sensorReading);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData) {
// if there's a successful connection:
if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("POST /v1.0/device/");
    client.print(deviceid);
    client.print("/sensor/");
    client.print(sensorid);
    client.print("/datapoints");
    client.println(" HTTP/1.1");
    client.println("Host: api.yeelink.net");
    client.print("Accept: *");
    client.print("/");
    client.println("*");
    client.print("U-ApiKey: ");
    client.println(APIKEY);
    client.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for {"value":} + number of digits of the data:
    int thisLength = 10 + getLength(thisData);
    client.println(thisLength);

    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("{\"value\":");
    client.print(thisData);
    client.println("}");
}
else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
}
   // note the time that the connection was made or attempted:
lastConnectionTime = millis();
}

// This method calculates the number of digits in the
// sensor reading.Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
    dividend = dividend /10;
    digits++;
}
// return the number of digits:
return digits;
}

///////////////////////////////////////////////////////////////////////////
// get data from light sensor
// you can replace this code for your sensor
int readLightSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature = ");
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");
   
    Serial.print("Pressure = ");
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");
uint16_t val=0;
BH1750_Init(BH1750address);
delay(200);
if(2==BH1750_Read(BH1750address))
{
    val=((buff<<8)|buff)/1.2;
}

Serial.print("Sensor value is: ");
Serial.println((int)val);

return val;
}

int BH1750_Read(int address) //
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()) //
{
    buff = Wire.read();// receive one byte
    i++;
}
Wire.endTransmission();
return i;
}

void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}

lovezypj 发表于 2016-2-19 22:50:16

代码太长了,建议你先单独调试模块,没问题再合并代码

a6641 发表于 2016-2-20 10:59:31

我模块都没问题

Super169 发表于 2016-2-20 11:37:31

正如楼上 lovezyph 大大所说, 先分开独立测试再合拼, 是找出问题的好方法.

简单来说, 可以分开两部份独立测试:
1) 接收传感器回传的数据
2) 向物联网发送数据

先看看3是那部份出了问题, 再合并来测试吧.
当然, 每部份也可以再细分 (比如 2, 有可能是网络问题, 也有可能是物联网连线问题, 亦有可能是帐户设定问题), 先从大的地方分开, 慢慢细分, 就可以看出问题.

a6641 发表于 2016-2-20 12:16:34

那你们谁有将DHT11数据上传yeelink的代码
页: [1]
查看完整版本: 为什么我收不到大气压,温度湿度数据,帮我看看