README.rst
1.. zephyr:code-sample:: mqtt-sn-publisher
2 :name: MQTT-SN publisher
3 :relevant-api: mqtt_sn_socket
4
5 Send MQTT-SN PUBLISH messages to an MQTT-SN gateway.
6
7Overview
8********
9
10`MQTT <http://mqtt.org/>`_ (MQ Telemetry Transport) is a lightweight
11publish/subscribe messaging protocol optimized for small sensors and
12mobile devices.
13
14MQTT-SN can be considered as a version of MQTT which is adapted to
15the peculiarities of a wireless communication environment. While MQTT
16requires a reliable TCP/IP transport, MQTT-SN is designed to be usable
17on any datagram-based transport like UDP, ZigBee or even a plain UART
18(with an additional framing protocol).
19
20The Zephyr MQTT-SN Publisher sample application is an MQTT-SN v1.2
21client that sends MQTT-SN PUBLISH messages to an MQTT-SN gateway.
22It also SUBSCRIBEs to a topic.
23See the `MQTT-SN v1.2 spec`_ for more information.
24
25.. _MQTT-SN v1.2 spec: https://www.oasis-open.org/committees/download.php/66091/MQTT-SN_spec_v1.2.pdf
26
27The source code of this sample application can be found at:
28:zephyr_file:`samples/net/mqtt_sn_publisher`.
29
30Requirements
31************
32
33- Linux machine
34- MQTT-SN gateway, like Eclipse Paho
35- Mosquitto server: any version that supports MQTT v3.1.1. This sample
36 was tested with mosquitto 1.6.
37- Mosquitto subscriber
38- LAN for testing purposes (Ethernet)
39
40Build and Running
41*****************
42
43This sample application supports both static IP addresses and the Gateway Discovery process.
44Open the :zephyr_file:`samples/net/mqtt_sn_publisher/prj.conf` file and set the IP addresses according
45to the LAN environment. CONFIG_NET_SAMPLE_MQTT_SN_STATIC_GATEWAY can be used to select the
46static IP or Gateway discovery process.
47
48You will also need to start an MQTT-SN gateway. A convenience Docker Compose specification file
49is provided in :zephyr_file:`samples/net/mqtt_sn_publisher/compose/compose.yaml`.
50First, Start the net-tools configuration from[here](https://github.com/zephyrproject-rtos/net-tools)
51with:
52
53.. code-block:: console
54
55 $ ./net-setup.sh --config docker.conf
56
57Then bring up the Docker environment in a separate terminal window with:
58
59.. code-block:: console
60
61 $ cd ./compose
62 $ docker compose up
63
64You can also set up this environment manually.With Paho, you can either build it
65from source - see `PAHO MQTT-SN Gateway`_ - or run an unofficial docker image, l
66ike `kyberpunk/paho`_.
67
68.. _PAHO MQTT-SN Gateway: https://www.eclipse.org/paho/index.php?page=components/mqtt-sn-transparent-gateway/index.php
69.. _kyberpunk/paho: https://hub.docker.com/r/kyberpunk/paho
70
71On your Linux host computer, open 3 terminal windows. At first, start mosquitto:
72
73.. code-block:: console
74
75 $ sudo mosquitto -v -p 1883
76
77Then, in another window, start the gateway, e.g. by using docker:
78
79.. code-block:: console
80
81 $ docker run -it -p 10000:10000 -p 10000:10000/udp --name paho -v $PWD/gateway.conf:/etc/paho/gateway.conf:ro kyberpunk/paho
82
83Then, locate your zephyr directory and type:
84
85.. zephyr-app-commands::
86 :zephyr-app: samples/net/mqtt_sn_publisher
87 :board: native_sim/native/64
88 :goals: run
89 :compact:
90
91Optionally, use any MQTT explorer to connect to your broker.
92
93Sample output
94=============
95
96This is the applications output:
97
98.. code-block:: console
99
100 WARNING: Using a test - not safe - entropy source
101 *** Booting Zephyr OS build zephyr-v3.2.0-279-gc7fa387cea81 ***
102 [00:00:00.000,000] <inf> net_config: Initializing network
103 [00:00:00.000,000] <inf> net_config: IPv4 address: 172.18.0.20
104 [00:00:00.000,000] <inf> mqtt_sn_publisher_sample: MQTT-SN sample
105 [00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Network connected
106 [00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Waiting for connection...
107 [00:00:00.000,000] <inf> mqtt_sn_publisher_sample: Connecting client
108 [00:00:00.510,000] <inf> net_mqtt_sn: Decoding message type: 5
109 [00:00:00.510,000] <inf> net_mqtt_sn: Got message of type 5
110 [00:00:00.510,000] <inf> net_mqtt_sn: MQTT_SN client connected
111 [00:00:00.510,000] <inf> mqtt_sn_publisher_sample: MQTT-SN event EVT_CONNECTED
112 [00:00:01.020,000] <inf> net_mqtt_sn: Decoding message type: 19
113 [00:00:01.020,000] <inf> net_mqtt_sn: Got message of type 19
114 [00:00:10.200,000] <inf> mqtt_sn_publisher_sample: Publishing timestamp
115 [00:00:10.200,000] <inf> net_mqtt_sn: Registering topic
116 2f 75 70 74 69 6d 65 |/uptime
117 [00:00:10.200,000] <inf> net_mqtt_sn: Can't publish; topic is not ready
118 [00:00:10.710,000] <inf> net_mqtt_sn: Decoding message type: 11
119 [00:00:10.710,000] <inf> net_mqtt_sn: Got message of type 11
120 [00:00:10.710,000] <inf> net_mqtt_sn: Publishing to topic ID 14
121 [00:00:20.400,000] <inf> mqtt_sn_publisher_sample: Publishing timestamp
122 [00:00:20.400,000] <inf> net_mqtt_sn: Publishing to topic ID 14
123
124This is the output from the MQTT-SN gateway:
125
126.. code-block:: console
127
128 20221024 140210.191 CONNECT <--- ZEPHYR 0C 04 04 01 00 3C 5A 45 50 48 59 52
129 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
130 20221024 140210.192 CONNACK <=== ZEPHYR 20 02 00 00
131 20221024 140210.192 CONNACK ---> ZEPHYR 03 05 00
132
133 20221024 140210.643 SUBSCRIBE 0001 <--- ZEPHYR 0C 12 00 00 01 2F 6E 75 6D 62 65 72
134 20221024 140210.648 SUBSCRIBE 0001 ===> ZEPHYR 82 0C 00 01 00 07 2F 6E 75 6D 62 65 72 00
135 20221024 140210.660 SUBACK 0001 <=== ZEPHYR 90 03 00 01 00
136 20221024 140210.661 SUBACK 0001 ---> ZEPHYR 08 13 00 00 0D 00 01 00
137
138 20221024 140220.338 REGISTER 0002 <--- ZEPHYR 0D 0A 00 00 00 02 2F 75 70 74 69 6D 65
139 20221024 140220.348 REGACK 0002 ---> ZEPHYR 07 0B 00 0E 00 02 00
140
141 20221024 140220.848 PUBLISH <--- ZEPHYR 0C 0C 00 00 0E 00 00 31 30 32 30 30
142 20221024 140220.850 PUBLISH ===> ZEPHYR 30 0E 00 07 2F 75 70 74 69 6D 65 31 30 32 30 30
143
144 20221024 140230.539 PUBLISH <--- ZEPHYR 0C 0C 00 00 0E 00 00 32 30 34 30 30
145 20221024 140230.542 PUBLISH ===> ZEPHYR 30 0E 00 07 2F 75 70 74 69 6D 65 32 30 34 30 30
146