Powered By GitBook
Introduction to IoT Core
Create an IoT Core device registry, add a device, and connect.
Take the interactive version of this tutorial, which runs in the Cloud Console:
​​
​
​
</walkthrough-alt>

Introduction

​IoT Core is a fully managed service for connecting and managing IoT devices. This tutorial uses the gcloud command-line tool to create an IoT Core device registry, add a device, and run an MQTT sample to connect a device and publish device telemetry events.

Project setup

Google Cloud organizes resources into projects. This allows you to collect all of the related resources for a single application in one place.
Begin by creating a new project or selecting an existing project for this tutorial.
</walkthrough-project-billing-setup>
For details, see Creating a project.

Using Cloud Shell

In this tutorial, you do all of your work in Cloud Shell, which is a built-in command-line tool for the Cloud Console.
Open Cloud Shell by clicking the </walkthrough-cloud-shell-icon>Activate Cloud Shell button in the navigation bar in the upper-right corner of the console.

Enable the IoT Core API

To use the IoT Core API, you must first enable it.
Use the following to enable the API:

Create your first Pub/Sub topic

A Pub/Sub topic is a named resource to which devices send messages. Create your first topic with the following command:
1
gcloud pubsub topics create my-topic
Copied!
You will send several messages to this topic later.

Create a subscription to the device's topic

Run the following command to create a subscription, which allows you to view the messages published by your device:
1
gcloud pubsub subscriptions create \
2
projects/{{project_id}}/subscriptions/my-subscription \
3
--topic=my-topic
Copied!

Clone the IoT Core Node.js sample files from GitHub

You use the MQTT sample to send messages to IoT Core.
Clone the sample program with the following command:
1
git clone https://github.com/googleapis/nodejs-iot.git
Copied!

Grant permission to the IoT Core service account

In this section, you use a helper script to add the [email protected] service account to the Pub/Sub topic with the Publisher role.
1
cd nodejs-iot/samples
Copied!

Install the dependencies:

1
npm install
Copied!

Run the helper script:

1
node scripts/iam.js my-topic
Copied!
The script grants permission to the IoT Core service account on the my-topic topic.

Create a device registry

A device registry contains devices and defines properties shared by all of the contained devices. Create your device registry with the following command:
1
gcloud iot registries create my-registry \
2
--project={{project_id}} \
3
--region=us-central1 \
4
--event-notification-config=topic=projects/{{project_id}}/topics/my-topic
Copied!

Generate your signing keys

To authenticate to IoT Core, a device needs a private key and a public key. Generate your signing keys by running the following command:
1
./scripts/generate_keys.sh
Copied!
This script creates RS256 and ES256 keys in PEM format, but you'll only need the RS256 keys for this tutorial. The private key must be securely stored on the device and is used to sign the authentication (JWT (JSON Web Token)). The public key is stored in the device registry.

Create a device and add it to the registry

Run the following command to create a device and add it to the registry:
1
gcloud iot devices create my-node-device \
2
--project={{project_id}} \
3
--region=us-central1 \
4
--registry=my-registry \
5
--public-key path=rsa_cert.pem,type=rs256
Copied!

Download root credentials

Download Google's CA root certificate and note the location where you downloaded it. You'll need the file path when you run the Node.js command in the next step.

Connect your device and publish messages

In this section, you send messages from a virtual device to Pub/Sub.
1
cd mqtt_example
Copied!

Install the Node.js dependencies

1
npm install
Copied!

Connect a virtual device to Cloud IoT Core using the MQTT bridge

1
node cloudiot_mqtt_example_nodejs.js \
2
mqttDeviceDemo \
3
--cloudRegion=us-central1 \
4
--projectId={{project_id}} \
5
--registryId=my-registry \
6
--deviceId=my-node-device \
7
--privateKeyFile=../rsa_private.pem \
8
--serverCertFile=../roots.pem \
9
--numMessages=25 \
10
--algorithm=RS256 \
11
--mqttBridgePort=443
Copied!
The output shows that the virtual device is publishing messages to the telemetry topic. 25 messages are published.

Pull published messages

Pull the messages published by the device with the following command:
1
gcloud pubsub subscriptions pull --auto-ack \
2
projects/{{project_id}}/subscriptions/my-subscription
Copied!
Running this command returns the messages published by the device. The messages have the following data, my-registry/my-node-device-payload-[INTEGER], a MESSAGE_ID, and an ATTRIBUTES list of information about the device. The MESSAGE_ID is a unique ID assigned by the server.
Note: Pub/Sub doesn't guarantee the order of the messages. It is also possible that you'll see only one message in Cloud Shell. In that case, run the same command multiple times to see the other messages.

View resources in the Cloud Console

This concludes the gcloud command-line tutorial, but you can also use the Cloud Console to view the resources you just created.
Open the Navigation menu in the upper-left corner of the console, and then select IoT Core.
You can also use this graphical user interface to create and manage device registries and devices.

Conclusion

Congratulations! You just walked through the basic concepts of Cloud IoT Core using the gcloud command-line tool, and you used the Cloud Console to view IoT Core resources. The next step is to create awesome applications! For more information, see the IoT Core documentation.

Here's what you can do next

View more IoT Core samples on GitHub in any of several programming languages:
Last modified 6mo ago