Part 1
요약·해설
Static network 설정, Watson topic format, TLS certificate, CC3220SF offloading과 실제 broker log를 단계별로 보존합니다.
MQTT v3.1.1
TLS · SNI
Ethernet · SOCKS5
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
E7023D156B2C66108C5C1C1965C9F5A5D78548B42A4B36A6411757B9E809E262
.. zephyr:code-sample:: mqtt-publisher
:name: MQTT publisher
:relevant-api: mqtt_socket
Send MQTT PUBLISH messages to an MQTT server.
Overview
********
`MQTT <http://mqtt.org/>`_ (MQ Telemetry Transport) is a lightweight
publish/subscribe messaging protocol optimized for small sensors and
mobile devices.
The Zephyr MQTT Publisher sample application is a MQTT v3.1.1
client that sends MQTT PUBLISH messages to a MQTT broker.
See the `MQTT V3.1.1 spec`_ for more information.
.. _MQTT V3.1.1 spec: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
The source code of this sample application can be found at:
:zephyr_file:`samples/net/mqtt_publisher`.
Requirements
************
- Linux machine
- Freedom Board (FRDM-K64F)
- Mosquitto server: any version that supports MQTT v3.1.1. This sample
was tested with mosquitto 1.3.4.
- Mosquitto subscriber
- LAN for testing purposes (Ethernet)
Build and Running
*****************
Currently, this sample application only supports static IP addresses.
Open the :file:`src/config.h` file and set the IP addresses according
to the LAN environment.
Alternatively, set the IP addresses in the :file:`prj.conf` file.
The file :file:`src/config.h` also contains some variables that may be changed:
MQTT broker TCP port:
.. code-block:: c
#define SERVER_PORT 1883
Application sleep time:
.. code-block:: c
#define APP_SLEEP_MSECS 500
Max number of connection tries:
.. code-block:: c
#define APP_CONNECT_TRIES 10
MQTT Client Identifier:
.. code-block:: c
#define MQTT_CLIENTID "zephyr_publisher"
This sample application supports the IBM Bluemix Watson topic format that can
be enabled by changing the default value of APP_BLUEMIX_TOPIC from 0 to 1:
.. code-block:: c
#define APP_BLUEMIX_TOPIC 1
The Bluemix topic may include some parameters like device type, device
identifier, event type and message format. This application uses the
following macros to specify those values:
.. code-block:: c
#define BLUEMIX_DEVTYPE "sensor"
#define BLUEMIX_DEVID "carbon"
#define BLUEMIX_EVENT "status"
#define BLUEMIX_FORMAT "json"
Max number of MQTT PUBLISH iterations is defined in Kconfig:
.. code-block:: cfg
CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS=5
On your Linux host computer, open a terminal window, locate the source code
of this sample application (i.e., :zephyr_file:`samples/net/mqtt_publisher`) and type:
.. zephyr-app-commands::
:zephyr-app: samples/net/mqtt_publisher
:board: frdm_k64f
:goals: build flash
:compact:
Open another terminal window and type:
.. code-block:: console
$ sudo mosquitto -v -p 1883
Open another terminal window and type:
.. code-block:: console
$ mosquitto_sub -t sensors
Connecting securely using TLS
=============================
While it is possible to set up a local secure MQTT server and update the
sample to connect to it, it does require some work on the user's part to
create the certificates and to set them up with the server.
Alternatively, a `publicly available Mosquitto MQTT server/broker
<https://test.mosquitto.org/>`_ is available to quickly and easily
try this sample with TLS enabled, by following these steps:
- Download the server's CA certificate file in DER format from
https://test.mosquitto.org
- In :file:`src/test_certs.h`, set ``ca_certificate[]`` using the certificate
contents (or set it to its filename if the socket offloading feature is
enabled on your platform and :kconfig:option:`CONFIG_TLS_CREDENTIAL_FILENAMES` is
set to ``y``).
- In :file:`src/config.h`, set SERVER_ADDR to the IP address to connect to,
i.e., the IP address of test.mosquitto.org ``"37.187.106.16"``
- In :file:`src/main.c`, set TLS_SNI_HOSTNAME to ``"test.mosquitto.org"``
to match the Common Name (CN) in the downloaded certificate.
- Build the sample by specifying ``-DEXTRA_CONF_FILE=overlay-tls.conf``
when running ``west build`` or ``cmake`` (or refer to the TLS offloading
section below if your platform uses the offloading feature).
- Flash the binary onto the device to run the sample:
.. code-block:: console
$ ninja flash
TLS offloading
==============
For boards that support this feature, TLS offloading is used by
specifying ``-DEXTRA_CONF_FILE=overlay-tls-offload.conf`` when running ``west
build`` or ``cmake``.
Using this overlay enables TLS without bringing in mbedtls.
SOCKS5 proxy support
====================
It is also possible to connect to the MQTT broker through a SOCKS5 proxy.
To enable it, use ``-DEXTRA_CONF_FILE=overlay-socks5.conf`` when running ``west
build`` or ``cmake``.
By default, to make the testing easier, the proxy is expected to run on the
same host as the MQTT broker.
To start a proxy server, ``ssh`` can be used.
Use the following command to run it on your host with the default port:
.. code-block:: console
$ ssh -N -D 0.0.0.0:1080 localhost
To connect to a proxy server that is not running under the same IP as the MQTT
broker or uses a different port number, modify the following values:
.. code-block:: c
#define SOCKS5_PROXY_ADDR SERVER_ADDR
#define SOCKS5_PROXY_PORT 1080
Running on cc3220sf_launchxl
============================
Offloading on cc3220sf_launchxl also provides DHCP services, so the sample
uses dynamic IP addresses on this board.
By default, the sample is set up to connect to the broker at the address
specified by SERVER_ADDR in config.h. If the broker is secured using TLS, users
should enable TLS offloading, upload the server's certificate
authority file in DER format to the device filesystem using TI Uniflash,
and name it "ca_cert.der".
In addition, TLS_SNI_HOSTNAME in main.c should be defined to match the
Common Name (CN) in the certificate file in order for the TLS domain
name verification to succeed.
See the note on Provisioning and Fast Connect in :ref:`cc3220sf_launchxl`.
The Secure Socket Offload section has information on programming the
certificate to flash.
Proceed to test as above.
Sample output
=============
This is the output from the FRDM UART console, with:
.. code-block:: cfg
CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS=5
.. code-block:: console
[dev/eth_mcux] [INF] eth_0_init: Enabled 100M full-duplex mode.
[dev/eth_mcux] [DBG] eth_0_init: MAC 00:04:9f:3e:1a:0a
[publisher:233] network_setup: 0 <OK>
[publisher:258] mqtt_init: 0 <OK>
[connect_cb:81] user_data: CONNECTED
[try_to_connect:212] mqtt_tx_connect: 0 <OK>
[publisher:276] try_to_connect: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 1888, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 16356, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 16356, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 45861, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 53870, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 53870, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 60144, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 6561, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 6561, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 38355, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 60656, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 60656, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 28420, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 49829, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 49829, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[disconnect_cb:101] user_data: DISCONNECTED
[publisher:304] mqtt_tx_disconnect: 0 <OK>
Bye!
The line:
.. code-block:: console
[try_to_connect:220] mqtt_connect: -5 <ERROR>
means that an error was detected and a new connect message will be sent.
The MQTT API is asynchronous, so messages are displayed as the callbacks are
executed.
This is the information that the subscriber will receive:
.. code-block:: console
$ mosquitto_sub -t sensors
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
This is the output from the MQTT broker:
.. code-block:: console
$ sudo mosquitto -v
1485663791: mosquitto version 1.3.4 (build date 2014-08-17 00:14:52-0300) starting
1485663791: Using default config.
1485663791: Opening ipv4 listen socket on port 1883.
1485663791: Opening ipv6 listen socket on port 1883.
1485663797: New connection from 192.168.1.101 on port 1883.
1485663797: New client connected from 192.168.1.101 as zephyr_publisher (c1, k0).
1485663797: Sending CONNACK to zephyr_publisher (0)
1485663798: Received PINGREQ from zephyr_publisher
1485663798: Sending PINGRESP to zephyr_publisher
1485663798: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663799: Received PUBLISH from zephyr_publisher (d0, q1, r0, m1888, 'sensors', ... (15 bytes))
1485663799: Sending PUBACK to zephyr_publisher (Mid: 1888)
1485663799: Received PUBLISH from zephyr_publisher (d0, q2, r0, m16356, 'sensors', ... (15 bytes))
1485663799: Sending PUBREC to zephyr_publisher (Mid: 16356)
1485663799: Received PUBREL from zephyr_publisher (Mid: 16356)
1485663799: Sending PUBCOMP to zephyr_publisher (Mid: 16356)
1485663800: Received PINGREQ from zephyr_publisher
1485663800: Sending PINGRESP to zephyr_publisher
1485663800: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663801: Received PUBLISH from zephyr_publisher (d0, q1, r0, m45861, 'sensors', ... (15 bytes))
1485663801: Sending PUBACK to zephyr_publisher (Mid: 45861)
1485663801: Received PUBLISH from zephyr_publisher (d0, q2, r0, m53870, 'sensors', ... (15 bytes))
1485663801: Sending PUBREC to zephyr_publisher (Mid: 53870)
1485663801: Received PUBREL from zephyr_publisher (Mid: 53870)
1485663801: Sending PUBCOMP to zephyr_publisher (Mid: 53870)
1485663802: Received PINGREQ from zephyr_publisher
1485663802: Sending PINGRESP to zephyr_publisher
1485663802: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663803: Received PUBLISH from zephyr_publisher (d0, q1, r0, m60144, 'sensors', ... (15 bytes))
1485663803: Sending PUBACK to zephyr_publisher (Mid: 60144)
1485663803: Received PUBLISH from zephyr_publisher (d0, q2, r0, m6561, 'sensors', ... (15 bytes))
1485663803: Sending PUBREC to zephyr_publisher (Mid: 6561)
1485663803: Received PUBREL from zephyr_publisher (Mid: 6561)
1485663803: Sending PUBCOMP to zephyr_publisher (Mid: 6561)
1485663804: Received PINGREQ from zephyr_publisher
1485663804: Sending PINGRESP to zephyr_publisher
1485663804: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663805: Received PUBLISH from zephyr_publisher (d0, q1, r0, m38355, 'sensors', ... (15 bytes))
1485663805: Sending PUBACK to zephyr_publisher (Mid: 38355)
1485663805: Received PUBLISH from zephyr_publisher (d0, q2, r0, m60656, 'sensors', ... (15 bytes))
1485663805: Sending PUBREC to zephyr_publisher (Mid: 60656)
1485663805: Received PUBREL from zephyr_publisher (Mid: 60656)
1485663805: Sending PUBCOMP to zephyr_publisher (Mid: 60656)
1485663806: Received PINGREQ from zephyr_publisher
1485663806: Sending PINGRESP to zephyr_publisher
1485663806: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663807: Received PUBLISH from zephyr_publisher (d0, q1, r0, m28420, 'sensors', ... (15 bytes))
1485663807: Sending PUBACK to zephyr_publisher (Mid: 28420)
1485663807: Received PUBLISH from zephyr_publisher (d0, q2, r0, m49829, 'sensors', ... (15 bytes))
1485663807: Sending PUBREC to zephyr_publisher (Mid: 49829)
1485663807: Received PUBREL from zephyr_publisher (Mid: 49829)
1485663807: Sending PUBCOMP to zephyr_publisher (Mid: 49829)
1485663808: Received DISCONNECT from zephyr_publisher
Part 3
한국어 전문 번역
개요
MQTT (MQ Telemetry Transport)는 작은 sensor와 mobile device에 최적화된 경량 publish/subscribe messaging protocol입니다.
Zephyr MQTT Publisher sample application은 MQTT broker로 MQTT PUBLISH message를 보내는 MQTT v3.1.1 client입니다. 자세한 내용은 MQTT V3.1.1 specification을 참조하십시오.
이 sample application의 source code는 samples/net/mqtt_publisher에 있습니다.
요구 사항
- Linux machine
- Freedom Board(FRDM-K64F)
- MQTT v3.1.1을 지원하는 Mosquitto server. 이 sample은 mosquitto 1.3.4로 시험했습니다.
- Mosquitto subscriber
- 시험용 LAN(Ethernet)
빌드 및 실행
현재 이 sample application은 static IP address만 지원합니다. src/config.h를 열어 LAN environment에 맞게 IP address를 설정하십시오. 대신 prj.conf에서 IP address를 설정할 수도 있습니다.
src/config.h에는 변경할 수 있는 다음 변수도 들어 있습니다.
MQTT broker TCP port:
#define SERVER_PORT 1883
Application sleep time:
#define APP_SLEEP_MSECS 500
Maximum connection attempt count:
#define APP_CONNECT_TRIES 10
MQTT Client Identifier:
#define MQTT_CLIENTID "zephyr_publisher"
이 sample application은 IBM Bluemix Watson topic format을 지원합니다. APP_BLUEMIX_TOPIC의 기본값을 0에서 1로 바꾸면 활성화됩니다.
#define APP_BLUEMIX_TOPIC 1
Bluemix topic에는 device type, device identifier, event type, message format 같은 parameter가 들어갈 수 있습니다. 이 application은 다음 macro로 값을 지정합니다.
#define BLUEMIX_DEVTYPE "sensor"
#define BLUEMIX_DEVID "carbon"
#define BLUEMIX_EVENT "status"
#define BLUEMIX_FORMAT "json"
MQTT PUBLISH의 최대 반복 횟수는 Kconfig에서 정의합니다.
CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS=5
Linux host computer에서 terminal을 열고 이 sample application의 source code 위치인 samples/net/mqtt_publisher로 이동한 뒤 다음 명령을 입력합니다.
west build -b frdm_k64f samples/net/mqtt_publisher
west flash
다른 terminal window를 열고 다음 명령으로 Mosquitto broker를 TCP port 1883에서 verbose mode로 실행합니다.
$ sudo mosquitto -v -p 1883
또 다른 terminal window를 열고 sensors topic subscriber를 실행합니다.
$ mosquitto_sub -t sensors
TLS를 사용한 보안 연결
Local secure MQTT server를 구성하고 sample을 그 server에 연결하도록 갱신할 수 있지만, certificate를 만들고 server에 설정하는 작업을 사용자가 직접 해야 합니다.
더 간단하게 TLS를 시험하려면 공개 Mosquitto MQTT server/broker를 사용할 수 있습니다. 다음 순서로 진행합니다.
https://test.mosquitto.org에서 server CA certificate file을 DER format으로 내려받습니다.src/test_certs.h의ca_certificate[]에 certificate 내용을 넣습니다. Platform이 socket offloading을 지원하고CONFIG_TLS_CREDENTIAL_FILENAMES=y이면 내용 대신 filename을 지정합니다.src/config.h의SERVER_ADDR를 접속할 IP address로 설정합니다. 예시 test.mosquitto.org IP address는"37.187.106.16"입니다.src/main.c의TLS_SNI_HOSTNAME을 내려받은 certificate의 Common Name(CN)과 일치하는"test.mosquitto.org"로 설정합니다.west build또는cmake실행 시-DEXTRA_CONF_FILE=overlay-tls.conf를 지정해 sample을 build합니다. Platform이 offloading을 사용한다면 다음 TLS offloading 절을 따릅니다.- Binary를 device에 flash하여 sample을 실행합니다.
$ ninja flash
TLS 오프로딩
이 기능을 지원하는 board에서는 west build 또는 cmake 실행 시 -DEXTRA_CONF_FILE=overlay-tls-offload.conf를 지정해 TLS offloading을 사용합니다.
이 overlay를 사용하면 mbedtls를 포함하지 않고도 TLS를 활성화할 수 있습니다.
SOCKS5 프록시 지원
SOCKS5 proxy를 통해 MQTT broker에 연결할 수도 있습니다. 활성화하려면 west build 또는 cmake 실행 시 -DEXTRA_CONF_FILE=overlay-socks5.conf를 사용합니다.
시험을 쉽게 하도록 proxy는 기본적으로 MQTT broker와 같은 host에서 실행되는 것으로 가정합니다.
Proxy server는 ssh로 시작할 수 있습니다. 기본 port로 host에서 실행하려면 다음 명령을 사용합니다.
$ ssh -N -D 0.0.0.0:1080 localhost
MQTT broker와 다른 IP에서 실행되거나 다른 port number를 사용하는 proxy server에 연결하려면 다음 값을 수정합니다.
#define SOCKS5_PROXY_ADDR SERVER_ADDR
#define SOCKS5_PROXY_PORT 1080
cc3220sf_launchxl에서 실행
cc3220sf_launchxl의 offloading 기능은 DHCP service도 제공하므로, 이 board에서는 sample이 dynamic IP address를 사용합니다.
기본적으로 sample은 config.h의 SERVER_ADDR가 지정한 broker address에 연결합니다. Broker가 TLS로 보호되면 TLS offloading을 활성화하고, TI Uniflash로 DER format server certificate authority file을 device filesystem에 올린 뒤 이름을 ca_cert.der로 지정해야 합니다.
또한 TLS domain name verification이 성공하도록 main.c의 TLS_SNI_HOSTNAME을 certificate file의 Common Name(CN)과 일치하게 정의해야 합니다.
cc3220sf_launchxl 문서의 Provisioning and Fast Connect note를 참조하십시오.
Secure Socket Offload section에는 certificate를 flash에 programming하는 방법이 설명되어 있습니다.
그 다음 위와 같은 방법으로 시험을 진행합니다.
Sample 출력
다음은 FRDM UART console 출력이며, 반복 횟수는 아래처럼 5로 설정했습니다.
CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS=5
[dev/eth_mcux] [INF] eth_0_init: Enabled 100M full-duplex mode.
[dev/eth_mcux] [DBG] eth_0_init: MAC 00:04:9f:3e:1a:0a
[publisher:233] network_setup: 0 <OK>
[publisher:258] mqtt_init: 0 <OK>
[connect_cb:81] user_data: CONNECTED
[try_to_connect:212] mqtt_tx_connect: 0 <OK>
[publisher:276] try_to_connect: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 1888, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 16356, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 16356, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 45861, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 53870, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 53870, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 60144, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 6561, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 6561, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 38355, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 60656, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 60656, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[publisher:285] mqtt_tx_pingreq: 0 <OK>
[publisher:290] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBACK> packet id: 28420, user_data: PUBLISH
[publisher:295] mqtt_tx_publish: 0 <OK>
[publish_cb:149] <MQTT_PUBREC> packet id: 49829, user_data: PUBLISH
[publish_cb:149] <MQTT_PUBCOMP> packet id: 49829, user_data: PUBLISH
[publisher:300] mqtt_tx_publish: 0 <OK>
[disconnect_cb:101] user_data: DISCONNECTED
[publisher:304] mqtt_tx_disconnect: 0 <OK>
Bye!
다음 line은 error가 감지되어 새 connect message를 보낼 것이라는 뜻입니다.
[try_to_connect:220] mqtt_connect: -5 <ERROR>
MQTT API는 asynchronous 방식이므로 callback이 실행될 때 message가 표시됩니다.
Subscriber가 수신하는 정보는 다음과 같습니다. 각 반복에서 QoS 0, 1, 2 message가 차례로 전달됩니다.
$ mosquitto_sub -t sensors
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
DOORS:OPEN_QoS0
DOORS:OPEN_QoS1
DOORS:OPEN_QoS2
MQTT broker 출력은 다음과 같습니다. Connection과 CONNACK 뒤 PINGREQ/PINGRESP가 오가며, QoS 0은 acknowledgement 없이, QoS 1은 PUBACK으로, QoS 2는 PUBREC·PUBREL·PUBCOMP handshake로 완료된 뒤 client가 disconnect합니다.
$ sudo mosquitto -v
1485663791: mosquitto version 1.3.4 (build date 2014-08-17 00:14:52-0300) starting
1485663791: Using default config.
1485663791: Opening ipv4 listen socket on port 1883.
1485663791: Opening ipv6 listen socket on port 1883.
1485663797: New connection from 192.168.1.101 on port 1883.
1485663797: New client connected from 192.168.1.101 as zephyr_publisher (c1, k0).
1485663797: Sending CONNACK to zephyr_publisher (0)
1485663798: Received PINGREQ from zephyr_publisher
1485663798: Sending PINGRESP to zephyr_publisher
1485663798: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663799: Received PUBLISH from zephyr_publisher (d0, q1, r0, m1888, 'sensors', ... (15 bytes))
1485663799: Sending PUBACK to zephyr_publisher (Mid: 1888)
1485663799: Received PUBLISH from zephyr_publisher (d0, q2, r0, m16356, 'sensors', ... (15 bytes))
1485663799: Sending PUBREC to zephyr_publisher (Mid: 16356)
1485663799: Received PUBREL from zephyr_publisher (Mid: 16356)
1485663799: Sending PUBCOMP to zephyr_publisher (Mid: 16356)
1485663800: Received PINGREQ from zephyr_publisher
1485663800: Sending PINGRESP to zephyr_publisher
1485663800: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663801: Received PUBLISH from zephyr_publisher (d0, q1, r0, m45861, 'sensors', ... (15 bytes))
1485663801: Sending PUBACK to zephyr_publisher (Mid: 45861)
1485663801: Received PUBLISH from zephyr_publisher (d0, q2, r0, m53870, 'sensors', ... (15 bytes))
1485663801: Sending PUBREC to zephyr_publisher (Mid: 53870)
1485663801: Received PUBREL from zephyr_publisher (Mid: 53870)
1485663801: Sending PUBCOMP to zephyr_publisher (Mid: 53870)
1485663802: Received PINGREQ from zephyr_publisher
1485663802: Sending PINGRESP to zephyr_publisher
1485663802: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663803: Received PUBLISH from zephyr_publisher (d0, q1, r0, m60144, 'sensors', ... (15 bytes))
1485663803: Sending PUBACK to zephyr_publisher (Mid: 60144)
1485663803: Received PUBLISH from zephyr_publisher (d0, q2, r0, m6561, 'sensors', ... (15 bytes))
1485663803: Sending PUBREC to zephyr_publisher (Mid: 6561)
1485663803: Received PUBREL from zephyr_publisher (Mid: 6561)
1485663803: Sending PUBCOMP to zephyr_publisher (Mid: 6561)
1485663804: Received PINGREQ from zephyr_publisher
1485663804: Sending PINGRESP to zephyr_publisher
1485663804: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663805: Received PUBLISH from zephyr_publisher (d0, q1, r0, m38355, 'sensors', ... (15 bytes))
1485663805: Sending PUBACK to zephyr_publisher (Mid: 38355)
1485663805: Received PUBLISH from zephyr_publisher (d0, q2, r0, m60656, 'sensors', ... (15 bytes))
1485663805: Sending PUBREC to zephyr_publisher (Mid: 60656)
1485663805: Received PUBREL from zephyr_publisher (Mid: 60656)
1485663805: Sending PUBCOMP to zephyr_publisher (Mid: 60656)
1485663806: Received PINGREQ from zephyr_publisher
1485663806: Sending PINGRESP to zephyr_publisher
1485663806: Received PUBLISH from zephyr_publisher (d0, q0, r0, m0, 'sensors', ... (15 bytes))
1485663807: Received PUBLISH from zephyr_publisher (d0, q1, r0, m28420, 'sensors', ... (15 bytes))
1485663807: Sending PUBACK to zephyr_publisher (Mid: 28420)
1485663807: Received PUBLISH from zephyr_publisher (d0, q2, r0, m49829, 'sensors', ... (15 bytes))
1485663807: Sending PUBREC to zephyr_publisher (Mid: 49829)
1485663807: Received PUBREL from zephyr_publisher (Mid: 49829)
1485663807: Sending PUBCOMP to zephyr_publisher (Mid: 49829)
1485663808: Received DISCONNECT from zephyr_publisher
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.