2013-05-27 18:55:09 +02:00
|
|
|
/*
|
2013-10-21 09:58:40 +02:00
|
|
|
Xively sensor client with Strings
|
|
|
|
|
2013-05-27 18:55:09 +02:00
|
|
|
This sketch connects an analog sensor to Xively,
|
2013-10-21 09:58:40 +02:00
|
|
|
using an Arduino Yún.
|
|
|
|
|
2013-05-31 13:27:01 +02:00
|
|
|
created 15 March 2010
|
2013-05-27 18:55:09 +02:00
|
|
|
updated 27 May 2013
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-09-09 20:56:15 +02:00
|
|
|
http://arduino.cc/en/Tutorial/YunXivelyClient
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-05-27 18:55:09 +02:00
|
|
|
*/
|
|
|
|
|
2013-09-09 20:56:15 +02:00
|
|
|
|
2013-05-27 18:55:09 +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:
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-05-27 18:55:09 +02:00
|
|
|
#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
|
|
|
*/
|
2013-05-27 18:55:09 +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
|
2013-05-27 18:55:09 +02:00
|
|
|
String dataString = "";
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// start serial port:
|
|
|
|
Bridge.begin();
|
2013-06-25 16:42:30 +02:00
|
|
|
Serial.begin(9600);
|
2013-05-31 05:09:57 +02:00
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!Serial); // wait for Network Serial to open
|
2013-06-25 16:42:30 +02:00
|
|
|
Serial.println("Xively client");
|
2013-05-31 05:09:57 +02:00
|
|
|
|
2013-06-05 14:51:15 +02:00
|
|
|
// Do a first update immediately
|
2013-05-31 13:27:01 +02:00
|
|
|
updateData();
|
2013-05-31 05:09:57 +02:00
|
|
|
sendData();
|
2013-05-31 13:27:01 +02:00
|
|
|
lastRequest = millis();
|
2013-05-27 18:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2013-05-27 18:55:09 +02:00
|
|
|
// convert the readings to a String to send it:
|
2013-05-31 13:27:01 +02:00
|
|
|
dataString = "Temperature,";
|
2013-05-27 18:55:09 +02:00
|
|
|
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: ";
|
2013-05-27 18:55:09 +02:00
|
|
|
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/";
|
2013-05-27 18:55:09 +02:00
|
|
|
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;
|
2013-06-25 16:42:30 +02:00
|
|
|
Serial.print("\n\nSending data... ");
|
2013-05-31 05:09:57 +02:00
|
|
|
xively.begin("curl");
|
|
|
|
xively.addParameter("-k");
|
2013-05-31 13:27:01 +02:00
|
|
|
xively.addParameter("--request");
|
|
|
|
xively.addParameter("PUT");
|
|
|
|
xively.addParameter("--data");
|
2013-05-27 18:55:09 +02:00
|
|
|
xively.addParameter(dataString);
|
2013-05-31 13:27:01 +02:00
|
|
|
xively.addParameter("--header");
|
2013-10-21 09:58:40 +02:00
|
|
|
xively.addParameter(apiString);
|
2013-05-27 18:55:09 +02:00
|
|
|
xively.addParameter(url);
|
|
|
|
xively.run();
|
2013-06-25 16:42:30 +02:00
|
|
|
Serial.println("done!");
|
2013-05-31 05:09:57 +02:00
|
|
|
|
2013-05-31 13:27:01 +02:00
|
|
|
// If there's incoming data from the net connection,
|
2013-06-25 16:42:30 +02:00
|
|
|
// send it out the Serial:
|
2013-10-21 09:58:40 +02:00
|
|
|
while (xively.available() > 0) {
|
2013-05-31 13:27:01 +02:00
|
|
|
char c = xively.read();
|
2013-06-25 16:42:30 +02:00
|
|
|
Serial.write(c);
|
2013-05-31 13:27:01 +02:00
|
|
|
}
|
2013-05-31 05:09:57 +02:00
|
|
|
|
2013-05-31 13:27:01 +02:00
|
|
|
}
|
2013-05-31 05:09:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|