|
| 1 | +# Greengrass V2 samples using AWS IoT Device SDK v2 for Python |
| 2 | + |
| 3 | +You can find the full API documentation for the Greengrass V2 IPC interface using the Python SDK here: https://aws.github.io/aws-iot-device-sdk-python-v2/awsiot/greengrasscoreipc.html |
| 4 | + |
| 5 | +## Sample: Low-Level IPC |
| 6 | + |
| 7 | +Folder: `low_level_ipc/` |
| 8 | + |
| 9 | +Once installed and running, this sample publishes messages to AWS IoT Core. It uses the low-level Greengrass v2 [Inter-Process-Communication API](https://docs.aws.amazon.com/greengrass/v2/developerguide/interprocess-communication.html). |
| 10 | + |
| 11 | +See the other samples for higher-level APIs to reduce the amount of code you have to write and maintain. |
| 12 | + |
| 13 | +## Sample: Publish/Subscribe to the cloud with AWS IoT Core |
| 14 | + |
| 15 | +Folder: `pubsub_cloud` |
| 16 | + |
| 17 | +Once installed and running, this sample subscribes to the `hello/world` topic. You can use the [MQTT Test Client](https://console.aws.amazon.com/iot/home#/test) to publish a message to this topic, while also subscribing to `hello/world/response` in the MQTT Test Client. The Greengrass device will receive the message and reply back on the response topic. |
| 18 | + |
| 19 | +## Sample: Public/Subscribe on the local device between Greengrass components |
| 20 | + |
| 21 | +Folder: `pubsub_local` |
| 22 | + |
| 23 | +Once installed and running, this sample subscribes to the `hello/world` topic. You can use a second component to publish a message and receive a reply message on the `hello/world/response` topic. These messages are **not** sent to AWS IoT Core (the cloud). This pub/sub mechanism is only connecting different components running on the same Greengrass device. You can use the Greegrass CLI to publish or subscribe to these local topics: |
| 24 | + |
| 25 | +* `greengrass-cli pubsub sub --topic hello/world/response` |
| 26 | +* `greengrass-cli pubsub pub --topic hello/world --message Hi!` |
| 27 | + |
| 28 | +## Sample: Shadow Management |
| 29 | + |
| 30 | +Folder: `shadows` |
| 31 | + |
| 32 | +Once installed and running, this sample will retrieve a named shadow `special_shadow` when the component first starts executing, and then periodically update the shadow document with a new reported state every few seconds. |
| 33 | + |
| 34 | +This component depends on the [AWS-provided ShadowManager component](https://docs.aws.amazon.com/greengrass/v2/developerguide/shadow-manager-component.html#shadow-manager-component-configuration). You [need to configure](https://docs.aws.amazon.com/greengrass/v2/developerguide/shadow-manager-component.html#shadow-manager-component-configuration) it to synchronize named shadows from the local device to the cloud: |
| 35 | + |
| 36 | +```yaml |
| 37 | +strategy: |
| 38 | + type: realTime |
| 39 | +synchronize: |
| 40 | + coreThing: |
| 41 | + namedShadows: |
| 42 | + - special_shadow |
| 43 | + direction: betweenDeviceAndCloud |
| 44 | +``` |
| 45 | +
|
| 46 | +## Sample: Deployment Configuration |
| 47 | +
|
| 48 | +Folder: `deployment_configuration` |
| 49 | + |
| 50 | +Once installed and running, this sample will retrieve the component's deployment configuration and start a web server based on the provided parameters. Re-deploying with different parameters will update the component and upon restart of the Python process, it will start the web server based on these new parameters. |
| 51 | + |
| 52 | +## Deployment Helpers |
| 53 | + |
| 54 | +Deploy component locally using Greengrass CLI: |
| 55 | + |
| 56 | +```bash |
| 57 | +func gg_deploy() { |
| 58 | + COMPONENT_NAME=$(sed -nr 's/ComponentName: ([a-zA-Z.-_]+)/\1/p' recipe.yaml) |
| 59 | + COMPONENT_VERSION=$(sed -nr 's/ComponentVersion: (.+)/\1/p' recipe.yaml | tr -d '"' | tr -d "'") |
| 60 | + |
| 61 | + mkdir -p build/artifacts/$COMPONENT_NAME/$COMPONENT_VERSION/ |
| 62 | + command cp code.py build/artifacts/$COMPONENT_NAME/$COMPONENT_VERSION/ |
| 63 | + |
| 64 | + mkdir -p build/recipes/ |
| 65 | + command cp recipe.yaml build/recipes/$COMPONENT_NAME.yaml |
| 66 | +
|
| 67 | + RECIPES=$PWD/build/recipes |
| 68 | + ARTIFACTS=$PWD/build/artifacts |
| 69 | + sudo /greengrass/v2/bin/greengrass-cli deployment create \ |
| 70 | + --recipeDir=$RECIPES \ |
| 71 | + --artifactDir=$ARTIFACTS \ |
| 72 | + --merge=$COMPONENT_NAME=$COMPONENT_VERSION |
| 73 | +} |
| 74 | +
|
| 75 | +func gg_remove() { |
| 76 | + COMPONENT_NAME=$(sed -nr 's/ComponentName: ([a-zA-Z.-_]+)/\1/p' recipe.yaml) |
| 77 | + sudo /greengrass/v2/bin/greengrass-cli deployment create \ |
| 78 | + --recipeDir=$RECIPES \ |
| 79 | + --artifactDir=$ARTIFACTS \ |
| 80 | + --remove=$COMPONENT_NAME |
| 81 | +} |
| 82 | +``` |
0 commit comments