Demo: MQTT gateway hub

This documentation is still in development - use at your own risk

This page provides a Python script that demonstrates how gateways work. The script first creates a demo registry, a gateway, and a device. Then it binds the device to the gateway, listens for configuration messages, and sends state data on the device's behalf. Finally, the script automatically unbinds the device, deletes the device and gateway, and cleans up the registry.

Features

The script demonstrates the following gateway features:

  • Creating a gateway

  • Creating a device to bind to the gateway

  • Binding the device to the gateway

  • Listening for configuration messages

  • Sending state data

  • Unbinding the device from the gateway

  • Deleting the device then deleting the gateway

Objectives

After running this script, you'll understand the code that performs the following:

  • Creating a registry

  • Creating a gateway and a device to bind

  • Binding the gateway to the device

  • Listening for configuration messages

Pricing

The amount data transmitted in this demo falls within the free usage tier. See Pricing for more information.

Before you begin

This demonstration assumes that you're familiar with Python and that you've reviewed the gateways overview.

Before you can run this sample, you need to get a project ID, the Google Cloud CLI, and check that billing is enabled:

  1. Create a new console project or retrieve the project ID of an existing project from the Google Cloud console:

    Go to the Projects page

  2. Install and then initialize the Google Cloud CLI:

    Download the SDK

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

    Learn How to Enable Billing

Download the script

Download the demo script and set your current directory:

git clone https://github.com/GoogleCloudPlatform/python-docs-samples cd python-docs-samples/iot/api-client/mqtt_example

Install dependencies

From within the iot/api-client/mqtt_example directory, install the dependencies needed to run the example:

pip install -r requirements.txt --user

For more information on setting up your Python development environment, such as installing pip on your system, see the Python Development Environment Setup Guide.

Create your credentials

Before running the example, complete the following steps:

  1. Create a service account:

    1. In the console, go to the Service Accounts page.

      Go to Service Accounts

    2. Select your project.

    3. Click addCreate Service Account.

    4. In the Service account name field, enter a name. The console fills in the Service account ID field based on this name.

    5. Optional: In the Service account description field, enter a description for the service account.

    6. Click Create and continue.

    7. Click the Select a role field and select Basic > Editor.

    8. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  2. Download a JSON key for the service account you just created:

    1. In the console, click the email address for the service account that you created.

    2. Click Keys.

    3. Click Add key, then click Create new key.

    4. Click Create. A JSON key file is downloaded to your computer.

      Save this key in the iot/api-client/mqtt_example directory, and rename it service_account.json.

    5. Click Close.

  3. Download Google's CA root certificate into the same directory as the example files. You can optionally set the location of the certificate with the --ca_certs flag.

Add project ID and credentials to the script

  1. In the script, set the GOOGLE_CLOUD_PROJECT environment variable to your project ID.

  2. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to service_account.json.

Run the script locally

In the project subdirectory python-docs-samples/iot/api-client/mqtt_example, run the script by invoking the command:

python gateway_demo.py

 

When the script runs, it writes the output to the terminal.

gateway_demo.py walkthrough

This section describes what happens in each step of the script.

First, the script creates a temporary device registry, which will contain both the gateway and the device that uses the gateway to communicate with ClearBlade IoT Core:

The following code in the registry management sample is called by the script:

Next, the script creates a gateway and adds it to the registry:

The following code in the device management sample is called by the script:

Now, the script creates a device in the same registry as the gateway:

The following code is called by the script:

After the device and gateway are in the demo registry, the script can then bind the device to the gateway. Binding the device enables the gateway to attach and detach the device over the gateway's connection to the ClearBlade IoT Core protocol bridge.

The following code is called by the script:

After the device is bound to the gateway, the script pauses so you can set configuration data for the gateway or bound device.

To set the configuration data:

  1. Navigate to the Google Cloud console.

  2. Set configuration data for the gateway or bound device.

  3. Verify that the gateway and device are receiving the latest configuration before the gateway starts listening.

After you set the configuration data and continue the script, the gateway connects to the ClearBlade IoT Core protocol bridge, attaches the device, then receives configuration messages for the device.

To listen for configuration messages, the script calls a helper function from the MQTT sample as follows:

The following code is called by the script:

When the script runs, it writes the configuration messages to a log that can be tailed to transmit the configurations to the attached devices.

After demonstrating how you can receive configuration messages on behalf of a gateway and its bound device, the script sends state data on behalf of the bound device. To do this, the script calls a helper function from the MQTT sample:

The following code is called by the script:

 

The script pauses after transmitting state data on behalf of the bound device so you can navigate to the console to see the state data before the demo registry, gateway, and device are removed in the next step.

When the script finishes, it frees up the gateway, device, and registry that were allocated at the beginning.

The device is unbound from the gateway before it is deleted:

The script calls a helper function to delete both the bound device and the gateway:

Finally, the script calls a helper function to delete the registry: