2

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

这是预期的行为吗?

下面是我的代码。如您所见,它基本上是 Xively 的 Arduino API 附带的示例之一。我所做的只是更新 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 秒。

4

1 回答 1

1

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

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

我想它退出这个循环的唯一原因是服务器正在关闭连接。

为了使该函数对我有用,我在 if 语句中添加了最后两行,就在 while 循环上方,并删除了 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;
}

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

于 2013-05-16T11:52:26.793 回答