2013-05-15 26 views
2

我正在使用Xively Arduino API。到目前爲止,我使用的所有API調用都按預期工作,除了xivelyclient.get()調用需要1分鐘才能返回數據。xivelyclient.get API調用需要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秒。

+0

這取決於它有多少數據返回我猜。 – leppie

+0

它最終返回了我所期望的。即少於100字節。 – Nick

+0

這很有可能,可能是某種網絡問題。我會嘗試在不同的網絡上運行。此外,您可能希望擺脫'delay()'並計算一下您可以在幾分鐘內查看Xively設備工作臺一分鐘的請求次數,這通常很明顯。我還會添加一個代碼來實際測量確切的時間。 – errordeveloper

回答

1

我做了一些調試,發現xivelyclient.get()的XivelyClient.cpp(API的一部分)的實施被掛在下面的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; 
} 

我敢肯定這不是一個完美的解決方案,但它爲我的作品現在...

+0

現已在[問題跟蹤器](https://github.com/xively/xively-arduino/issues/1)上報告了此問題, – errordeveloper

相關問題