Xively API:无法上传两个变量

Xively API: can´t upload two variables

本文关键字:两个 变量 API Xively      更新时间:2023-10-16

我对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);  
}