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

Zephyr 3.7.0 · Samples · Networking · Secure MQTT

Secure MQTT Sensor/Actuator

DHCPv4·TLS 1.2·JSON sensor publish·MQTT command subscription을 하나의 device workflow로 구성합니다.

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

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

Part 1

요약·해설

Secure·static·insecure 세 configuration, CA certificate provisioning과 실제 QoS 1 sensor/LED log를 보존합니다.

Security

TLS 1.2

Sensor

ambient-temp0 · JSON

Actuator

led_on · led_off

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 3844A2A6AE5225346B46B2138790BFAA48E3ADD9323746223908F17A0FE4C0CA
.. zephyr:code-sample:: secure-mqtt-sensor-actuator
   :name: Secure MQTT Sensor/Actuator
   :relevant-api: mqtt_socket sensor_interface

   Implement an MQTT-based IoT sensor/actuator device

Overview
********

This sample demonstrates the implementation of an IoT sensor/actuator device.
The application uses the MQTT protocol to securely send sensor data
to a remote MQTT broker, while responding to commands received over MQTT.

Features:

- Establishing network connectivity using a DHCPv4 lease
- Establishing a secure MQTT connection (using TLS 1.2) to MQTT broker
- Publishing temperature sensor data in JSON format to the MQTT broker at a user-defined interval
- Subscribing to user-defined topic(s) on MQTT broker
- Responding to commands received over the network (LED control)
- Handling of MQTT connection, re-connecting and keep-alive
- Network status LED

Requirements
************
- Board with network capability (tested with adi_eval_adin1110ebz)
- `Eclipse Mosquitto`_ MQTT broker
- DHCP server
- Network connection between the board and Mosquitto broker

Build and Running
*****************
This application relies on an network connection between the board and an MQTT broker.
This broker can exist locally (e.g. on a host PC) or a publicly available MQTT broker
<https://test.mosquitto.org/> can be used.
For quick sampling/testing, a configuration is provided to connect to a local MQTT broker
without security, using a static IP address.

Hardware Setup
==============
If using Ethernet, connect the board to the MQTT broker. This may be your host PC
(for locally hosted Mosquitto broker) or your internet router
(to connect to the public Mosquitto broker).
If required, connect a temperature sensor to the board.

Software Setup
==============
The temperature sensor should be aliased in devicetree as ``ambient-temp0``.
If a board does not include an on-board temperature sensor, one can be connected externally
and a board overlay file used to add the sensor and alias:

.. code-block:: devicetree

	/ {
		aliases {
				ambient-temp0 = &adt7420;
			};
		};
	};

It is possible to use other types of sensors, by adding them in devicetree and by changing
``SENSOR_CHAN`` :file:`in device.c` to match the desired sensor type.

There are a few ways to configure the application:

.. list-table::

   * - :file:`prj.conf`
     - Default config: Secure MQTT, dynamic IP address (DHCP)

   * - :file:`overlay-static.conf`
     - Secure MQTT, static IP address

   * - :file:`overlay-static-insecure.conf`
     - Unsecure MQTT, static IP address

**Default Config:**

Using the default config, the application will use DHCP to acquire an IP address and attempt
to securely connect to an MQTT broker using TLS 1.2.

- The MQTT broker to which the board will connect is specified by
  ``CONFIG_NET_SAMPLE_MQTT_BROKER_HOSTNAME``.
  By default, this is set to test.mosquitto.org.
- Connecting securely using TLS, requires the inclusion of the broker's CA certificate
  in the application.
- Download the CA certificate in DER or PEM format from https://test.mosquitto.org
- In :file:`tls_config/cert.h`, set ``ca_certificate[]`` to the contents of the cert.
- By connecting the board to your internet router, it should automatically be assigned
  an IPv4 address using DHCP.
- The application will then attempt to connect to the public Mosquitto broker
  and begin publishing data.
- It is also possible to connect securely to a locally hosted MQTT broker.
  This will require provisioning of certificates.
  The CA cert should be included in the build as described above.
  ``CONFIG_NET_SAMPLE_MQTT_BROKER_HOSTNAME`` should be configured to match the
  local broker hostname/IP address.
  Depending on the CA cert being used, additional MbedTLS config options may need to be enabled.
  This can be done using Kconfig or using a custom MbedTLS config file
  (see modules/mbedtls/Kconfig).
  See https://mosquitto.org/man/mosquitto-tls-7.html for more info on setting up
  TLS support for Mosquitto locally.
- A DHCP server can be installed on the host PC to handle assigning an IP to the board
  e.g. dnsmasq (Linux) or DHCP Server for Windows (Windows).
- Build the sample with default config:

.. zephyr-app-commands::
	 :zephyr-app: samples/net/secure_mqtt_sensor_actuator
	 :board: adi_eval_adin1110ebz
	 :goals: build
	 :compact:

**Static IP Config:**

Use the :file:`overlay-static.conf` Kconfig overlay to disable DHCP and use
a static IP address config.
The device, gateway, and DNS server IP addresses should be set according to
your local network configuration.

.. zephyr-app-commands::
	 :zephyr-app: samples/net/secure_mqtt_sensor_actuator
	 :board: adi_eval_adin1110ebz
	 :conf: "prj.conf overlay-static.conf"
	 :goals: build
	 :compact:

**Static IP/Unsecure MQTT Config:**

Use the :file:`overlay-static-insecure.conf` Kconfig overlay to disable TLS and DHCP.
This config requires connecting to a locally hosted Mosquitto MQTT broker.

- In :file:`overlay-static-insecure.conf`, set the IP address of the board and the Mosquitto
  broker (i.e. IP address of Ethernet port on host PC). These addresses should be in the
  same subnet e.g. 192.0.2.1 and 192.0.2.2.
- On your host PC, install Mosquitto.
- Create a file called ``unsecure.conf`` with the following content:

.. code-block:: console

		listener 1883 0.0.0.0
		allow_anonymous true


- Start a Mosquitto broker using the configuration file:

.. code-block:: console

		$ sudo mosquitto -v -c unsecure.conf

- Build the sample with quick test config:

.. zephyr-app-commands::
	 :zephyr-app: samples/net/secure_mqtt_sensor_actuator
	 :board: adi_eval_adin1110ebz
	 :conf: "prj.conf overlay-static-insecure.conf"
	 :goals: build
	 :compact:

Using the Sample
================
- Once the board establishes an MQTT connection with the Mosquitto broker, the network
  LED will turn on and the board will begin publishing sensor readings in JSON format
  at a regular interval determined by ``CONFIG_NET_SAMPLE_MQTT_PUBLISH_INTERVAL``.

- Use Mosquitto to subscribe to the sensor data being sent from the board:

.. code-block:: console

		$ mosquitto_sub -d -h <test.mosquitto.org/local broker IP> -t zephyr_sample/sensor

- The application will subscribe to a topic determined by ``CONFIG_NET_SAMPLE_MQTT_SUB_TOPIC_CMD``.
  If a supported command string is received by the board on this topic, the board will execute
  an associated command handler function.
  Supported commands (defined in :file:`device.c`):

	- ``led_on``, turn on board LED
	- ``led_off``, turn off board LED

- Use Mosquitto to publish these commands to the MQTT broker:

.. code-block:: console

		$ mosquitto_pub -d -h <test.mosquitto.org/local broker IP> --cafile <path/to/ca.crt> -t zephyr_sample/command -m "led_on"

- The Quality of Service (QoS) level that is used for MQTT publishing and
  subscriptions can be configured using Kconfig.

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

.. code-block:: console

	*** Booting Zephyr OS build v3.6.0-2212-g2c9c4f3733e9 ***
	[00:00:00.181,000] <inf> app_device: Device adt7420@48 is ready
	[00:00:00.181,000] <inf> app_device: Device leds is ready
	[00:00:00.181,000] <inf> app_main: MAC Address: 00:E0:FE:FE:DA:C8
	[00:00:00.181,000] <inf> app_main: Bringing up network..
	[00:00:00.801,000] <inf> net_dhcpv4: Received: 192.168.1.17
	[00:00:00.801,000] <inf> app_main: Network connectivity up!
	[00:00:00.818,000] <inf> app_mqtt: Connecting to MQTT broker @ 91.121.93.94
	[00:00:01.154,000] <inf> net_mqtt: Connect completed
	[00:00:01.197,000] <inf> app_mqtt: Connected to MQTT broker!
	[00:00:01.197,000] <inf> app_mqtt: Hostname: test.mosquitto.org
	[00:00:01.198,000] <inf> app_mqtt: Client ID: adi_eval_adin1110ebz_9a
	[00:00:01.198,000] <inf> app_mqtt: Port: 8883
	[00:00:01.198,000] <inf> app_mqtt: TLS: Enabled
	[00:00:01.198,000] <inf> app_mqtt: Subscribing to 1 topic(s)
	[00:00:01.238,000] <inf> app_mqtt: SUBACK packet ID: 5841
	[00:00:04.200,000] <inf> app_mqtt: Published to topic 'zephyr_sample/sensor', QoS 1
	[00:00:04.319,000] <inf> app_mqtt: PUBACK packet ID: 1
	[00:00:07.202,000] <inf> app_mqtt: Published to topic 'zephyr_sample/sensor', QoS 1
	[00:00:07.323,000] <inf> app_mqtt: PUBACK packet ID: 2
	[00:00:10.204,000] <inf> app_mqtt: Published to topic 'zephyr_sample/sensor', QoS 1
	[00:00:10.322,000] <inf> app_mqtt: PUBACK packet ID: 3
	[00:00:12.769,000] <inf> app_mqtt: MQTT payload received!
	[00:00:12.769,000] <inf> app_mqtt: topic: 'zephyr_sample/command', payload: led_on
	[00:00:12.770,000] <inf> app_device: Executing device command: led_on

.. _Eclipse Mosquitto: https://mosquitto.org/download/

Part 3

한국어 전문 번역

개요

이 sample은 IoT sensor/actuator device 구현을 보여 줍니다. Application은 MQTT protocol로 sensor data를 remote MQTT broker에 안전하게 보내는 동시에 MQTT로 받은 command에 응답합니다.

주요 기능은 다음과 같습니다.

  • DHCPv4 lease를 사용한 network connectivity 수립
  • TLS 1.2를 사용하는 secure MQTT connection을 MQTT broker와 수립
  • 사용자가 정의한 interval마다 temperature sensor data를 JSON format으로 MQTT broker에 publish
  • MQTT broker에서 사용자가 정의한 topic을 subscribe
  • Network로 받은 command에 응답하여 LED 제어
  • MQTT connection, reconnection과 keep-alive 처리
  • Network status LED

요구 사항

  • Network 기능이 있는 board. 이 sample은 adi_eval_adin1110ebz에서 시험했습니다.
  • Eclipse Mosquitto MQTT broker
  • DHCP server
  • Board와 Mosquitto broker 사이의 network connection

빌드 및 실행

이 application은 board와 MQTT broker 사이의 network connection을 사용합니다. Broker는 host PC 같은 local system에서 실행할 수도 있고 공개 test.mosquitto.org broker를 사용할 수도 있습니다.

빠른 sampling과 test를 위해 security 없이 static IP address로 local MQTT broker에 연결하는 configuration도 제공합니다.

Hardware 설정

Ethernet을 사용한다면 board를 MQTT broker에 연결합니다. Local Mosquitto broker를 쓰면 host PC에, public Mosquitto broker를 쓰면 internet router에 연결할 수 있습니다.

필요하다면 temperature sensor를 board에 연결합니다.

Software 설정

Temperature sensor는 Devicetree에서 ambient-temp0 alias로 지정해야 합니다. Board에 온보드 temperature sensor가 없다면 외부 sensor를 연결하고 board overlay에서 sensor와 alias를 추가할 수 있습니다.

/ {
        aliases {
                        ambient-temp0 = &adt7420;
                };
        };
};

다른 종류의 sensor도 사용할 수 있습니다. Devicetree에 sensor를 추가하고 device.c의 SENSOR_CHAN을 원하는 sensor type과 일치하도록 바꾸십시오.

Application은 다음 세 방법으로 구성할 수 있습니다.

FileConfiguration
prj.conf기본 구성: Secure MQTT와 dynamic IP address(DHCP)
overlay-static.confSecure MQTT와 static IP address
overlay-static-insecure.confUnsecure MQTT와 static IP address

기본 구성:

Default config에서 application은 DHCP로 IP address를 얻고 TLS 1.2를 사용해 MQTT broker에 secure connection을 시도합니다.

  • Board가 연결할 MQTT broker는 CONFIG_NET_SAMPLE_MQTT_BROKER_HOSTNAME으로 지정합니다. 기본값은 test.mosquitto.org입니다.
  • TLS로 안전하게 연결하려면 broker CA certificate를 application에 포함해야 합니다.
  • test.mosquitto.org에서 CA certificate를 DER 또는 PEM format으로 내려받습니다.
  • tls_config/cert.h의 ca_certificate[]에 certificate 내용을 설정합니다.
  • Board를 internet router에 연결하면 DHCP가 IPv4 address를 자동 할당합니다.
  • Application은 public Mosquitto broker에 연결을 시도하고 data publish를 시작합니다.

Local MQTT broker에도 secure connection을 만들 수 있지만 certificate provisioning이 필요합니다. 위와 같이 CA certificate를 build에 포함하고 CONFIG_NET_SAMPLE_MQTT_BROKER_HOSTNAME을 local broker hostname 또는 IP address와 일치시켜야 합니다.

사용하는 CA certificate에 따라 추가 MbedTLS config option이 필요할 수 있습니다. Kconfig나 custom MbedTLS config file(modules/mbedtls/Kconfig 참조)에서 활성화할 수 있습니다. Local Mosquitto TLS 설정은 mosquitto-tls manual을 참조하십시오.

Host PC에는 board IP를 할당할 DHCP server를 설치할 수 있습니다. 예를 들어 Linux에서는 dnsmasq, Windows에서는 DHCP Server for Windows를 사용할 수 있습니다.

Default config로 sample을 build합니다.

west build -b adi_eval_adin1110ebz samples/net/secure_mqtt_sensor_actuator

Static IP 구성:

overlay-static.conf Kconfig overlay를 사용하면 DHCP를 비활성화하고 static IP configuration을 사용합니다. Device, gateway와 DNS server IP address는 local network configuration에 맞게 설정해야 합니다.

west build -b adi_eval_adin1110ebz samples/net/secure_mqtt_sensor_actuator -- -DCONF_FILE="prj.conf overlay-static.conf"

Static IP/Unsecure MQTT 구성:

overlay-static-insecure.conf Kconfig overlay는 TLS와 DHCP를 모두 비활성화합니다. 이 구성은 local Mosquitto MQTT broker에 연결해야 합니다.

  • overlay-static-insecure.conf에서 board와 Mosquitto broker, 즉 host PC Ethernet port의 IP address를 설정합니다. 두 주소는 예시 192.0.2.1과 192.0.2.2처럼 같은 subnet에 있어야 합니다.
  • Host PC에 Mosquitto를 설치합니다.
  • 다음 내용으로 unsecure.conf file을 만듭니다.
listener 1883 0.0.0.0
allow_anonymous true

이 configuration file로 Mosquitto broker를 시작합니다.

$ sudo mosquitto -v -c unsecure.conf

Quick test configuration으로 sample을 build합니다.

west build -b adi_eval_adin1110ebz samples/net/secure_mqtt_sensor_actuator -- -DCONF_FILE="prj.conf overlay-static-insecure.conf"

Sample 사용

Board가 Mosquitto broker와 MQTT connection을 수립하면 network LED가 켜지고, CONFIG_NET_SAMPLE_MQTT_PUBLISH_INTERVAL로 정한 일정 interval마다 JSON format sensor reading을 publish하기 시작합니다.

Mosquitto subscriber로 board가 보내는 sensor data를 구독합니다.

$ mosquitto_sub -d -h <test.mosquitto.org/local broker IP> -t zephyr_sample/sensor

Application은 CONFIG_NET_SAMPLE_MQTT_SUB_TOPIC_CMD로 정한 topic을 subscribe합니다. 이 topic에서 지원하는 command string을 받으면 board가 연결된 command handler function을 실행합니다. device.c에 정의된 command는 다음 두 가지입니다.

  • led_on: board LED를 켭니다.
  • led_off: board LED를 끕니다.

Mosquitto publisher로 이 command를 MQTT broker에 publish합니다.

$ mosquitto_pub -d -h <test.mosquitto.org/local broker IP> --cafile <path/to/ca.crt> -t zephyr_sample/command -m "led_on"

MQTT publish와 subscription에 사용하는 Quality of Service(QoS) level은 Kconfig로 설정할 수 있습니다.

Sample 출력

다음 출력에서는 ADT7420 sensor와 LED device가 준비되고 DHCPv4로 IPv4 address를 받은 뒤, port 8883에서 TLS가 활성화된 public broker에 연결합니다. Command topic을 subscribe한 다음 QoS 1 sensor data를 세 차례 publish하고 PUBACK을 받으며, 마지막에는 led_on payload를 수신해 device command를 실행합니다.

*** Booting Zephyr OS build v3.6.0-2212-g2c9c4f3733e9 ***
[00:00:00.181,000] <inf> app_device: Device adt7420@48 is ready
[00:00:00.181,000] <inf> app_device: Device leds is ready
[00:00:00.181,000] <inf> app_main: MAC Address: 00:E0:FE:FE:DA:C8
[00:00:00.181,000] <inf> app_main: Bringing up network..
[00:00:00.801,000] <inf> net_dhcpv4: Received: 192.168.1.17
[00:00:00.801,000] <inf> app_main: Network connectivity up!
[00:00:00.818,000] <inf> app_mqtt: Connecting to MQTT broker @ 91.121.93.94
[00:00:01.154,000] <inf> net_mqtt: Connect completed
[00:00:01.197,000] <inf> app_mqtt: Connected to MQTT broker!
[00:00:01.197,000] <inf> app_mqtt: Hostname: test.mosquitto.org
[00:00:01.198,000] <inf> app_mqtt: Client ID: adi_eval_adin1110ebz_9a
[00:00:01.198,000] <inf> app_mqtt: Port: 8883
[00:00:01.198,000] <inf> app_mqtt: TLS: Enabled
[00:00:01.198,000] <inf> app_mqtt: Subscribing to 1 topic(s)
[00:00:01.238,000] <inf> app_mqtt: SUBACK packet ID: 5841
[00:00:04.200,000] <inf> app_mqtt: Published to topic 'zephyr_sample/sensor', QoS 1
[00:00:04.319,000] <inf> app_mqtt: PUBACK packet ID: 1
[00:00:07.202,000] <inf> app_mqtt: Published to topic 'zephyr_sample/sensor', QoS 1
[00:00:07.323,000] <inf> app_mqtt: PUBACK packet ID: 2
[00:00:10.204,000] <inf> app_mqtt: Published to topic 'zephyr_sample/sensor', QoS 1
[00:00:10.322,000] <inf> app_mqtt: PUBACK packet ID: 3
[00:00:12.769,000] <inf> app_mqtt: MQTT payload received!
[00:00:12.769,000] <inf> app_mqtt: topic: 'zephyr_sample/command', payload: led_on
[00:00:12.770,000] <inf> app_device: Executing device command: led_on

Source

출처

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