Create an IoT Core device registry
This page shows you how to use Google Cloud console to create a Cloud IoT Core device registry and register a device. It also shows you how to run a sample to connect a device and publish device telemetry events.
To follow step-by-step guidance for this task directly in the Cloud Shell Editor, click Guide me:
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Cloud IoT and Cloud Pub/Sub APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Cloud IoT and Cloud Pub/Sub APIs.
Set up your local environment and install prerequisites
Install and initialize the gcloud CLI. Cloud IoT Core requires version 173.0.0 or higher of the gcloud CLI.
Set up a Node.js development environment.
Alternatively, you can use Google Cloud Shell, which comes with gcloud CLI and Node.js already installed.
Create a device registry
- Go to the Google Cloud IoT Core page in Google Cloud console.
Click Create registry.
Enter
my-registry
for the Registry ID.If you're in the US, select us-central1 for the Region. If you're outside the US, select your preferred region.
Select MQTT for the Protocol.
In the Default telemetry topic dropdown list, select Create a topic.
In the Create a topic dialog, enter
my-device-events
in the Name field.Click Create in the Create a topic dialog.
The Device state topic and Certificate value fields are optional, so leave them blank.
Click Create on the Cloud IoT Core page.
You've just created a device registry with a Cloud Pub/Sub topic for publishing device telemetry events.
Create your credentials
Generate a device key pair
Open a terminal window and run the following multi-line command to create an RS256 key:
openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes \ -out rsa_cert.pem -subj "/CN=unused"
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 below.
In the following section, you'll add a device to the registry and associate the public key with the device.
Add a device to the registry
On the Registries page, select
my-registry
.Select the Devices tab and click Create a device.
Enter
my-device
for the Device ID.Select Allow for Device communication.
Add the public key information to the Authentication fields.
- Copy the contents of
rsa_cert.pem
to the clipboard. Make sure to include the lines that say-----BEGIN CERTIFICATE-----
and-----END CERTIFICATE-----
. - Select RS256_X509 for the Public key format.
- Paste the public key in the Public key value box.
- Click Add to associate the RS256_X509 key with the device.
- Copy the contents of
The Device metadata field is optional; leave it blank.
Click Create.
You've just added a device to your registry. The RS256_X509 key appears on the Device details page for your device.
Run a Node.js sample to connect a virtual device and view telemetry
Get the Cloud IoT Core Node.js samples from GitHub. The Cloud IoT Core samples are in the
iot
directory.git clone https://github.com/googleapis/nodejs-iot.git
In your cloned repository, navigate to the
iot/mqtt_example
directory. You'll complete the rest of these steps in this directory.cd nodejs-iot/samples/mqtt_example
Copy the private key you created in the previous section (
rsa_private.pem
) to the current directory (samples/mqtt_example
):cp ../../../rsa_private.pem .
Install the Node.js dependencies:
npm install
Run the following command to create a subscription to the registry's Pub/Sub topic, substituting your project ID:
gcloud pubsub subscriptions create \ projects/PROJECT_ID/subscriptions/my-subscription \ --topic=projects/PROJECT_ID/topics/my-device-events
Run the following command to connect a virtual device to Cloud IoT Core using the MQTT bridge, substituting your project ID. Make sure
serverCertFile
is set to the location where you downloaded Google's root certificate.node cloudiot_mqtt_example_nodejs.js \ mqttDeviceDemo \ --projectId=PROJECT_ID \ --cloudRegion=REGION \ --registryId=my-registry \ --deviceId=my-device \ --privateKeyFile=rsa_private.pem \ --serverCertFile=roots.pem \ --numMessages=25 \ --algorithm=RS256
The output shows that the sample device is publishing messages to the telemetry topic. Twenty-five messages are published.
Run the following command to read the messages published to the telemetry topic, substituting your project ID:
gcloud pubsub subscriptions pull --auto-ack \ projects/PROJECT_ID/subscriptions/my-subscription
Repeat the
subscriptions pull
command to view additional messages.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
- Go to the Google Cloud Pub/Sub Topics page in Google Cloud console.
Select the checkbox next to your topic in the list, then click Delete at the top of the page.
- Go to the Registries page in Google Cloud console.
Click the name of your registry in the list.
Click the name of your device in the list.
At the top of the Device details page, click Delete. Type the name of the device to confirm deletion.
To delete a registry, you must first delete all the devices within it. If you've added any other devices to this registry, delete those too.
- Go to the Registries page in Google Cloud console.
Click the name of your registry in the list.
At the top of the Registry details page, click Delete.