This Workshop has two sample apps meant to be run on a Raspberry Pi with a GrovePi+ (https://www.dexterindustries.com/grovepi/) using the following add-on modules:
- Button
- LED
- Temperature / Humidity Sensor
- LCD Screen
These apps integrate the Pi with the AWS IoT Service via MQTT and the Shadow. Once the data is in S3 we do some analytics with our serverless Data Lake services.
- Choose the
Sydney
Region in the upper right of the AWS Console - Open the
IoT Core
Service - On the left-hand side choose
Manage
->Things
- Click the
Register a thing
button - Click the
Create a single thing
button - Enter
username-grovepi
for the thing's name where username is your unique username then clickNext
- Click the
Create certificate
button to the right ofOne-click certificate creation
- Click the
Download
links next to all three certificates generated for the new Thing - Click
Done
- On the left-hand side go to
Secure
->Policies
- Click the
Create a policy
button - Enter
grovepi
for the policy's name - Enter
iot:*
for theAction
- Enter
*
for theResource
- Tick
Allow
and then clickCreate
- On the left-hand side go to
Manage
->Things
and click ongrovepi
- On the left-hand side go to
Security
- On the left-hand side go to Policies
- Click
Actions
in the upper right corner and chooseAttach policy
- Tick the
grovepi
Policy and then clickAttach
- Click
Actions
in the upper right corner and chooseActivate
- Click the
Activate
button
At this stage we now have everything ready to go in the Cloud. Now we'll set up our IoT Device.
- Open a Terminal (should default to
/home/pi
folder) - Clone the GitHub repo with the command
git clone https://github.com/jasonumiker/aws-iot-grovepi.git
- Run the command
cd aws-iot-grovepi
- Run the command
./download_root_ca.sh
to download the root-CA.crt file - Put the three certificate files downloaded from the Thing creation into this folder
- Connect the Button to D8
- Connect the LED to D3
- Connect the Temperature and Humidity Sensor to D7
- Connect the LCD screen to any of the I2C ports
This example illustrates IoT Core's shadow feature.
- Edit
button_light_shadow.py
and update the following variables near the top:- crt to
xxxxxxxxxx-certificate.pem.crt
- key to
xxxxxxxxxx-private.pem.key
- iot_endpoint to the
Endpoint
listed underSettings
on the left-hand side of the IoT Core Console
- crt to
- Run
./button_light_shadow.py
- Try clicking the button and watching the LED change
- Go to
Manage
thenThings
on the left-hand side of the IoT Core Console - Click on the
grovepi
Thing - Click
Shadow
on the left-hand side - Click the button and watch the Shadow document change
- Click the
Edit
link in the upper right of the Shadow document - Change the document's Desired to 1 if it's 0 or 0 if it's 1 and
Save
- Note the split-second
Delta
field that is added to the document to identify that there is a delta between the desired state in the cloud and the actual state on the device. The device subscribes to deltas and will change to match within a few seconds.
The shadow allows you to manage IoT Things that have inconsistent network connectivity by setting the Desired state in the cloud - the device will reconcile with that state when it reconnects to the network. It also lets you see the current Actual state of devices in the field if they are reporting back properly.
If you want to allow for local non-Cloud logic and processing for Things in areas where the network connectivity is unreliable or there is a large fleet of Things and them all reaching out to the Cloud is inefficient AWS Greengrass can help. It allows local on-site shadows and Lambdas to process the data and perhaps only send the relevant subset or aggregations back up to the Cloud.
This example illustrates one of the most common use-cases - IoT Sensors sending data back for analytics in the Cloud.
- Edit
tandh.py
and update the following variables near the top:- crt to
xxxxxxxxxx-certificate.pem.crt
- key to
xxxxxxxxxx-private.pem.key
- iot_endpoint to the
Endpoint
listed underSettings
on the left-hand side of the IoT Core Console
- crt to
- Run
./tandh.py
- Note that the Temperature and Humidity values are displayed on the LCD
- See that they are also sent to the IoT service via MQTT:
- Go to
Test
on the left-hand side of the Iot Core Service Console - Under
Subscription topic
typetandh
and clickSubscribe to topic
- Note that every few seconds the Thing is sending data to the Cloud about the Temperature and Humidity
- Go to
This example illustrates how the IoT Core's Rules work as well as how to collect IoT data into a Data Lake in S3.
- Go to the S3 Console and click
+ Create bucket
- For bucket name use
username-tandh-raw
e.g. jumiker-tandh-raw (this name needs to be globally unique) - Click
Create
- Go to the IoT Core Console
- On the left-hand side go to
Act
then clickCreate rule
- Enter
tandh_datalake_raw
for theName
- Enter
*
for theAttribute
- Enter
tandh
for theTopic filter
- Click
Add action
button - Click
Store messages in an Amazon S3 bucket
and then theConfigure action
button - Select the new S3 bucket from the
S3 bucket
drop-down - Enter
${timestamp()}
for theKey
- Click
Create a new role
- Enter
tandh-datalake-raw-rule
for theIAM role name
and then click theCreate a new role
button - Choose the new role from the drop-down and then click the
Add action
button - Click the
Create rule
button
This example shows how to analyse the data in S3 without needing to load it into a database or Elasticsearch first.
- Go to the Glue Console and choose
Crawlers
on the left-hand side - Click the
Add crawler
button - Enter
username-tandh
for theCrawler name
and clickNext
- Click the folder icon to the right of the
Include path
and choose the tandh bucket then clickSelect
- Click
Next
andNext
again - Leave the default option to
Create an IAM role
and entertandh
for the IAM role name then clickNext
- Leave the default
Frequency
ofRun on demand
and clickNext
- Click
Add database
, enterusername-tandh
for the name, and clickCreate
- Click
Next
and thenFinish
- Do an initial run of the new Crawler
- After that succeeds go to the Athena service in the Console
- Choose the new
username-tandh
database in the dropdown - Note the table name on the left-hand side which should correspond to the bucket name
- Run a query like
SELECT * from username_tandh_raw;
substituting in your own table name. - (Optional) Try other SQL queries and aggregations
- Go to the QuickSight Console ensuring you select N. Virginia for the region
- Click the User icon in the upper-right and choose
Manage QuickSight
- Choose
Account Settings
on the left-hand side - Click the
Edit AWS permissions
button - Tick
Athena
andS3
and then click theChoose S3 buckets
link and choose the tandh_raw bucket - Click the
Apply
button and then change the region back toSydney
This example shows how we can visualise the SQL queries that we are making against the data in S3 via Athena.
- Click the
Manage data
button in the upper-right - Click the
New data set
button in the upper-left - Click on
Athena
- Enter
username-tandh
for theData source name
substituting your own name or username - Click on the
Create data source
button - Pick your new Database and Table from the dropdowns then click the
Select
button - Tick
Directly query your data
then click theVisualize
button - In the
Fields list
clickTimestamp
thenTemperature
thenHumidity
- Under
Visual types
click theLine chart
- There is a number of SDKs for the other sensors/devices in the box in the
/home/pi/Dexter/GrovePi/Software/
folder.- The Python one seems to be the most fully-formed
- And to integrate these things with the cloud our IoT SDKs are here - https://docs.aws.amazon.com/iot/latest/developerguide/iot-sdks.html
- We'll reset the SD Card after the workshop so feel free to download and do whatever you like on the device for the rest of the session