mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-17 22:23:10 +03:00
Updated XivelyClient example
This commit is contained in:
@ -1,16 +1,13 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Xively sensor client with Strings
|
Xively sensor client with Strings
|
||||||
|
|
||||||
This sketch connects an analog sensor to Xively,
|
This sketch connects an analog sensor to Xively,
|
||||||
using an Arduino Yun.
|
using an Arduino Yún.
|
||||||
|
|
||||||
created 15 March 2010
|
created 15 March 2010
|
||||||
updated 27 May 2013
|
updated 27 May 2013
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
|
||||||
NOT CURRENTLY WORKING as of Arduino 1.5.3 27 May 2013
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// include all Libraries needed:
|
// include all Libraries needed:
|
||||||
@ -29,84 +26,85 @@
|
|||||||
|
|
||||||
|
|
||||||
// set up net client info:
|
// set up net client info:
|
||||||
boolean lastConnected = false; // state of the connection last time through the main loop
|
const unsigned long postingInterval = 60000; //delay between updates to xively.com
|
||||||
const unsigned long postingInterval = 60000; //delay between updates to Pachube.com
|
|
||||||
unsigned long lastRequest = 0; // when you last made a request
|
unsigned long lastRequest = 0; // when you last made a request
|
||||||
String dataString = "";
|
String dataString = "";
|
||||||
Process xively;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// start serial port:
|
// start serial port:
|
||||||
Serial.begin(9600);
|
|
||||||
Bridge.begin();
|
Bridge.begin();
|
||||||
delay(2000);
|
Console.begin();
|
||||||
|
|
||||||
while(!Serial); // wait for Serial Monitor to open
|
while(!Console); // wait for Network Console to open
|
||||||
Serial.println("Xively client");
|
Console.println("Xively client");
|
||||||
|
|
||||||
// reserve space for dataString:
|
// Do a first update immediatly
|
||||||
dataString.reserve(100);
|
updateData();
|
||||||
sendData();
|
sendData();
|
||||||
|
lastRequest = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// get a timestamp so you can calculate reading and sending intervals:
|
// get a timestamp so you can calculate reading and sending intervals:
|
||||||
long now = millis();
|
long now = millis();
|
||||||
|
|
||||||
// convert the readings to a String to send it:
|
// if the sending interval has passed since your
|
||||||
dataString = "temp,";
|
// last connection, then connect again and send data:
|
||||||
dataString += random(10) + 20;
|
|
||||||
// add pressure:
|
|
||||||
dataString += "\nPressure,";
|
|
||||||
dataString += random(5) + 100;
|
|
||||||
|
|
||||||
// if there's incoming data from the net connection,
|
|
||||||
// send it out the serial port:
|
|
||||||
if (xively.available()>0) {
|
|
||||||
char c = xively.read();
|
|
||||||
Serial.write(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// if you're not connected, and the sending interval has passed since
|
|
||||||
// your last connection, then connect again and send data:
|
|
||||||
if (now - lastRequest >= postingInterval) {
|
if (now - lastRequest >= postingInterval) {
|
||||||
|
updateData();
|
||||||
sendData();
|
sendData();
|
||||||
lastRequest = now;
|
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:
|
// this method makes a HTTP connection to the server:
|
||||||
void sendData() {
|
void sendData() {
|
||||||
Serial.println("Sending data");
|
|
||||||
dataString = "--data \'" + dataString;
|
|
||||||
dataString += "\'";
|
|
||||||
|
|
||||||
// form the string for the API header parameter:
|
// form the string for the API header parameter:
|
||||||
String apiString = "--header \"X-ApiKey: ";
|
String apiString = "X-ApiKey: ";
|
||||||
apiString += APIKEY;
|
apiString += APIKEY;
|
||||||
apiString += "\"";
|
|
||||||
|
|
||||||
// form the string for the URL parameter:
|
// form the string for the URL parameter:
|
||||||
String url = " \"https://api.xively.com/v2/feeds/";
|
String url = "https://api.xively.com/v2/feeds/";
|
||||||
url += FEEDID;
|
url += FEEDID;
|
||||||
url += ".csv\"";
|
url += ".csv";
|
||||||
|
|
||||||
// send the HTTP PUT request:
|
// Send the HTTP PUT request
|
||||||
|
|
||||||
|
// Is better to declare the Process here, so when the
|
||||||
|
// sendData function finishes the resources are immediatly
|
||||||
|
// released. Declaring it global works too, BTW.
|
||||||
|
Process xively;
|
||||||
|
Console.print("\n\nSending data... ");
|
||||||
xively.begin("curl");
|
xively.begin("curl");
|
||||||
xively.addParameter("-k");
|
xively.addParameter("-k");
|
||||||
xively.addParameter("--request PUT");
|
xively.addParameter("--request");
|
||||||
|
xively.addParameter("PUT");
|
||||||
|
xively.addParameter("--data");
|
||||||
xively.addParameter(dataString);
|
xively.addParameter(dataString);
|
||||||
|
xively.addParameter("--header");
|
||||||
xively.addParameter(apiString);
|
xively.addParameter(apiString);
|
||||||
xively.addParameter(url);
|
xively.addParameter(url);
|
||||||
xively.run();
|
xively.run();
|
||||||
|
Console.println("done!");
|
||||||
|
|
||||||
|
// If there's incoming data from the net connection,
|
||||||
|
// send it out the Console:
|
||||||
|
while (xively.available()>0) {
|
||||||
|
char c = xively.read();
|
||||||
|
Console.write(c);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user