Integrating Raspberry Pi with AWS IoT Core and AWS IoT Events

This article demonstrates the building of an IoT ecosystem using AWS IoT services and Raspberry Pi. I refer to AWS documentation for most of the setup (check the references at the end of this article for more) to build a generic model that can be extended as needed.
Overview
IoT devices (such as temperature sensors, motion sensors, and smoke detectors), send notifications to an AWS IoT core which triggers events to AWS IoT events. The detector model will then decide the pattern using states and trigger action based on conditions & events.

Pre-requisites
- Raspberry Pi 3Model b or latest (I used this model)
- External monitor, USB mouse and keyboard(This is necessary for the initial configuration, you can use a Bluetooth later on), SD card
- AWS account with admin privileges, free tier will work just fine
AWS components used
- AWS IoT Core: IoT Policy, IoT Things, Rule
- AWS IoT Events: Inputs, Detector Model
- AWS Detector Model
- AWS SNS
AWS & Raspberry Setup
1. Create an AWS IoT policy
AWS IoT console → Secure → Policies → Create a policy
Create an AWS IoT policy document that will authorize your device to interact with AWS IoT services. Select the following actions: iot:Connect, iot:Receive, iot:Publish, iot:Subscribe or iot:*
Under Effect, choose Allow, and then choose Create.

2. Create a thing in AWS IoT
A thing is a representation of a specific device or logical entity. In this case, it’s the Pi.
AWS IoT console → Manage → Things → Create → Create a single thing
Generate a certificate for the thing. Create thing and download the certificate, private key, and the root CA cert for AWS https://www.amazontrust.com/repository/AmazonRootCA1.pem

Certificates can be retrieved at any time but the private and public keys cannot be retrieved after you close this page. The certificate authenticates your device to AWS IoT Core
Associate the policy created in the previous step to the IoT thing and activate it. Make note of the endpoint of the thing: Thing → interact
example: a1584lq4k6ojlx-ats.iot.us-east-1.amazonaws.com
3. SNS Topic
Create an SNS topic to receive alerts. I created a simple email subscription to receive alerts.
SNS → Topics → Create Topic & SNS → Subscriptions → Create subscription

4. Create an IoT Event Input
Create an IoT event input to consume the event from IoT thing. IoT events ingest data from other services, the data format is defined using JSON.
AWS IoT Events console → Inputs → Create Input

Sample json :
{
"id": "88fed118-090c-11eb-adc1-0242ac120002",
"eventType": "motion",
"eventTime": "2020-10-07T19:30:51Z",
"location": "zone1"
}
5. Create a Detector Model
Create a Detector Model with states to check if the sensor event belongs to a secure zone and trigger the SNS topic. For each state, define conditional (Boolean) logic that evaluates the incoming inputs to detect a significant event.
AWS IoT Events console → Detector models → Create detector model
For state "Home""transitionEvents"
"condition": "$input.pc_sensor.location == 'secureZone'"
"setVariable": securitybreach = true
For state "SecureZone"
"onEnter": {
"events":[
{
"eventName": "sendAlert",
"condition": "$variable.securitybreach == true",
"actions": [
{
"sns": {
"targetArn": "xyz:iotSNS"
}
}
]
}
]
}
SNS -> SNS created in previous step

Create/use a role needed by model and publish the model

We can also test the model by sending a sample message. Detail steps for detector: https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-detector-model.html
6. Create an IoT rule
Create an IoT rule to trigger an IoT event to the input and start the detector model created in the previous step.
AWS IoT console → Act → Rules → Create
- Rule query: SELECT * FROM ‘sensors’
- Action: Send a message to an IoT Events Input, choose the input created in the previous step.

Setup Raspberry Pi

1. Install OS for Raspberry Pi
Download noobs on the SD card and use that to install on Raspberry Pi https://www.raspberrypi.org/documentation/installation/noobs.md
Noobs comes with Python3 so you don’t have to install that separately. You can also purchase pre-installed noobs SD card, however the step is pretty simple
2. Install libraries for the AWS IoT Device SDK
$ python3 -m pip install awsiotsdk
$ git clone https://github.com/aws/aws-iot-device-sdk-python-v2.git
Copy the certs downloaded in the previous step to a directory on Raspberry Pi (I used google drive to transfer the certs).
- Root CA certificate
- Device certificate
- Private key
3. Send a message to AWS IoT core from raspberry Pi
I used the sample code provided by AWS sdk which uses the MQTT library
$ cd home/pi/aws-iot-device-sdk-python-v2/samples
$ python3 pubsub.py --topic sensors --root-ca ~/certs/Amazon-root-CA-1.pem --cert ~/certs/device.pem.crt --key ~/certs/private.pem.key --endpoint your-iot-endpointI just modified the code for following things:
- IoT endpoint captured while creating the thing
- topic name
- message that needs to be published
For events that satisfy the criteria in the detector, you can see an email notification send to the subscribed email.
{
"id": "88fed118-090c-11eb-adc1-0242ac120002",
"eventType": "motion",
"eventTime": "2020-10-07T19:30:51Z",
"location": "zone1"
}This will send an alert using SNS{
"id": "88fed118-090c-11eb-adc1-0242ac120002",
"eventType": "motion",
"eventTime": "2020-10-07T19:40:51Z",
"location": "secureZone"
}
Conclusion
Congratulations, you now know how to integrate Raspberry Pi with AWS IoT core and AWS IotEvents. I hope that you have found this article helpful, please let me know if you have any questions or feedback in the comments section. For more details around the subject matter of this article, please consult the reference list below.
References
- https://www.raspberrypi.org/documentation/installation/noobs.md
- https://docs.aws.amazon.com/pt_br/iotevents/latest/developerguide/iotevents-start.html
- https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html
- https://docs.aws.amazon.com/iot/latest/developerguide/connecting-to-existing-device.html
- https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-detector-model.html
Thanks for reading this post, let me know if you have any questions!!