From 82937a0faa692ee90d28a3c6cdc8d0d1537991ee Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 13 Jan 2025 15:32:43 +0300 Subject: [PATCH] Fix undefined error in mqtt implemetation --- embed-proplet/prj.conf | 10 +- embed-proplet/src/main.c | 62 ++--- embed-proplet/src/mqtt_client.c | 392 ++++++++++++++++---------------- embed-proplet/src/mqtt_client.h | 24 +- 4 files changed, 245 insertions(+), 243 deletions(-) diff --git a/embed-proplet/prj.conf b/embed-proplet/prj.conf index b6b087f..b51ec4e 100644 --- a/embed-proplet/prj.conf +++ b/embed-proplet/prj.conf @@ -54,12 +54,12 @@ CONFIG_NET_STATISTICS=y CONFIG_NET_STATISTICS_PERIODIC_OUTPUT=n # Thread Analyzer -CONFIG_THREAD_NAME=y -CONFIG_THREAD_ANALYZER=y -CONFIG_THREAD_ANALYZER_AUTO=y +CONFIG_THREAD_NAME=n +CONFIG_THREAD_ANALYZER=n +CONFIG_THREAD_ANALYZER_AUTO=n CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=5 -CONFIG_THREAD_ANALYZER_LOG_LEVEL_DBG=y -CONFIG_THREAD_ANALYZER_ISR_STACK_USAGE=y +CONFIG_THREAD_ANALYZER_LOG_LEVEL_DBG=n +CONFIG_THREAD_ANALYZER_ISR_STACK_USAGE=n # Miscellaneous CONFIG_MAIN_STACK_SIZE=8192 diff --git a/embed-proplet/src/main.c b/embed-proplet/src/main.c index c7ff290..fc424cd 100644 --- a/embed-proplet/src/main.c +++ b/embed-proplet/src/main.c @@ -1,14 +1,14 @@ #include #include #include "wifi_manager.h" -// #include "mqtt_client.h" +#include "mqtt_client.h" LOG_MODULE_REGISTER(main); -#define WIFI_SSID "YourSSID" -#define WIFI_PSK "YourPassword" -// #define PROPLET_ID "proplet-esp32s3" -// #define CHANNEL_ID "default_channel" +#define WIFI_SSID "Octavifi" +#define WIFI_PSK "Unic0rn_2030" +#define PROPLET_ID "proplet-esp32s3" +#define CHANNEL_ID "default_channel" void main(void) { @@ -24,30 +24,30 @@ void main(void) return; } - // /* Initialize and connect MQTT client */ - // ret = mqtt_client_init_and_connect(); - // if (ret != 0) { - // LOG_ERR("Failed to initialize MQTT client, exiting"); - // return; - // } - - // /* Announce discovery */ - // ret = mqtt_client_discovery_announce(PROPLET_ID, CHANNEL_ID); - // if (ret != 0) { - // LOG_ERR("Failed to publish discovery announcement, exiting"); - // return; - // } - - // /* Subscribe to topics */ - // ret = mqtt_client_subscribe(CHANNEL_ID); - // if (ret != 0) { - // LOG_ERR("Failed to subscribe to topics, exiting"); - // return; - // } - - // /* Main loop for processing MQTT events */ - // while (1) { - // mqtt_client_process(); /* Process MQTT events */ - // k_sleep(K_SECONDS(5)); /* Sleep for a while */ - // } + /* Initialize and connect MQTT client */ + ret = mqtt_client_init_and_connect(); + if (ret != 0) { + LOG_ERR("Failed to initialize MQTT client, exiting"); + return; + } + + /* Announce discovery */ + ret = mqtt_client_discovery_announce(PROPLET_ID, CHANNEL_ID); + if (ret != 0) { + LOG_ERR("Failed to publish discovery announcement, exiting"); + return; + } + + /* Subscribe to topics */ + ret = mqtt_client_subscribe(CHANNEL_ID); + if (ret != 0) { + LOG_ERR("Failed to subscribe to topics, exiting"); + return; + } + + /* Main loop for processing MQTT events */ + while (1) { + mqtt_client_process(); /* Process MQTT events */ + k_sleep(K_SECONDS(5)); /* Sleep for a while */ + } } diff --git a/embed-proplet/src/mqtt_client.c b/embed-proplet/src/mqtt_client.c index 25a9954..e098263 100644 --- a/embed-proplet/src/mqtt_client.c +++ b/embed-proplet/src/mqtt_client.c @@ -1,195 +1,197 @@ -// #include "mqtt_client.h" -// #include -// #include -// #include - -// LOG_MODULE_REGISTER(mqtt_client); - -// #define RX_BUFFER_SIZE 256 -// #define TX_BUFFER_SIZE 256 - -// #define MQTT_BROKER_HOSTNAME "192.168.1.100" /* Replace with your broker's IP */ -// #define MQTT_BROKER_PORT 1883 - -// #define DISCOVERY_TOPIC_TEMPLATE "channels/%s/messages/control/proplet/create" -// #define START_TOPIC_TEMPLATE "channels/%s/messages/control/manager/start" -// #define STOP_TOPIC_TEMPLATE "channels/%s/messages/control/manager/stop" - -// #define CLIENT_ID "proplet-esp32s3" - -// /* Buffers for MQTT client */ -// static uint8_t rx_buffer[RX_BUFFER_SIZE]; -// static uint8_t tx_buffer[TX_BUFFER_SIZE]; - -// /* MQTT client context */ -// static struct mqtt_client client_ctx; -// static struct sockaddr_storage broker_addr; - -// /* Flags to indicate connection status */ -// static bool mqtt_connected = false; - -// static void mqtt_event_handler(struct mqtt_client *client, const struct mqtt_evt *evt) -// { -// switch (evt->type) { -// case MQTT_EVT_CONNACK: -// if (evt->result == 0) { -// mqtt_connected = true; -// LOG_INF("Connected to MQTT broker"); -// } else { -// LOG_ERR("Connection failed, result: %d", evt->result); -// } -// break; - -// case MQTT_EVT_DISCONNECT: -// mqtt_connected = false; -// LOG_INF("Disconnected from MQTT broker"); -// break; - -// case MQTT_EVT_PUBLISH: { -// const struct mqtt_publish_param *publish = &evt->param.publish; -// LOG_INF("Message received on topic: %s", publish->message.topic.topic.utf8); - -// /* Handle messages */ -// if (strstr(publish->message.topic.topic.utf8, "start")) { -// LOG_INF("Start command received"); -// /* Handle start command */ -// } else if (strstr(publish->message.topic.topic.utf8, "stop")) { -// LOG_INF("Stop command received"); -// /* Handle stop command */ -// } -// break; -// } - -// case MQTT_EVT_SUBACK: -// LOG_INF("Subscribed to topic(s)"); -// break; - -// case MQTT_EVT_PUBACK: -// LOG_INF("Message published successfully"); -// break; - -// default: -// break; -// } -// } - -// int mqtt_client_init_and_connect(void) -// { -// int ret; - -// /* Resolve broker address */ -// struct sockaddr_in *broker = (struct sockaddr_in *)&broker_addr; -// broker->sin_family = AF_INET; -// broker->sin_port = htons(MQTT_BROKER_PORT); -// ret = net_addr_pton(AF_INET, MQTT_BROKER_HOSTNAME, &broker->sin_addr); -// if (ret != 0) { -// LOG_ERR("Failed to resolve broker address, ret=%d", ret); -// return ret; -// } - -// /* Initialize MQTT client */ -// mqtt_client_init(&client_ctx); -// client_ctx.broker = &broker_addr; -// client_ctx.evt_cb = mqtt_event_handler; -// client_ctx.client_id.utf8 = CLIENT_ID; -// client_ctx.client_id.size = strlen(CLIENT_ID); -// client_ctx.protocol_version = MQTT_VERSION_3_1_1; -// client_ctx.transport.type = MQTT_TRANSPORT_NON_SECURE; - -// /* Assign buffers */ -// 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); - -// /* Connect to broker */ -// ret = mqtt_connect(&client_ctx); -// if (ret != 0) { -// LOG_ERR("MQTT connect failed, ret=%d", ret); -// return ret; -// } - -// LOG_INF("MQTT client initialized and connected"); -// return 0; -// } - -// int mqtt_client_discovery_announce(const char *proplet_id, const char *channel_id) -// { -// char topic[128]; -// snprintf(topic, sizeof(topic), DISCOVERY_TOPIC_TEMPLATE, channel_id); - -// char payload[128]; -// snprintf(payload, sizeof(payload), -// "{\"proplet_id\":\"%s\",\"mg_channel_id\":\"%s\"}", proplet_id, channel_id); - -// struct mqtt_publish_param param = { -// .message = { -// .topic = { -// .topic = topic, -// .topic_len = strlen(topic), -// }, -// .payload = { -// .data = payload, -// .len = strlen(payload), -// }, -// }, -// .message_id = 0, -// .dup_flag = 0, -// .retain_flag = 0, -// .qos = MQTT_QOS_1_AT_LEAST_ONCE, -// }; - -// int ret = mqtt_publish(&client_ctx, ¶m); -// if (ret != 0) { -// LOG_ERR("Failed to publish discovery announcement, ret=%d", ret); -// } - -// return ret; -// } - -// int mqtt_client_subscribe(const char *channel_id) -// { -// char start_topic[128]; -// snprintf(start_topic, sizeof(start_topic), START_TOPIC_TEMPLATE, channel_id); - -// char stop_topic[128]; -// snprintf(stop_topic, sizeof(stop_topic), STOP_TOPIC_TEMPLATE, channel_id); - -// struct mqtt_topic topics[] = { -// { -// .topic = { -// .topic = start_topic, -// .topic_len = strlen(start_topic), -// }, -// .qos = MQTT_QOS_1_AT_LEAST_ONCE, -// }, -// { -// .topic = { -// .topic = stop_topic, -// .topic_len = strlen(stop_topic), -// }, -// .qos = MQTT_QOS_1_AT_LEAST_ONCE, -// }, -// }; - -// struct mqtt_subscription_list sub_list = { -// .list = topics, -// .list_count = ARRAY_SIZE(topics), -// .message_id = 1, -// }; - -// int ret = mqtt_subscribe(&client_ctx, &sub_list); -// if (ret != 0) { -// LOG_ERR("Failed to subscribe to topics, ret=%d", ret); -// } - -// return ret; -// } - -// void mqtt_client_process(void) -// { -// if (mqtt_connected) { -// mqtt_input(&client_ctx); -// mqtt_live(&client_ctx); -// } -// } +#include "mqtt_client.h" +#include +#include +#include + +LOG_MODULE_REGISTER(mqtt_client); + +#define RX_BUFFER_SIZE 256 +#define TX_BUFFER_SIZE 256 + +#define MQTT_BROKER_HOSTNAME "192.168.1.100" /* Replace with your broker's IP */ +#define MQTT_BROKER_PORT 1883 + +#define DISCOVERY_TOPIC_TEMPLATE "channels/%s/messages/control/proplet/create" +#define START_TOPIC_TEMPLATE "channels/%s/messages/control/manager/start" +#define STOP_TOPIC_TEMPLATE "channels/%s/messages/control/manager/stop" + +#define CLIENT_ID "proplet-esp32s3" + +/* Buffers for MQTT client */ +static uint8_t rx_buffer[RX_BUFFER_SIZE]; +static uint8_t tx_buffer[TX_BUFFER_SIZE]; + +/* MQTT client context */ +static struct mqtt_client client_ctx; +static struct sockaddr_storage broker_addr; + +/* Flags to indicate connection status */ +static bool mqtt_connected = false; + +static void mqtt_event_handler(struct mqtt_client *client, const struct mqtt_evt *evt) +{ + switch (evt->type) { + case MQTT_EVT_CONNACK: + if (evt->result == 0) { + mqtt_connected = true; + LOG_INF("Connected to MQTT broker"); + } else { + LOG_ERR("Connection failed, result: %d", evt->result); + } + break; + + case MQTT_EVT_DISCONNECT: + mqtt_connected = false; + LOG_INF("Disconnected from MQTT broker"); + break; + + case MQTT_EVT_PUBLISH: { + const struct mqtt_publish_param *publish = &evt->param.publish; + LOG_INF("Message received on topic: %s", publish->message.topic.topic.utf8); + + /* Handle messages */ + if (strstr(publish->message.topic.topic.utf8, "start")) { + LOG_INF("Start command received"); + /* Handle start command */ + } else if (strstr(publish->message.topic.topic.utf8, "stop")) { + LOG_INF("Stop command received"); + /* Handle stop command */ + } + break; + } + + case MQTT_EVT_SUBACK: + LOG_INF("Subscribed to topic(s)"); + break; + + case MQTT_EVT_PUBACK: + LOG_INF("Message published successfully"); + break; + + default: + break; + } +} + +int mqtt_client_init_and_connect(void) +{ + int ret; + + /* Resolve broker address */ + struct sockaddr_in *broker = (struct sockaddr_in *)&broker_addr; + broker->sin_family = AF_INET; + broker->sin_port = htons(MQTT_BROKER_PORT); + ret = net_addr_pton(AF_INET, MQTT_BROKER_HOSTNAME, &broker->sin_addr); + if (ret != 0) { + LOG_ERR("Failed to resolve broker address, ret=%d", ret); + return ret; + } + + /* Initialize MQTT client */ + mqtt_client_init(&client_ctx); + client_ctx.broker = &broker_addr; + client_ctx.evt_cb = mqtt_event_handler; + client_ctx.client_id.utf8 = CLIENT_ID; + client_ctx.client_id.size = strlen(CLIENT_ID); + client_ctx.protocol_version = MQTT_VERSION_3_1_1; + client_ctx.transport.type = MQTT_TRANSPORT_NON_SECURE; + + /* Assign buffers */ + 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); + + /* Connect to broker */ + ret = mqtt_connect(&client_ctx); + if (ret != 0) { + LOG_ERR("MQTT connect failed, ret=%d", ret); + return ret; + } + + LOG_INF("MQTT client initialized and connected"); + return 0; +} + +int mqtt_client_discovery_announce(const char *proplet_id, const char *channel_id) +{ + char topic[128]; + snprintf(topic, sizeof(topic), DISCOVERY_TOPIC_TEMPLATE, channel_id); + + char payload[128]; + snprintf(payload, sizeof(payload), + "{\"proplet_id\":\"%s\",\"mg_channel_id\":\"%s\"}", proplet_id, channel_id); + + struct mqtt_publish_param param = { + .message = { + .topic = { + .topic = { + .utf8 = (uint8_t *)topic, // Correctly assign UTF-8 pointer + .size = strlen(topic), // Length of the topic string + }, + .qos = MQTT_QOS_1_AT_LEAST_ONCE, // QoS is part of mqtt_topic + }, + .payload = { + .data = (uint8_t *)payload, // Correctly assign payload data + .len = strlen(payload), // Length of the payload string + }, + }, + .message_id = 0, + .dup_flag = 0, + .retain_flag = 0, + }; + + int ret = mqtt_publish(&client_ctx, ¶m); + if (ret != 0) { + LOG_ERR("Failed to publish discovery announcement, ret=%d", ret); + } + + return ret; +} + +int mqtt_client_subscribe(const char *channel_id) +{ + char start_topic[128]; + snprintf(start_topic, sizeof(start_topic), START_TOPIC_TEMPLATE, channel_id); + + char stop_topic[128]; + snprintf(stop_topic, sizeof(stop_topic), STOP_TOPIC_TEMPLATE, channel_id); + + struct mqtt_topic topics[] = { + { + .topic = { + .utf8 = (uint8_t *)start_topic, // Correctly assign UTF-8 pointer + .size = strlen(start_topic), // Length of the topic string + }, + .qos = MQTT_QOS_1_AT_LEAST_ONCE, // QoS level + }, + { + .topic = { + .utf8 = (uint8_t *)stop_topic, // Correctly assign UTF-8 pointer + .size = strlen(stop_topic), // Length of the topic string + }, + .qos = MQTT_QOS_1_AT_LEAST_ONCE, // QoS level + }, + }; + + struct mqtt_subscription_list sub_list = { + .list = topics, + .list_count = ARRAY_SIZE(topics), + .message_id = 1, + }; + + int ret = mqtt_subscribe(&client_ctx, &sub_list); + if (ret != 0) { + LOG_ERR("Failed to subscribe to topics, ret=%d", ret); + } + + return ret; +} + +void mqtt_client_process(void) +{ + if (mqtt_connected) { + mqtt_input(&client_ctx); + mqtt_live(&client_ctx); + } +} diff --git a/embed-proplet/src/mqtt_client.h b/embed-proplet/src/mqtt_client.h index cff0bcb..f92ca95 100644 --- a/embed-proplet/src/mqtt_client.h +++ b/embed-proplet/src/mqtt_client.h @@ -1,18 +1,18 @@ -// #ifndef MQTT_CLIENT_H -// #define MQTT_CLIENT_H +#ifndef MQTT_CLIENT_H +#define MQTT_CLIENT_H -// #include +#include -// /* Initialize and connect the MQTT client */ -// int mqtt_client_init_and_connect(void); +/* Initialize and connect the MQTT client */ +int mqtt_client_init_and_connect(void); -// /* Process MQTT events */ -// void mqtt_client_process(void); +/* Process MQTT events */ +void mqtt_client_process(void); -// /* Publish discovery announcement */ -// int mqtt_client_discovery_announce(const char *proplet_id, const char *channel_id); +/* Publish discovery announcement */ +int mqtt_client_discovery_announce(const char *proplet_id, const char *channel_id); -// /* Subscribe to required topics */ -// int mqtt_client_subscribe(const char *channel_id); +/* Subscribe to required topics */ +int mqtt_client_subscribe(const char *channel_id); -// #endif /* MQTT_CLIENT_H */ +#endif /* MQTT_CLIENT_H */