.. This file is automatically generated. Do not edit this file directly.

Google Cloud IoT Core Python Samples
===============================================================================

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
   :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-iot&page=editor&open_in_editor=iot/api-client/mqtt_example/README.rst


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 <https://cloud.google.com/iot/docs/how-tos/devices>`_.  For the gateway samples, you must register and bind a device as described in the  `Cloud IoT gateway docs <https://cloud.google.com/iot/docs/how-tos/gateways#setup>`_.




.. _Google Cloud IoT Core: https://cloud.google.com/iot/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.

.. _Authentication Getting Started Guide:
    https://cloud.google.com/docs/authentication/getting-started

Install Dependencies
++++++++++++++++++++

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

    .. code-block:: bash

        $ git clone https://github.com/googleapis/python-iot.git

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

   .. _Python Development Environment Setup Guide:
       https://cloud.google.com/python/setup

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

    .. code-block:: bash

        $ virtualenv env
        $ source env/bin/activate

#. Install the dependencies needed to run the samples.

    .. code-block:: bash

        $ pip install -r requirements.txt

.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/

Samples
-------------------------------------------------------------------------------

MQTT example
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
   :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-iot&page=editor&open_in_editor=iot/api-client/mqtt_example/cloudiot_mqtt_example.py,iot/api-client/mqtt_example/README.rst




To run this sample:

.. code-block:: no-highlight

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





.. _Google Cloud SDK: https://cloud.google.com/sdk/