Part 1
요약·해설
FRDM-K64F build, overlay template, PUBACK 출력과 Azure Portal의 cloud-to-device 절차를 보존합니다.
Azure IoT Hub
SAS token
SOCKS5
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
E0250F81D5EC2B64426D0011EF7B589262B4E11BDDEB08E955E186A330BBBC16
.. zephyr:code-sample:: mqtt-azure
:name: Microsoft Azure IoT Hub MQTT
:relevant-api: bsd_sockets mqtt_socket tls_credentials random_api
Connect to Azure IoT Hub and publish messages using MQTT.
Overview
********
This sample application demonstrates how an MQTT client
can publish messages to an Azure Cloud IoT hub based on MQTT protocol.
- Acquire a DHCPv4 lease
- Establish a TLS connection with Azure Cloud IoT hub
- Publish data to the Azure cloud
- SOCKS5 supported
- DNS supported
The source code of this sample application can be found at:
:zephyr_file:`samples/net/cloud/mqtt_azure`.
Requirements
************
- Azure Cloud account
- Azure IOT Cloud credentials and required information
- Freedom Board (FRDM-K64F)
- Network connectivity
Building and Running
********************
This application has been built and tested on the NXP FRDMK64F.
Certs are required to authenticate to the Azure Cloud IoT hub.
Current certs in :zephyr_file:`samples/net/cloud/mqtt_azure/src/digicert.cer` are
copied from `<https://github.com/Azure/azure-iot-sdk-c/blob/master/certs/certs.c>`_
Configure the following Kconfig options based on your Azure Cloud IoT Hub
in your own overlay config file:
- ``SAMPLE_CLOUD_AZURE_USERNAME`` - Username field use::
{iothubhostname}/{device_id}/?api-version=2018-06-30,
where ``{iothubhostname}`` is the full CName of the IoT hub.
- ``SAMPLE_CLOUD_AZURE_PASSWORD`` - Password field, use an SAS token.
- ``SAMPLE_CLOUD_AZURE_CLIENT_ID`` - ClientId field, use the deviceId.
- ``SAMPLE_CLOUD_AZURE_HOSTNAME`` - IoT hub hostname
- ``SAMPLE_CLOUD_AZURE_SERVER_ADDR`` - IP address of the Azure MQTT broker
- ``SAMPLE_CLOUD_AZURE_SERVER_PORT`` - Port number of the Azure MQTT broker
You'll also need to set these Kconfig options if you're running
the sample behind a proxy:
- ``SAMPLE_SOCKS_ADDR`` - IP address of SOCKS5 Proxy server
- ``SAMPLE_SOCKS_PORT`` - Port number of SOCKS5 Proxy server
On your Linux host computer, open a terminal window, locate the source code
of this sample application (i.e., :zephyr_file:`samples/net/cloud/mqtt_azure`) and type:
.. zephyr-app-commands::
:zephyr-app: samples/net/cloud/mqtt_azure
:board: frdm_k64f
:conf: "prj.conf <overlay.conf>"
:goals: build flash
:compact:
Also this application can be tested with QEMU. This is described in
:ref:`networking_with_qemu`. Set up Zephyr and NAT/masquerading on host
to access Internet and use :file:`overlay-qemu_x86.conf`.
DHCP support is not enabled with QEMU. It uses static IP addresses.
Sample overlay file
===================
This is the overlay template for Azure IoT hub and other details:
.. code-block:: cfg
CONFIG_SAMPLE_CLOUD_AZURE_USERNAME="<username>"
CONFIG_SAMPLE_CLOUD_AZURE_PASSWORD="<SAS token>"
CONFIG_SAMPLE_CLOUD_AZURE_CLIENT_ID="<device id>"
CONFIG_SAMPLE_CLOUD_AZURE_HOSTNAME="<IoT hub hostname>"
CONFIG_SAMPLE_SOCKS_ADDR="<proxy addr>"
CONFIG_SAMPLE_SOCKS_PORT=<proxy port>
CONFIG_SAMPLE_CLOUD_AZURE_SERVER_ADDR="<server ip addr, if DNS disabled set this>"
CONFIG_SAMPLE_CLOUD_AZURE_SERVER_PORT=<server port, if DNS disabled set this>
Sample output
=============
This is the output from the FRDM UART console, with:
.. code-block:: console
[00:00:03.001,000] <inf> eth_mcux: Enabled 100M full-duplex mode.
[00:00:03.010,000] <dbg> mqtt_azure.main: Waiting for network to setup...
[00:00:03.115,000] <inf> net_dhcpv4: Received: 10.0.0.2
[00:00:03.124,000] <inf> net_config: IPv4 address: 10.0.0.2
[00:00:03.132,000] <inf> net_config: Lease time: 43200 seconds
[00:00:03.140,000] <inf> net_config: Subnet: 255.255.255.0
[00:00:03.149,000] <inf> net_config: Router: 10.0.0.10
[00:00:06.157,000] <dbg> mqtt_azure.try_to_connect: attempting to connect...
[00:00:06.167,000] <dbg> net_sock_tls.tls_alloc: (0x200024f8): Allocated TLS context, 0x20001110
[00:00:19.412,000] <dbg> mqtt_azure.mqtt_event_handler: MQTT client connected!
[00:00:19.424,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
[00:00:19.830,000] <dbg> mqtt_azure.mqtt_event_handler: PUBACK packet id: 63387
[00:00:31.842,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
[00:00:51.852,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
[00:00:51.861,000] <dbg> mqtt_azure.mqtt_event_handler: PUBACK packet id: 38106
You can also check events or messages information on Azure Portal.
Cloud to device communication
=============================
Goto IoT devices section in Azure Portal. Click on the device from
IoT devices. If you have configured multiple devices, select correct device.
Goto Message to Device section. Enter text in Message Body section.
Click on Send Message.
See `Azure Cloud MQTT Documentation
<https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support>`_.
Part 3
한국어 전문 번역
개요
이 sample application은 MQTT client가 MQTT protocol 기반의 Azure Cloud IoT Hub로 message를 publish하는 방법을 보여줍니다.
- DHCPv4 lease 획득
- Azure Cloud IoT Hub와 TLS 연결 수립
- Azure cloud로 data publish
- SOCKS5 지원
- DNS 지원
Sample application source code는 samples/net/cloud/mqtt_azure에 있습니다.
요구 사항
- Azure Cloud account
- Azure IoT Cloud credential과 필요한 설정 정보
- Freedom Board FRDM-K64F
- Network 연결
Build와 실행
이 application은 NXP FRDM-K64F에서 build하고 시험했습니다. Azure Cloud IoT Hub 인증에는 certificate가 필요합니다. samples/net/cloud/mqtt_azure/src/digicert.cer의 현재 certificate는 Azure IoT C SDK의 certs/certs.c에서 복사한 것입니다.
자체 overlay config file에서 Azure Cloud IoT Hub 설정에 맞춰 다음 Kconfig option을 지정합니다.
CONFIG_SAMPLE_CLOUD_AZURE_USERNAME은 username field이며 다음 형식을 사용합니다.
{iothubhostname}/{device_id}/?api-version=2018-06-30,
여기서 {iothubhostname}은 IoT Hub의 전체 CName입니다.
CONFIG_SAMPLE_CLOUD_AZURE_PASSWORD: Password field이며 SAS token을 사용합니다.CONFIG_SAMPLE_CLOUD_AZURE_CLIENT_ID: ClientId field이며 deviceId를 사용합니다.CONFIG_SAMPLE_CLOUD_AZURE_HOSTNAME: IoT Hub hostname입니다.CONFIG_SAMPLE_CLOUD_AZURE_SERVER_ADDR: Azure MQTT broker의 IP address입니다.CONFIG_SAMPLE_CLOUD_AZURE_SERVER_PORT: Azure MQTT broker의 port 번호입니다.
Proxy 뒤에서 sample을 실행한다면 다음 Kconfig option도 설정해야 합니다.
CONFIG_SAMPLE_SOCKS_ADDR: SOCKS5 proxy server의 IP address입니다.CONFIG_SAMPLE_SOCKS_PORT: SOCKS5 proxy server의 port 번호입니다.
Linux host에서 terminal을 열고 samples/net/cloud/mqtt_azure source directory를 기준으로 다음과 같이 build하고 flash합니다.
west build -b frdm_k64f samples/net/cloud/mqtt_azure -- -DCONF_FILE="prj.conf <overlay.conf>"
west flash
이 application은 QEMU에서도 시험할 수 있습니다. QEMU network 설정에 따라 host에 Zephyr와 NAT/masquerading을 설정해 Internet에 접근하고 overlay-qemu_x86.conf를 사용합니다. QEMU에서는 DHCP가 활성화되지 않으며 static IP address를 사용합니다.
Sample overlay file
다음은 Azure IoT Hub와 나머지 접속 정보를 지정하는 overlay template입니다.
CONFIG_SAMPLE_CLOUD_AZURE_USERNAME="<username>"
CONFIG_SAMPLE_CLOUD_AZURE_PASSWORD="<SAS token>"
CONFIG_SAMPLE_CLOUD_AZURE_CLIENT_ID="<device id>"
CONFIG_SAMPLE_CLOUD_AZURE_HOSTNAME="<IoT hub hostname>"
CONFIG_SAMPLE_SOCKS_ADDR="<proxy addr>"
CONFIG_SAMPLE_SOCKS_PORT=<proxy port>
CONFIG_SAMPLE_CLOUD_AZURE_SERVER_ADDR="<server ip addr, if DNS disabled set this>"
CONFIG_SAMPLE_CLOUD_AZURE_SERVER_PORT=<server port, if DNS disabled set this>
Sample 출력
다음은 FRDM UART console 출력입니다. DHCP lease를 받은 뒤 TLS context를 할당해 MQTT client를 연결하고, publish마다 broker의 PUBACK을 받습니다.
[00:00:03.001,000] <inf> eth_mcux: Enabled 100M full-duplex mode.
[00:00:03.010,000] <dbg> mqtt_azure.main: Waiting for network to setup...
[00:00:03.115,000] <inf> net_dhcpv4: Received: 10.0.0.2
[00:00:03.124,000] <inf> net_config: IPv4 address: 10.0.0.2
[00:00:03.132,000] <inf> net_config: Lease time: 43200 seconds
[00:00:03.140,000] <inf> net_config: Subnet: 255.255.255.0
[00:00:03.149,000] <inf> net_config: Router: 10.0.0.10
[00:00:06.157,000] <dbg> mqtt_azure.try_to_connect: attempting to connect...
[00:00:06.167,000] <dbg> net_sock_tls.tls_alloc: (0x200024f8): Allocated TLS context, 0x20001110
[00:00:19.412,000] <dbg> mqtt_azure.mqtt_event_handler: MQTT client connected!
[00:00:19.424,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
[00:00:19.830,000] <dbg> mqtt_azure.mqtt_event_handler: PUBACK packet id: 63387
[00:00:31.842,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
[00:00:51.852,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
[00:00:51.861,000] <dbg> mqtt_azure.mqtt_event_handler: PUBACK packet id: 38106
Azure Portal에서도 event와 message 정보를 확인할 수 있습니다.
Cloud-to-device 통신
Azure Portal의 IoT devices 절로 이동해 device를 선택합니다. Device를 여러 개 설정했다면 올바른 device를 선택해야 합니다. Message to Device 절로 이동해 Message Body에 text를 입력하고 Send Message를 누릅니다.
자세한 내용은 Azure Cloud MQTT Documentation을 참고하십시오.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.