Skip to content

Commit 2bf86a3

Browse files
arifbalikdkalowsk
authored andcommitted
samples: net: update mqtt publisher
Added MQTT logging backend to publisher sample Signed-off-by: Arif Balik <arifbalik@outlook.com>
1 parent 1939ba6 commit 2bf86a3

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

samples/net/mqtt_publisher/README.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,27 @@ broker or uses a different port number, modify the following values:
221221
#define SOCKS5_PROXY_ADDR SERVER_ADDR
222222
#define SOCKS5_PROXY_PORT 1080
223223
224+
MQTT logging backend
225+
====================
226+
227+
The sample can be configured to use an MQTT logging backend, which allows log
228+
messages from the application to be published to an MQTT broker. This feature
229+
uses the same MQTT client instance as the main sample application.
230+
231+
The MQTT logging backend uses the :c:func:`log_backend_mqtt_client_set` API to
232+
register an MQTT client for publishing log messages. The backend only uses the
233+
client's :c:func:`mqtt_publish` function and does not manage the client's
234+
connection lifecycle - this remains the application's responsibility.
235+
236+
To enable the MQTT logging backend in the sample, build it with
237+
``-DEXTRA_CONF_FILE=overlay-log-backend-mqtt.conf`` parameter.
238+
239+
Key configuration options available:
240+
241+
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_TOPIC_DEFAULT`: Topic for publishing logs (default: "zephyr/logs")
242+
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_QOS`: QoS level for log messages (default: 0)
243+
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_RETAIN`: Whether to retain log messages (default: disabled)
244+
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_MAX_MSG_SIZE`: Maximum log message size (default: 256 bytes)
224245

225246
Running on cc3220sf_launchxl
226247
============================
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_LOG_BACKEND_MQTT=y
2+
CONFIG_LOG_BACKEND_MQTT_QOS=0
3+
CONFIG_LOG_BACKEND_MQTT_RETAIN=n
4+
CONFIG_LOG_BACKEND_MQTT_MAX_MSG_SIZE=1024
5+
CONFIG_LOG_MODE_DEFERRED=y

samples/net/mqtt_publisher/src/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG);
1111
#include <zephyr/net/socket.h>
1212
#include <zephyr/net/mqtt.h>
1313
#include <zephyr/random/random.h>
14+
#if defined(CONFIG_LOG_BACKEND_MQTT)
15+
#include <zephyr/logging/log_backend_mqtt.h>
16+
#endif
1417

1518
#include <string.h>
1619
#include <errno.h>
@@ -172,6 +175,10 @@ void mqtt_evt_handler(struct mqtt_client *const client,
172175
}
173176
#endif
174177

178+
#if defined(CONFIG_LOG_BACKEND_MQTT)
179+
log_backend_mqtt_client_set(client);
180+
#endif
181+
175182
break;
176183

177184
case MQTT_EVT_DISCONNECT:
@@ -180,6 +187,10 @@ void mqtt_evt_handler(struct mqtt_client *const client,
180187
connected = false;
181188
clear_fds();
182189

190+
#if defined(CONFIG_LOG_BACKEND_MQTT)
191+
log_backend_mqtt_client_set(NULL);
192+
#endif
193+
183194
break;
184195

185196
case MQTT_EVT_PUBACK:

0 commit comments

Comments
 (0)