Send sensor data to Google BigQuery

In this tutorial we'll show you how to use your microcontroller to send data to Google BigQuery. This will enable you to store and query large amounts of sensor data extremely efficiently.

This sketch uses our Google > BigQuery > InsertAll Choreo.

We're using an Arduino Yún for this tutorial. If you're not using a Yún, we have the same tutorial for the following embedded devices:

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.

2Next, make sure that you've got the latest version the Arduino IDE installed.

3Since this sketch uses Google BigQuery, you'll need a BigQuery account and you'll need to go through the OAuth process so that you can programmatically send sensor data to your BigQuery account. Head over to our BigQuery Choreo instructions and get set up. Here's one helpful tip: when you get to the BigQuery OAuth process, it's sufficient to specify the following scope as an input to the InitializeOAuth Choreo:


4As you're going through the BigQuery setup process, make sure to save the ProjectID, ClientID, ClientSecret, and RefreshToken that you generate while setting up your BigQuery app and running through the OAuth process - you'll need them while generating code in the next section.

Create Your Dataset & Table

5Now, log into the BigQuery console and create a new dataset called TembooSensorReadings.

BigQuery create dataset

6Next, create a table within your dataset called SensorReadings. Here's the details for your table:

Here's a screenshot showing you how everything should look.

BigQuery create table

Generate your BigQuery Code

7Now, go to the BigQuery > TableData > InsertAll Choreo. Make sure that IoT Mode is turned on and that you've selected the microcontoller device that you're generating code for.

8Next, fill out all the required inputs. You'll need to use the project ID for the BigQuery app you set up earlier, along with its ClientID and ClientSecret. You'll also need your OAuth refresh token, the name of your dataset (TembooSensorReadings) for the DatasetID, and your sensor data table name (SensorReadings) for the TableID. Finally, add some JSON describing the sensor data that you want to send to BigQuery. Here's some you can use that matches the schema that we set up earlier:

    "json": {
      "Temperature": 30,
      "Humidity": 35

BigQuery Choreo inputs

9Once you've got all of your inputs filled in, you can run the Choreo and add a row of data to BigQuery. This will prove that everything is working correctly and we can move on to trying out our generated code. To check that your data is adding to BigQuery successfully, you can compose a SELECT * ... query in the BigQuery query composer and you should see the two rows of data the we've added so far.

BigQuery results

Run The Code

10Scroll down and copy the Arduino code from the Code section and paste it into your Arduino IDE.

11The 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 sketch. Create a new tab in the Arduino IDE called TembooAccount.h and copy in the header file information.

Extending The Sketch

You'll notice that your sketch always sends the same value to BigQuery - the values we were testing with. Of course, it's far more interesting to send dynamic values i.e., sensor values. Here's an example of what you need to do to make that happen.

12Add the following lines of code to your setup() method so that you're reading the value on pins A0 and A1:

pinMode(A0, INPUT);
pinMode(A1, INPUT);

13Next, replace the line of code that currently adds static data to BigQuery with the following code so that your sketch reads the value on pin A0 and A1 and sends that data to BigQuery instead.

String temperature = (String)analogRead(A0);
String humidity = (String)analogRead(A1);
String sensorJSON = "[\n  {\n    \"json\": {\n      \"Temperature\":" + temperature +",\n      \"Humidity\":" + humidity + "\n    }\n  }\n]";
InsertAllChoreo.addInput("Rows", sensorJSON);

14That's it! Now your sketch should be generating dynamic values and adding them to your BigQuery table. Go back and run your SELECT * ... query to see the updated sensor data.

What's Next?

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

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Get Live Updates About Your Application

Integrate Temboo with Slack to get up-to-the-second notifications and alerts about your deployed Temboo code.

Commit Code to GitHub

Commit Temboo-generated code directly to GitHub and share with the world. Learn more.

Need Help?

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