Google Cloud IoT and

This post was written in partnership with our friends at Google IoT


In this tutorial, we'll show you how to integrate Google Cloud IoT Core with balena. This will enable you to gather and analyze device telemetry data in the Google Cloud Platform, while remotely managing and updating your devices with balena.

To get started, you'll first need to signup for a balena account, if you don't already have one. We'll walk through the steps to create an application and add your device so that it automatically creates a Google Cloud IoT device on first boot.

You'll then pull sample code from to start publishing telemetry events to the Google Cloud platform.

Let's get started!

IoT Core Setup on GCP

Google Cloud Platform Account

Before you set up Google IoT Core, you'll need to create a Google account to log into the Google Cloud Platform, if you don't already have one. Once an account is created successfully, you will be able to run the gcloud commands below.

Install the ​Google Cloud Command Line Tool

Follow these instructions to install the Google command line tool. After the Google command line tool is installed, run the commands below:

  • Installing the components: gcloud components install beta

  • Authenticate with Google Cloud: gcloud auth login

  • Create cloud project (choose your unique project name): gcloud projects create ​YOUR_PROJECT_NAME

  • Set default values for gcloud: gcloud config set project ​YOUR_PROJECT_NAME

  • Create PubSub topic for device data: gcloud beta pubsub topics create ​ <iot-topic>

  • Create PubSub subscription for device data: gcloud beta pubsub subscriptions create --topic ​ <iot-topic>​ ​ <iot-subscription>

  • Create device registry: gcloud beta iot registries create ​<iot-registry>​ --region us-central1 --event-pubsub-topic=​<iot-topic>

Create Service Account:
  • Navigate to the IAM & admin page in the GCP Console and make sure your project is selected.
  • Open the Service Accounts page.
  • Click Create Service Account.
  • Enter a service account name and service account ID
  • Select the Pub/Sub project role
  • Click Save.

balena setup

After you've created a balena account, follow the balena getting started guide to setup your account and create an application. Don't add a device just yet - we'll get to that after creating the environment variables to automatically create a device on Google Cloud IoT!

Create environment variables

Select your application and click “Environment Variables” in the menu. Click on “Add variable” to create the following keys and their matching values:

  • GOOGLE_IOT_PROJECT - Enter the Project ID for your GCP Project, which you can find on the GCP home page
  • GOOGLE_IOT_REGION - Enter the GCP region you selected above (us-central1)
  • GOOGLE_IOT_REGISTRY - Enter the device registry name you've selected above (my-registry)
  • GOOGLE_IOT_SERVICE_JSON - Paste the entire content of the credentials json file you've downloaded above as value
Add a new device

You're now ready to provision a balena device and push code to your application. Once it boots, the device will automatically register itself with Google Cloud IoT, and allow you to push telemetry data to the pubsub channel you've created.

Follow the steps in the balena getting started guide to download balenaOS and provision a device to your application.

Deploy IoT sample code to the balena application

Now that you have a device or two connected to your balena application, let's deploy the sample code and start sending telemetry to GCP!

On your local machine, run the following command in a terminal or your preferred git client to clone the IoT sample code:

$ git clone

Now change directory to the sample code folder:
$ cd google-iot

And add your balena application's remote repository to your local repository configuration:
$ git remote add balena <USERNAME><USERNAME>/<APPNAME>.git (you can also copy this from the balena dashboard).

Now push the sample code to your balena application's remote repository using $ git push balena master. You'll know your code has been successfully compiled and built when the friendly unicorn mascot appears in the terminal of your local development machine.

It should only take a few minutes to build your code, and subsequent builds will be quicker because of build caching. Your application will now be downloaded and executed by all the devices you have connected in your application fleet!

Viewing published messages

Once the device is online, the sample app will start pushing event messages with the CPU load and memory usage for the device, which will be visible in the logs viewer in the balenaCloud dashboard.

You can retrieve and view published messages from Pub/Sub using the gcloud CLI:

  • Go to the GCP Pub/Sub page and click on my-device-events topic.
  • Click Create Subscription in the top toolbar. Enter my-subscription as the subscription name.
  • Click Create.

You can then view the messages by running the following command in your terminal, replacing PROJECT_ID with your project ID:

gcloud pubsub subscriptions pull --limit 100 --auto-ack projects/PROJECT_ID/subscriptions/my-subscription

Next steps

Build your own application using this sample app, or the Google samples for C, Java, NodeJS and Python available at

When building your app, or using one of the samples, use the private key available at /data/rsa-priv.pem, and GOOGLE_IOT_REGION, GOOGLE_IOT_PROJECT and GOOGLE_IOT_REGISTRY environment variables to configure your client.

An overview of Google's cloud services that can be used to ingest, transform, and run analytics on the data is available at:

See more in the balena docs, and let us know what you build!

comments powered by Disqus
Terms of Service | Privacy Statement | Master agreement | Copyright 2019 Balena | All Rights Reserved