-
Notifications
You must be signed in to change notification settings - Fork 12
Arduino Yun
The Arduino Yun is an extremely powerful board. It can actually run Linux in tandem with the traditional Arduino setup! Ironically, this means that you can actually just use Initial State's Python library to stream, but I'm going to show you in a sketch anyways 😜.
Because the Yun is so powerful, we can construct a cURL command inside of our sketch to send. This method requires the ability to handle HTTPS calls.
Now let's go over the relevant parts of the arduinostreamyun.ino sketch:
////////////////////////////
// Initial State Streamer //
////////////////////////////
// URL to IS Bucket API
String ISBucketURL = "https://groker.initialstate.com/api/buckets";
// URL to IS Event API
String ISEventURL = "https://groker.initialstate.com/api/events";
// Access key (the one you find in your account settings):
String accessKey = "Your_Access_Key";
// Bucket key (hidden reference to your bucket that allows appending):
String bucketKey = "arduino_stream";
// Bucket name (name your data will be associated with in Initial State):
String bucketName = "Arduino Stream";
The Initial State API has 2 different endpoints, one that creates buckets (https://groker.initialstate.com/api/buckets) and one that accepts events (https://groker.initialstate.com/api/events). Your access key tells the API which account the data belongs to and the bucket key tells the API which data bucket the data should go to. The bucket name is optional, but is what you see in your bucket shelf.
void postBucket()
{
// Initialize the process
Process isbucket;
isbucket.begin("curl");
isbucket.addParameter("-k");
isbucket.addParameter("-v");
isbucket.addParameter("-X");
isbucket.addParameter("POST");
isbucket.addParameter("-H");
isbucket.addParameter("Content-Type:application/json");
isbucket.addParameter("-H");
isbucket.addParameter("Accept-Version:0.0.1");
// IS Access Key Header
isbucket.addParameter("-H");
isbucket.addParameter("X-IS-AccessKey:" + accessKey);
// IS Bucket Key Header
isbucket.addParameter("-d");
isbucket.addParameter("{\"bucketKey\": \"" + bucketKey + "\", \"bucketName\": \"" + bucketName + "\"}");
isbucket.addParameter("https://groker.initialstate.com/api/buckets");
// Run the process
isbucket.run();
Serial.flush();
}
This function constructs the cURL command that creates a bucket. You can skip creating a bucket in your script if you choose to create your bucket inside of Initial State.
We are basically creating this format and running it as a process:
curl --include \
--request POST \
--header "Content-Type: application/json" \
--header "X-IS-AccessKey: YOUR_KEY" \
--header "Accept-Version: ~0" \
--data-binary "{
\"bucketKey\": \"curl_example_bucket\",
\"bucketName\": \"cURL Example Bucket\"
}" \
'https://groker.initialstate.com/api/buckets'
The "-k -v -X" are necessary cURL flags. "-H" is a substitute for the "--header" and "-d" is a substitute for "--data-binary".
Posting data happens exactly the same way, just directed at the events API endpoint instead:
void postData()
{
// Initialize the process
Process isstreamer;
isstreamer.begin("curl");
isstreamer.addParameter("-k");
isstreamer.addParameter("-v");
isstreamer.addParameter("-X");
isstreamer.addParameter("POST");
isstreamer.addParameter("-H");
isstreamer.addParameter("Content-Type:application/json");
isstreamer.addParameter("-H");
isstreamer.addParameter("Accept-Version:0.0.1");
// IS Access Key Header
isstreamer.addParameter("-H");
isstreamer.addParameter("X-IS-AccessKey:" + accessKey);
// IS Bucket Key Header
// Note that bucketName is not needed here
isstreamer.addParameter("-H");
isstreamer.addParameter("X-IS-BucketKey:" + bucketKey);
isstreamer.addParameter("-d");
// Initialize a string to hold our signal data
String jsonData;
jsonData = "[";
for (int i=0; i<NUM_SIGNALS; i++)
{
jsonData += "{\"key\": \"" + signalName[i] + "\", \"value\": \"" + signalData[i] + "\"}";
if (i != NUM_SIGNALS - 1)
{
jsonData += ",";
}
}
jsonData += "]";
isstreamer.addParameter(jsonData);
isstreamer.addParameter("https://groker.initialstate.com/api/events");
// Print posted data for debug
Serial.print("Sending data: ");
Serial.println(jsonData);
// Run the process
isstreamer.run();
Serial.flush();
}
The most important part here is how we are stringing together our events. Each event must be enclosed in brackets and separated by commas, like so:
curl --include \
--request POST \
--header "Content-Type: application/json" \
--header "X-IS-AccessKey: YOUR_KEY" \
--header "X-IS-BucketKey: curl_example_bucket" \
--header "Accept-Version: ~0" \
--data-binary "[
{
\"key\": \"temperature\",
\"value\": \"22.2\"
},
{
\"key\": \"power\",
\"value\": \"6\"
},
{
\"key\": \"status\",
\"value\": \":thumbsup:\"
},
{
\"key\": \"current\",
\"value\": \"156\"
},
{
\"key\": \"door\",
\"value\": \"open\"
}
]" \
'https://groker.initialstate.com/api/events'
This particular sketch was written in a way that will be dynamically formatted based on the number of different events you are trying to send.
And that's how to build cURL commands in a sketch! Note - you can also stream from the Yun using the Ciao library method.
Now on to using the ESP8266!