Examples: lambda functions on AWS IoT Greengrass - TeamViewer Support
<main> <article class="userContent"> <p><br></p><p>📌<strong>Note: </strong>Replace the paths, client id and sensor id with your own in the lambda functions described below.</p><p><br></p><h3></h3><h2 data-id="publish-data-from-the-mqtt-broker-to-the-amazon-cloud">Publish data from the <strong>MQTT-Broker</strong> to the Amazon Cloud</h2><p>This lambda function subscribes data from <strong>MQTT-Broker</strong> from <strong>Edge Device</strong> and publishes is to the <strong>Amazon Cloud</strong> to the topic <code class="code codeInline" spellcheck="false" tabindex="0">tv/greengrass</code>.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">#!/usr/bin/python import paho.mqtt.subscribe as subscribe import paho.mqtt.publish as publish import traceback import json def function_handler(event, context): return # AWS part # Change following parameters aws_host = "a10kx625z9icrs-ats.iot.eu-central-1.amazonaws.com" # Endpoint aws_port = 8883 aws_tls = {'certfile': "/home/pi/Downloads/certs/1b9a6e0dcb.cert.pem", 'keyfile': "/home/pi/Downloads/certs/1b9a6e0dcb.private.key", 'ca_certs': "/home/pi/Downloads/certs/root.ca.pem" } # TV part #Change following parameters tv_iot_api_version = "v1.0" tv_host = "localhost" tv_port = 18884 tv_tls = {'certfile': "/home/pi/Downloads/test_client_greengrass/cert-7a2b7efc1ad1473d87e049517a19628b.pem", 'keyfile': "/home/pi/Downloads/test_client_greengrass/key-7a2b7efc1ad1473d87e049517a19628b.pem", 'ca_certs': "/home/pi/Downloads/test_client_greengrass/TeamViewerAuthority.crt"} client_id = "fd9e67783b7441ffb7cd62dda121567a" sensor_id = "111804" def subscribe_data(): try: mqtt_topic_subscribe_data = "/" + tv_iot_api_version + "/" + client_id + "/sensor/" + sensor_id + "/livedata" print("Suscribing to " + mqtt_topic_subscribe_data) subscribe.callback(on_subscribe, mqtt_topic_subscribe_data, hostname=tv_host, port=tv_port, tls=tv_tls) except Exception as e: traceback.print_exc() print(str(e)) def on_subscribe(client, userdata, message): json_message = json.loads(message.payload.decode('utf8')) msgArr = [] for metric in json_message["metrics"]: messageJ = {} messageJ['metricId'] = metric["metricId"] messageJ['metricValue'] = metric["value"] msgArr.append(messageJ) msgs = [] msgs.append({'topic': "tv/greengrass", 'payload': json.dumps(msgArr)}) print(msgs) publish.multiple(msgs, hostname=aws_host, port=aws_port, tls=aws_tls) subscribe_data() </pre><p><br></p><h3 data-id="-1"></h3><h2 data-id="fetch-data-from-the-amazon-cloud">Fetch data from the Amazon Cloud</h2><p>This lambda function subscribes the data from <strong>Amazon Cloud</strong> and pushes the data to TeamViewer metrics and real-time data can be visualized with the <a href="https://community.teamviewer.com/English/kb/articles/109817-iot-management-console" rel="nofollow noreferrer ugc">IoT Management Console</a> and the dashboard.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">#!/usr/bin/python import paho.mqtt.subscribe as subscribe import paho.mqtt.publish as publish import traceback import json import ssl def function_handler(event, context): return # AWS part # Change following parameters aws_host = "a10kx625z9icrs-ats.iot.eu-central-1.amazonaws.com" # Endpoint aws_port = 8883 aws_tls = {'certfile': "/home/pi/Downloads/certs/1b9a6e0dcb.cert.pem", 'keyfile': "/home/pi/Downloads/certs/1b9a6e0dcb.private.key", 'ca_certs': "/home/pi/Downloads/certs/root.ca.pem", 'tls_version': ssl.PROTOCOL_TLSv1_2 } # TV part #Change following parameters tv_iot_api_version = "v1.0" tv_host = "localhost" tv_port = 18884 tv_tls = {'certfile': "/home/pi/Downloads/test_client_greengrass/cert-7a2b7efc1ad1473d87e049517a19628b.pem", 'keyfile': "/home/pi/Downloads/test_client_greengrass/key-7a2b7efc1ad1473d87e049517a19628b.pem", 'ca_certs': "/home/pi/Downloads/test_client_greengrass/TeamViewerAuthority.crt"} client_id = "7a2b7efc1ad1473d87e049517a19628b" sensor_id = "71b91c40fb2d4f61bec2fa593172085d" def aws_subscribe(): print("Suscribing to tv/greengrassSub") subscribe.callback(on_subscribe, "tv/greengrassSub", hostname=aws_host, port=aws_port, tls=aws_tls) def on_subscribe(client, userdata, message): try: metric_data = json.loads(message.payload.decode('utf8')) mqtt_topic_push_data = "/" + tv_iot_api_version + "/" + client_id + "/sensor/" + sensor_id + "/metric/pushValues" # metric_data = {"metrics": [{"value": 1, "metricId": "7ad8ad3e2dbe4cdcba0ee3ec0cbd2717"}]} msgs = [] msgs.append({'topic': mqtt_topic_push_data, 'payload': json.dumps(metric_data)}) print(msgs) publish.multiple(msgs, hostname=tv_host, port=tv_port, tls=tv_tls) except Exception as e: traceback.print_exc() print(str(e)) aws_subscribe() </pre><p><br></p><h3 data-id="-2"></h3> </article> </main>