Last active
November 9, 2018 17:37
-
-
Save dave-malone/891ae707773a8443bd242283541dedf5 to your computer and use it in GitHub Desktop.
A simple Python program to demonstrate submitting an Avro encoded file as a binary payload into AWS IoT Core
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 avro.datafile import DataFileWriter | |
from avro.io import DatumWriter | |
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient | |
import avro.schema | |
import logging | |
import time | |
import os | |
avro_users_schema_file = "user.avsc" | |
avro_users_file = "users.avro" | |
mqtt_host = os.getenv("MQTT_HOST") | |
mqtt_rootCAPath = os.getenv("MQTT_ROOT_CA_PATH") | |
mqtt_certificatePath = os.getenv("MQTT_CERT_PATH") | |
mqtt_privateKeyPath = os.getenv("MQTT_PRIVATE_KEY_PATH") | |
mqtt_clientId = os.getenv("MQTT_CLIENT_ID") | |
mqtt_port = 8883 | |
mqtt_topic = os.getenv("MQTT_TOPIC") | |
# Configure logging | |
logger = logging.getLogger("AWSIoTPythonSDK.core") | |
logger.setLevel(logging.DEBUG) | |
streamHandler = logging.StreamHandler() | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
streamHandler.setFormatter(formatter) | |
logger.addHandler(streamHandler) | |
# Create Avro users file if it doesn't already exist | |
try: | |
open(avro_users_file, "rb") | |
print('users file exists; skipping creation') | |
except IOError: | |
print('users file did not exist; creating now') | |
schema = avro.schema.parse(open(avro_users_schema_file, "rb").read()) | |
writer = DataFileWriter(open(avro_users_file, "wb"), DatumWriter(), schema) | |
writer.append({"name": "Alyssa", "favorite_number": 256}) | |
writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"}) | |
writer.close() | |
mqtt_client = AWSIoTMQTTClient(mqtt_clientId) | |
mqtt_client.configureEndpoint(mqtt_host, mqtt_port) | |
mqtt_client.configureCredentials(mqtt_rootCAPath, mqtt_privateKeyPath, mqtt_certificatePath) | |
# mqtt_client connection configuration | |
mqtt_client.configureAutoReconnectBackoffTime(1, 32, 20) | |
mqtt_client.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing | |
mqtt_client.configureDrainingFrequency(2) # Draining: 2 Hz | |
mqtt_client.configureConnectDisconnectTimeout(10) # 10 sec | |
mqtt_client.configureMQTTOperationTimeout(5) # 5 sec | |
# Connect to AWS IoT | |
mqtt_client.connect() | |
# Publish to the same topic in a loop forever | |
loopCount = 0 | |
while True: | |
avroFile = open(avro_users_file, "rb") | |
message = bytearray(avroFile.read()) | |
mqtt_client.publish(mqtt_topic, message, 1) | |
print('Published to topic %s: %s\n' % (mqtt_topic, message)) | |
loopCount += 1 | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This program was written using Python 2.7, and has the following dependencies:
This program also assumes that you have an AWS account, and a Thing provisioned in AWS IoT. Follow these instructions for getting started with AWS IoT: https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html
Once you have provisioned your Thing and downloaded your device certificates, you can run this example using the following bash script. Please note that there are placeholders in this script that must be updated with your own configuration.
user.avsc
schema file used in this example: