diff options
Diffstat (limited to 'libraries/Bridge/examples/XivelyClient/XivelyClient.ino')
-rw-r--r-- | libraries/Bridge/examples/XivelyClient/XivelyClient.ino | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/libraries/Bridge/examples/XivelyClient/XivelyClient.ino b/libraries/Bridge/examples/XivelyClient/XivelyClient.ino new file mode 100644 index 0000000..69f979c --- /dev/null +++ b/libraries/Bridge/examples/XivelyClient/XivelyClient.ino @@ -0,0 +1,110 @@ +/* + Xively sensor client with Strings + + This sketch connects an analog sensor to Xively, + using an Arduino Yún. + + created 15 March 2010 + updated 27 May 2013 + by Tom Igoe + + */ + +// include all Libraries needed: +#include <Process.h> +#include "passwords.h" // contains my passwords, see below + +/* + NOTE: passwords.h is not included with this repo because it contains my passwords. + You need to create it for your own version of this application. To do so, make + a new tab in Arduino, call it passwords.h, and include the following variables and constants: + + #define APIKEY "foo" // replace your pachube api key here + #define FEEDID 0000 // replace your feed ID + #define USERAGENT "my-project" // user agent is the project name + */ + + +// set up net client info: +const unsigned long postingInterval = 60000; //delay between updates to xively.com +unsigned long lastRequest = 0; // when you last made a request +String dataString = ""; + +void setup() { + // start serial port: + Bridge.begin(); + Serial.begin(9600); + + while(!Serial); // wait for Network Serial to open + Serial.println("Xively client"); + + // Do a first update immediately + updateData(); + sendData(); + lastRequest = millis(); +} + +void loop() { + // get a timestamp so you can calculate reading and sending intervals: + long now = millis(); + + // if the sending interval has passed since your + // last connection, then connect again and send data: + if (now - lastRequest >= postingInterval) { + updateData(); + sendData(); + lastRequest = now; + } +} + +void updateData() { + // convert the readings to a String to send it: + dataString = "Temperature,"; + dataString += random(10) + 20; + // add pressure: + dataString += "\nPressure,"; + dataString += random(5) + 100; +} + +// this method makes a HTTP connection to the server: +void sendData() { + // form the string for the API header parameter: + String apiString = "X-ApiKey: "; + apiString += APIKEY; + + // form the string for the URL parameter: + String url = "https://api.xively.com/v2/feeds/"; + url += FEEDID; + url += ".csv"; + + // Send the HTTP PUT request + + // Is better to declare the Process here, so when the + // sendData function finishes the resources are immediately + // released. Declaring it global works too, BTW. + Process xively; + Serial.print("\n\nSending data... "); + xively.begin("curl"); + xively.addParameter("-k"); + xively.addParameter("--request"); + xively.addParameter("PUT"); + xively.addParameter("--data"); + xively.addParameter(dataString); + xively.addParameter("--header"); + xively.addParameter(apiString); + xively.addParameter(url); + xively.run(); + Serial.println("done!"); + + // If there's incoming data from the net connection, + // send it out the Serial: + while (xively.available()>0) { + char c = xively.read(); + Serial.write(c); + } + +} + + + + |