Greengrass is a software that allows you to extend AWS cloud capabilities to local devices, making it possible to collect data and securely communicate with local network and AWS Cloud. Fits very well for IoT solutions as gateway that could handle communication with Bluetooth, Zigbee devices, for example.
Greengrass Group, Core and Devices
Greengrass Group: a set of Cores and Devices
Greengrass Core: a set of lambdas and devices, runs greengrass daemon
Greengrass Device: small devices, no greengrass daemon
Before we start using Greengrass we need to configure our Raspberry PI AWS credentials for awscli and also be sure you have access to your Pi using SSH.
Step #1: Open IAM Console to create an access key
Step #2: Click in your AWS user
Step #3: Click "Security Credentials"
Step #4: Click "Create Access Key"
Step #5: Copy the Access Key ID and Secret Access
You can also download a CSV file with your keys!
Step #6: Access your Raspberry Pi using SSH and configure awscli
Open ssh: ssh pi@RASP-IP-ADDRESSS
Type: aws configure
Enter your keys
Choose us-east-1 as region
Choose json as output
Step #7: Return to IAM Console and select your user
Step #13: Choose "AWSIoTFullAccess" and click "Next Review"
Step #14: Click "Add permissions" and done!
2.Create a Lambda Function
Now we are going to create a Lambda function that we will deploy to our Raspberry Pi later. This Lambda will be sending simulated blood pressure data.
Step #1: Open Lambda Console and click "Create a function"
Step #2: Select "Author from scratch"
Name: healthcare-data-generator
Runtime: Python 2.7
Existing Role: lambda_basic_execution
Step #3: Scroll down to Function code panel
Download to your machine
Select "Upload a .ZIP file" in "Code entry type" combo
Step #4: Click "Upload" and select the .ZIP file downloaded in the previous step and then click "Save"
Step #5: Select "Edit code line" in "Code entry type" to see the Lambda code
Step #6: Click "Actions -> Publish new version"
Step #7: Name it "v1" and done!
3.Create Greengrass Group
It's time to create a Greengrass Group that represents our Raspberry Pi as a Core Device.
Step #1: Open Greengrass console and click "Get started" to create a group
Step #2: Select "Use easy creation"
Step #3: Name your group as "healthcare"
Step #4: Accept the suggested core name
Step #5: Click "Create group and core"
Step #6: Click "Download these resoures as .tar.gz"
Step #7: Download Greengrass security resources to your machine
Step #8: Click "Finish"
Step #9: Access your Raspberry Pi and type this command to create a fresh Greengrass core
Step #10: Type "cd greengrass"
Step #11: Upload your .tar.gz security resources file and uncompress into Greengrass directory
Step #12: Type these commands to copy the root.ca.pem certificatre
Step #13: Now you can start your Greengrass core!
4.Deploy Lambda to your Raspberry Pi
Let's deploy our Lambda function to run locally in our Raspberry Pi.
Step #1: Open Greengrass console, select "healthcare" group and click "Lambdas"
Step #2: Select "Use existing Lambda"
Step #3: Select "helthcare-data-generator" Lambda
Step #4: Select the published version
Step #5: Click "Edit configuration"
Step #6: Change Lambda Life-cycle to Long-lived function
Step #7: Your configuration should be like this
Step #8: Back to healthcare group and click "Subscriptions"
Step #12: Select "Lambdas -> healthcare-data-genertor" as Source
Step #13: Selecr "IoT Cloud" as Target
Step #14: With this subscrition our Lambda will be able to reach any topic in the Cloud
Step #15: Click finish
Step #16: Now let's deploy by clicking "Actions -> Deploy" menu
Step #17: Click "Automatic detection"
Step #18: Grant the required permissions
Step #19: In Group Role click "Add role" link
Step #20: Choose "Greengrass_ServiceRole" and click "Save"
Step #21: Now let's do the final deploy, click "Actions -> Deploy" again
Step #22: You should see "Successfully completed message"
5.Monitoring and Processing AWS IoT Messages
Now it's time to monitor AWS IoT to check for incoming MQTT messages from your Raspberry Pi.
Step #1: Open AWS IoT Console
Step #2: Click "Test" and then subscribe to healthcare/data topic
Step #3: You should start seeing health data generated by Greengrass / Raspi
Step #4: Now let's start processing this data
Open Lambda console and create a new Lambda
Name: Healthcare-Data-JSONToDynamoDB
Step #5: Use the following code
Step #6: Open IoT Console and click "Act"
Step #7: Click "Create a rule"
This rule will trigger our lambda function for each message received on healthcare/data topic
Name: HealthData
Step #8: As topic filter, type healthcare/data
Step #9: Add an action and select Lambda
Step #10: Select your lambda function and done!
After you finish this, your healthcare data will be stored on DynamoDB, let's check it!
Step #11: Open DynamoDB Console
Step #12: Click "Tables", choose the "patient" table and click "Items"
If you can see the data coming to the data, congrats, evertything is working!
6.Greengrass Controlling Bluetooth Blood Pressure Sensor
Now that we simulated the blood pressure and heart beat data, we will progress to a real example using a Bluetooth Low-energy Blood Pressure monitor. For this case we are using Libelium / MySignals that provides an open platform for e-helath hardware and software development.
We are going to use a Python script to control and collect the readout from the blood pressure sensor:
Step #1: Open your Lambda console and create a new Python 2.7 function
Step #2: Select "Upload a .ZIP file"
Step #3: Download and upload to your function
Step #4: Click "Save"
Step #5: You should see the source code in Python to communicate with BLE blood pressure sensor
Step #6: Click "Actions" and "Publish new version"
Step #7: Name the version
Step #8: Return to your Greengrass console and select your Greeengrass group
Step #9: Click "Lambdas" and "Add Lambda"
Step #10: Click "Use existing Lambda"
Step #11: Select "BloodPressureBLE" Lambda function
Step #12: Click "Next"
Step #13: Select the version and click finish
Step #14: Go to Subscritions menu and click "Add Subscription"
Step #15: Select "BloodPressureBLE" Lambda as Source and IoT Cloud as target
Step #16: Enter "healthcare/data" as filter topic
Step #17: Click "Finish" to confirm the subscription
Step #18: Add a new Subscrition and Select IoT Cloud as source and "BloodPressureBLE" Lambda as target
Step #19: As filter topic, enter "healthcare/bloodpressure". This is the topic that will trigger the lambda / blood pressure sensor.
Step #20: Click finish to confirm the subscription
Step #21: Go to Deployments and Click "Actions -> Deploy"
Step #22: After the deplotyment, go to AWS IoT Console and click "Test"
Step #23: Click "Publish to a topic", enter "healthcare/bloodpressure" as a topic and click "Publish to topic". If everything is working the blood pressure sensor should start working! You need to change the bluetooth Mac addrress to yours!