0

我在 xively 中创建了两个数据流,一个是我想在 xively 上发送数据,另一个是我想获取的数据流,但是在使用 put 方法时,它改变了我的两个值,但我不想更新我正在使用的数据流之一 xively。为此放置方法

#include <SPI.h>  //spi library
#include <Ethernet.h> //ethernet library 
#include <HttpClient.h>//http client library
#include <Xively.h> //xively library


//using the mac id of ethernrt shield Mac_ID =  90-A2-DA-0E-99-85 
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x99, 0x85 };

//Xively api key for upload and download
char xivelyKey[] = "api_key";

// Dtastream id i.e Device create in Xively account with same name otherwise will get an error
char ledId[] = "led";
char sensorId[]="stepper";

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0; 

//Initializing the xively datastream for the devices created in xively account

XivelyDatastream datastreams[] = {
  XivelyDatastream(ledId, strlen(ledId), DATASTREAM_FLOAT),
  XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};

//The  transmission happen through feed initialize it
//XivelyFeed feed(FEED_ID,XIVELY_DATASTREAM,NO_OF_DATASTREAM)
XivelyFeed feed(feedid, datastreams, 2);

//Initializing the ethernet client for http support 
EthernetClient myclient;

//Use the ethernet client to initialize xively client
XivelyClient xivelyclient(myclient);


/* Initial setup of arduino */ 
void setup(){
  /* initialize the serial communication to monitor the transmission */
  //starting the serial communication with the baud rate 9600
  Serial.begin(9600);

  Serial.println("Serial Communication started");
  Serial.println();

  //assigning the ip address using dhcp
  while (Ethernet.begin(mac) != 1){
     Serial.println("Error getting IP address via DHCP, trying again...");
     delay(15000);
  }
}

/* loop for doing continous upload and dowload of data*/
void loop(){
    int sensor;
    sensor=analogRead(sensorPin);

    datastreams[1].setFloat(sensor);

  /* uploading the data */ 
      Serial.print("uploading data of stepper");
    Serial.println(datastreams[1].getFloat());

    Serial.println("Uploading it to Xively");
    while((xivelyclient.put(feed, xivelyKey) != 200));
    Serial.println();
    delay(15000);

    Serial.println("uploading of data completed");
  /* finidh uploading data*/

  /* Downloading the data*/
    Serial.println("downloading data");
    while((xivelyclient.get(feed, xivelyKey) != 200));

    Serial.println("Datastream is...");
    Serial.println(feed[0]);
    Serial.println("Datastream is...");
    Serial.println(feed[1]);


    Serial.print("stepper motor downloaded data is: ");
    Serial.println(feed[1].getFloat());

    Serial.println("downloadind data completed");
    Serial.println();
    delay(15000);
  /* Downloading data completed */

}
4

1 回答 1

0

制作两个 XivelyDatastream:一个带有您要发送的 Id,另一个带有您要阅读的内容。

否则,数据流中的所有内容都会更新。

// Setup two different streams:
XivelyDatastream datastreams_get[] = {
  XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};

XivelyDatastream datastreams_put[] = {
  XivelyDatastream(ledId, strlen(ledId), DATASTREAM_FLOAT),
};

//The  transmission happen through feed initialize it
//XivelyFeed feed(FEED_ID,XIVELY_DATASTREAM,NO_OF_DATASTREAM)
XivelyFeed feedget(feedid, datastreams_get, 1);
XivelyFeed feedput(feedid, datastreams_put, 1);

// then use different stream definitions for PUT and GET:
xivelyclient.get(feedget, xivelyKey);
xivelyclient.put(feedput, xivelyKey);
于 2013-10-18T22:19:41.990 回答