API Xively: невозможно загрузить две переменные

У меня есть несколько проблем с Xively API для Arduino. Мой проект состоит в отправке данных, собранных аналоговыми датчиками, через Ethernet Shield и распечатывании их на веб-сайте Xively (в моем аккаунте на данный момент). Проблема в том, что я должен отправить две разные переменные в xively: одну для значений LDR (LDREsq), а другую для значений температуры, которые собираются датчиком температуры DHT_11. Однако я могу отправлять только значения LDR, а не temeperature. Я построил две void-функции, по одной для каждой переменной, и обе они подключены друг к другу с использованием разных API-ключей. Но я просто не могу загрузить значения температуры.

Вот мой код - только две функции - sendData для LDREsq и sendData2 для DHT.temperam, который читается ранее (если вы не понимаете одну вещь, просто скажите мне, я объясню, потому что часть кода может быть в португальский):

 `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 ответ

Просто позвольте библиотеке 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);  
}
Другие вопросы по тегам