arrow_back

APIs Explorer: PubSub and IoT

Join Sign in

APIs Explorer: PubSub and IoT

1 hour 5 Credits

GSP284

Google Cloud self-paced labs logo

Overview

The Google APIs Explorer is a tool that helps you explore various Google APIs interactively. With the APIs Explorer, you can:

  • Browse quickly through available APIs and versions.
  • See methods available for each API and what parameters they support along with inline documentation.
  • Execute requests for any method and see responses in real time.
  • Make authenticated and authorized API calls.
  • Search across all services, methods, and your recent requests to quickly find what you are looking for.

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. In this lab you'll build a simple but complete IoT system using Cloud IoT Core and Pub/Sub.

The devices in this system publish temperature data to their telemetry feeds, and a server consumes the telemetry data from a Cloud Pub/Sub topic. The server then decides whether to turn on or off the individual devices' fans, via a Cloud IoT Core configuration update. The device will respond to configuration changes from a server based on real-time data.

To accomplish this, you will create Pub/Sub topics and subscriptions as well as IoT registries and devices. You will configure and deploy these services with the Google APIs Explorer. The following model illustrates how the system's components are interconnected:

18c36a1229a5aaee.png

Objectives

In this lab, you will:

  • Create a Pub/Sub topic and subscription with the APIs Explorer.
  • Create an IoT registry and add a device to it.
  • Provision a device and transmit telemetry data from it.
  • Control a device using a server based on a telemetry stream.

Prerequisites

This is an advanced level lab. You should be familiar with the basic functioning and architecture of APIs. Experience with Cloud Shell and command line interface tools is recommended.

Familiarity with the the APIs Explorer tool, Cloud IoT, and Cloud Pub/Sub is recommended, so please at a minimum take the following labs before attempting this one:

Once you're ready, scroll down and follow the steps below to get your lab environment set up.

Setup and Requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

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

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:

gcloud auth list

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Download the Sample Code

In Cloud Shell run the following command to clone the codebase that contains the telemetry server:

git clone https://github.com/GoogleCloudPlatform/python-docs-samples

Enter the following command to cd into the sample folder:

cd python-docs-samples/iot/api-client/end_to_end_example

Python virtual environments are used to isolate package installation from the system.

apt-get install -y virtualenv Note: If prompted [Y/n], press Y and then Enter. If you get Permission denied error, then run the command sudo apt-get install -y virtualenv. python3 -m venv venv

Activate the virtual environment.

source venv/bin/activate

Install the sample dependencies:

pip install -r requirements.txt

Check that you have installed the Python dependencies correctly by running all the Python scripts without passing any parameters:

python cloudiot_pubsub_example_mqtt_device.py python cloudiot_pubsub_example_server.py

If the dependencies installed successfully, the programs will print their respective usage messages. This is an example of a successful installation:

gcpstaging20065_student@qwiklabs-gcp-4f438a9a5e0b68c9:~/python-docs-samples/iot/api-client/end_to_end_example$ python cloudiot_pubsub_example_mqtt_device.py usage: cloudiot_pubsub_example_mqtt_device.py [-h] --project_id PROJECT_ID --registry_id REGISTRY_ID --device_id DEVICE_ID --private_key_file PRIVATE_KEY_FILE --algorithm {RS256,ES256} [--cloud_region CLOUD_REGION] [--ca_certs CA_CERTS] [--num_messages NUM_MESSAGES] [--mqtt_bridge_hostname MQTT_BRIDGE_HOSTNAME] [--mqtt_bridge_port MQTT_BRIDGE_PORT] [--message_type {event,state}] cloudiot_pubsub_example_mqtt_device.py: error: the following arguments are required: --project_id, --registry_id, --device_id, --private_key_file, --algorithm (env) gcpstaging20065_student@qwiklabs-gcp-4f438a9a5e0b68c9:~/python-docs-samples/iot/api-client/end_to_end_example$ python cloudiot_pubsub_example_server.py usage: cloudiot_pubsub_example_server.py [-h] --project_id PROJECT_ID --pubsub_subscription PUBSUB_SUBSCRIPTION [--service_account_json SERVICE_ACCOUNT_JSON] cloudiot_pubsub_example_server.py: error: the following arguments are required: --project_id, --pubsub_subscription

You can safely ignore any errors that resemble cloudiot_pubsub_example_mqtt_device.py: error: argument --project_id is required

Create a Pub/Sub Topic

To access the Pub/Sub APIs Explorer tool to use the projects.topics.create method, open this link in a new tab.

You will see a "Try this API" panel on the right of the screen.

Now fill out the method so that the name field matches the following, replacing <YOUR-QWIKLABS-PROJECT> with your Project ID for this lab:

projects/<YOUR-QWIKLABS-PROJECT>/topics/pubsub-topic

Your method should now resemble the following:

8d4c3b341a79a40b.png

Make sure that there are no trailing spaces in the name field. Click the Execute button, choose your Qwiklabs student account and click Allow.

Your response should resemble the following:

200 - Show headers - { "name": "projects/qwiklabs-gcp-a50ec278400a3be5/topics/pubsub-topic" }

The devices in this system publish temperature data to their telemetry feeds, and the server will consume telemetry data from the Cloud Pub/Sub topic you just created.

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Create a Pub/Sub Topic.

Close this browser tab once you have verified the above output.

Create a Pub/Sub Subscription

To access the Pub/Sub APIs Explorer tool to use the projects.subscriptions.create method, open this link in a new tab.

You will see a "Try this API" panel on the right side of the screen.

Now fill out the method so that the name field matches the following, replacing <YOUR-QWIKLABS-PROJECT> with your Qwiklabs project ID:

projects/<YOUR-QWIKLABS-PROJECT>/subscriptions/iot-sub

Click inside the curly braces of the request body and select topic from the dropdown menu. Then click inside the quotations for topic and add the following, replacing <YOUR-PROJECT-ID> with your Google Cloud project ID:

projects/<YOUR-PROJECT-ID>/topics/pubsub-topic

Your method should now resemble the following:

72bbfc47064e1ff0.png

Make sure that there are no trailing spaces in the name field. Click the Execute button. Your response should resemble the following:

200 - Show headers - { "name": "projects/qwiklabs-gcp-00-2a912aa77c89/subscriptions/iot-sub", "topic": "projects/qwiklabs-gcp-00-2a912aa77c89/topics/pubsub-topic", "pushConfig": {}, "ackDeadlineSeconds": 10, "messageRetentionDuration": "604800s", "expirationPolicy": { "ttl": "2678400s" } }

This subscription is a direct channel to the Pub/Sub topic where the telemetry data is published and it allows your device to pull messages directly from pubsub-topic.

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Create a Pub/Sub Subscription.

You can now close the Pub/Sub APIs Explorer tab.

View the topic and subscription in the Console

Return to the Cloud Console and from the Navigation menu under the Big Data header click Pub/Sub > Topics. You should see the pubsub-topic you created:

c6e4313e04c9d8a8.png

From the left-hand menu, select Subscriptions. You should see the iot-sub subscription that you created:

dab5da49799ab4bf.png

Now that you have your Pub/Sub topic and subscription created, you will build a registry to contain your IoT device.

Create an IoT Registry

In your Cloud Shell session, run the following command to create an IoT registry named iot-registry:

gcloud iot registries create iot-registry \ --region=us-central1 --event-notification-config=topic=pubsub-topic

From the Navigation menu under the Big Data header click IoT Core. This should take you to the registries page, and you should see the iot-registry you just created:

d65eb583da7f6cc2.png

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Create an IoT Registry (region: us-central1).

Generate RSA keys and create a device

Now generate RSA public and private keys that will be used for authenticating your virtual device when it connects.

In Cloud Shell, verify that you are still in the /python-docs-samples/iot/api-client/end_to_end_example directory.

Run the following command to generate RSA keys:

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

Run the following command to view the contents of the key:

nano rsa_public.pem

You should get a similar output:

-----BEGIN CERTIFICATE----- MIIC+DCCAeCgAwIBAgIJAP1GA1sC2yFmMA0GCSqGSIb3DQEBCwUAMBExDzANBgNV BAMMBnVudXNlZDAeFw0xODEwMjUxNzQ3NDRaFw0yODEwMjIxNzQ3NDRaMBExDzAN BgNVBAMMBnVudXNlZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKSQ RUUwhJAxFSpkz2V4/6eR3XIwi9eXlc/qWlZnxlOWdCOuyoMXQZZs4DzlOI1fCbKg opa3zUfoz9QdlShJP9e4gXgqWlrxDlJdPT4ixsaeWk3E9oE22smpGCea8ZvhcZF/ NFow9i3D2j+DihvELuYPROxBP2keg3wZiEBo8U5imxf81x00+sTPQ0v5ArqvTW3P GBU2DlZ6MhwmgilmKGc4Q6pUSQzWYYbsmkT5/VMO1cTVSYKJN7G/zQRwbMFfAslI R4bvC8jdibnw2oIQoAjPS+I7gaeGLSFMkYGkg9BNB5RasHOszOMedvqu8QjYcpgO VXNB8JJv8e48wFygmfMCAwEAAaNTMFEwHQYDVR0OBBYEFF+XBeH7OF7E65fcfj00 Hg0rNFGEMB8GA1UdIwQYMBaAFF+XBeH7OF7E65fcfj00Hg0rNFGEMA8GA1UdEwEB /wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAI/sijZDeIOJ7+ffs4IBgBVTekCN nU3NZ9H6bxTItb8dOIzUKBGW80p3VJtjqjixaJQl2JLfit4kCU89IPq1glPY9oab GaoikrvF3kgxJRC7H+vtVGKmu3bH12iTAqVgh9hjuGIyB+CWGJExfumTs7dcWF1+ GT5NeQKdmak4mzx/POVwqE/NY+UVoRt9rOduv6UZyBc+X0516hOcI5omrPKh/7c6 hknHidsKzTK1oxSPUD2OCQjuKz55ugYVpEUu/TvvUEx26YbWFkIjrlGWyyzaVjEs UL4HuPT51u4rdNC2TSqcTyQW/m6i3wuVzfqr9jhGFFB7amDIdRX4bJ8qx5Q= -----END CERTIFICATE-----

Then exit the nano editor by holding CTRL + X.

Now run the following command to create a device called my-device:

gcloud iot devices create my-device --region=us-central1 \ --registry=iot-registry \ --public-key path=rsa_public.pem,type=rs256

Alternatively you can create my-device using below steps. Skip this step, if you have already created my-device

  • Now in the Console click on iot-registry. In the left panel click Devices then click Create a Device.

  • Device ID = my-device

  • Click Create.

Click on iot-registry and then click Devices in the left panel and you should see your newly created device my-device.

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Create a Device.

Start the Virtual Server

Now that you have created all of the Cloud resources, connect your device and communicate with it via Pub/Sub, simulating a device and server.

Run the following command to open the cloudiot_pubsub_example_server.py file with the nano text editor:

nano cloudiot_pubsub_example_server.py

Scroll down and replace the code used to generate service account credentials from a provided JSON file to instead use the built-in credentials for Compute Engine, which is running under the hood of the Cloud Shell.

Replace the following code:

def __init__(self, service_account_json): credentials = service_account.Credentials.from_service_account_file( service_account_json).with_scopes(API_SCOPES)

With this:

def __init__(self, service_account_json): from google.auth import compute_engine credentials = compute_engine.Credentials()

Then press CTRL+X → Y → ENTER to save the file and exit the text editor.

Now that you have changed the server to use the Compute Engine credentials, start the server by running the following command, replacing <YOUR-PROJECT-ID> with your Project ID:

python cloudiot_pubsub_example_server.py \ --project_id="<YOUR-PROJECT-ID>" \ --pubsub_subscription=iot-sub

When the server starts, you will see the message Listening for messages on projects/your-project-id/subscriptions/iot-sub, which indicates the server is running.

Start the Virtual Device and Observe

Add a new tab to your Cloud Shell by clicking the + icon on the Cloud Shell ribbon.

In the new Cloud Shell tab, run the following to navigate to the device sample folder and initialize your virtual environment:

cd python-docs-samples/iot/api-client/end_to_end_example source venv/bin/activate

Retrieve the latest root certificate from Google:

curl pki.goog/roots.pem -o roots.pem

Now, connect the virtual device using the private key, registry ID, device ID, and so on by running the following command, replacing <YOUR-PROJECT-ID> with your Project ID:

python cloudiot_pubsub_example_mqtt_device.py \ --registry_id iot-registry \ --device_id my-device \ --project_id "<YOUR-PROJECT-ID>" \ --private_key_file rsa_private.pem \ --mqtt_bridge_port 443 \ --algorithm RS256 \ --ca_certs roots.pem \ --cloud_region us-central1

When you connect the device, it will show and report its temperature, which increases when the fan is turned off. If the fan is enabled, the virtual device's temperature will decrease. Because the device is controlled from the server, which is analyzing the stream of incoming sensor data and making this decision for it, the device does not need to be aware of the conditions for enabling or disabling its fan.

The following section shows the output of the server that is subscribed to the telemetry events from the device:

Published message acked. ('Publishing payload', '{"temperature": 0}') Published message acked. ('Publishing payload', '{"temperature": -1}') Published message acked. Received message '{"fan_on": false}' on topic '/devices/my-device/config' with Qos 1 Fan turned off. ('Publishing payload', '{"temperature": 0}') Published message acked. ('Publishing payload', '{"temperature": 1}') Published message acked. ('Publishing payload', '{"temperature": 2}')

Test your Understanding

Below are a multiple choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.

Congratulations!

You have now set up a virtual device and are successfully transmitting telemetry data and receiving configuration changes. At this point you have a solid understanding of Google Cloud IoT Core, Pub/Sub, and how you can provision their services through the APIs Explorer.

Finish Your Quest

83147878eab81187.png API_Ex_badge.png

This self-paced lab is part of the Qwiklabs IoT in the Google Cloud and Exploring APIs Quests. A Quest is a series of related labs that form a learning path. Completing a Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in a Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Next Steps / Learn More

Be sure to check out the following labs for more practice with APIs:

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated May 6, 2022
Lab Last Tested May 6, 2022

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.