xivelyclient.get API调用需要1分钟才能返回吗?

时间:2013-05-15 08:04:19

标签: arduino xively

我正在使用Xively Arduino API。到目前为止我使用的所有API调用都按预期工作,除了xivelyclient.get()调用需要1分钟返回数据。

这是预期的行为吗?

以下是我的代码。正如您所看到的,它基本上是Arduino API for Xively附带的一个示例。我所做的就是更新xivelyKey和feedID。

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>

// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Your Xively key to let you upload data
char xivelyKey[] = "abcdefghijklmnopqrstuvwxyz";

// Define the string for our datastream ID
char temperatureId[] = "temperature";

XivelyDatastream datastreams[] = {
XivelyDatastream(temperatureId, strlen(temperatureId), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(123456789, datastreams, 1 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println("Reading from Xively example");
  Serial.println();

   while (Ethernet.begin(mac) != 1)
   {
     Serial.println("Error getting IP address via DHCP, trying again...");
     delay(15000);
   }
 }

 void loop() {
   int ret = xivelyclient.get(feed, xivelyKey);
   Serial.print("xivelyclient.get returned ");
   Serial.println(ret);

   if (ret > 0)
   {
     Serial.println("Datastream is...");
     Serial.println(feed[0]);

     Serial.print("Temperature is: ");
     Serial.println(feed[0].getFloat());
   }

   Serial.println();
   delay(15000UL);
 }

串行监视器上的输出符合预期:

Reading from Xively example

xivelyclient.get returned 200
Datastream is...
{ "id" : "temperature", "current_value" : "23.00" }
Temperature is: 23.00

xivelyclient.get returned 200
Datastream is...
{ "id" : "temperature", "current_value" : "23.00" }
Temperature is: 23.00

响应时间约为1分10秒。

1 个答案:

答案 0 :(得分:1)

我做了一些调试,发现XivelyClient.cpp(API的一部分)中的xivelyclient.get()的实现挂在以下while循环中:

while ((next != '\r') && (next != '\n') && (http.available() || http.connected()))
{
   next = http.read();
}

我想这个循环出来的唯一原因是因为服务器关闭了连接。

为了让函数对我起作用,我在while循环上方的if语句中添加了最后两行并删除了while循环。

if ((idBitfield & 1<<i) && (aFeed[i].idLength() == idIdx))
{
   // We've found a matching datastream
   // FIXME cope with any errors returned
   aFeed[i].updateValue(http);

   // When we get here we'll be at the end of the line, but if aFeed[i]
   // was a string or buffer type, we'll have consumed the '\n'
   next = '\n';
   http.stop();
   return ret;
}

我确信这不是一个优雅的解决方案,但它现在对我有用......