Last active
April 9, 2016 05:15
-
-
Save shawngrimes/2d7722f3f992af78276df45ff4819d91 to your computer and use it in GitHub Desktop.
data.sparkfun.com template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Process.h> //Library for Sparkfun network connection | |
///////////////////////////////// | |
// Data.SparkFun.Com variables // | |
//////////////////////////////// | |
// URL to phant server (only change if you're not using data.sparkfun | |
String phantURL = "http://data.sparkfun.com/input/"; | |
// Public key (the one you see in the URL): | |
String publicKey = "xxxxxx"; | |
// Private key, which only someone posting to the stream knows | |
String privateKey = "xxxxxxx"; | |
// How many data fields are in your stream? | |
const int NUM_FIELDS = 3; | |
// What are the names of your fields? | |
String fieldName[NUM_FIELDS] = {"light","temp","humidity"}; | |
// We'll use this array later to store our field data | |
String fieldData[NUM_FIELDS]; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); //Setup serial console | |
Bridge.begin(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
///Start of Data.SparkFun.Com pieces of code... | |
// Gather Data | |
fieldData[0] = String(light); | |
fieldData[1] = String(temp); | |
fieldData[2] = String(humidity); | |
// Post Data | |
Serial.println("Posting Data!"); | |
postData(); // the postData() function does all the work, | |
// see below. | |
///End of Data.SparkFun.Com pieces of code... | |
delay(60000); //We are going to pause for 60 seconds before taking another reading so we don't overwhelm the server | |
} | |
//Data.SparkFun.Com Function | |
void postData() | |
{ | |
Process phant; // Used to send command to Shell, and view response | |
String curlCmd; // Where we'll put our curl command | |
String curlData[NUM_FIELDS]; // temp variables to store curl data | |
// Construct curl data fields | |
// Should look like: --data "fieldName=fieldData" | |
for (int i=0; i<NUM_FIELDS; i++) | |
{ | |
curlData[i] = "--data \"" + fieldName[i] + "=" + fieldData[i] + "\" "; | |
} | |
// Construct the curl command: | |
curlCmd = "curl "; | |
curlCmd += "--header "; // Put our private key in the header. | |
curlCmd += "\"Phant-Private-Key: "; // Tells our server the key is coming | |
curlCmd += privateKey; | |
curlCmd += "\" "; // Enclose the entire header with quotes. | |
for (int i=0; i<NUM_FIELDS; i++) | |
curlCmd += curlData[i]; // Add our data fields to the command | |
curlCmd += phantURL + publicKey; // Add the server URL, including public key | |
// Send the curl command: | |
Serial.print("Sending command: "); | |
Serial.println(curlCmd); // Print command for debug | |
phant.runShellCommand(curlCmd); // Send command through Shell | |
// Read out the response: | |
Serial.print("Response: "); | |
// Use the phant process to read in any response from Linux: | |
while (phant.available()) | |
{ | |
char c = phant.read(); | |
Serial.write(c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment