- 0. Introduction
- 1. Set up the example data
- 2. Create a Google Spreadsheet
- 3. Get the Google credentials
- 4. Send data from Altair SmartCore to Google Spreadsheets using a listener
If you have a Google account, you can send data from Altair SmartCore to the Google API. In this example, we will send data to Google spreadsheets. Google spreadsheets gives you many options to organize, visualize, share and export your data.
In this tutorial we will follow the example used in the Arduino Alert System. You could also do the example using the Arduino Yun Alert System or even just enter data values directly into the Send Stream Wizard in the Altair SmartCore Control Panel.
We will make an alert system that sends sensor data to Altair SmartCore every time the lights turn off or on. In a Altair SmartCore listener, we will send that data to a Google Spreadsheet.
Follow the Alert System tutorial to establish the correct connections for the Arduino. When you get to the code part, copy and paste the following code in the sketch instead:
Run the code and check and see in Altair SmartCore if your data is being received correctly.
Now you need to create your spreadsheet, to do so, you need to have a Google account. If you don't have one, you must create one before continuing.
To create a Google spreadsheet go to this page and click on Get Google Sheets and then Go to Sheets as shown in the picture below.
- Make a Spreadsheet
To make a new spreadsheet, click on the + button which is in a green circle in the bottom left-hand corner of the screen. You should now see a new spreadsheet. Click on the Untitled spreadsheet text to rename the document. In our case we used the name SpreadsheetTest.
- Rename your Spreadsheet
Now, you must name each column to which you will add data. In our case, we have three data columns: Time, Sensor and Lights status.
- Name the columns
The step we need to take to be able to connect to our Spreadsheet is to get an authorization token for our Google account. To do this, you can use curl requests (you must have curl installed on your computer) or using hurl requests using an online HTTP request tool. Below we show how to do both methods.
Our curl command is this one:
Click here to make a hurl request using the online tool. In destination select POST and enter the url www.google.com/accounts/ClientLogin. In headers enter Content-Type and application/x-www-form-urlencoded. In parameters add the following code, substituting your values:
It should look something like this:
- Hurl request
In both requests the only fields you need to change are the Email and Passwd fields. This request will return three values like the ones below:
- SID = DQAAANcAAABjXXX
- LSID = DQAAANoAXXX
- Auth = DQAAANkAXXX
The only value we care about is the third one, the Auth value. This is the value that will give us permission to modify the spreadsheet. Now look at your spreadsheet url. There is a key in the url that we will need to perform the listener step.
- Spreadsheet Key
Now we are ready to create the listener in Altair SmartCore. To create a listener go to the control panel, then to Rules” → “Listeners”. Then click on the new button above the Listeners list. Select "Device" as entity type and locate next the device asigned to your Arduino. In the If expression text box copy and paste the following line:
Now, in the Then expression text box copy and paste the following code, changing the values for your personal values where necessary:
In the url you must add your specific key. In the auth= part of the headers, you must put your auth key.
Your listener should look something like this.
- Create a listener from device
The listener will "listen" to the "data_received" event.
When Altair SmartCore receives a data stream from the default device that contains the sensor and light data values it will fire the execution path detailed in the "if_expression".
Now that you have created the listener, any time you run the Arduino sketch your data should be sent automatically to your Google Spreadsheet. This way, you can see your data represented in an orderly manner at any time. You can also easily make charts and graphs to represent your data. To make a graph simply highlight the information you want to be included in the graph. Once the data is selected, click Insert and then chart. Select Charts and then Area.
- Make Graph
Now that you have successfully connected Altair SmartCore to Google API, you can use the many other functions that the Google API provides for users. The solution is easy. Explore and enjoy!TOP