木夕子 发表于 2015-8-4 11:19:34

ardiuno+ESP8266+DHT11将数据上传yeelink失败问题

各位大神,请教一个问题,我在测试网上提供的一个教程,它是一个用ardiuno+ESP8266+DHT11将数据上传yeelink的项目,链接是http://www.geek-workshop.com/forum.php?mod=viewthread&tid=11266&highlight=esp8266,发现使用教程提供的程序,使用它自己的APIKEY、设备号、传感器号可以将数据成功传到网上,但是将APIKEY、设备号、传感器号换成自己申请的,就无法将数据上传到网上,这是为什么呢?是不是yeelink的账户有权限设置,要升级之后才能将上传数据呢?下面两幅图是我的硬件连线图和ardiuno串口信息。

mc.six 发表于 2015-8-4 11:36:48

把程序贴上来。估计是长度计算错了。

木夕子 发表于 2015-8-4 11:47:23

本帖最后由 木夕子 于 2015-8-4 11:49 编辑

mc.six 发表于 2015-8-4 11:36 static/image/common/back.gif
把程序贴上来。估计是长度计算错了。

//这是我的程序,还有一个是库文件
#define SSID       "TP-LINK-abs"                //type your own SSID name
#define PASSWORD   "12345678"                              //type your own WIFI password


#include "uartWIFI.h"
#include <SoftwareSerial.h>
WIFI wifi;

extern int chlID;      //client id(0-4)

// for yeelink api
#define APIKEY         "902a3ee0022f77acee0a7cff24f38993" // replace your yeelink api key here

//replace the device ID and sensor ID for temperature sensor.
#define DEVICEID0       227416 // replace your device ID
#define SENSORID0       245932 // replace your sensor ID

//replace the device ID and sensor ID for humidity sensor.
#define DEVICEID1       227416 // replace your device ID
#define SENSORID1       245933 // replace your sensor ID

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, 5s
String returnValue = "";
boolean ResponseBegin = false;


int DHT11PIN=25;                        //Connect D25 to data pin of DHT11


int humidity;
int temperature;

int post_number;

void setup()
{

wifi.begin();
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if(!b)
{
    DebugSerial.println("Init error");
}
delay(8000);//make sure the module can have enough time to get an IP address
String ipstring= wifi.showIP();
DebugSerial.println(ipstring);                //show the ip address of module




}
void loop()
{
char message;
   // if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if((millis() - lastConnectionTime > postingInterval)) {

//read dht11
int chk = dht11_read(DHT11PIN);
if(chk==0)
{
      if(post_number==0)
      {
                sendData(DEVICEID0,SENSORID0,temperature);
                post_number++;
      }
      else
      {
                post_number = 0;
                sendData(DEVICEID1,SENSORID1,humidity);
      }

}

}

// if there's incoming data from the net connection.
// send it out the serial port.This is for debugging
// purposes only:
if(wifi.ReceiveMessage(message))
{
      DebugSerial.println(message);   
}


delay(10);

}

// this method makes a HTTP connection to the server:
void sendData(int device_id,int sensor_id,int thisData) {
// if there's a successful connection:
if (wifi.ipConfig(TCP,server, 80)) {
    DebugSerial.println("connecting...");
    // send the HTTP PUT request:
    String cmd;
      cmd = "POST /v1.0/device/";
      cmd += String(device_id);
      cmd += "/sensor/";
      cmd += String(sensor_id);
      cmd += "/datapoints";
      cmd += " HTTP/1.1\r\n";
      cmd += "Host: api.yeelink.net\r\n";
      cmd += "Accept: *";
      cmd += "/";
      cmd += "*\r\n";
      cmd += "U-ApiKey: ";
      cmd += APIKEY;
      cmd += "\r\n";
      cmd += "Content-Length: ";
      int thisLength = 10 + getLength(thisData);
    cmd += String(thisLength);
      cmd += "\r\n";
      cmd += "Content-Type: application/x-www-form-urlencoded\r\n";
      cmd += "Connection: close\r\n";
      cmd += "\r\n";
      cmd += "{\"value\":";
      cmd += String(thisData);
      cmd += "}\r\n";


      DebugSerial.println(cmd);

    wifi.Send(cmd);
    // note the time that the connection was made:
    lastConnectionTime = millis();
}
else {
    // if you couldn't make a connection:
    DebugSerial.println("connection failed");
    DebugSerial.println("disconnecting.");
    wifi.closeMux();
}
}

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;
}




int dht11_read(int pin)
{
      // BUFFER TO RECEIVE
      int bits;
      int cnt = 7;
      int idx = 0;

      // EMPTY BUFFER
      for (int i=0; i< 5; i++)
      {bits= 0;}

      // REQUEST SAMPLE
      pinMode(pin, OUTPUT);
      digitalWrite(pin, LOW);
      delay(18);
      digitalWrite(pin, HIGH);
      delayMicroseconds(40);
      pinMode(pin, INPUT);

      // ACKNOWLEDGE or TIMEOUT
      unsigned int loopCnt = 10000;
      while(digitalRead(pin) == LOW)
                if (loopCnt-- == 0) return -2;

      loopCnt = 10000;
      while(digitalRead(pin) == HIGH)
                if (loopCnt-- == 0) return -2;

      // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
      for (int i=0; i<40; i++)
      {
                loopCnt = 10000;
                while(digitalRead(pin) == LOW)
                        if (loopCnt-- == 0) return -2;

                unsigned long t = micros();

                loopCnt = 10000;
                while(digitalRead(pin) == HIGH)
                        if (loopCnt-- == 0) return -2;

                if ((micros() - t) > 40) bits |= (1 << cnt);
                if (cnt == 0)   // next byte?
                {
                        cnt = 7;    // restart at MSB
                        idx++;      // next byte!
                }
                else cnt--;
      }

      // WRITE TO RIGHT VARS
      // as bits and bits are allways zero they are omitted in formulas.
      humidity    = bits;
      temperature = bits;

      int sum = bits + bits;

      if (bits != sum) return -1;
      return 0;
}

mc.six 发表于 2015-8-5 08:19:18

本帖最后由 mc.six 于 2015-8-5 08:36 编辑

估计找到问题所在了,int类型为16位二进制数,换成十进制就是-32768到32767,你看看你的DEVICEID和SENSORID都超过这个范围了。还有你看串口返回的信息里面的DEVICEID和SENSORID都成了什么了。
DEVICEID和Apikey对不上当然被拒绝了。
所以应该把void sendData(int device_id,int sensor_id,int thisData) 改成void sendData(long device_id,long sensor_id,int thisData)
long类型是-2,147,483,648到2,147,483,647
你再试试

mc.six 发表于 2015-8-5 08:43:46

APIKEY怎么看着也不对呀?#define APIKEY         "902a3ee0022f77acee0a7cff24f38993" // replace your yeelink api key here和你上面贴图的APIKEY怎么不一样?

mc.six 发表于 2015-8-5 08:44:43

木夕子 发表于 2015-8-4 11:47 static/image/common/back.gif
//这是我的程序,还有一个是库文件
#define SSID       "TP-LINK-abs"                //type your ow ...

个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。

木夕子 发表于 2015-8-5 09:33:45

mc.six 发表于 2015-8-5 08:44 static/image/common/back.gif
个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。

感谢大神的指点,将int改成long果然可以了,真是佩服的五体投地,也怪自己的C语言功底太差,真是惭愧啊!

木夕子 发表于 2015-8-5 09:37:17

mc.six 发表于 2015-8-5 08:44 static/image/common/back.gif
个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。

十分感谢大神的指点,将int改成long果然行得通了,真是眼泪都流出来,搞了这么多天,呵呵。万里长征终于走出第一步了,真是高兴。

木夕子 发表于 2015-8-5 09:38:57

mc.six 发表于 2015-8-5 08:44 static/image/common/back.gif
个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。

十分感谢大神的指点,将int改成long果然行得通了,真是眼泪都流出来,搞了这么多天,呵呵。那个APIKEY的话是我后来重新申请了一个,所以不一样,万里长征终于走出第一步了,真是高兴。

zhiqiu1001 发表于 2015-8-5 09:50:40

学习中看不懂!

mc.six 发表于 2015-8-5 10:18:26

木夕子 发表于 2015-8-5 09:38 static/image/common/back.gif
十分感谢大神的指点,将int改成long果然行得通了,真是眼泪都流出来,搞了这么多天,呵呵。那个APIKEY的话 ...

我也是个初学者,慢慢的一点一滴的学起来的,也没有学过C。o(∩_∩)o...哈哈!!!

mc.six 发表于 2015-8-5 10:48:27

木夕子 发表于 2015-8-5 09:38 static/image/common/back.gif
十分感谢大神的指点,将int改成long果然行得通了,真是眼泪都流出来,搞了这么多天,呵呵。那个APIKEY的话 ...

OneNet也不错,可以试试。
页: [1]
查看完整版本: ardiuno+ESP8266+DHT11将数据上传yeelink失败问题