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.

You can also try out an interactive tutorial in GCP Console.

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 GCP Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to 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 GCP Console.

    Go to the Google Cloud IoT Core page

  2. Click Create a registry.

  3. Enter my-registry for the Registry ID.

  4. If you're in the US, select us-central1 for the Cloud region. If you're outside the US, select your preferred region.

  5. Select MQTT for the Protocol.

  6. In the Telemetry 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. The Device state topic and Certificate value fields are optional, so leave them blank.

  10. 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.

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"

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

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

  2. Enter my-device for the Device ID.

  3. Select Allow for Device communication.

  4. 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.
  5. The Device metadata field is optional; leave it blank.

  6. Click Add.

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

  1. 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/GoogleCloudPlatform/nodejs-docs-samples
    

  2. In your cloned repository, navigate to the iot/mqtt_example directory. You'll complete the rest of these steps in this directory.

    cd nodejs-docs-samples/iot/mqtt_example
    

  3. Copy the private key you created in the previous section (rsa_private.pem) to the current directory (iot/mqtt_example):

    cp ../../../rsa_private.pem .
    

  4. Install the Node.js dependencies:

    npm install
    

  5. 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
    

  6. 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 \
        --projectId=PROJECT_ID \
        --cloudRegion=REGION \
        --registryId=my-registry \
        --deviceId=my-device \
        --privateKeyFile=rsa_private.pem \
        --numMessages=25 \
        --algorithm=RS256
    

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

  7. 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
    

  8. 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 GCP 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 Registries page in GCP Console.

    Go to the 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.

    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.

  7. Go to the Registries page in GCP Console.

    Go to the 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

Was this page helpful? Let us know how we did:

Send feedback about...

Google Cloud Internet of Things Core