Skip to content

Latest commit

 

History

History
 
 

README.rst

Google Cloud IoT Core Python Samples

https://gstatic.com/cloudssh/images/open-btn.png

This directory contains samples for Google Cloud IoT Core. Cloud IoT Core allows developers to easily integrate Publish and Subscribe functionality with devices and programmatically manage device authorization. This example connects a device to Cloud IoT Core via MQTT, using a JWT for authentication. After connecting, the device publishes messages to the device's MQTT topic at a rate of one per second. Note that before you can run this sample, you must register a device as described in the Cloud IoT device docs. For the gateway samples, you must register and bind a device as described in the Cloud IoT gateway docs.

Setup

Authentication

This sample requires you to have authentication setup. Refer to the Authentication Getting Started Guide for instructions on setting up credentials for applications.

Install Dependencies

  1. Clone python-iot and change directory to the sample directory you want to use.

    $ git clone https://github.com/googleapis/python-iot.git
  2. Install pip and virtualenv if you do not already have them. You may want to refer to the Python Development Environment Setup Guide for Google Cloud Platform for instructions.

  3. Create a virtualenv. Samples are compatible with Python 3.6+.

    $ virtualenv env
    $ source env/bin/activate
  4. Install the dependencies needed to run the samples.

    $ pip install -r requirements.txt

Samples

MQTT example

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python cloudiot_mqtt_example.py

usage: cloudiot_mqtt_example.py [-h] --algorithm {RS256,ES256}
                                [--ca_certs CA_CERTS]
                                [--cloud_region CLOUD_REGION] [--data DATA]
                                --device_id DEVICE_ID
                                [--gateway_id GATEWAY_ID]
                                [--jwt_expires_minutes JWT_EXPIRES_MINUTES]
                                [--listen_dur LISTEN_DUR]
                                [--message_type {event,state}]
                                [--mqtt_bridge_hostname MQTT_BRIDGE_HOSTNAME]
                                [--mqtt_bridge_port {8883,443}]
                                [--num_messages NUM_MESSAGES]
                                --private_key_file PRIVATE_KEY_FILE
                                [--project_id PROJECT_ID] --registry_id
                                REGISTRY_ID
                                [--service_account_json SERVICE_ACCOUNT_JSON]
                                {device_demo,gateway_send,gateway_listen} ...

Example Google Cloud IoT Core MQTT device connection code.

positional arguments:
  {device_demo,gateway_send,gateway_listen}
    device_demo         Connects a device, sends data, and receives data.
    gateway_send        Sends data from a gateway on behalf of a device that
                        is bound to it.
    gateway_listen      Listens for messages sent to the gateway and bound
                        devices.

optional arguments:
  -h, --help            show this help message and exit
  --algorithm {RS256,ES256}
                        Which encryption algorithm to use to generate the JWT.
  --ca_certs CA_CERTS   CA root from https://pki.google.com/roots.pem
  --cloud_region CLOUD_REGION
                        GCP cloud region
  --data DATA           The telemetry data sent on behalf of a device
  --device_id DEVICE_ID
                        Cloud IoT Core device id
  --gateway_id GATEWAY_ID
                        Gateway identifier.
  --jwt_expires_minutes JWT_EXPIRES_MINUTES
                        Expiration time, in minutes, for JWT tokens.
  --listen_dur LISTEN_DUR
                        Duration (seconds) to listen for configuration
                        messages
  --message_type {event,state}
                        Indicates whether the message to be published is a
                        telemetry event or a device state message.
  --mqtt_bridge_hostname MQTT_BRIDGE_HOSTNAME
                        MQTT bridge hostname.
  --mqtt_bridge_port {8883,443}
                        MQTT bridge port.
  --num_messages NUM_MESSAGES
                        Number of messages to publish.
  --private_key_file PRIVATE_KEY_FILE
                        Path to private key file.
  --project_id PROJECT_ID
                        GCP cloud project name
  --registry_id REGISTRY_ID
                        Cloud IoT Core registry id
  --service_account_json SERVICE_ACCOUNT_JSON
                        Path to service account json file.