Xively API:无法上传两个变量
Xively API: can´t upload two variables
我对Arduino的Xively API有一些问题。我的项目包括通过以太网屏蔽发送模拟传感器收集的数据,并将其打印在Xively网站上(目前在我的账户上)。问题是,我必须向xively发送两个不同的变量:一个用于LDR值(LDREsq),另一个用于DHT_11温度传感器收集的温度值。但是,我只能发送LDR值,不能发送温度值。我已经建立了两个空函数,每个变量一个,都连接到使用不同的API密钥。但是我就是不能上传温度值。
以下是我的代码-只有两个函数- sendData用于LDREsq和sendData2用于DHT。前面读取的温度(如果你不明白一件事就告诉我,我会解释的,因为部分代码可能是葡萄牙语): `void sendData(int thisData) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.xively.com");
client.print("X-ApiKey: ");//http://forum.arduino.cc/index.php?PHPSESSID=tork80mn5auvtpqsblge27jvn1&topic=229543.0
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = 8 + getLength(thisData);
client.println(thisLength);
// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
client.print("LDREsq,");// the coma in the end is needed:
client.println(thisData);
Serial.println ("Success!");
}
else {
// if you couldn't make a connection:
Serial.println();
Serial.println("connection failed");
Serial.println("disconnecting.");
Serial.println();
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
void sendData2(int thisData2) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting2...");
// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.xively.com");
client.print("X-ApiKey: ");//http://forum.arduino.cc/index.php?PHPSESSID=tork80mn5auvtpqsblge27jvn1&topic=229543.0
client.println(APIKEY_2);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = 8 + getLength(thisData2);
client.println(thisLength);
// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
client.print("Temperatura,");
client.println(thisData2);
Serial.println ("Success 2!");
}
else {
// if you couldn't make a connection:
Serial.println("connection failed 2");
Serial.println();
Serial.println("disconnecting 2.");
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}`
就是这里的
temp3++;
if(temp3 >= 20)
{
sendData2(DHT.temperature);
delay(100);
temp3 = 0;
}
temp2++;
if (temp2 >= 10)
{
sendData(estadoLDREsq);
temp2 = 0;
}
就让Xivley图书馆为你做这件事吧:
#include <SPI.h>
#include <Ethernet.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[] = "[Put your Key here]";
// Define a datastream textual name
char sensorId[] = "TEMP_001";
// Create as many datastreams you need (one in this case)
XivelyDatastream datastreams[] = {
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed([put your feed number here], datastreams, 1); // Where 1 is the number of datastreams we are wrapping
// Create a Etherent client
EthernetClient client;
// Let Xively know about the Ethernet client
XivelyClient xivelyclient(client);
// Run all the setup you need
void setup(void) {
Serial.begin(9600);
while (Ethernet.begin(mac) != 1){
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
}
// Loop over
void loop(void) {
// Read your sensor
float celsius = [put your sensor reading value here];
// Copy sensor reading to the apropriate datastream
datastreams[0].setFloat(celsius);
// Ask Xively lib to PUT all datastreams values at once
int ret = xivelyclient.put(feed, xivelyKey);
// Printout PUT result
Serial.print("xivelyclient.put returned ");
Serial.println(ret);
// Wait 10 sec.
delay(10000);
}
相关文章:
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 命令行参数,cant 或两个变量
- 在 for 循环中更新两个变量时遇到问题C++
- C++ 编译时在两个变量之间交替
- C++ 如何在包含两个变量的结构中存储与变量不同数量的值?
- 为什么一个接一个声明的两个变量在内存中不相邻?
- 为什么动态分配的两个变量的内存位置不是连续的?
- 是否可以在C++中使用宏交换两个变量的出现?
- 计算机如何分配两个变量,我们如何计算两个变量之间的距离?
- 正确证明C 中的两个变量合理(我想将它们视为一个变量)
- 可视C 编译器将相同的存储位置提供给两个变量
- 尝试使用 {} 和 std::make_pair() 交换两个变量时的行为不同
- 在rootframework中的根文件中编写两个变量
- 最短路径,带有两个变量
- 加入两个变量和LCD打印
- 在循环中声明两个变量以逆转字符串?在C 中
- 具有两个变量的"for"循环,一个变量每次迭代递增,每隔一次迭代递增
- 如何在C++中使用一个类中两个变量的值
- 仅在C 中的一个变量上,两个变量函数的数值集成(使用数值配方库)
- 如何在我存储的两个变量之间生成随机数