|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0. |
| 3 | + |
| 4 | +""" |
| 5 | +This sample uses AWS IoT Greengrass v2 to publish messages from the device to |
| 6 | +the AWS IoT Core MQTT broker. |
| 7 | +
|
| 8 | +This example can be deployed as Greengrass v2 component and it will start |
| 9 | +publishing telemetry data as MQTT messages in periodic intervals. The IPC |
| 10 | +integration with Greegrass v2 allows this code to run without additional IoT |
| 11 | +certificates or secrets, because it directly communicates with Greengrass Core |
| 12 | +on the device. |
| 13 | +
|
| 14 | +See this page for more information on Greengrass v2 components: |
| 15 | +https://docs.aws.amazon.com/greengrass/v2/developerguide/create-components.html |
| 16 | +
|
| 17 | +See this page for more information on IPC in Greengrass v2: |
| 18 | +https://docs.aws.amazon.com/greengrass/v2/developerguide/interprocess-communication.html |
| 19 | +
|
| 20 | +To run the sample, create a new AWS IoT Greengrass component and deploy it to |
| 21 | +your device with the following recipe snippet to allow your device to publish to |
| 22 | +the AWS IoT Core MQTT broker: |
| 23 | +
|
| 24 | +```json |
| 25 | +... |
| 26 | +"ComponentConfiguration": { |
| 27 | + "DefaultConfiguration": { |
| 28 | + "accessControl": { |
| 29 | + "aws.greengrass.ipc.mqttproxy": { |
| 30 | + "Your.Component.Name:mqttproxy:1": { |
| 31 | + "policyDescription": "Allows access to publish to all AWS IoT Core topics.", |
| 32 | + "operations": [ |
| 33 | + "aws.greengrass#PublishToIoTCore" |
| 34 | + ], |
| 35 | + "resources": [ |
| 36 | + "*" |
| 37 | + ] |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +}, |
| 43 | +... |
| 44 | +``` |
| 45 | +(replace `Your.Component.Name` with your component name) |
| 46 | +
|
| 47 | +You can use this recipe `Manifest` snippet to install Python and `AWS IoT Device |
| 48 | +SDK v2 for Python` as dependency: |
| 49 | +```json |
| 50 | +... |
| 51 | +"Manifests": [{ |
| 52 | + ... |
| 53 | + "Lifecycle": { |
| 54 | + "Install": { |
| 55 | + "RequiresPrivilege": true, |
| 56 | + "Script": "apt-get update --quiet && apt-get --yes install python3 python3-pip && pip3 install awsiotsdk" |
| 57 | + }, |
| 58 | + ... |
| 59 | +``` |
| 60 | +""" |
| 61 | + |
| 62 | +import json |
| 63 | +import time |
| 64 | +import os |
| 65 | + |
| 66 | +import awsiot.greengrasscoreipc |
| 67 | +import awsiot.greengrasscoreipc.model as model |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + ipc_client = awsiot.greengrasscoreipc.connect() |
| 71 | + |
| 72 | + while True: |
| 73 | + telemetry_data = { |
| 74 | + "timestamp": int(round(time.time() * 1000)), |
| 75 | + "battery_state_of_charge": 42.5, |
| 76 | + "location": { |
| 77 | + "longitude": 48.15743, |
| 78 | + "latitude": 11.57549, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + op = ipc_client.new_publish_to_iot_core() |
| 83 | + op.activate(model.PublishToIoTCoreRequest( |
| 84 | + topic_name="my/iot/{}/telemetry".format(os.getenv("AWS_IOT_THING_NAME")), |
| 85 | + qos=model.QOS.AT_LEAST_ONCE, |
| 86 | + payload=json.dumps(telemetry_data).encode(), |
| 87 | + )) |
| 88 | + try: |
| 89 | + result = op.get_response().result(timeout=5.0) |
| 90 | + print("successfully published message:", result) |
| 91 | + except Exception as e: |
| 92 | + print("failed to publish message:", e) |
| 93 | + |
| 94 | + time.sleep(5) |
0 commit comments