How to send a stream from an ArduinoYUN
This tutorial will show you how to send data streams to Carriots from an ArduinoYUN. The idea is simple: grab Arduino sensing data, pass it to the Linino and send it to Carriots using our old friend cURL.
We will suppose that you have an Arduino Yun, the correct Arduino IDE (according to the Arduino guys) and you know how to upload sketches.
- Arduino YUN board
First, we need to know what we want to send and how to send it. Data is sent to Carriots REST API. You need:
- A device in Carriots
- A valid Apikey
- An Arduino YUN
- Your arduino YUN connected to the Internet
- Some data to send
If you're registered in Carriots, you have a default device already created for you.
Check your control panel and see what it looks like ().
Basically you need the device id_developer that might be something like defaultDevice@myusername.
We will use test@carriots device for this tutorial.
Now, go to your control panel's "My account" menu and check your Apikey. You can use the "Automatic apikey for sending streams" or the "Full Privileges Apikey". We strongly recommend to use the former one to not expose the full privileges apikey.
Its a big alphanumeric token like 98346673a6377ef1fde2357ebdcb0da582b150b00cabcd5a0d83045425407ab4.
Now we define the data to be sent. In this example, it will be a simple data set with temperature (or whatever you want):
Finally we need a REST client. This client can be whatever you want that can create full HTTP request including headers, verbs and so on.
In this tutorial we will use Linino's installed cURL. If you are not familiar with cURL you might want to check the official page: http://curl.haxx.se/ for further details. You can also follow this tutorial with other REST clients such as hurl (http://www.hurl.it/) or Poster plugin (https://addons.mozilla.org/en-us/firefox/addon/poster/) to check that you're doing thing right.
Your payload, the stream to send to Carriots, will be:
This is the Arduino code to build the payload (see the whole code here):
"protocol" can be used along with "checksum" for security enhancement (see https://www.carriots.com/documentation/protocols_and_checksum). Use "v2" for the "protocol", it wil be easier for our purposes.
The "at" field is the epoch time for the stream you can run "date +%s" to get it in Unix/Linux shell, or you can go go to http://www.epochconverter.com/ and create one or simply use "now" reserved word for automatic timestamping. "data" field contains the data to be sent.
The target URL is: http://api.carriots.com/streams/
Lets send it to Carriots!
We will use the Process type from Yun's bridge (Yun Bridge Library), that will invoke Linux shell commands from the Arduino sketch. This code shows how to instantiate a Process, add the corresponding parameters for a POST request with an carriots.apikey header, the payload and the destintation URL described above.
Ok, that's pretty much it. Now its time to pack it up in one single sketch with some random temperature sent every 10.000ms = 10s. Here is the code:
Just upload the code. Open your IDE's serial monitor and select 9600. You should see something like the following:
Now you can see your streams listed in the control panel's stream list. Go to "Data management" → "Data streams" and you will see your random temperature uploaded each 10 seconds.