samples: net: update mqtt publisher

Added MQTT logging backend to publisher sample

Signed-off-by: Arif Balik <arifbalik@outlook.com>
This commit is contained in:
Arif Balik 2025-06-09 12:56:46 +03:00 committed by Dan Kalowsky
parent 1939ba64c3
commit 2bf86a3ff9
3 changed files with 37 additions and 0 deletions

View File

@ -221,6 +221,27 @@ broker or uses a different port number, modify the following values:
#define SOCKS5_PROXY_ADDR SERVER_ADDR
#define SOCKS5_PROXY_PORT 1080
MQTT logging backend
====================
The sample can be configured to use an MQTT logging backend, which allows log
messages from the application to be published to an MQTT broker. This feature
uses the same MQTT client instance as the main sample application.
The MQTT logging backend uses the :c:func:`log_backend_mqtt_client_set` API to
register an MQTT client for publishing log messages. The backend only uses the
client's :c:func:`mqtt_publish` function and does not manage the client's
connection lifecycle - this remains the application's responsibility.
To enable the MQTT logging backend in the sample, build it with
``-DEXTRA_CONF_FILE=overlay-log-backend-mqtt.conf`` parameter.
Key configuration options available:
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_TOPIC_DEFAULT`: Topic for publishing logs (default: "zephyr/logs")
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_QOS`: QoS level for log messages (default: 0)
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_RETAIN`: Whether to retain log messages (default: disabled)
- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_MAX_MSG_SIZE`: Maximum log message size (default: 256 bytes)
Running on cc3220sf_launchxl
============================

View File

@ -0,0 +1,5 @@
CONFIG_LOG_BACKEND_MQTT=y
CONFIG_LOG_BACKEND_MQTT_QOS=0
CONFIG_LOG_BACKEND_MQTT_RETAIN=n
CONFIG_LOG_BACKEND_MQTT_MAX_MSG_SIZE=1024
CONFIG_LOG_MODE_DEFERRED=y

View File

@ -11,6 +11,9 @@ LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG);
#include <zephyr/net/socket.h>
#include <zephyr/net/mqtt.h>
#include <zephyr/random/random.h>
#if defined(CONFIG_LOG_BACKEND_MQTT)
#include <zephyr/logging/log_backend_mqtt.h>
#endif
#include <string.h>
#include <errno.h>
@ -172,6 +175,10 @@ void mqtt_evt_handler(struct mqtt_client *const client,
}
#endif
#if defined(CONFIG_LOG_BACKEND_MQTT)
log_backend_mqtt_client_set(client);
#endif
break;
case MQTT_EVT_DISCONNECT:
@ -180,6 +187,10 @@ void mqtt_evt_handler(struct mqtt_client *const client,
connected = false;
clear_fds();
#if defined(CONFIG_LOG_BACKEND_MQTT)
log_backend_mqtt_client_set(NULL);
#endif
break;
case MQTT_EVT_PUBACK: