Unparalleled suite of productivity-boosting Web APIs & cloud-based micro-service applications for developers and companies of any size.

APIAutomationJavascript

Ultimate Guide To Google Cloud IoT Core And APIs With Node.js

google cloud iot core and apilayer

The Internet of Things, or IoT for short, is the process of connecting things or devices to the Internet via network devices or routers to facilitate data exchange and communication between them as well as remote access. This method is also self-sufficient, meaning it can operate any device without human intervention.

Google Cloud architecture, sometimes called GCP, uses the same internal architecture as Google services and consumer products, such as YouTube and Google Search, to offer various cloud computing services for intelligent transformation. Under GCP, Google provides a range of services, including:

  1. Google BigQuery Service
  2. Google App Engine
  3. Google Cloud DataLab
  4. Google Cloud DataFlow
  5. Google Cloud SQL

Google’s IoT space includes a crucial service called Google Cloud IoT Core. Using Google Cloud IoT API, a fully managed service, we can easily and securely connect, monitor, and collect data from devices connected to the Internet. Furthermore, it allows for real-time collection, processing, organization, and display of IoT data for other services on the Google Cloud Platform.

What is Google Cloud IoT Core?

Google Cloud IoT Core is a fully managed service that allows you to easily and securely connect, manage, and ingest data from millions of globally dispersed devices.

Cloud IoT Core, in combination with other services on the Google Cloud IoT platform, provides a complete solution for collecting, processing, analyzing, and visualizing IoT data in real-time to support improved operational efficiency.

 

google cloud iot core platform connection between devices and provisioner
Service Components and Data Flow of Google Cloud IoT Core. Image Source: Google QwikLabs.

What are the Components of Google Cloud Platform IoT?

One of the standout features of Google Cloud Platform (GCP) is its specialized offerings for Big Data, Artificial Intelligence, and IoT PLATFORM. GCP’s serverless architecture is particularly advantageous for scalability, making it an ideal choice for projects with big store data needs where growth and adaptability are key concerns. Moreover, it incorporates services for massive amounts of data analytics, API management, and cloud computing. Below are some key GCP IoT Core components used for IoT solutions:

BigQuery: 

This is a cloud-based Infrastructure-as-a-Service (IaaS) model developed by Google for storing and processing large datasets via SQL queries. Unlike transactional databases such as MySQL and MongoDB, BigQuery is one of the best alternative solutions, and it serves as a highly scalable analytical database.

Cloud IoT Core:

This fully managed service enables secure and straightforward connection, management, and massive amounts of data ingestion from internet-connected devices. It integrates with other Cloud Platform services to allow real-time collection, processing, management, and visualization of IoT data. 

Additionally, it supports Pub/Sub, an asynchronous service-to-service communication method suited for serverless and microservices architectures. This model ensures immediate data receipt by all subscribers of a topic, facilitating event-driven architectures.

Hardware: 

Google provides a version of Android tailored for IoT devices, known as Android Things. This platform allows developers to seamlessly manage and deploy software updates to devices’ operating systems.

Cloud Functions:

These are used to route data upon its entry into the Pub/Sub topics with familiar capabilities. A Cloud Function is essentially a piece of JavaScript code triggered by specific events, running autonomously without the need for infrastructure management, as Google handles this.

How to Get Started with Google Cloud IoT Core?

 

Before you begin, here are some steps you need to prepare:

 

1. Select or create a Cloud Platform project.

 

First, navigate to this URL, to start your project: https://console.cloud.google.com/project

 

creating a new project in google cloud

 

Once you have created your project, take note of the auto-generated ID. We will need it for the next sections:

 

auto generated id when creating new account on google cloud platform gcp

 

2. Enable billing for your project.

 

In the Cloud Console, click the Navigation menu > Billing.

 

Billing option in google cloud platform gcp

 

Set the billing account for your project:

 

setting the billing account for creating the first project on google cloud platform

 

If you enabled your billing successfully, you will be redirected to this dashboard:

 

dashboard of google cloud iot platform after billing setup

 

Congratulations, you enabled billing for your account!

 

3. Enable the Google Cloud Internet of Things (IoT) Core API.

 

After that, visit the following URL to enable the Google Cloud Internet of Things (IoT) Core API:

 

https://console.cloud.google.com/flows/enableapi?apiid=cloudiot.googleapis.com, or  simply search for “iot” on the Google Cloud Console:

 

enabling the google cloud iot core api console

 

Next, register your application for Google Cloud Platform IoT Core:

 

registering the fist project on google cloud platform

 

You have enabled the IoT Core API!

 

google cloud iot core api enebled

 

 

How to Activate Google Cloud Shell?

 

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

 

First, in the Google Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

 

activating the google cloud shell

 

Cloud Shell provides command-line access to your Google Cloud resources.

 

 

For the first command, you can list the active account name with this command:

 

 

When you want to execute your first command, this window will show up. Click “Authorize”:

 

gcloud requesting credentials to make a gcp api call to authorize google cloud shell

 

Be patient! It takes a few moments to connect to the environment. When you connect and receive authorization, the project is set to your PROJECT_ID. For example:

 

 

List the project ID with this command:

 

 

The Project_ID is an auto-generated name that Google assigns when you first set up your project in the Google Cloud Console (see the previous section).  

 

 

 

How to Create a Pub/Sub Topic for Your Google Cloud IoT Core?

 

1. In the Cloud Console, click the Navigation menu > Pub/Sub > Topics:

 

selecting the pub/sub topic in the google cloud console from the navigation menu

 

2. Click + CREATE TOPIC from the top menu:

 

creating new topic and setting all the permissions

 

3. Give the topic the ID cloud-builds and click CREATE TOPIC:

 

giving the topic id cloud builds to create new project

 

4. If you created your Pub/Sub Topic successfully, then this dashboard will show up:

 

dashboard after successfully creating a pub/sub topic in google cloud iot core

 

 

How to Create a Device Registry for IoT Core?

 

1. First, in the Cloud Console, click Navigation menu > IoT Core.

 

creating a device registry on google cloud iot platform

 

2. Click Create Registry and set the following fields (you will need to click on the Show Advanced Options button):

 

Field Value
Registry ID apilayer-iot
Region us-central1
Cloud Pub/Sub Topics Select a Cloud Pub/Sub Topic
Select the default cloud-builds topic from the drop-down.
Click Show Advanced Options
Device State Topic Leave at the default value
Protocols MQTT only
Cloud Logging Ignore
CA Certificate Ignore

 

Here is the screenshot of the field:

 

Filling the registry and project details for google cloud

 

Advanced Options:

 

advance options in google cloud iot core platform

 

3. Finally, click Create. You should see a similar page once you have successfully created your registry:

 

creating the account after getting the registry

 

You’ve just created a device registry with a Cloud Pub/Sub topic for publishing device telemetry events! The next section is about adding a device to the registry.

 

 

How to Add a Device to the Registry?

 

1. On the IoT Core page, click on Devices in the left menu, then click + Create a Device.

 

navigating devices menu on the google iot platform integration

 

creating a new device on google cloud

 

2. Then, set the following fields (you will need to click on the COMMUNICATION, CLOUD LOGGING, AUTHENTICATION link):

 

Field Value
Device ID my-device
Click COMMUNICATION, CLOUD LOGGING, AUTHENTICATION
Device Communication Allow
Authentication Default value (for now)
Public Key Format Default value (for now)
Public Key Value Default value (for now)

 

Setting device metadata device communication cloud logging authentication link on google cloud platform

 

3. Finally, click Create.

 

You’ve just added a device to your registry! The device won’t be able to connect to Google Cloud without a valid key.

 

Final step of creating a new device on google cloud iot platform 

 

 

How to Add a Public Key to the Device?

 

For your device to transmit telemetry data through the cloud, you must add a key to the device.

 

1. In Cloud Shell, run the following command to create an RS256 key:

 

 

Table of Contents