|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +"""Python sample for connecting to Google Cloud IoT Core via MQTT, using JWT. |
| 17 | +This example connects to Google Cloud IoT Core via MQTT, using a JWT for device |
| 18 | +authentication. After connecting, by default the device publishes 100 messages |
| 19 | +to the device's MQTT topic at a rate of one per second, and then exits. |
| 20 | +Before you run the sample, you must follow the instructions in the README |
| 21 | +for this sample. |
| 22 | +""" |
| 23 | + |
| 24 | +# [START iot_mqtt_includes] |
| 25 | +import argparse |
| 26 | +import datetime |
| 27 | +import os |
| 28 | +import ssl |
| 29 | +import time |
| 30 | + |
| 31 | +import jwt |
| 32 | +import paho.mqtt.client as mqtt |
| 33 | +# [END iot_mqtt_includes] |
| 34 | + |
| 35 | +# The initial backoff time after a disconnection occurs, in seconds. |
| 36 | +minimum_backoff_time = 1 |
| 37 | + |
| 38 | +# The maximum backoff time before giving up, in seconds. |
| 39 | +MAXIMUM_BACKOFF_TIME = 32 |
| 40 | + |
| 41 | +# Whether to wait with exponential backoff before publishing. |
| 42 | +should_backoff = False |
| 43 | + |
| 44 | + |
| 45 | +# [START iot_mqtt_jwt] |
| 46 | +def create_jwt(project_id, private_key_file, algorithm): |
| 47 | + """Creates a JWT (https://jwt.io) to establish an MQTT connection. |
| 48 | + Args: |
| 49 | + project_id: The cloud project ID this device belongs to |
| 50 | + private_key_file: A path to a file containing either an RSA256 or |
| 51 | + ES256 private key. |
| 52 | + algorithm: The encryption algorithm to use. Either 'RS256' or 'ES256' |
| 53 | + Returns: |
| 54 | + A str for the JWT from the given project id and private key path, |
| 55 | + set to expire in 60 minutes. After 60 minutes, your client will be |
| 56 | + disconnected, and a new JWT will have to be generated. |
| 57 | + Raises: |
| 58 | + ValueError: If the private_key_file does not contain a known key. |
| 59 | + """ |
| 60 | + |
| 61 | + token = { |
| 62 | + # The time that the token was issued at |
| 63 | + 'iat': datetime.datetime.utcnow(), |
| 64 | + # The time the token expires. |
| 65 | + 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60), |
| 66 | + # The audience field should always be set to the GCP project id. |
| 67 | + 'aud': project_id |
| 68 | + } |
| 69 | + |
| 70 | + # Read the private key file. |
| 71 | + with open(private_key_file, 'r') as f: |
| 72 | + private_key = f.read() |
| 73 | + |
| 74 | + print('Creating JWT using {} from private key file {}'.format( |
| 75 | + algorithm, private_key_file)) |
| 76 | + |
| 77 | + return jwt.encode(token, private_key, algorithm=algorithm) |
| 78 | +# [END iot_mqtt_jwt] |
| 79 | + |
| 80 | + |
| 81 | +# [START iot_mqtt_config] |
| 82 | +def error_str(rc): |
| 83 | + """Convert a Paho error to a human readable string.""" |
| 84 | + return '{}: {}'.format(rc, mqtt.error_string(rc)) |
| 85 | + |
| 86 | + |
| 87 | +def on_connect(unused_client, unused_userdata, unused_flags, rc): |
| 88 | + """Callback for when a device connects.""" |
| 89 | + print('on_connect', mqtt.connack_string(rc)) |
| 90 | + |
| 91 | + # After a successful connect, reset backoff time and stop backing off. |
| 92 | + global should_backoff |
| 93 | + global minimum_backoff_time |
| 94 | + should_backoff = False |
| 95 | + minimum_backoff_time = 1 |
| 96 | + |
| 97 | + |
| 98 | +def on_disconnect(unused_client, unused_userdata, rc): |
| 99 | + """Paho callback for when a device disconnects.""" |
| 100 | + print('on_disconnect', error_str(rc)) |
| 101 | + |
| 102 | + # Since a disconnect occurred, the next loop iteration will wait with |
| 103 | + # exponential backoff. |
| 104 | + global should_backoff |
| 105 | + should_backoff = True |
| 106 | + |
| 107 | + |
| 108 | +def on_publish(unused_client, unused_userdata, unused_mid): |
| 109 | + """Paho callback when a message is sent to the broker.""" |
| 110 | + print('on_publish') |
| 111 | + |
| 112 | + |
| 113 | +def on_message(unused_client, unused_userdata, message): |
| 114 | + """Callback when the device receives a message on a subscription.""" |
| 115 | + payload = str(message.payload) |
| 116 | + print('Received message \'{}\' on topic \'{}\' with Qos {}'.format( |
| 117 | + payload, message.topic, str(message.qos))) |
| 118 | + |
| 119 | + |
| 120 | +def get_client( |
| 121 | + project_id, cloud_region, registry_id, device_id, private_key_file, |
| 122 | + algorithm, ca_certs, mqtt_bridge_hostname, mqtt_bridge_port): |
| 123 | + """Create our MQTT client. The client_id is a unique string that identifies |
| 124 | + this device. For Google Cloud IoT Core, it must be in the format below.""" |
| 125 | + client_id = 'projects/{}/locations/{}/registries/{}/devices/{}'.format( |
| 126 | + project_id, cloud_region, registry_id, device_id) |
| 127 | + client = mqtt.Client(client_id=client_id) |
| 128 | + |
| 129 | + password = create_jwt(project_id, private_key_file, algorithm) |
| 130 | + |
| 131 | + # With Google Cloud IoT Core, the username field is ignored, and the |
| 132 | + # password field is used to transmit a JWT to authorize the device. |
| 133 | + client.username_pw_set(username='unused', password=password) |
| 134 | + |
| 135 | + # Enable SSL/TLS support. |
| 136 | + client.tls_set(ca_certs=ca_certs, tls_version=ssl.PROTOCOL_TLSv1_2) |
| 137 | + |
| 138 | + # Register message callbacks. https://eclipse.org/paho/clients/python/docs/ |
| 139 | + # describes additional callbacks that Paho supports. In this example, the |
| 140 | + # callbacks just print to standard out. |
| 141 | + client.on_connect = on_connect |
| 142 | + client.on_publish = on_publish |
| 143 | + client.on_disconnect = on_disconnect |
| 144 | + client.on_message = on_message |
| 145 | + |
| 146 | + # Connect to the Google MQTT bridge. |
| 147 | + print('Connecting with id: {} and pass: {}'.format(client_id, password)) |
| 148 | + client.connect(mqtt_bridge_hostname, mqtt_bridge_port) |
| 149 | + |
| 150 | + # This is the topic that the device will receive configuration updates on. |
| 151 | + mqtt_command_topic = '/devices/{}/commands/#'.format(device_id) |
| 152 | + |
| 153 | + # Subscribe to the config topic. |
| 154 | + print('Subscribing to {}'.format(mqtt_command_topic)) |
| 155 | + client.subscribe(mqtt_command_topic, qos=1) |
| 156 | + |
| 157 | + return client |
| 158 | +# [END iot_mqtt_config] |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + # [START iot_mqtt_run] |
| 163 | + """Parse command line arguments.""" |
| 164 | + parser = argparse.ArgumentParser(description=( |
| 165 | + 'Example Google Cloud IoT Core MQTT device connection code.')) |
| 166 | + parser.add_argument( |
| 167 | + '--project_id', |
| 168 | + default=os.environ.get('GOOGLE_CLOUD_PROJECT'), |
| 169 | + help='GCP cloud project name') |
| 170 | + parser.add_argument( |
| 171 | + '--registry_id', required=True, help='Cloud IoT Core registry id') |
| 172 | + parser.add_argument( |
| 173 | + '--device_id', required=True, help='Cloud IoT Core device id') |
| 174 | + parser.add_argument( |
| 175 | + '--private_key_file', |
| 176 | + required=True, help='Path to private key file.') |
| 177 | + parser.add_argument( |
| 178 | + '--algorithm', |
| 179 | + choices=('RS256', 'ES256'), |
| 180 | + required=True, |
| 181 | + help='Which encryption algorithm to use to generate the JWT.') |
| 182 | + parser.add_argument( |
| 183 | + '--cloud_region', default='us-central1', help='GCP cloud region') |
| 184 | + parser.add_argument( |
| 185 | + '--ca_certs', |
| 186 | + default='roots.pem', |
| 187 | + help=('CA root from https://pki.google.com/roots.pem')) |
| 188 | + parser.add_argument( |
| 189 | + '--mqtt_bridge_hostname', |
| 190 | + default='mqtt.googleapis.com', |
| 191 | + help='MQTT bridge hostname.') |
| 192 | + parser.add_argument( |
| 193 | + '--mqtt_bridge_port', |
| 194 | + choices=(8883, 443), |
| 195 | + default=8883, |
| 196 | + type=int, |
| 197 | + help='MQTT bridge port.') |
| 198 | + parser.add_argument( |
| 199 | + '--jwt_expires_minutes', |
| 200 | + default=20, |
| 201 | + type=int, |
| 202 | + help=('Expiration time, in minutes, for JWT tokens.')) |
| 203 | + |
| 204 | + args = parser.parse_args() |
| 205 | + |
| 206 | + # Add any JWT refresh logic here |
| 207 | + client = get_client( |
| 208 | + args.project_id, args.cloud_region, args.registry_id, args.device_id, |
| 209 | + args.private_key_file, args.algorithm, args.ca_certs, |
| 210 | + args.mqtt_bridge_hostname, args.mqtt_bridge_port) |
| 211 | + |
| 212 | + # Wait two minutes for commands, for production you may want while True |
| 213 | + for i in range(1, 120): |
| 214 | + # Process network events. |
| 215 | + client.loop() |
| 216 | + print('Sleeping...') |
| 217 | + time.sleep(1) |
| 218 | + |
| 219 | + print('Finished.') |
| 220 | + # [END iot_mqtt_run] |
0 commit comments