Created
June 26, 2019 23:01
-
-
Save dave-malone/9fcc947b027426e4386c2d818869611e to your computer and use it in GitHub Desktop.
Demo Python program illustrating connecting to AWS IoT Core and establishing multiple subscriptions in a single Subscription request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import sys | |
import ssl | |
import time | |
import datetime | |
import logging, traceback | |
import paho.mqtt.client as mqtt | |
IoT_protocol_name = "x-amzn-mqtt-ca" | |
aws_iot_endpoint = "a1tq0bx5we8tnk-ats.iot.us-east-1.amazonaws.com" | |
url = "https://{}".format(aws_iot_endpoint) | |
ca = "root-CA.crt" | |
cert = "daves_macbook.cert.pem" | |
private = "daves_macbook.private.key" | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
handler = logging.StreamHandler(sys.stdout) | |
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
handler.setFormatter(log_format) | |
logger.addHandler(handler) | |
def on_message(client, userdata, message): | |
logger.info("Received message '" + str(message.payload) + "' on topic '" + message.topic + "' with QoS " + str(message.qos)) | |
def ssl_alpn(): | |
try: | |
#debug print opnessl version | |
logger.info("open ssl version:{}".format(ssl.OPENSSL_VERSION)) | |
ssl_context = ssl.create_default_context() | |
ssl_context.set_alpn_protocols([IoT_protocol_name]) | |
ssl_context.load_verify_locations(cafile=ca) | |
ssl_context.load_cert_chain(certfile=cert, keyfile=private) | |
return ssl_context | |
except Exception as e: | |
print("exception ssl_alpn()") | |
raise e | |
if __name__ == '__main__': | |
try: | |
mqttc = mqtt.Client() | |
mqttc.on_message = on_message | |
ssl_context= ssl_alpn() | |
mqttc.tls_set_context(context=ssl_context) | |
logger.info("start connect") | |
mqttc.connect(aws_iot_endpoint, port=8883) | |
logger.info("connect success") | |
mqttc.loop_start() | |
(result, message_id) = mqttc.subscribe([("sdk/test/Python0", 1), ("sdk/test/Python1", 1), ("sdk/test/Python2", 1)]) | |
logger.info("subscribe result: {}".format(result)) | |
logger.info("subscription request success? {}".format(mqtt.MQTT_ERR_SUCCESS == result)) | |
logger.info("subscribe message_id: {}".format(message_id)) | |
while True: | |
now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') | |
for x in range(3): | |
topic = "{}{}".format("sdk/test/Python", x) | |
logger.info("publishing {} on {}".format(now, topic)) | |
mqttc.publish(topic, now) | |
time.sleep(1) | |
except Exception as e: | |
logger.error("exception main()") | |
logger.error("e obj:{}".format(vars(e))) | |
logger.error("message:{}".format(e.message)) | |
traceback.print_exc(file=sys.stdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment