帮忙看下代码,WIDO启动后只能连接yeelink服务器一次的问题

2015-05-127689
AI 快速预览详细 收起
本文讨论了WIDO设备连接Yeelink服务器的问题。具体表现为WIDO每次重启后只能连接Yeelink服务器一次,无法实时更新LED状态。文章提供了详细的故障排查步骤和解决方案,包括检查WidoClient.connected()状态并调整连接间隔时间。通过这些方法,可以确保WIDO能够持续连接Yeelink服务器,实现LED状态的实时更新。
下面的代码传到我的WIDO上,每次重新启动可以连接yeelink服务器一次,这个时候LED灯的状态是和服务器一样的。但是,这个时候我在服务器上修改了灯的状态,WIDO的灯还是不会变的,必须要重新启动一次才能获取服务器灯的状态。
下面是我的代码和COM口信息:com口信息:
Connected
Request DHCP
Yeelink:Try to connect the cloud server
api.yeelink.net -> 42.96.164.52Connecting...
Print get done.
{"timestamp":"2015-05-08T15:09:17","value":1
Turn on the LED

Disconnecting.
Yeelink:Try to connect the cloud server
api.yeelink.net -> 42.96.164.52Connecting...



代码:

#include <SPI.h>
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <string.h>
#include "utility/debug.h"

#define WEBSITE         "api.yeelink.net"
#define APIKEY          "5b1fb34ee17278a360fc3772888888"
#define DEVICEID        "20888"
#define SENSORID1       "36888"

#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);
Adafruit_CC3000_Client WidoClient;

#define WLAN_SSID       "xx88888"
#define WLAN_PASS       "8888888"
#define WLAN_SECURITY   WLAN_SEC_WPA2

uint32_t ip = 0;
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 3*1000;
String returnValue = "";
boolean ResponseBegin = false;


void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("Initialising the CC3000 ..."));
  if (!Wido.begin())
  {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while(1);
  }

  char *ssid = WLAN_SSID;
  Serial.print(F("Attempting to connect to "));
  Serial.println(ssid);

  if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY))
  {
    Serial.println(F("Failed!"));
    while(1);
  }
  Serial.println(F("Connected"));

  Serial.println(F("Request DHCP"));
  while (!Wido.checkDHCP())
  {
    delay(100);
  }
    while (! displayConnectionDetails()) {
    delay(1000);
  }
}


void loop()
{
  if (WidoClient.available())
  {
    char c = WidoClient.read();

    if (c == '{')
      ResponseBegin = true;
    else if (c == '}')
      ResponseBegin = false;

    if (ResponseBegin)
      returnValue += c;
  }

  if (returnValue.length() != 0 && (ResponseBegin == false))
  {
    Serial.println(returnValue);

    if (returnValue.charAt(returnValue.length() - 1) == '1')
    {
      Serial.println("Turn on the LED");
      digitalWrite(13, HIGH);
    }
    else if(returnValue.charAt(returnValue.length() - 1) == '0')
    {
      Serial.println("Turn off the LED");
      digitalWrite(13, LOW);
    }
    returnValue = "";
  }


  if (!WidoClient.connected() && lastConnected)
  {
    Serial.println();
    Serial.println("Disconnecting.");
    WidoClient.close();
  }

  if(!WidoClient.connected() && (millis() - lastConnectionTime > postingInterval))
  {
    Serial.print("Yeelink:");
    getData();
  }
  lastConnected = WidoClient.connected();
}


void getData(void)
{
  if(!WidoClient.connected())
  {
    Serial.println(F("Try to connect the cloud server"));
    WidoClient.close();

    Serial.print(F("api.yeelink.net -> "));
    while (ip  ==  0)
    {
      if (!Wido.getHostByName(WEBSITE, &ip))
      {
        Serial.println(F("Couldn't resolve!"));
      }
      delay(500);
    }
    Wido.printIPdotsRev(ip);
    WidoClient = Wido.connectTCP(ip, 80);
  }

  if (WidoClient.connected())
  {
    Serial.println("Connecting...");

    WidoClient.fastrprint("GET /v1.0/device/");
    WidoClient.fastrprint(DEVICEID);
    WidoClient.fastrprint("/sensor/");
    WidoClient.fastrprint(SENSORID1);
    WidoClient.fastrprint("/datapoints");
    WidoClient.fastrprintln(" HTTP/1.1");
    WidoClient.fastrprintln("Host: api.yeelink.net");
    WidoClient.fastrprint("Accept: *");
    WidoClient.fastrprint("/");
    WidoClient.fastrprintln("*");
    WidoClient.fastrprint("U-ApiKey: ");
    WidoClient.fastrprintln(APIKEY);
    WidoClient.fastrprintln("Content-Length: 0");
    WidoClient.fastrprintln("Connection: close");
    WidoClient.fastrprintln("");

    Serial.println("Print get done.");
  }
  else
  {
    Serial.println("Connection failed");
    Serial.println();
    Serial.println("Disconnecting.");
    WidoClient.close();
  }
  lastConnectionTime = millis();
}


bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  if(!Wido.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); Wido.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); Wido.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); Wido.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); Wido.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); Wido.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}


创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(2)
Cain
Cain
亲,不知道是不是这原因,用1.0.6版本的arduinoIDE好像就可以了
Cain
Cain
试了一下你的程序,可以一直连接的,会有一定延迟,等一下就行。
- 没有更多了 -