samples/net/cloud/aws_iot_mqtt/README.html · samples/net/cloud/aws_iot_mqtt/README.rst 공식 원문 ↗

Zephyr 3.7.0 · Samples · Networking · Cloud

AWS IoT Core MQTT

DHCP·SNTP·certificate 인증부터 DQP, keep-alive와 exponential backoff까지 구성합니다.

Official pathsamples/net/cloud/aws_iot_mqtt/README.html
Source filesamples/net/cloud/aws_iot_mqtt/README.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Thing credential 변환, 7개 Kconfig option, MQTT test client와 QEMU NAT 실행을 원문 구조대로 설명합니다.

Cloud

AWS IoT Core

Security

TLS 1.2

Reliability

Keep-alive · Backoff

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 70E15B5D894521D966416B976C518FD776BFB5D6E4877622F00270D2FFD3E846
.. zephyr:code-sample:: aws-iot-mqtt
   :name: AWS IoT Core MQTT
   :relevant-api: bsd_sockets mqtt_socket dns_resolve tls_credentials json sntp random_api

   Connect to AWS IoT Core and publish messages using MQTT.

Overview
********

This sample application demonstrates the implementation of an MQTT client that
can publish messages to AWS IoT Core using the MQTT protocol. Key features include:

- Acquiring a DHCPv4 lease
- Connecting to an SNTP server to obtain the current time
- Establishing a TLS 1.2 connection with AWS IoT Core servers
- Subscribing to a topic on AWS IoT Core
- Publishing data to AWS IoT Core
- Passing the AWS Device Qualification Program (DQP) test suite: `Device Qualification Program (DQP) <https://aws.amazon.com/partners/programs/dqp/>`_
- Sending and receiving keep-alive pings
- Retrying connections using an exponential backoff algorithm

Requirements
************

- An entropy source
- An AWS account with access to AWS IoT Core
- AWS credentials and necessary information
- Network connectivity

Building and Running
********************

This application has been built and tested on the ST NUCLEO-F429ZI board and
QEMU x86 target. A valid certificate and private key are required to
authenticate to the AWS IoT Core. The sample includes a script to convert
the certificate and private key in order to embed them in the application.

Register a *thing* in AWS IoT Core and download the certificate and private key.
Copy these files to the :zephyr_file:`samples/net/cloud/aws_iot_mqtt/src/creds`
directory. Run the :zephyr_file:`samples/net/cloud/aws_iot_mqtt/src/creds/convert_keys.py`
script, which will generate files ``ca.c``, ``cert.c`` and ``key.c``.

To configure the sample, set the following Kconfig options based on your AWS IoT
Core region, thing, and device advisor configuration:

- :kconfig:option:`CONFIG_AWS_ENDPOINT`: The AWS IoT Core broker endpoint, found in the AWS IoT Core
  console. This will be specific if running a test suite using device advisor.
- :kconfig:option:`CONFIG_AWS_MQTT_PORT`: Port number for AWS IoT Core MQTT broker.
- :kconfig:option:`CONFIG_AWS_THING_NAME`: The name of the thing created in AWS IoT Core. Associated
  with the certificate it will be used as the client id. We will use
  ``zephyr_sample`` in this example.
- :kconfig:option:`CONFIG_AWS_SUBSCRIBE_TOPIC`: The topic to subscribe to.
- :kconfig:option:`CONFIG_AWS_PUBLISH_TOPIC`: The topic to publish to.
- :kconfig:option:`CONFIG_AWS_QOS`: The QoS level for subscriptions and publications.
- :kconfig:option:`CONFIG_AWS_EXPONENTIAL_BACKOFF`: Enable the exponential backoff algorithm.

Refer to the `AWS IoT Core Documentation <https://docs.aws.amazon.com/iot/index.html>`_
for more information.

Additionnaly, it is possible to tune the firmware to pass the AWS DQP test
suite, to do set Kconfig option :kconfig:option:`CONFIG_AWS_TEST_SUITE_DQP` to ``y``.

More information about the AWS device advisor can be found here:
`AWS IoT Core Device Advisor <https://aws.amazon.com/iot-core/device-advisor/>`_.

MQTT test client
================

Access the MQTT test client in the AWS IoT Core console, subscribe to the
``zephyr_sample/data`` topic, and publish a payload to the ``zephyr_sample/downlink``
topic. The device console will display the payload received by your device, and
the AWS console will show the JSON message sent by the device under the
``zephyr_sample/data`` topic.

Sample output
=============

This is the output from the ST-Link UART on the NUCLEO-F429ZI board.

.. code-block:: console

  *** Booting Zephyr OS build zephyr-v3.3.0 ***
  [00:00:01.626,000] <inf> aws: starting DHCPv4
  [00:00:01.969,000] <dbg> aws: sntp_sync_time: Acquired time from NTP server: 1683472436
  [00:00:01.977,000] <inf> aws: Resolved: 52.212.60.110:8883
  [00:00:03.327,000] <dbg> aws: mqtt_event_cb: MQTT event: CONNACK [0] result: 0
  [00:00:03.327,000] <inf> aws: Subscribing to 1 topic(s)
  [00:00:03.390,000] <dbg> aws: mqtt_event_cb: MQTT event: SUBACK [7] result: 0
  [00:00:03.390,000] <inf> aws: PUBLISHED on topic "zephyr_sample/data" [ id: 1 qos: 0 ], payload: 13 B
  [00:00:03.390,000] <dbg> aws: publish_message: Published payload:
                                7b 22 63 6f 75 6e 74 65  72 22 3a 30 7d          |{"counte r":0}
  [00:00:11.856,000] <dbg> aws: mqtt_event_cb: MQTT event: PUBLISH [2] result: 0
  [00:00:11.856,000] <inf> aws: RECEIVED on topic "zephyr_sample/downlink" [ id: 13 qos: 0 ] payload: 45 / 4096 B
  [00:00:11.856,000] <dbg> aws: handle_published_message: Received payload:
                                7b 0a 20 20 22 6d 65 73  73 61 67 65 22 3a 20 22 |{.  "mes sage": "
                                48 65 6c 6c 6f 20 66 72  6f 6d 20 41 57 53 20 49 |Hello fr om AWS I
                                6f 54 20 63 6f 6e 73 6f  6c 65 22 0a 7d          |oT conso le".}
  [00:00:11.857,000] <inf> aws: PUBLISHED on topic "zephyr_sample/data" [ id: 2 qos: 0 ], payload: 13 B
  [00:00:11.857,000] <dbg> aws: publish_message: Published payload:
                                7b 22 63 6f 75 6e 74 65  72 22 3a 31 7d          |{"counte r":1}
  [00:01:11.755,000] <dbg> aws: mqtt_event_cb: MQTT event: 9 result: 0
  [00:02:11.755,000] <dbg> aws: mqtt_event_cb: MQTT event: 9 result: 0

Run in QEMU x86
===============

The sample can be run in QEMU x86. To do so, you will need to configure
NAT/MASQUERADE on your host machine. Refer to the Zephyr documentation
:ref:`networking_with_qemu`. for more information.

Part 3

한국어 전문 번역

개요

이 sample application은 MQTT protocol을 사용해 AWS IoT Core로 message를 publish할 수 있는 MQTT client 구현을 보여줍니다. 주요 기능은 다음과 같습니다.

  • DHCPv4 lease 획득
  • SNTP server에 연결해 현재 시간 획득
  • AWS IoT Core server와 TLS 1.2 연결 수립
  • AWS IoT Core topic 구독
  • AWS IoT Core로 data publish
  • AWS Device Qualification Program(DQP) test suite 통과
  • Keep-alive ping 송수신
  • Exponential backoff algorithm을 사용한 연결 재시도

요구 사항

  • Entropy source
  • AWS IoT Core에 접근할 수 있는 AWS account
  • AWS credential과 필요한 설정 정보
  • Network 연결

Build와 실행

이 application은 ST NUCLEO-F429ZI board와 QEMU x86 target에서 build하고 시험했습니다. AWS IoT Core 인증에는 유효한 certificate와 private key가 필요합니다. Sample에는 이 두 파일을 application에 embed할 수 있는 형태로 변환하는 script가 포함되어 있습니다.

AWS IoT Core에 thing을 등록하고 certificate와 private key를 내려받습니다. 파일을 samples/net/cloud/aws_iot_mqtt/src/creds directory에 복사한 뒤 samples/net/cloud/aws_iot_mqtt/src/creds/convert_keys.py를 실행합니다. Script는 ca.c, cert.c, key.c를 생성합니다.

AWS IoT Core region, thing, Device Advisor 설정에 맞춰 다음 Kconfig option을 지정합니다.

  • CONFIG_AWS_ENDPOINT: AWS IoT Core console에서 확인한 broker endpoint입니다. Device Advisor test suite를 실행하면 전용 endpoint가 사용됩니다.
  • CONFIG_AWS_MQTT_PORT: AWS IoT Core MQTT broker의 port 번호입니다.
  • CONFIG_AWS_THING_NAME: AWS IoT Core에서 생성한 thing 이름입니다. Certificate와 연결되어 client ID로 사용되며, 이 예에서는 zephyr_sample을 사용합니다.
  • CONFIG_AWS_SUBSCRIBE_TOPIC: 구독할 topic입니다.
  • CONFIG_AWS_PUBLISH_TOPIC: publish할 topic입니다.
  • CONFIG_AWS_QOS: subscribe와 publish에 사용할 QoS level입니다.
  • CONFIG_AWS_EXPONENTIAL_BACKOFF: exponential backoff algorithm을 활성화합니다.

자세한 내용은 AWS IoT Core Documentation을 참고하십시오.

Firmware가 AWS DQP test suite를 통과하도록 조정할 수도 있습니다. 이 경우 CONFIG_AWS_TEST_SUITE_DQP=y를 설정합니다.

AWS IoT Core Device Advisor에서 Device Advisor 관련 정보를 확인할 수 있습니다.

MQTT test client

AWS IoT Core console에서 MQTT test client를 열고 zephyr_sample/data topic을 구독한 다음 zephyr_sample/downlink topic으로 payload를 publish합니다.

Device console에는 device가 받은 payload가 표시되고, AWS console의 zephyr_sample/data topic에는 device가 보낸 JSON message가 나타납니다.

Sample 출력

다음은 NUCLEO-F429ZI board의 ST-Link UART 출력입니다. DHCP와 SNTP가 완료된 뒤 port 8883의 AWS endpoint를 resolve하고, MQTT CONNACK·SUBACK 이후 uplink JSON을 publish합니다. Downlink message를 받으면 payload를 표시하고 keep-alive event를 계속 처리합니다.

*** Booting Zephyr OS build zephyr-v3.3.0 ***
[00:00:01.626,000] <inf> aws: starting DHCPv4
[00:00:01.969,000] <dbg> aws: sntp_sync_time: Acquired time from NTP server: 1683472436
[00:00:01.977,000] <inf> aws: Resolved: 52.212.60.110:8883
[00:00:03.327,000] <dbg> aws: mqtt_event_cb: MQTT event: CONNACK [0] result: 0
[00:00:03.327,000] <inf> aws: Subscribing to 1 topic(s)
[00:00:03.390,000] <dbg> aws: mqtt_event_cb: MQTT event: SUBACK [7] result: 0
[00:00:03.390,000] <inf> aws: PUBLISHED on topic "zephyr_sample/data" [ id: 1 qos: 0 ], payload: 13 B
[00:00:03.390,000] <dbg> aws: publish_message: Published payload:
                              7b 22 63 6f 75 6e 74 65  72 22 3a 30 7d          |{"counte r":0}
[00:00:11.856,000] <dbg> aws: mqtt_event_cb: MQTT event: PUBLISH [2] result: 0
[00:00:11.856,000] <inf> aws: RECEIVED on topic "zephyr_sample/downlink" [ id: 13 qos: 0 ] payload: 45 / 4096 B
[00:00:11.856,000] <dbg> aws: handle_published_message: Received payload:
                              7b 0a 20 20 22 6d 65 73  73 61 67 65 22 3a 20 22 |{.  "mes sage": "
                              48 65 6c 6c 6f 20 66 72  6f 6d 20 41 57 53 20 49 |Hello fr om AWS I
                              6f 54 20 63 6f 6e 73 6f  6c 65 22 0a 7d          |oT conso le".}
[00:00:11.857,000] <inf> aws: PUBLISHED on topic "zephyr_sample/data" [ id: 2 qos: 0 ], payload: 13 B
[00:00:11.857,000] <dbg> aws: publish_message: Published payload:
                              7b 22 63 6f 75 6e 74 65  72 22 3a 31 7d          |{"counte r":1}
[00:01:11.755,000] <dbg> aws: mqtt_event_cb: MQTT event: 9 result: 0
[00:02:11.755,000] <dbg> aws: mqtt_event_cb: MQTT event: 9 result: 0

QEMU x86에서 실행

이 sample은 QEMU x86에서도 실행할 수 있습니다. Host machine에 NAT/MASQUERADE를 설정해야 하며 자세한 내용은 QEMU network 설정을 참고하십시오.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.