Skip to content

Commit cb0c5ee

Browse files
committed
Merge branch 'feature/add_aws_iot_demos' into 'master'
add aws iot demos See merge request sdk/ESP8266_RTOS_SDK!284
2 parents 6419ab1 + fd299df commit cb0c5ee

File tree

17 files changed

+1174
-0
lines changed

17 files changed

+1174
-0
lines changed

examples/protocols/aws_iot/README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Amazon Web Services IoT Examples
2+
3+
These examples are adaptations of some of the [AWS IoT C SDK](https://github.com/aws/aws-iot-device-sdk-embedded-C) examples.
4+
5+
The provisioning/configuration steps for these examples are the same, and are given in this README.
6+
7+
This README also contains some troubleshooting information for common problems found when connecting to AWS IoT.
8+
9+
# Provisioning/Configuration
10+
11+
There are some additional steps that need to be run before you can build this example.
12+
13+
The [Getting Started section of the AWS IoT Developer Guide](http://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html) lays out the steps to get started with AWS IoT.
14+
15+
To build and use this example, follow all the AWS IoT Getting Started steps from the beginning ("Sign in to the AWS Iot Console") up until "Configuring Your Device". For configuring the device, these are the steps:
16+
17+
## Configuring Your Device
18+
19+
### Installing Private Key & Certificate
20+
21+
As part of creating a device certificate, you downloaded a Private Key (`xxx-private.pem.key`) and a Certificate file (`xxx-certificate.pem.crt`). These keys need to be loaded by the ESP32 to identify itself.
22+
23+
There are currently two options for how to load the key & cert.
24+
25+
* Embed the files into the app binary (default)
26+
* Load the files from SD card
27+
28+
### Option 1: Embedded Key & Cert into App Binary
29+
30+
Copy the `.pem.key` and `.pem.crt` files to the `main/certs` subdirectory of the example. Rename them by removing the device-specific prefix - the new names are `private.pem.key` and `certificate.pem.crt`.
31+
32+
As these files are bound to your AWS IoT account, take care not to accidentally commit them to public source control. In a commercial IoT device these files would be flashed to the device via a provisioning step, but for these examples they are compiled in.
33+
34+
### Option 2: Loading Key & Cert from SD Card
35+
36+
The alternative to embedding the key and certificate is to load them from a FAT filesystem on an SD card.
37+
38+
Before loading data from SD, format your SD card as FAT and run the `examples/storage/sd_card` example on it to verify that it's working as expected in ESP-IDF. This helps cut down the possible causes of errors in the more complex AWS IoT examples!
39+
40+
Run `make menuconfig`, navigate to "Example Configuration" and change "AWS IoT Certificate Source" to "Load from SD card".
41+
42+
Three new prompts will appear for filenames for the device key, device certificate and root CA certificate path. These paths start with `/sdcard/` as this is where the example mounts the (FAT formatted) SD card.
43+
44+
Copy the certificate and key files to the SD card, and make sure the file names match the names given in the example configuration (either rename the files, or change the config). For the Root CA certificate file (which is not device-specific), you can find the file in the `main/certs` directory or download it from AWS.
45+
46+
*Note: By default, esp-idf's FATFS support only allows 8.3 character filenames. However, the AWS IoT examples pre-configure the sdkconfig to enable long filenames. If you're setting up your projects, you will probably want to enable these options as well (under Component Config -> FAT Filesystem Support). You can also consider configure the FAT filesystem for read-only support, if you don't need to write to the SD card.*
47+
48+
## Find & Set AWS Endpoint Hostname
49+
50+
Your AWS IoT account has a unique endpoint hostname to connect to. To find it, open the AWS IoT Console and click the "Settings" button on the bottom left side. The endpoint hostname is shown under the "Custom Endpoint" heading on this page.
51+
52+
Run `make menuconfig` and navigate to `Component Config` -> `Amazon Web Service IoT Config` -> `AWS IoT MQTT Hostname`. Enter the host name here.
53+
54+
*Note: It may seem odd that you have to configure parts of the AWS settings under Component Config and some under Example Configuration.* The IoT MQTT Hostname and Port are set as part of the component because when using the AWS IoT SDK's Thing Shadow API (in examples or in other projects) the `ShadowInitParametersDefault` structure means the Thing Shadow connection will default to that host & port. You're not forced to use these config values in your own projects, you can set the values in code via the AWS IoT SDK's init parameter structures - `ShadowInitParameters_t` for Thing Shadow API or `IoT_Client_Init_Params` for MQTT API.
55+
56+
### (Optional) Set Client ID
57+
58+
Run `make menuconfig`. Under `Example Configuration`, set the `AWS IoT Client ID` to a unique value.
59+
60+
The Client ID is used in the MQTT protocol used to send messages to/from AWS IoT. AWS IoT requires that each connected device within a single AWS account uses a unique Client ID. Other than this restriction, the Client ID can be any value that you like. The example default should be fine if you're only connecting one ESP32 at a time.
61+
62+
In a production IoT app this ID would be set dynamically, but for these examples it is compiled in via menuconfig.
63+
64+
### (Optional) Locally Check The Root Certificate
65+
66+
The Root CA certificate provides a root-of-trust when the ESP32 connects to AWS IoT. We have supplied the root CA certificate already (in PEM format) in the file `main/certs/aws-root-ca.pem`.
67+
68+
If you want to locally verify that this Root CA certificate hasn't changed, you can run the following command against your AWS MQTT Host:
69+
70+
```
71+
openssl s_client -showcerts -connect hostname:8883 < /dev/null
72+
```
73+
74+
(Replace hostname with your AWS MQTT endpoint host.) The Root CA certificate is the last certificate in the list of certificates printed. You can copy-paste this in place of the existing `aws-root-ca.pem` file.
75+
76+
77+
# Troubleshooting
78+
79+
## Tips
80+
81+
* Raise the ESP debug log level to Debug in order to see messages about the connection to AWS, certificate contents, etc.
82+
83+
* Enable mbedTLS debugging (under Components -> mbedTLS -> mbedTLS Debug) in order to see even more low-level debug output from the mbedTLS layer.
84+
85+
* To create a successful AWS IoT connection, the following factors must all be present:
86+
- Endpoint hostname is correct for your AWS account.
87+
- Certificate & private key are both attached to correct Thing in AWS IoT Console.
88+
- Certificate is activated.
89+
- Policy is attached to the Certificate in AWS IoT Console.
90+
- Policy contains sufficient permissions to authorize AWS IoT connection.
91+
92+
## TLS connection fails
93+
94+
If connecting fails entirely (handshake doesn't complete), this usually indicates a problem with certification configuration. The error usually looks like this:
95+
96+
```
97+
aws_iot: failed! mbedtls_ssl_handshake returned -0x7780
98+
```
99+
100+
(0x7780 is the mbedTLS error code when the server sends an alert message and closes the connection.)
101+
102+
* Check your client private key and certificate file match a Certificate registered and **activated** in AWS IoT console. You can find the Certificate in IoT Console in one of two ways, via the Thing or via Certificates:
103+
- To find the Certificate directly, click on "Registry" -> "Security Certificates". Then click on the Certificate itself to view it.
104+
- To find the Certificate via the Thing, click on "Registry" -> "Things", then click on the particular Thing you are using. Click "Certificates" in the sidebar to view all Certificates attached to that Thing. Then click on the Certificate itself to view it.
105+
106+
Verify the Certificate is activated (when viewing the Certificate, it will say "ACTIVE" or "INACTIVE" near the top under the certificate name).
107+
108+
If the Certificate appears correct and activated, verify that you are connecting to the correct AWS IoT endpoint (see above.)
109+
110+
## TLS connection closes immediately
111+
112+
Sometimes connecting is successful (the handshake completes) but as soon as the client sends its `MQTT CONNECT` message the server sends back a TLS alert and closes the connection, without anything else happening.
113+
114+
The error returned from AWS IoT is usually -28 (`MQTT_REQUEST_TIMEOUT_ERROR`). You may also see mbedtls error `-0x7780` (server alert), although if this error comes during `mbedtls_ssl_handshake` then it's usually a different problem (see above).
115+
116+
In the subscribe_publish example, the error may look like this in the log:
117+
118+
```
119+
subpub: Error(-28) connecting to (endpoint)...
120+
```
121+
122+
In the thing_shadow example, the error may look like this in the log:
123+
124+
```
125+
shadow: aws_iot_shadow_connect returned error -28, aborting...
126+
```
127+
128+
This error implies the Certificate is recognised, but the Certificate is either missing the correct Thing or the correct Policy attached to it.
129+
130+
* Check in the AWS IoT console that your certificate is activated and has both a **security policy** and a **Thing** attached to it. You can find this in IoT Console by clicking "Registry" -> "Security Certificates", then click the Certificate. Once viewing the Certificate, you can click the "Policies" and "Things" links in the sidebar.
131+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
PROJECT_NAME := aws_iot_subpub
7+
8+
include $(IDF_PATH)/make/project.mk
9+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Amazon Web Services IoT MQTT Subscribe/Publish Example
2+
3+
This is an adaptation of the [AWS IoT C SDK](https://github.com/aws/aws-iot-device-sdk-embedded-C) "subscribe_publish" example for ESP-IDF.
4+
5+
# Configuration
6+
7+
See the README.md in the parent directory for information about configuring the AWS IoT examples.
8+
9+
# Monitoring MQTT Data from the device
10+
11+
After flashing the example to your ESP32, it should connect to Amazon and start subscribing/publishing MQTT data.
12+
13+
The example code publishes MQTT data to the topic `test_topic/esp32`. Amazon provides a web interface to subscribe to MQTT topics for testing:
14+
15+
* On the AWS IoT console, click "MQTT Client" near the top-right.
16+
* Click "Generate Client ID" to generate a random client ID.
17+
* Click "Connect"
18+
19+
One connection succeeds, you can subscribe to the data published by the ESP32:
20+
21+
* Click "Subscribe to Topic"
22+
* Enter "Subscription Topic" `test_topic/esp32`
23+
* Click "Subscribe"
24+
25+
... you should see MQTT data published from the running example.
26+
27+
To publish data back to the device:
28+
29+
* Click "Publish to Topic"
30+
* Enter "Publish Topic" `test_topic/esp32`
31+
* Enter a message in the payload field
32+
* Click Publish
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
menu "Example Configuration"
2+
3+
config WIFI_SSID
4+
string "WiFi SSID"
5+
default "myssid"
6+
help
7+
SSID (network name) for the example to connect to.
8+
9+
config WIFI_PASSWORD
10+
string "WiFi Password"
11+
default "mypassword"
12+
help
13+
WiFi password (WPA or WPA2) for the example to use.
14+
15+
Can be left blank if the network has no security set.
16+
17+
config AWS_EXAMPLE_CLIENT_ID
18+
string "AWS IoT Client ID"
19+
default "myesp32"
20+
help
21+
AWS IoT Client ID for the example. Should be unique for every device.
22+
23+
choice EXAMPLE_CERT_SOURCE
24+
prompt "AWS IoT Certificate Source"
25+
default EXAMPLE_EMBEDDED_CERTS
26+
help
27+
AWS IoT requires loading of a device-specific certificate and private key,
28+
and a common Root CA Certificate. These can be compiled into the example
29+
app, or they can be loaded via the filesystem from an SD card.
30+
31+
config EXAMPLE_EMBEDDED_CERTS
32+
bool "Embed into app"
33+
config EXAMPLE_SDCARD_CERTS
34+
bool "Load from SD card"
35+
select EXAMPLE_FILESYSTEM_CERTS
36+
endchoice
37+
38+
# Currently this is equivalent to EXAMPLE_SDCARD_CERTS,
39+
# however eventually we want to support more filesystem
40+
# sources (SPIFFS, etc.) So this hidden config item
41+
# is selected whenever the item should load from filesystem.
42+
config EXAMPLE_FILESYSTEM_CERTS
43+
bool
44+
45+
config EXAMPLE_CERTIFICATE_PATH
46+
string "Device Certificate Path"
47+
depends on EXAMPLE_FILESYSTEM_CERTS
48+
default "/sdcard/certificate.pem.crt"
49+
50+
config EXAMPLE_PRIVATE_KEY_PATH
51+
string "Device Private Key Path"
52+
depends on EXAMPLE_FILESYSTEM_CERTS
53+
default "/sdcard/private.pem.key"
54+
55+
config EXAMPLE_ROOT_CA_PATH
56+
string "Root CA Certificate Path"
57+
depends on EXAMPLE_FILESYSTEM_CERTS
58+
default "/sdcard/aws-root-ca.pem"
59+
60+
endmenu
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Copy certificate files for AWS IoT SDK example here
2+
3+
See README.md in main example directory for details.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB
3+
yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL
4+
ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp
5+
U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW
6+
ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0
7+
aG9yaXR5IC0gRzUwHhcNMDYxMTA4MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCByjEL
8+
MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZW
9+
ZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJpU2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6BFoMtEMze/aiCKm0oHw0LxOXnGiYZ
26+
4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N
27+
hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq
28+
-----END CERTIFICATE-----
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Main Makefile. This is basically the same as a component makefile.
3+
#
4+
5+
ifdef CONFIG_EXAMPLE_EMBEDDED_CERTS
6+
# Certificate files. certificate.pem.crt & private.pem.key must be downloaded
7+
# from AWS, see README for details.
8+
COMPONENT_EMBED_TXTFILES := certs/aws-root-ca.pem certs/certificate.pem.crt certs/private.pem.key
9+
10+
ifndef IDF_CI_BUILD
11+
# Print an error if the certificate/key files are missing
12+
$(COMPONENT_PATH)/certs/certificate.pem.crt $(COMPONENT_PATH)/certs/private.pem.key:
13+
@echo "Missing PEM file $@. This file identifies the ESP32 to AWS for the example, see README for details."
14+
exit 1
15+
else # IDF_CI_BUILD
16+
# this case is for the internal Continuous Integration build which
17+
# compiles all examples. Add some dummy certs so the example can
18+
# compile (even though it won't work)
19+
$(COMPONENT_PATH)/certs/certificate.pem.crt $(COMPONENT_PATH)/certs/private.pem.key:
20+
echo "Dummy certificate data for continuous integration" > $@
21+
endif
22+
endif

0 commit comments

Comments
 (0)