import sys import traceback import time from datetime import datetime from awsiot.greengrasscoreipc.clientv2 import GreengrassCoreIPCClientV2 from awsiot.greengrasscoreipc.model import ( PublishMessage, BinaryMessage ) def main(): topic = "test/topic" try: ipc_client = GreengrassCoreIPCClientV2() while True: message = "Current time is " current_datetime = datetime.now() message = message + current_datetime.strftime("%Y-%m-%d %H:%M:%S") publish_binary_message_to_topic(ipc_client, topic, message) print('Successfully published to topic: ' + topic) time.sleep(1) except Exception: print('Exception occurred', file=sys.stderr) traceback.print_exc() exit(1) def publish_binary_message_to_topic(ipc_client, topic, message): binary_message = BinaryMessage(message=bytes(message, 'utf-8')) publish_message = PublishMessage(binary_message=binary_message) return ipc_client.publish_to_topic(topic=topic, publish_message=publish_message) if __name__ == '__main__': main()