背景:我正在尝试从一个简单的开关传感器上传数据以学习 Xively 方法。我正在使用带有 WiFi Shield 的 Arduino Uno。我对 Xively 库中的示例草图进行了一些简单的修改,以使其非常简单。我已阅读文档和常见问题解答,并在网上搜索过。有一个类似的问题(Can't connect to Xively using Arduino + wifi shield,“ret = -1 No sockets available),答案是减少加载的库数量。我使用的是推荐的相同库列表post.我还更新了我的 Arduino IDE 并下载了最新的 xively 和 HTTP 库。代码编译没有错误。我在 xively 网站上重新加载了我的设备并获得了新的 API 密钥和编号。另外,我确保通道添加了正确的传感器名称。
问题:我在 Xively 网站上看不到数据,并且在 Arduino 的串行监视器上不断收到以下错误消息:
xivelyclint.put 返回 -1
此外,经过几次尝试,我得到“没有可用的套接字”
问题:代码有问题还是其他问题?
当前的 Arduino 代码(实际 ssid、密码和 API 密钥和号码已删除):
#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// Your Xively key to let you upload data
char xivelyKey[] = "api_key";
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
// Define the strings for our datastream IDs
char sensorId[] = "sensor_id";
XivelyDatastream datastreams[] = {
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_INT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(feed no., datastreams, 1 /* number of datastreams */);
WiFiClient client;
XivelyClient xivelyclient(client);
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Starting single datastream upload to Xively...");
Serial.println();
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
int sensorValue = digitalRead(sensorPin);
datastreams[0].setInt(sensorValue);
Serial.print("Read sensor value ");
Serial.println(datastreams[0].getInt());
Serial.println("Uploading it to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
Serial.println(ret);
Serial.println();
delay(15000);
}