This example accompanies the tutorial How to Connect to AWS IoT Core using MQTT & ALPN. It demonstrates how to create an MQTT connection to AWS IoT Core using ALPN and mutual TLS. The same example is intended to work with the Mako Server and with Xedge32.
If you are using an ESP32, also see the related tutorial How to Connect ESP32 to AWS IoT Core Using MQTT.
www/.preload- Loads the MQTT example, creates the SharkSSL configuration, connects to AWS IoT Core, subscribes to two topics, and publishes JSON test messages once the connection is established.
Prepare the AWS credentials and certificates first:
- Follow the Amazon video tutorial referenced in the article.
- Download AmazonRootCA1.pem and save it in the
wwwdirectory. - Unzip
connect_device_package.zip. - Copy
Demo_Thing.cert.pemandDemo_Thing.private.keyinto thewwwdirectory. - Open
start.shfrom the AWS package and copy the broker endpoint after the-eoption. - Open
www/.preloadand setawsBroker = ""to your broker endpoint.
Then start the example:
cd AWS-MQTT
mako -l::wwwFor more detail on starting the Mako Server, see the command line video tutorial and the command line options documentation.
The startup script creates a certificate store, loads the Amazon root CA and the device certificate/key pair, and builds a SharkSSL object for the AWS mutual TLS handshake. It then creates an MQTT client configured for port 443 with alpn = "x-amzn-mqtt-ca". When the connection succeeds, the script subscribes to topic_1 and topic_2, starts a timer, and publishes a JSON message to topic_1 once per second. Incoming published data is decoded from JSON and printed to the trace output.
The script also unlinks the application directory when running as an LSP app so that the private key files are not exposed through HTTP.
- The example will not connect until
awsBrokeris set correctly inwww/.preload. - The certificate and key filenames in the script must match the files you copied into
www/. - If you stop the app,
onunload()cancels the timer and disconnects the MQTT client cleanly.