Part 1
요약·해설
MQTT와 MQTT-SN의 transport 차이, 3개 terminal 구성, topic ID 등록과 packet 변환 trace를 보존합니다.
MQTT-SN v1.2
UDP · Datagram
Eclipse Paho
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
B79D3D52C9B707533B69529DBB0EEDD2A69C2C007BE2E738569F1466765F608A
.. zephyr:code-sample:: mqtt-sn-publisher
:name: MQTT-SN publisher
:relevant-api: mqtt_sn_socket
Send MQTT-SN PUBLISH messages to an MQTT-SN gateway.
Overview
********
`MQTT <http://mqtt.org/>`_ (MQ Telemetry Transport) is a lightweight
publish/subscribe messaging protocol optimized for small sensors and
mobile devices.
MQTT-SN can be considered as a version of MQTT which is adapted to
the peculiarities of a wireless communication environment. While MQTT
requires a reliable TCP/IP transport, MQTT-SN is designed to be usable
on any datagram-based transport like UDP, ZigBee or even a plain UART
(with an additional framing protocol).
The Zephyr MQTT-SN Publisher sample application is an MQTT-SN v1.2
client that sends MQTT-SN PUBLISH messages to an MQTT-SN gateway.
It also SUBSCRIBEs to a topic.
See the `MQTT-SN v1.2 spec`_ for more information.
.. _MQTT-SN v1.2 spec: https://www.oasis-open.org/committees/download.php/66091/MQTT-SN_spec_v1.2.pdf
The source code of this sample application can be found at:
:zephyr_file:`samples/net/mqtt_sn_publisher`.
Requirements
************
- Linux machine
- MQTT-SN gateway, like Eclipse Paho
- Mosquitto server: any version that supports MQTT v3.1.1. This sample
was tested with mosquitto 1.6.
- Mosquitto subscriber
- LAN for testing purposes (Ethernet)
Build and Running
*****************
Currently, this sample application only supports static IP addresses.
Open the :file:`prj.conf` file and set the IP addresses according
to the LAN environment.
You will also need to start an MQTT-SN gateway. With Paho, you can either
build it from source - see `PAHO MQTT-SN Gateway`_ - or run an unofficial
docker image, like `kyberpunk/paho`_.
.. _PAHO MQTT-SN Gateway: https://www.eclipse.org/paho/index.php?page=components/mqtt-sn-transparent-gateway/index.php
.. _kyberpunk/paho: https://hub.docker.com/r/kyberpunk/paho
On your Linux host computer, open 3 terminal windows. At first, start mosquitto:
.. code-block:: console
$ sudo mosquitto -v -p 1883
Then, in another window, start the gateway, e.g. by using docker:
.. code-block:: console
$ docker run -it -p 10000:10000 -p 10000:10000/udp --name paho -v $PWD/gateway.conf:/etc/paho/gateway.conf:ro kyberpunk/paho
Then, locate your zephyr directory and type:
.. zephyr-app-commands::
:zephyr-app: samples/net/mqtt_sn_publisher
:board: native_sim/native/64
:goals: run
:compact:
Optionally, use any MQTT explorer to connect to your broker.
Sample output
=============
This is the applications output:
.. code-block:: console
WARNING: Using a test - not safe - entropy source
*** Booting Zephyr OS build zephyr-v3.2.0-279-gc7fa387cea81 ***
[00:00:00.000,000] <inf> net_config: Initializing network
[00:00:00.000,000] <inf> net_config: IPv4 address: 172.18.0.20
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: MQTT-SN sample
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Network connected
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Waiting for connection...
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Connecting client
[00:00:00.510,000] <inf> net_mqtt_sn: Decoding message type: 5
[00:00:00.510,000] <inf> net_mqtt_sn: Got message of type 5
[00:00:00.510,000] <inf> net_mqtt_sn: MQTT_SN client connected
[00:00:00.510,000] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_CONNECTED
[00:00:01.020,000] <inf> net_mqtt_sn: Decoding message type: 19
[00:00:01.020,000] <inf> net_mqtt_sn: Got message of type 19
[00:00:10.200,000] <inf> mqtt_sn_publisher_sample: Publishing timestamp
[00:00:10.200,000] <inf> net_mqtt_sn: Registering topic
2f 75 70 74 69 6d 65 |/uptime
[00:00:10.200,000] <inf> net_mqtt_sn: Can't publish; topic is not ready
[00:00:10.710,000] <inf> net_mqtt_sn: Decoding message type: 11
[00:00:10.710,000] <inf> net_mqtt_sn: Got message of type 11
[00:00:10.710,000] <inf> net_mqtt_sn: Publishing to topic ID 14
[00:00:20.400,000] <inf> mqtt_sn_publisher_sample: Publishing timestamp
[00:00:20.400,000] <inf> net_mqtt_sn: Publishing to topic ID 14
This is the output from the MQTT-SN gateway:
.. code-block:: console
20221024 140210.191 CONNECT <--- ZEPHYR 0C 04 04 01 00 3C 5A 45 50 48 59 52
20221024 140210.192 CONNECT ===> ZEPHYR 10 12 00 04 4D 51 54 54 04 02 00 3C 00 06 5A 45 50 48 59 52
20221024 140210.192 CONNACK <=== ZEPHYR 20 02 00 00
20221024 140210.192 CONNACK ---> ZEPHYR 03 05 00
20221024 140210.643 SUBSCRIBE 0001 <--- ZEPHYR 0C 12 00 00 01 2F 6E 75 6D 62 65 72
20221024 140210.648 SUBSCRIBE 0001 ===> ZEPHYR 82 0C 00 01 00 07 2F 6E 75 6D 62 65 72 00
20221024 140210.660 SUBACK 0001 <=== ZEPHYR 90 03 00 01 00
20221024 140210.661 SUBACK 0001 ---> ZEPHYR 08 13 00 00 0D 00 01 00
20221024 140220.338 REGISTER 0002 <--- ZEPHYR 0D 0A 00 00 00 02 2F 75 70 74 69 6D 65
20221024 140220.348 REGACK 0002 ---> ZEPHYR 07 0B 00 0E 00 02 00
20221024 140220.848 PUBLISH <--- ZEPHYR 0C 0C 00 00 0E 00 00 31 30 32 30 30
20221024 140220.850 PUBLISH ===> ZEPHYR 30 0E 00 07 2F 75 70 74 69 6D 65 31 30 32 30 30
20221024 140230.539 PUBLISH <--- ZEPHYR 0C 0C 00 00 0E 00 00 32 30 34 30 30
20221024 140230.542 PUBLISH ===> ZEPHYR 30 0E 00 07 2F 75 70 74 69 6D 65 32 30 34 30 30
Part 3
한국어 전문 번역
개요
MQTT (MQ Telemetry Transport)는 작은 sensor와 mobile device에 최적화된 경량 publish/subscribe messaging protocol입니다.
MQTT-SN은 wireless communication environment의 특성에 맞춘 MQTT version이라고 볼 수 있습니다. MQTT는 신뢰성 있는 TCP/IP transport가 필요하지만, MQTT-SN은 UDP나 ZigBee 같은 datagram 기반 transport뿐 아니라 추가 framing protocol을 사용하는 plain UART에서도 동작하도록 설계됐습니다.
Zephyr MQTT-SN Publisher sample application은 MQTT-SN gateway로 MQTT-SN PUBLISH message를 보내는 MQTT-SN v1.2 client입니다. 또한 하나의 topic을 SUBSCRIBE합니다. 자세한 내용은 MQTT-SN v1.2 specification을 참조하십시오.
이 sample application의 source code는 samples/net/mqtt_sn_publisher에 있습니다.
요구 사항
- Linux machine
- Eclipse Paho 같은 MQTT-SN gateway
- MQTT v3.1.1을 지원하는 Mosquitto server. 이 sample은 mosquitto 1.6으로 시험했습니다.
- Mosquitto subscriber
- 시험용 LAN(Ethernet)
빌드 및 실행
현재 이 sample application은 static IP address만 지원합니다. prj.conf를 열고 LAN environment에 맞게 IP address를 설정하십시오.
MQTT-SN gateway도 시작해야 합니다. Paho는 PAHO MQTT-SN Gateway 안내에 따라 source에서 build하거나 kyberpunk/paho 같은 비공식 Docker image로 실행할 수 있습니다.
Linux host computer에서 terminal window 세 개를 엽니다. 첫 번째 terminal에서 Mosquitto broker를 시작합니다.
$ sudo mosquitto -v -p 1883
두 번째 terminal에서는 다음 Docker example처럼 gateway를 시작합니다. TCP와 UDP port 10000을 모두 publish하고, 현재 directory의 gateway.conf를 read-only configuration으로 mount합니다.
$ docker run -it -p 10000:10000 -p 10000:10000/udp --name paho -v $PWD/gateway.conf:/etc/paho/gateway.conf:ro kyberpunk/paho
세 번째 terminal에서 Zephyr directory로 이동해 native_sim 64-bit target으로 sample을 build하고 실행합니다.
west build -b native_sim/native/64 samples/net/mqtt_sn_publisher
west build -t run
필요하면 원하는 MQTT explorer를 broker에 연결해 message와 topic을 관찰할 수 있습니다.
Sample 출력
다음은 application 출력입니다. Network와 MQTT-SN client가 연결된 뒤 /number topic을 subscribe합니다. 10초 후 /uptime topic을 처음 publish하려 할 때는 topic ID가 아직 없어 REGISTER를 먼저 수행하며, REGACK에서 ID 14를 받은 뒤 현재 timestamp를 publish합니다.
WARNING: Using a test - not safe - entropy source
*** Booting Zephyr OS build zephyr-v3.2.0-279-gc7fa387cea81 ***
[00:00:00.000,000] <inf> net_config: Initializing network
[00:00:00.000,000] <inf> net_config: IPv4 address: 172.18.0.20
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: MQTT-SN sample
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Network connected
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Waiting for connection...
[00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Connecting client
[00:00:00.510,000] <inf> net_mqtt_sn: Decoding message type: 5
[00:00:00.510,000] <inf> net_mqtt_sn: Got message of type 5
[00:00:00.510,000] <inf> net_mqtt_sn: MQTT_SN client connected
[00:00:00.510,000] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_CONNECTED
[00:00:01.020,000] <inf> net_mqtt_sn: Decoding message type: 19
[00:00:01.020,000] <inf> net_mqtt_sn: Got message of type 19
[00:00:10.200,000] <inf> mqtt_sn_publisher_sample: Publishing timestamp
[00:00:10.200,000] <inf> net_mqtt_sn: Registering topic
2f 75 70 74 69 6d 65 |/uptime
[00:00:10.200,000] <inf> net_mqtt_sn: Can't publish; topic is not ready
[00:00:10.710,000] <inf> net_mqtt_sn: Decoding message type: 11
[00:00:10.710,000] <inf> net_mqtt_sn: Got message of type 11
[00:00:10.710,000] <inf> net_mqtt_sn: Publishing to topic ID 14
[00:00:20.400,000] <inf> mqtt_sn_publisher_sample: Publishing timestamp
[00:00:20.400,000] <inf> net_mqtt_sn: Publishing to topic ID 14
다음은 MQTT-SN gateway 출력입니다. 왼쪽 화살표는 Zephyr MQTT-SN client와 gateway 사이의 MQTT-SN packet을, 이중 화살표는 gateway와 MQTT broker 사이의 MQTT packet을 보여 줍니다. Gateway는 CONNECT/CONNACK과 SUBSCRIBE/SUBACK을 중계하고, /uptime 이름을 REGISTER/REGACK으로 topic ID 14에 연결한 뒤 PUBLISH payload를 MQTT 형식으로 전달합니다.
20221024 140210.191 CONNECT <--- ZEPHYR 0C 04 04 01 00 3C 5A 45 50 48 59 52
20221024 140210.192 CONNECT ===> ZEPHYR 10 12 00 04 4D 51 54 54 04 02 00 3C 00 06 5A 45 50 48 59 52
20221024 140210.192 CONNACK <=== ZEPHYR 20 02 00 00
20221024 140210.192 CONNACK ---> ZEPHYR 03 05 00
20221024 140210.643 SUBSCRIBE 0001 <--- ZEPHYR 0C 12 00 00 01 2F 6E 75 6D 62 65 72
20221024 140210.648 SUBSCRIBE 0001 ===> ZEPHYR 82 0C 00 01 00 07 2F 6E 75 6D 62 65 72 00
20221024 140210.660 SUBACK 0001 <=== ZEPHYR 90 03 00 01 00
20221024 140210.661 SUBACK 0001 ---> ZEPHYR 08 13 00 00 0D 00 01 00
20221024 140220.338 REGISTER 0002 <--- ZEPHYR 0D 0A 00 00 00 02 2F 75 70 74 69 6D 65
20221024 140220.348 REGACK 0002 ---> ZEPHYR 07 0B 00 0E 00 02 00
20221024 140220.848 PUBLISH <--- ZEPHYR 0C 0C 00 00 0E 00 00 31 30 32 30 30
20221024 140220.850 PUBLISH ===> ZEPHYR 30 0E 00 07 2F 75 70 74 69 6D 65 31 30 32 30 30
20221024 140230.539 PUBLISH <--- ZEPHYR 0C 0C 00 00 0E 00 00 32 30 34 30 30
20221024 140230.542 PUBLISH ===> ZEPHYR 30 0E 00 07 2F 75 70 74 69 6D 65 32 30 34 30 30
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.