Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mqtt_url = "mqtt.googleapis.com"
- mqtt_port = 8883
- topic = "/devices/sm1/config"
- def on_connect(client, userdata, flags, response_code):
- print("Connected with status: {0}".format(response_code))
- client.subscribe(topic, 1)
- def on_message(client, userdata, msg):
- print("Topic: {0} -- Payload: {1}".format(msg.topic, msg.payload))
- if __name__ == "__main__":
- client = mqtt.Client("projects/{}/locations/{}/registries/{}/devices/{}".format(
- project_id,
- cloud_region,
- registry_id,
- device_id))
- client.username_pw_set(username='unused',
- password=jwt_maker.create_jwt(project_id,
- private_key,
- algorithm="RS256"))
- client.tls_set(root_ca,
- certfile = public_crt,
- keyfile = private_key,
- cert_reqs = ssl.CERT_REQUIRED,
- tls_version = ssl.PROTOCOL_TLSv1_2,
- ciphers = None)
- client.on_connect = on_connect
- client.on_message = on_message
- print "Connecting to Google IoT Broker..."
- client.connect(mqtt_url, mqtt_port, keepalive=60)
- client.loop_forever()
Add Comment
Please, Sign In to add comment