Skip to content

Instantly share code, notes, and snippets.

@xively-gists
Last active December 17, 2015 00:18
Show Gist options
  • Save xively-gists/5519661 to your computer and use it in GitHub Desktop.
Save xively-gists/5519661 to your computer and use it in GitHub Desktop.
Sample Xively Arduino Sketch
/**
* Xively Arduino sensor client example.
*
* This sketch demonstrates connecting an Arduino to Xively (https://xively.com),
* using the new Arduino library to send and receive data.
*
* Requirements
* * Arduino with Ethernet shield or Arduino Ethernet (board must use the
* Wiznet Ethernet chipset)
* * Arduino software with version >= 1.0
* * An account at Xively (https://xively.com)
*
* Optional
* * An analog sensor connected to pin 2 (note we can still read a value from
* the pin without this)
*
* Created 8th January, 2013 using code written by Adrian McEwen with
* modifications by Sam Mulube
*
* Full tutorial available here: https://xively.com/docs/quickstart/arduino.html
*
* This code is in the public domain.
**/
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#define API_KEY "YOUR_API_KEY" // your Xively API key
#define FEED_ID YOUR_FEED_ID // your Xively feed ID
// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
unsigned long lastConnectionTime = 0; // last time we connected to Xively
const unsigned long connectionInterval = 15000; // delay between connecting to Xively in milliseconds
// Initialize the Xively library
// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
XivelyDatastream datastreams[] = {
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
XivelyFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
XivelyClient xivelyclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Xively Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
}
void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read a value from the pin
int sensorValue = analogRead(sensorPin);
// send it to Xively
sendData(sensorValue);
// read the datastream back from Xively
getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
// send the supplied value to Xively, printing some debug information as we go
void sendData(int sensorValue) {
datastreams[0].setFloat(sensorValue);
Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading to Xively");
int ret = xivelyclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);
Serial.println();
}
// get the value of the datastream from Xively, printing out the value we received
void getData() {
Serial.println("Reading data from Xively");
int ret = xivelyclient.get(feed, API_KEY);
Serial.print("GET return code: ");
Serial.println(ret);
if (ret > 0) {
Serial.print("Datastream is: ");
Serial.println(feed[0]);
Serial.print("Sensor value is: ");
Serial.println(feed[0].getFloat());
}
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment