Quickstart

This page shows you how to use Google Cloud Platform 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.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Platform Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Cloud IoT Core and Cloud Pub/Sub APIs.

    Enable the APIs

Set up your local environment and install prerequisites

  1. Install and initialize the Cloud SDK. Cloud IoT Core requires version 173.0.0 or higher of the SDK.

  2. Set up a Node.js development environment.

    Alternatively, you can use Google Cloud Shell, which comes with Cloud SDK and Node.js already installed.

Create a device registry

  1. Go to the Google Cloud IoT Core page in Cloud Platform Console.

    Go to the Google Cloud IoT Core page

  2. Click Create device registry.

  3. Enter my-registry for the Registry ID.

  4. Select us-central1 for the Cloud region.

  5. Select MQTT for the Protocol.

  6. In the Pub/Sub topic dropdown list, select Create a topic.

  7. In the Create a topic dialog, enter my-device-events in the Name field.

  8. Click Create in the Create a topic dialog.

  9. Click Create on the Cloud IoT Core page.

  10. Click Continue in the Grant permission to service account dialog.

You've just created a device registry with a Cloud Pub/Sub topic for publishing device telemetry events.

Add a device to the registry

  1. On the Registry Details page, click Add device.

  2. Enter my-device for the Device ID.

  3. Select Allow for Device communication.

  4. Select None for the Public key format. (You'll add a key in the next section.)

  5. Click Add.

You've just added a device to your registry.

Add a public key to the device

  1. Open a terminal window and run the following command to create an RS256 key:

     openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes \
        -out rsa_cert.pem -subj "/CN=unused"
    

  2. Copy the contents of rsa_cert.pem to the clipboard. Make sure to include the lines that say-----BEGIN CERTIFICATE----- and-----END CERTIFICATE-----.

  3. On the Device details page for the device you created in the preceding section, click Add public key.

  4. Select RS256_X509 for the Public key format.

  5. Paste your public key in the Public key value box.

  6. Click Add.

An 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

  1. Download or clone the Cloud IoT Core Node.js sample files from GitHub. Make sure the MQTT example files are in the same directory as the keys you created in the previous section, and complete the rest of these steps in that directory.

  2. Install the Node.js dependencies:

    npm install
    

  3. Run the following command to create a subscription to the registry's Pub/Sub topic, substituting your project ID:

    gcloud beta pubsub subscriptions create \
        projects/PROJECT_ID/subscriptions/my-subscription \
        --topic=projects/PROJECT_ID/topics/my-device-events
    

  4. Run the following command to connect a virtual device to Cloud IoT Core using the MQTT bridge, substituting your project ID:

     node cloudiot_mqtt_example_nodejs.js \
        --project_id=PROJECT_ID \
        --registry_id=my-registry \
        --device_id=my-device \
        --private_key_file=rsa_private.pem \
        --num_messages=25 \
        --algorithm=RS256
    

    The output shows that the sample device is publishing messages to the telemetry topic. Twenty-five messages are published.

  5. Run the following command to read the messages published to the telemetry topic, substituting your project ID:

    gcloud beta pubsub subscriptions pull --auto-ack \
        projects/PROJECT_ID/subscriptions/my-subscription
    

  6. Repeat the subscriptions pull command to view additional messages.

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Go to the Google Cloud Pub/Sub Topics page in Cloud Platform Console.

    Go to the Google Cloud Pub/Sub Topics page

  2. Select the checkbox next to your topic in the list, then click Delete at the top of the page.

  3. Go to the Device registries page in Cloud Platform Console.

    Go to the Device registries page

  4. Click the name of your registry in the list.

  5. Click the name of your device in the list.

  6. At the top of the Device details page, click Delete. Type the name of the device to confirm deletion.

    If you've added any other devices to this registry, delete those too.

  7. Go to the Device registries page in Cloud Platform Console.

    Go to the Device registries page

  8. Click the name of your registry in the list.

  9. At the top of the Registry details page, click Delete.

What's next

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Google Cloud Internet of Things Core