背景: 我试图从一个简单的开关传感器上传数据,以学习Xively方法。我正在使用带有WiFi Shield的Arduino Uno。我对Xively库中的示例草图进行了一些简单的更改,以使其非常简单。我已阅读文档和常见问题解答,并在网上搜索。有一个类似的问题(无法使用Arduino + wifi屏蔽连接到Xively,“ret = -1没有可用的套接字),答案是减少加载的库的数量。我使用的是相同的库列表post.I还更新了我的Arduino IDE并下载了最新的xively和HTTP库。代码编译没有错误。我在xively网站上重新加载了我的设备并获得了一个新的API密钥和编号。另外,我保证了使用正确的传感器名称添加了通道。我还检查了我的路由器并确保WiFi屏蔽连接正确。
问题:我无法在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);
}