2015-4-20 20:33:29 [显示全部楼层]
12345浏览
查看: 12345|回复: 9

[常见问题] wido例程连接yeelink的问题

[复制链接]
本帖最后由 dapanqi 于 2015-4-20 20:34 编辑

在使用wido的例程向yeelink发送传感器数据时,发送第一个数据,收到回复后,再发生地二个数据就死在哪里了。
如果把程序中接收服务器回复的程序去除,就可以不断上传数据。请求帮助!!
附上例程
/***************************************************
* This is an example for the DFRobot Wido - Wifi Integrated IoT lite sensor and control node
* Product Page & More info: https://www.dfrobot.com.cn/goods-997.html
* Designed specifically to work with the DFRobot Wido products:
*
* The library is forked from Adafruit
*
* Written by Lauren
* BSD license, all text above must be included in any redistribution
*
****************************************************/

/*
This example code is used to connect the Yeelink cloud service (Official homepage: www.yeelink.net).

The device required is just:

1. LM35 low cost temperature sensor or any device you used to upload data
2. And Wido

Note: Please don't forget to change the setting below before using!
1. WLAN_SSID & WlAN_PASS
2. API_key
3. device ID & sensor ID

*/


#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#define Wido_IRQ   7
#define Wido_VBAT  5
#define Wido_CS    10

Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2


#define WLAN_SSID       "xxx"           // cannot be longer than 32 characters!
#define WLAN_PASS       "xxxxxxxx"          // For connecting router or AP, don't forget to set the SSID and password here!!


#define TCP_TIMEOUT      3000
//#define CC3000_TINY_DRIVER

#define WEBSITE  "api.yeelink.net"
#define API_key  "xxxxxxx47dfb3c572bb770axxxxxxxx"  // Update Your API Key. To get your API Key, please check the link below
                                                     // http://www.yeelink.net/user/user_profile

void setup(){

  Serial.begin(115200);
  Serial.println(F("Hello, CC3000!\n"));

  /* Initialise the module */
  Serial.println(F("\nInitialising the CC3000 ..."));
  if (!Wido.begin())
  {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while(1);
  }

  /* Attempt to connect to an access point */
  char *ssid = WLAN_SSID;             /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to "));
  Serial.println(ssid);

  /* NOTE: Secure connections are not available in 'Tiny' mode!
   By default connectToAP will retry indefinitely, however you can pass an
   optional maximum number of retries (greater than zero) as the fourth parameter.
   */
  if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.println(F("Connected!"));

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!Wido.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

}

uint32_t ip = 0;
float temp = 123;

void loop(){

  static Adafruit_CC3000_Client WidoClient;
  static unsigned long RetryMillis = 0;
  static unsigned long uploadtStamp = 0;
  static unsigned long sensortStamp = 0;

  if(!WidoClient.connected() && millis() - RetryMillis > TCP_TIMEOUT){
    // Update the time stamp
    RetryMillis = millis();

    Serial.println(F("Try to connect the cloud server"));
    WidoClient.close();

    // Get Yeelink IP address
    Serial.print(F("api.yeelink.net -> "));
    while  (ip  ==  0)  {
      if  (!Wido.getHostByName(WEBSITE, &ip))  {    //  Get the server IP address based on the domain name
        Serial.println(F("Couldn't resolve!"));
      }
      delay(500);
    }  
    Wido.printIPdotsRev(ip);
    Serial.println(F(""));

    // Connect to the Yeelink Server
    WidoClient = Wido.connectTCP(ip, 80);          // Try to connect cloud server
  }

  if(WidoClient.connected() && millis() - uploadtStamp > 2000){
    uploadtStamp = millis();
    // If the device is connected to the cloud server, upload the data every 2000ms.

    // Prepare Http Package for Yeelink & get length
    int length = 0;
    char lengthstr[3];

    // Create Http data package
    char httpPackage[60] = "";

    strcat(httpPackage,"{\"value\":");
    itoa(temp,httpPackage+strlen(httpPackage),10);          // push the data(temp) to the http data package
    strcat(httpPackage,"}");

    length = strlen(httpPackage);                           // get the length of data package
    itoa(length,lengthstr,10);                              // convert int to char array for posting
    Serial.print(F("Length = "));
    Serial.println(length);

    Serial.println(F("Connected to Yeelink server."));

    // Send headers
    Serial.print(F("Sending headers"));

    WidoClient.fastrprint(F("POST /v1.0/device/"));
    WidoClient.fastrprint(F("10000/sensor/10000/datapoints"));  //Please change your device ID and sensor ID here, after creating
                                                           //Please check the link: http://www.yeelink.net/user/devices
                                                           //The example URL: http://api.yeelink.net/v1.0/device/100/sensor/20/datapoints
    WidoClient.fastrprintln(F(" HTTP/1.1"));
    Serial.print(F("."));

    WidoClient.fastrprintln(F("Host: api.yeelink.net"));
    Serial.print(F("."));

    WidoClient.fastrprint(F("U-ApiKey: "));
    WidoClient.fastrprintln(API_key);
    Serial.print(F("."));

    WidoClient.fastrprint("Content-Length: ");
    WidoClient.fastrprintln(lengthstr);
    WidoClient.fastrprintln("");
    Serial.print(F("."));

    Serial.println(F(" done."));

    // Send data
    Serial.print(F("Sending data"));
    WidoClient.fastrprintln(httpPackage);

    Serial.println(F(" done."));

    /********** Get the http page feedback ***********/

    unsigned long rTimer = millis();
    Serial.println(F("Reading Cloud Response!!!\r\n"));
    while (millis() - rTimer < 2000) {
      while (WidoClient.connected() && WidoClient.available()) {
        char c = WidoClient.read();
        Serial.print(c);
      }
    }
    delay(1000);             // Wait for 1s to finish posting the data stream
    WidoClient.close();      // Close the service connection

    RetryMillis = millis();  // Reset the timer stamp for applying the connection with the service
  }

  if(millis() - sensortStamp > 1000){
    sensortStamp = millis();
    // read the LM35 sensor value and convert to the degrees every 100ms.

    int reading = analogRead(0);
    temp = reading *0.0048828125*100;
    Serial.print(F("Real Time Temp: "));
    Serial.println(temp);
  }
}

wido1.png

Cain  初级技匠

发表于 2015-4-21 11:26:57

试着把发送间隔设长一点看看,会不会是因为发送与接受同时进行导致收不到正确数据了
回复

使用道具 举报

dapanqi  学徒
 楼主|

发表于 2015-4-23 09:59:16

好像不是时间问题,
回复

使用道具 举报

Cain  初级技匠

发表于 2015-4-23 10:23:40

yeelink现在好像要求上传间隔要大于等于10秒,试试是不是这问题吧
无标题.png
回复

使用道具 举报

hgmyaoming  见习技师

发表于 2015-4-23 20:52:50

你试过用手机热点连接吗,我用热点连接的话运行很流畅,但是几十分钟后就停止了,昨天有拿出来用连路由器wifi结果能运行(前连天我跟你情况也是一样的),但是上传的频率很慢,而且不稳定,有时几十秒,有事四五分钟。这样测试了一天,结果发现还是会卡死,基本是没两个半小时就卡停。我是彻底无语了,不知带是程序还是硬件问题,相信已经有很多人买这个来玩了(官网现在都缺货),希望大家能一起交流看看是什么问题!
回复

使用道具 举报

dapanqi  学徒
 楼主|

发表于 2015-4-25 14:36:33

我用的是笔记本的热点,有时发几个数据就停了
回复

使用道具 举报

zhengshaoyi  见习技师

发表于 2015-7-23 02:41:06

何解,楼主解决了吗?
回复

使用道具 举报

crceg  见习技师

发表于 2015-9-24 15:58:59

我用的是wido连接machtalk平台的方式,也出现了相似的问题。经常是发送几次数据就停在Sending headers 那里了,过个几分钟或几十分钟,又能发送了。非常不稳地。正在排查,究竟是哪部分的故障。
回复

使用道具 举报

warpgate_hzk  初级技师

发表于 2016-1-28 00:30:13

遇到同样的问题。。。。
回复

使用道具 举报

明天  见习技师

发表于 2017-7-17 10:06:13

楼主这个代码在哪找的:)
回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则

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

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主

楼主的其它帖子

上海智位机器人股份有限公司 沪ICP备09038501号-4

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

mail