Skip to content

Instantly share code, notes, and snippets.

@shawngrimes
Created April 8, 2016 04:12
Show Gist options
  • Save shawngrimes/c2ec49d6ecba150bd68d6490f3f3dea9 to your computer and use it in GitHub Desktop.
Save shawngrimes/c2ec49d6ecba150bd68d6490f3f3dea9 to your computer and use it in GitHub Desktop.
#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
/////////////////
// 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 = 1;
// What are the names of your fields?
String fieldName[NUM_FIELDS] = {"light"};
// 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
// Gather Data
fieldData[0] = String(potentValue);
// 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