Created
April 9, 2016 05:30
-
-
Save shawngrimes/22df215552c02b3c4d43b69a80d1ef1f to your computer and use it in GitHub Desktop.
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> | |
int ledPin=9; //Variable to store pin of LED | |
int potentPin=A0; //Variable to store pin of potentiometer | |
int potentValue=0; //Variable to store last known value of potentiometer | |
int brightnessValue=0; //Variable to store LED brightness | |
int moistPin=A1; //Variable to store pin of moisture sensor | |
int moistValue=0; //Variable to store moisture value | |
///////////////// | |
// Phant Stuff // | |
///////////////// | |
// 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 = 2; | |
// What are the names of your fields? | |
String fieldName[NUM_FIELDS] = {"light","moist"}; | |
// 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(); | |
pinMode(ledPin, OUTPUT); //Setup LED pin for output | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
potentValue=analogRead(potentPin); //Read the value of the potentiometer pin | |
Serial.print("Analog Read: "); //Output to serial console | |
Serial.println(potentValue); //Output potentValue to serial console and add a new line at the end | |
brightnessValue=map(potentValue,0,1023,0,255); //Map the potentiometer value to a brightness | |
analogWrite(ledPin,brightnessValue); //Set the brightness of the ledPin | |
//Read moisture value | |
moistValue=analogRead(moistPin); //Read moisture value from moisture pin | |
// Gather Data | |
fieldData[0] = String(potentValue); | |
fieldData[1] = String(moistValue); | |
// Post Data | |
Serial.println("Posting Data!"); | |
postData(); // the postData() function does all the work, | |
// see below. | |
delay(5000); //We are going to pause for 5 seconds before taking another reading so we don't overwhelm the server | |
} | |
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