Xively API:无法上传两个变量

时间:2014-07-17 19:46:09

标签: c++ arduino xively

我在使用Arduino的Xively API时遇到了一些问题。我的项目包括通过以太网盾发送模拟传感器收集的数据,并将它们打印在Xively网站上(现在我的帐户中)。问题是,我要向xively发送两个不同的变量:一个用于LDR值(LDREsq),另一个用于使用DHT_11温度传感器收集的温度值。但是,我只能发送LDR值,而不是temeperature值。我为每个变量构建了两个void函数,并且两个函数都使用不同的API密钥连接到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;
}

1 个答案:

答案 0 :(得分:1)

让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);  
}
相关问题