Hardware

Save ARTIK data to a Google Spreadsheet


We'll show you how to make your Samsung ARTIK add rows of data to a Google Spreadsheet. You can use your ARTIK to log data from sensors connected to your board like temperature readings over time, or other instances where your ARTIK is receiving information that you'd like to record.

This example uses our Google > Sheets > AppendValues Choreo.

Get Set Up

1Make sure you have a Temboo account. If you don't already have one, you can register for a free account here.

2If this is the first time using Temboo, you'll need to set up your ARTIK board to find the Temboo libraries. To do so, you'll need to create the file temboo.conf in /etc/ld.so.conf.d. Inside of temboo.conf, you'll want to copy the line below and paste it into the file:

/opt/iothub/artik/temboo/temboo_artik_library/lib

After saving temboo.conf, run ldconfig from the command line to add the Temboo library directory to the system library search path.

You’ll also need to get the IP address of your ARTIK board using ifconfig. The example below shows the results when the ARTIK board is connected to the internet through an ethernet cable. The IP address you'll want to use is listed next to inet.

[root@localhost ~]$ ifconfig
eth0: flags=4163  mtu 1500
        inet 10.11.6.200  netmask 255.255.255.0  broadcast 10.11.6.255

3Since this sketch uses a Google spreadsheet, you'll also need a Google account.

4Login to Google's Developer Console, and create a new Project if you haven't done so already.

5Under the API Manager section, select Library and make sure you've enabled API Access for the Google Sheets API.

6Select Credentials in the API Manager section, and create a new Client ID specifying Web application for the Application Type.

7When configuring the Consent Screen, make sure you fill out the Email Address and Product Name fields.

8Save the Consent Screen details and specify the callback URL below as the Authorized Redirect URI.

https://ACCOUNT_NAME.temboolive.com/callback/google

9Go to the Google > OAuth > InitializeOAuth Choreo page, and fill in the Client ID from the app you registered at Google and the following Scope: https://www.googleapis.com/auth/spreadsheets . Then click Generate Code to run the Choreo from our site.

Google OAuth Inputs

The InitializeOAuth choreo will return an authorization URL and a callback ID (required for the FinalizeOAuth step).

10Open a new web browser, navigate to the authorization URL returned by the InitializeOAuth Choreo, and click "Accept" to grant the app access to your Google account.

Google OAuth Accept

11Go to the Google > OAuth > FinalizeOAuth Choreo page, and fill in the callback ID returned earlier by the InitializeOAuth Choreo. Next, click Generate Code to run the Choreo from our site. This process will return a Refresh Token which can be used along with the Client ID and Client Secret to authenticate with Google.

12Create a Google Spreadsheet. In this example our spreadsheet has two columns as seen below: time (in milliseconds) and sensor values.

Google Spreadsheet

A screenshot of a spreadsheet taking data from an ARTIK - note the column names

13When viewing your spreadsheet, you'll see a spreadsheet ID in your browser's URL bar. Copy this ID because you'll need it when running the AppendValues Choreo. In the example below, the highlighted part is the Spreadsheet ID.

Google Spreadsheet

Auto-Generate your ARTIK Code

14Go to the Google > Sheets > AppendValues in our Library.

15Select Samsung from the drop down menu at the top of the Choreo page.

Selecting the Samsung ARTIK 10

16Fill in the required Choreo inputs, then click Generate Code to test out the Choreo from our website and confirm that your Google Spreadsheet was updated successfully.

Google Spreadsheets Inputs

Testing the AppendValues Choreo from our website

17When you've confirmed that the Choreo runs successfully, you can download the auto-generated ARTIK code from the Code section.

18The auto-generated sketch references the TembooAccount.h header file, which contains your Temboo account information. You'll find the code for this file beneath your generated code. Make sure you keep the two files together when running them on your ARTIK.

Run The Code

19Copy the downloaded zip file to your ARTIK board using scp. From your computer, go to the directory where the zip file is located and type the command below. Make sure you use the IP address for your board you found using ifconfig.

[root@localhost ~]$ scp appendvalues.zip root@xxx.xxx.xxx.xxx:/home

20Now go to the appendvalues directory and compile your code and start adding data to your spreadsheet. Run the first command below to compile your code. After compiling has finished and no errors have been reported, run your program using the second command.

[root@localhost ~]$ gcc -L/opt/iothub/artik/temboo/temboo_artik_library/lib -ltemboo -I/opt/iothub/artik/temboo/temboo_artik_library/include appendrvalues.c -o appendvalues
[root@localhost ~]$ ./appendvalues

Note: If you get an error about cdefs.h not being found, this could mean your ARTIK board was not shipped with glibc-headers. This can be solved by reinstalling glibc-headers using the command below. After reinstalling glibc-headers, run the two commands above to compile and run your code.

[root@localhost ~]$ yum reinstall glibc-headers

Extending The Code

You'll notice that your code always sends the same hard-coded values to your spreadsheet. Of course, it's far more interesting to send dynamic values i.e., sensor values. We'll show how to add a sensor value and current time to our spreadsheet. Here's an example of what you need to do to make that happen.

21First, we'll want to define the pin, we'll use pin 2, that we're using to read values from our sensor. Before your setup() method, add the following line:

//define the pin you'll be reading sensor data from
const int sensorPin = 2;

22In our setup() method, we should also specify that our sensor pin will be an INPUT by adding the following line:

// set the sensor pin as an Input 
digitalPinMode(sensorPin, INPUT);

23At the beginning of your loop() function, create a variable that holds the current time in seconds.

// get the current time in seconds
unsigned long now = time(NULL);

24We'll now want to read the sensor data coming in from our sensor pin, and plug that data into our rowData variable by adding the following lines after initChoreo(&choreo, choreoName);.

//read the values coming off of the sensor
unsigned long sensorValue = analogRead(sensorPin);

//convert the time and sensor data into a json array
 char rowData[32];
 memset(rowData, 0, sizeof(rowData));
 snprintf(rowData, sizeof(rowData), "[[\"%i\",\"%i\"]]", now, sensorValue);
 ChoreoInput ValuesIn;
ValuesIn.name = "Values";
ValuesIn.value = rowData;
 addChoreoInput(&choreo, &ValuesIn);

25That's it! Now your code should be generating dynamic values and adding them to your Google Spreadsheet.

What's Next?

Now that you've mastered working with Google Spreadsheets, why not check out the rest of the 2000+ Choreos in our Library and get inspired for your next project.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back