gpt4 book ai didi

c++ - Xively API : can´t upload two variables

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:55 25 4
gpt4 key购买 nike

我在使用 Arduino 的 Xively API 时遇到了一些问题。我的项目包括通过 Ethernet Shield 发送模拟传感器收集的数据,并将其打印在 Xively 网站上(目前在我的帐户中)。问题是,我必须向 xively 发送两个不同的变量:一个用于 LDR 值 (LDREsq),另一个用于使用 DHT_11 温度传感器收集的温度值。但是,我只能发送 LDR 值,不能发送温度值。我为每个变量构建了两个 void 函数,并且都使用不同的 API key 连接到 xively。但我无法上传温度值。

这是我的代码 - 只有两个函数 - 用于 LDREsq 的 sendData 和用于 DHT.temperature 的 sendData2 早先阅读过(如果你不明白一件事,请告诉我,我会解释,因为部分代码可能是葡萄牙语):

 `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);
}

关于c++ - Xively API : can´t upload two variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24812235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com