From 9a0ec170e536549ce1b825e8a2981270e0cc0031 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Mon, 9 Feb 2026 18:55:17 +0000 Subject: [PATCH] refine mqtt mod to receive params --- domestic-climate-monitor/main/main.c | 3 ++- domestic-climate-monitor/main/mqtt.c | 17 +++++++++-------- domestic-climate-monitor/main/mqtt.h | 3 ++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/domestic-climate-monitor/main/main.c b/domestic-climate-monitor/main/main.c index b173a61..d4abd72 100644 --- a/domestic-climate-monitor/main/main.c +++ b/domestic-climate-monitor/main/main.c @@ -9,11 +9,12 @@ #include "wifi.h" static const char *TAG = "main"; +static char *topic = "test_topic"; void loop(esp_mqtt_client_handle_t mqtt_client) { ESP_LOGI(TAG, "Loop running..."); - mqtt_publish(mqtt_client, "Test message"); + mqtt_publish(mqtt_client, topic, "Test message"); vTaskDelay(pdMS_TO_TICKS(1000)); } diff --git a/domestic-climate-monitor/main/mqtt.c b/domestic-climate-monitor/main/mqtt.c index 66b053e..75e1689 100644 --- a/domestic-climate-monitor/main/mqtt.c +++ b/domestic-climate-monitor/main/mqtt.c @@ -1,6 +1,7 @@ #include "esp_event_base.h" #include "esp_log.h" #include "mqtt_client.h" +#include "secrets.h" #include #include @@ -10,8 +11,6 @@ static const char *TAG = "mqtt"; // Logs are coming from this mqtt module static uint32_t MQTT_CONNECTED = 0; static int s_retry_num = 0; -#define MQTT_TOPIC "/topic/test" -#define MQTT_BROKER "mqtt://192.168.178.53:1883" #define MAX_RETRY 5 esp_mqtt_client_handle_t mqtt_connect(); @@ -46,8 +45,9 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, esp_mqtt_client_handle_t mqtt_connect() { - ESP_LOGI(TAG, "Attempting to connect to MQTT broker %s", MQTT_BROKER); - esp_mqtt_client_config_t mqtt_config = {.broker.address.uri = MQTT_BROKER}; + ESP_LOGI(TAG, "Attempting to connect to MQTT broker %s", SECRETS_MQTT_BROKER); + esp_mqtt_client_config_t mqtt_config = {.broker.address.uri = + SECRETS_MQTT_BROKER}; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_config); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); @@ -55,14 +55,15 @@ esp_mqtt_client_handle_t mqtt_connect() return client; } -void mqtt_publish(esp_mqtt_client_handle_t client, const char *payload) +void mqtt_publish(esp_mqtt_client_handle_t client, char *topic, + const char *payload) { if (MQTT_CONNECTED) { - ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, MQTT_TOPIC); - esp_mqtt_client_publish(client, MQTT_TOPIC, payload, strlen(payload), 0, 0); + ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, topic); + esp_mqtt_client_publish(client, topic, payload, strlen(payload), 0, 0); } else { ESP_LOGI(TAG, "Could not publish to MQTT topic %s. Not connected to broker", - MQTT_TOPIC); + topic); } } diff --git a/domestic-climate-monitor/main/mqtt.h b/domestic-climate-monitor/main/mqtt.h index 8dc0b26..fe269c5 100644 --- a/domestic-climate-monitor/main/mqtt.h +++ b/domestic-climate-monitor/main/mqtt.h @@ -3,6 +3,7 @@ #include "mqtt_client.h" esp_mqtt_client_handle_t mqtt_connect(); -void mqtt_publish(esp_mqtt_client_handle_t client, const char *payload); +void mqtt_publish(esp_mqtt_client_handle_t client, char *topic, + const char *payload); #endif // MQTT_H