1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-04 15:24:12 +01:00
Arduino/libraries/Bridge/examples/XivelyClient/XivelyClient.ino

114 lines
2.9 KiB
Arduino
Raw Normal View History

/*
Xively sensor client with Strings
This sketch connects an analog sensor to Xively,
using an Arduino Yún.
2013-05-31 13:27:01 +02:00
created 15 March 2010
updated 27 May 2013
by Tom Igoe
2013-09-09 20:56:15 +02:00
http://arduino.cc/en/Tutorial/YunXivelyClient
*/
2013-09-09 20:56:15 +02:00
// 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
2013-05-31 13:27:01 +02:00
*/
// set up net client info:
2013-05-31 13:27:01 +02:00
const unsigned long postingInterval = 60000; //delay between updates to xively.com
2013-05-27 19:12:05 +02:00
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");
2013-06-05 14:51:15 +02:00
// Do a first update immediately
2013-05-31 13:27:01 +02:00
updateData();
sendData();
2013-05-31 13:27:01 +02:00
lastRequest = millis();
}
void loop() {
// get a timestamp so you can calculate reading and sending intervals:
long now = millis();
2013-05-31 13:27:01 +02:00
// 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:
2013-05-31 13:27:01 +02:00
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:
2013-05-31 13:27:01 +02:00
String apiString = "X-ApiKey: ";
apiString += APIKEY;
// form the string for the URL parameter:
2013-05-31 13:27:01 +02:00
String url = "https://api.xively.com/v2/feeds/";
url += FEEDID;
2013-05-31 13:27:01 +02:00
url += ".csv";
// Send the HTTP PUT request
// Is better to declare the Process here, so when the
2013-06-05 14:51:15 +02:00
// sendData function finishes the resources are immediately
2013-05-31 13:27:01 +02:00
// released. Declaring it global works too, BTW.
Process xively;
Serial.print("\n\nSending data... ");
xively.begin("curl");
xively.addParameter("-k");
2013-05-31 13:27:01 +02:00
xively.addParameter("--request");
xively.addParameter("PUT");
xively.addParameter("--data");
xively.addParameter(dataString);
2013-05-31 13:27:01 +02:00
xively.addParameter("--header");
xively.addParameter(apiString);
xively.addParameter(url);
xively.run();
Serial.println("done!");
2013-05-31 13:27:01 +02:00
// If there's incoming data from the net connection,
// send it out the Serial:
while (xively.available() > 0) {
2013-05-31 13:27:01 +02:00
char c = xively.read();
Serial.write(c);
2013-05-31 13:27:01 +02:00
}
2013-05-31 13:27:01 +02:00
}