summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2025-07-29 12:14:12 +0200
committerxengineering <me@xengineering.eu>2025-07-30 21:53:35 +0200
commit0eae46d834489f07db868300b79660a0a739c218 (patch)
tree74e4917cd15d9b31e0d2f3c93600b7d6adc9cc85
parent467be807cde370b1ec6f514bcb519842140c192f (diff)
downloadiot-contact-0eae46d834489f07db868300b79660a0a739c218.tar
iot-contact-0eae46d834489f07db868300b79660a0a739c218.tar.zst
iot-contact-0eae46d834489f07db868300b79660a0a739c218.zip
fw: app: mqtt: Add MQTT broker connection
This is the first minimal step towards MQTT: Connecting to a broker. Wireshark validates that there is actually MQTT data flow in both directions.
-rw-r--r--fw/app/CMakeLists.txt1
-rw-r--r--fw/app/prj.conf2
-rw-r--r--fw/app/src/mqtt.c97
-rw-r--r--fw/app/src/mqtt.h10
4 files changed, 110 insertions, 0 deletions
diff --git a/fw/app/CMakeLists.txt b/fw/app/CMakeLists.txt
index de1c418..7e06a0c 100644
--- a/fw/app/CMakeLists.txt
+++ b/fw/app/CMakeLists.txt
@@ -20,6 +20,7 @@ target_sources(app
"${CMAKE_CURRENT_SOURCE_DIR}/src/ws.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/heart.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/settings.c"
+ "${CMAKE_CURRENT_SOURCE_DIR}/src/mqtt.c"
)
target_sources_ifdef(
diff --git a/fw/app/prj.conf b/fw/app/prj.conf
index 6589ec0..7400b7c 100644
--- a/fw/app/prj.conf
+++ b/fw/app/prj.conf
@@ -59,3 +59,5 @@ CONFIG_SETTINGS_SHELL=y
CONFIG_GNU_C_EXTENSIONS=y
CONFIG_JSON_LIBRARY=y
+
+CONFIG_MQTT_LIB=y
diff --git a/fw/app/src/mqtt.c b/fw/app/src/mqtt.c
new file mode 100644
index 0000000..605cfd1
--- /dev/null
+++ b/fw/app/src/mqtt.c
@@ -0,0 +1,97 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public License,
+ * v. 2.0. If a copy of the MPL was not distributed with this file, You can
+ * obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <poll.h>
+
+#include <zephyr/kernel.h>
+#include <zephyr/logging/log.h>
+#include <zephyr/net/socket.h>
+#include <zephyr/net/mqtt.h>
+#include <zephyr/net/net_ip.h>
+
+
+LOG_MODULE_REGISTER(mqtt);
+
+
+#define MQTT_STACK_SIZE 500
+#define MQTT_PRIO K_PRIO_PREEMPT(8)
+
+
+static uint8_t rx_buffer[256];
+static uint8_t tx_buffer[256];
+static struct mqtt_client client_ctx;
+static struct sockaddr_storage broker;
+static struct zsock_pollfd fds[1];
+static bool connected = false;
+
+
+void mqtt_evt_handler(struct mqtt_client *client, const struct mqtt_evt *evt) {
+ switch (evt->type) {
+ case MQTT_EVT_CONNACK:
+ LOG_INF("Connected to MQTT broker");
+ connected = true;
+ break;
+ default:
+ LOG_ERR("Unhandled MQTT event %d", evt->type);
+ }
+}
+
+
+static void mqtt_thread_function(void *ptr1, void *ptr2, void *ptr3) {
+ LOG_INF("Starting MQTT thread");
+
+ struct sockaddr_in6 *broker6 = (struct sockaddr_in6 *)&broker;
+ broker6->sin6_family = AF_INET6;
+ broker6->sin6_port = htons(1883);
+ int rc = zsock_inet_pton(AF_INET6, "2001:db8::36", &broker6->sin6_addr);
+ if (rc != 1) {
+ LOG_ERR("Failed to convert IPv6 address (%d)", rc);
+ } else {
+ LOG_INF("Converted IPv6 address successfully");
+ }
+
+ mqtt_client_init(&client_ctx);
+
+ /* MQTT client configuration */
+ client_ctx.broker = &broker;
+ client_ctx.evt_cb = mqtt_evt_handler;
+ client_ctx.client_id.utf8 = (uint8_t *)"zephyr_mqtt_client";
+ client_ctx.client_id.size = sizeof("zephyr_mqtt_client") - 1;
+ client_ctx.password = NULL;
+ client_ctx.user_name = NULL;
+ client_ctx.protocol_version = MQTT_VERSION_3_1_1;
+ client_ctx.transport.type = MQTT_TRANSPORT_NON_SECURE;
+
+ /* MQTT buffers configuration */
+ client_ctx.rx_buf = rx_buffer;
+ client_ctx.rx_buf_size = sizeof(rx_buffer);
+ client_ctx.tx_buf = tx_buffer;
+ client_ctx.tx_buf_size = sizeof(tx_buffer);
+
+ rc = mqtt_connect(&client_ctx);
+ if (rc != 0) {
+ LOG_ERR("Failed to connect (%d)", rc);
+ return;
+ }
+
+ fds[0].fd = client_ctx.transport.tcp.sock;
+ fds[0].events = ZSOCK_POLLIN;
+ zsock_poll(fds, 1, 5000);
+
+ mqtt_input(&client_ctx);
+
+ if (!connected) {
+ mqtt_abort(&client_ctx);
+ }
+}
+
+
+K_THREAD_DEFINE(mqtt_thread, MQTT_STACK_SIZE,
+ mqtt_thread_function, NULL, NULL, NULL,
+ MQTT_PRIO, 0, 0);
diff --git a/fw/app/src/mqtt.h b/fw/app/src/mqtt.h
new file mode 100644
index 0000000..241620f
--- /dev/null
+++ b/fw/app/src/mqtt.h
@@ -0,0 +1,10 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public License,
+ * v. 2.0. If a copy of the MPL was not distributed with this file, You can
+ * obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef SRC_MQTT_H
+#define SRC_MQTT_H
+
+#endif // !SRC_MQTT_H