极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 24615|回复: 11

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

[复制链接]
发表于 2015-8-4 11:19:34 | 显示全部楼层 |阅读模式
各位大神,请教一个问题,我在测试网上提供的一个教程,它是一个用ardiuno+ESP8266+DHT11将数据上传yeelink的项目,链接是http://www.geek-workshop.com/for ... p;highlight=esp8266,发现使用教程提供的程序,使用它自己的APIKEY、设备号、传感器号可以将数据成功传到网上,但是将APIKEY、设备号、传感器号换成自己申请的,就无法将数据上传到网上,这是为什么呢?是不是yeelink的账户有权限设置,要升级之后才能将上传数据呢?下面两幅图是我的硬件连线图和ardiuno串口信息。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-8-4 11:36:48 | 显示全部楼层
把程序贴上来。估计是长度计算错了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-4 11:47:23 | 显示全部楼层
本帖最后由 木夕子 于 2015-8-4 11:49 编辑
mc.six 发表于 2015-8-4 11:36
把程序贴上来。估计是长度计算错了。


//这是我的程序,还有一个是库文件
#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[400];
   // 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[5];
        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[idx] |= (1 << cnt);
                if (cnt == 0)   // next byte?
                {
                        cnt = 7;    // restart at MSB
                        idx++;      // next byte!
                }
                else cnt--;
        }
  
        // WRITE TO RIGHT VARS
        // as bits[1] and bits[3] are allways zero they are omitted in formulas.
        humidity    = bits[0];
        temperature = bits[2];
  
        int sum = bits[0] + bits[2];  
  
        if (bits[4] != sum) return -1;
        return 0;
}
[/code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 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
你再试试

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2015-8-5 08:43:46 | 显示全部楼层
APIKEY怎么看着也不对呀?#define APIKEY         "902a3ee0022f77acee0a7cff24f38993" // replace your yeelink api key here和你上面贴图的APIKEY怎么不一样?
回复 支持 反对

使用道具 举报

发表于 2015-8-5 08:44:43 | 显示全部楼层
木夕子 发表于 2015-8-4 11:47
//这是我的程序,还有一个是库文件
#define SSID       "TP-LINK-abs"                //type your ow ...

个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-5 09:33:45 | 显示全部楼层
mc.six 发表于 2015-8-5 08:44
个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-5 09:37:17 | 显示全部楼层
mc.six 发表于 2015-8-5 08:44
个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。

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

使用道具 举报

 楼主| 发表于 2015-8-5 09:38:57 | 显示全部楼层
mc.six 发表于 2015-8-5 08:44
个人感觉就是APIKEY和DEVICEID以及SENSORID的问题。

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

使用道具 举报

发表于 2015-8-5 09:50:40 | 显示全部楼层
学习中看不懂!
回复 支持 反对

使用道具 举报

发表于 2015-8-5 10:18:26 | 显示全部楼层
木夕子 发表于 2015-8-5 09:38
十分感谢大神的指点,将int改成long果然行得通了,真是眼泪都流出来,搞了这么多天,呵呵。那个APIKEY的话 ...

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

使用道具 举报

发表于 2015-8-5 10:48:27 | 显示全部楼层
木夕子 发表于 2015-8-5 09:38
十分感谢大神的指点,将int改成long果然行得通了,真是眼泪都流出来,搞了这么多天,呵呵。那个APIKEY的话 ...

OneNet也不错,可以试试。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 03:03 , Processed in 0.042410 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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