Replace the paths, client id and sensor id with your own in the lambda functions described below.
Publish Data from the MQTT-Broker to the Amazon Cloud
This lambda function subscribes data from MQTT-Broker from Edge Device and publishes is to the Amazon Cloud to the topic tv/greengrass
.
#!/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()
Fetch Data from the Amazon Cloud
This lambda function subscribes the data from Amazon Cloud and pushes the data to TeamViewer metrics and real-time data can be visualized with the IoT Management Console and the dashboard.
#!/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()