Skip to content

Instantly share code, notes, and snippets.

@krhoyt krhoyt/parse.ino
Last active Mar 16, 2017

Embed
What would you like to do?
Arduino Yun to Parse.com (Process, curl).
// Original:
// http://hypernephelist.com/2014/08/19/https_on_arduino_yun.html
// Libraries
#include <Process.h>
// Literals
// #define DEBUG
// Constants
const char *PARSE_API = "api.parse.com";
const char *PARSE_APP = "_YOUR_PARSE_APP_";
const char *PARSE_CLASS = "Temperature";
const char *PARSE_KEY = "_YOUR_PARSE_REST_KEY_";
const char *PARSE_VERSION = "1";
// Counter
int counter = 0;
// Leverage Yun Linux (curl)
Process process;
// Buffer for parameters
char buffer[80];
// Setup
void setup()
{
// Bridge communication
Bridge.begin();
// Console debugging
// Wait for console
#ifdef DEBUG
Console.begin();
while( !Console ) {;}
#endif
}
// Loop
void loop()
{
// Increment counter
counter = counter + 1;
// Put value in data store
request( counter );
wait();
response();
// Wait for next sample
delay( 5000 );
}
// Send the data to Parse.com
void request( int value )
{
// Curl request per Parse.com documentation
/*
curl -X POST \
-H "X-Parse-Application-Id: APPLICATION_ID" \
-H "X-Parse-REST-API-Key: REST_API_KEY" \
-H "Content-Type: application/json" \
-d '{"reading":123}' \
https://api.parse.com/1/classes/Temperature
*/
// Build curl command line
// Includes HTTPS support
// POST
// JSON
process.begin( "curl" );
process.addParameter( "-k" );
process.addParameter( "-X" );
process.addParameter( "POST" );
process.addParameter( "-H" );
process.addParameter( "Content-Type:application/json" );
// Parse.com application
process.addParameter( "-H" );
sprintf( buffer, "X-Parse-Application-Id: %s", PARSE_APP );
process.addParameter( buffer );
// Parse.com key
process.addParameter( "-H" );
sprintf( buffer, "X-Parse-REST-API-Key: %s", PARSE_KEY );
process.addParameter( buffer );
// JSON body
process.addParameter( "-d" );
sprintf( buffer, "{\"reading\": %d}", value );
process.addParameter( buffer );
// URI
sprintf( buffer, "https://%s/%s/classes/%s", PARSE_API, PARSE_VERSION, PARSE_CLASS );
process.addParameter( buffer );
// Run the command
// Synchronous
process.run();
}
// Response from Parse.com
void response()
{
bool print = true;
char c;
// While there is data to read
while( process.available() )
{
// Get character
c = process.read();
// Debugging
#ifdef DEBUG
// Print until newline
if( c == '\n' )
{
print = false;
}
// Not newline yet
// Print to console
if( print )
{
Console.print( c );
}
#endif
}
// Debugging
#ifdef DEBUG
// Newline for console
Console.println();
#endif
}
// Wait for a response from Parse.com
void wait()
{
// Periodically check curl process
while( !process.available() )
{
delay( 100 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.