Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 12 ■ IOT PLATFORMS

If you have multiple sensors and want to send data to Xively for all of them, you
can simply set up multiple channels in Xively. In Arduino code you need to specify the
channel name in a similar way that you defined moistureSensorChannel. All these
channel names need to be passed to the datastreams array.
The XivelyFeed variable feed passes data for all the channels with a number that
specifies how many datastreams are contained in the feed. In this case, there is only one
datastream , so the value will be 1.
Next you define a XivelyClient variable using the WiFiClient. It will be used to
actually create a connection and pass the feed.
All of these are one time setups and the repetitive code is inside the transmitData()
function. The transmitData() function sets the latest moistureSensorValue in
datastreams[0] and then sends the feed to Xively. If the status code returned from Xively
in the ret variable is 200 , that means your feed was successfully sent to Xively.


Listing 12-3. Code for Publishing Data to Xively


// API Key - required for data upload
char xivelyKey[] = "YOUR_API_KEY";


#define xivelyFeed FEED_ID // Feed ID


char moistureSensorChannel[] = "SoilMoistureSensor1"; //Channel Name


// Datastream/Channel IDs
XivelyDatastream datastreams[] =
{
XivelyDatastream(moistureSensorChannel,
strlen(moistureSensorChannel),
DATASTREAM_FLOAT),
};


// Create Feed
XivelyFeed feed(xivelyFeed, datastreams, 1); // Number of Channels
// in Datastream


XivelyClient xivelyclient(client);


void transmitData()
{
//Set Xively Datastream
datastreams[0].setFloat(moistureSensorValue);


//Transmit Data to Xively
Serial.println("[INFO] Transmitting Data to Xively");


int ret = xivelyclient.put(feed, xivelyKey);


Serial.print("[INFO] Xively Response (xivelyclient.put): ");

Free download pdf