README.rst
1.. zephyr:code-sample:: aws-iot-mqtt
2 :name: AWS IoT Core MQTT
3 :relevant-api: bsd_sockets mqtt_socket dns_resolve tls_credentials json sntp random_api
4
5 Connect to AWS IoT Core and publish messages using MQTT.
6
7Overview
8********
9
10This sample application demonstrates the implementation of an MQTT client that
11can publish messages to AWS IoT Core using the MQTT protocol. Key features include:
12
13- Acquiring a DHCPv4 lease
14- Connecting to an SNTP server to obtain the current time
15- Establishing a TLS 1.2 connection with AWS IoT Core servers
16- Subscribing to a topic on AWS IoT Core
17- Publishing data to AWS IoT Core
18- Passing the AWS Device Qualification Program (DQP) test suite: `Device Qualification Program (DQP) <https://aws.amazon.com/partners/programs/dqp/>`_
19- Sending and receiving keep-alive pings
20- Retrying connections using an exponential backoff algorithm
21
22Requirements
23************
24
25- An entropy source
26- An AWS account with access to AWS IoT Core
27- AWS credentials and necessary information
28- Network connectivity
29
30Building and Running
31********************
32
33This application has been built and tested on the ST NUCLEO-F429ZI board and
34QEMU x86 target. A valid certificate and private key are required to
35authenticate to the AWS IoT Core. The sample includes a script to convert
36the certificate and private key in order to embed them in the application.
37
38Register a *thing* in AWS IoT Core and download the certificate and private key.
39Copy these files to the :zephyr_file:`samples/net/cloud/aws_iot_mqtt/src/creds`
40directory. Run the :zephyr_file:`samples/net/cloud/aws_iot_mqtt/src/creds/convert_keys.py`
41script, which will generate files ``ca.c``, ``cert.c`` and ``key.c``.
42
43To configure the sample, set the following Kconfig options based on your AWS IoT
44Core region, thing, and device advisor configuration:
45
46- :kconfig:option:`CONFIG_AWS_ENDPOINT`: The AWS IoT Core broker endpoint, found in the AWS IoT Core
47 console. This will be specific if running a test suite using device advisor.
48- :kconfig:option:`CONFIG_AWS_THING_NAME`: The name of the thing created in AWS IoT Core. Associated
49 with the certificate it will be used as the client id. We will use
50 ``zephyr_sample`` in this example.
51- :kconfig:option:`CONFIG_AWS_SUBSCRIBE_TOPIC`: The topic to subscribe to.
52- :kconfig:option:`CONFIG_AWS_PUBLISH_TOPIC`: The topic to publish to.
53- :kconfig:option:`CONFIG_AWS_QOS`: The QoS level for subscriptions and publications.
54- :kconfig:option:`CONFIG_AWS_EXPONENTIAL_BACKOFF`: Enable the exponential backoff algorithm.
55
56Refer to the `AWS IoT Core Documentation <https://docs.aws.amazon.com/iot/index.html>`_
57for more information.
58
59Additionnaly, it is possible to tune the firmware to pass the AWS DQP test
60suite, to do set Kconfig option :kconfig:option:`CONFIG_AWS_TEST_SUITE_DQP` to ``y``.
61
62More information about the AWS device advisor can be found here:
63`AWS IoT Core Device Advisor <https://aws.amazon.com/iot-core/device-advisor/>`_.
64
65MQTT test client
66================
67
68Access the MQTT test client in the AWS IoT Core console, subscribe to the
69``zephyr_sample/data`` topic, and publish a payload to the ``zephyr_sample/downlink``
70topic. The device console will display the payload received by your device, and
71the AWS console will show the JSON message sent by the device under the
72``zephyr_sample/data`` topic.
73
74Sample output
75=============
76
77This is the output from the ST-Link UART on the NUCLEO-F429ZI board.
78
79.. code-block:: console
80
81 *** Booting Zephyr OS build zephyr-v3.3.0 ***
82 [00:00:01.626,000] <inf> aws: starting DHCPv4
83 [00:00:01.969,000] <dbg> aws: sntp_sync_time: Acquired time from NTP server: 1683472436
84 [00:00:01.977,000] <inf> aws: Resolved: 52.212.60.110:8883
85 [00:00:03.327,000] <dbg> aws: mqtt_event_cb: MQTT event: CONNACK [0] result: 0
86 [00:00:03.327,000] <inf> aws: Subscribing to 1 topic(s)
87 [00:00:03.390,000] <dbg> aws: mqtt_event_cb: MQTT event: SUBACK [7] result: 0
88 [00:00:03.390,000] <inf> aws: PUBLISHED on topic "zephyr_sample/data" [ id: 1 qos: 0 ], payload: 13 B
89 [00:00:03.390,000] <dbg> aws: publish_message: Published payload:
90 7b 22 63 6f 75 6e 74 65 72 22 3a 30 7d |{"counte r":0}
91 [00:00:11.856,000] <dbg> aws: mqtt_event_cb: MQTT event: PUBLISH [2] result: 0
92 [00:00:11.856,000] <inf> aws: RECEIVED on topic "zephyr_sample/downlink" [ id: 13 qos: 0 ] payload: 45 / 4096 B
93 [00:00:11.856,000] <dbg> aws: handle_published_message: Received payload:
94 7b 0a 20 20 22 6d 65 73 73 61 67 65 22 3a 20 22 |{. "mes sage": "
95 48 65 6c 6c 6f 20 66 72 6f 6d 20 41 57 53 20 49 |Hello fr om AWS I
96 6f 54 20 63 6f 6e 73 6f 6c 65 22 0a 7d |oT conso le".}
97 [00:00:11.857,000] <inf> aws: PUBLISHED on topic "zephyr_sample/data" [ id: 2 qos: 0 ], payload: 13 B
98 [00:00:11.857,000] <dbg> aws: publish_message: Published payload:
99 7b 22 63 6f 75 6e 74 65 72 22 3a 31 7d |{"counte r":1}
100 [00:01:11.755,000] <dbg> aws: mqtt_event_cb: MQTT event: 9 result: 0
101 [00:02:11.755,000] <dbg> aws: mqtt_event_cb: MQTT event: 9 result: 0
102
103Run in QEMU x86
104===============
105
106The sample can be run in QEMU x86. To do so, you will need to configure
107NAT/MASQUERADE on your host machine. Refer to the Zephyr documentation
108:ref:`networking_with_qemu`. for more information.
109