From 6b75285228ea9a96ee456357fb11a6524bd9d567 Mon Sep 17 00:00:00 2001 From: SylvanArnold <89144178+SylvanArnold@users.noreply.github.com> Date: Tue, 29 Apr 2025 16:59:06 +0200 Subject: [PATCH] feat: topic name for sensor update can now be set in terminal --- pico-sensor/README.md | 5 +++++ pico-sensor/src/MinIniKeys.h | 1 + pico-sensor/src/mqtt_client.c | 9 +++++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pico-sensor/README.md b/pico-sensor/README.md index ddfdc89..4a4ae09 100644 --- a/pico-sensor/README.md +++ b/pico-sensor/README.md @@ -14,12 +14,17 @@ McuMinINI write settings.ini WiFi pass "YOUR_PASSWORD" ### MQTT +Broker informations: ```shell McuMinINI write settings.ini MQTT broker "BROKER_NAME" McuMinINI write settings.ini MQTT user "USERNAME" McuMinINI write settings.ini MQTT pass "PASSWORD" ``` +Topic name: +```shell +McuMinINI write settings.ini MQTT topic_sensor_update "///update" +``` ## Build Project has `Debug`, `Release` and `Test` targets, using CMake Presets. diff --git a/pico-sensor/src/MinIniKeys.h b/pico-sensor/src/MinIniKeys.h index b3ca3b4..7f1ae88 100644 --- a/pico-sensor/src/MinIniKeys.h +++ b/pico-sensor/src/MinIniKeys.h @@ -34,6 +34,7 @@ #define NVMC_MININI_KEY_MQTT_USER "user" /* string, username */ #define NVMC_MININI_KEY_MQTT_PASS "pass" /* string, password */ #define NVMC_MININI_KEY_MQTT_PUBLISH "publish" /* bool, if publishing */ + #define NVMC_TOPIC_NAME_SENSORS_UPDATE "topic_sensor_update" /*///update*/ #endif #if PL_CONFIG_USE_NTP_CLIENT diff --git a/pico-sensor/src/mqtt_client.c b/pico-sensor/src/mqtt_client.c index 41fb4a5..32fd1f3 100644 --- a/pico-sensor/src/mqtt_client.c +++ b/pico-sensor/src/mqtt_client.c @@ -37,8 +37,6 @@ #define TOPIC_NAME_BATTERY_POWER "homeassistant/sensor/powerwall_battery_now/state" #define TOPIC_NAME_BATTERY_PERCENTAGE "homeassistant/sensor/powerwall_charge/state" #define TOPIC_NAME_CHARGER_CHARGING_POWER "home/charger/power" -#elif MQTT_CLIENT_IS_SENSOR - #define TOPIC_NAME_SENSORS_UPDATE "sylvan/Home/PicoSensor/update" /*///update*/ #endif typedef enum topic_ID_e { @@ -61,6 +59,7 @@ typedef enum topic_ID_e { #define MQTT_DEFAULT_USER "user" #define MQTT_DEFAULT_PASS "password" #define MQTT_DEFAULT_PUBLISH true +#define DEFAULT_TOPIC_NAME_SENSORS_UPDATE "user/room/device/update" /*///update*/ typedef struct mqtt_t { mqtt_client_t *mqtt_client; /* lwIP MQTT client handle */ @@ -69,6 +68,7 @@ typedef struct mqtt_t { unsigned char client_id[32]; /* client ID used for connection: each client should have a unique ID */ unsigned char client_user[32]; /* client user name used for connection */ unsigned char client_pass[96]; /* client user password */ + unsigned char sensors_update_topic[64]; /* topic name for sensor updates */ topic_ID_e in_pub_ID; /* incoming published ID, set in the incoming_publish_cb and used in the incoming_data_cb */ /* configuration settings */ bool doLogging; /* if it shall write log messages */ @@ -119,7 +119,7 @@ int MqttClient_Publish_SensorValues(float temperature, float humidity) { McuUtility_strcatNumFloat(buf, sizeof(buf), humidity, 2); McuUtility_strcat(buf, sizeof(buf), (unsigned char*)"}}"); - res = mqtt_publish(mqtt.mqtt_client, TOPIC_NAME_SENSORS_UPDATE, buf, strlen(buf), qos, retain, mqtt_publish_request_cb, NULL); + res = mqtt_publish(mqtt.mqtt_client, mqtt.sensors_update_topic, buf, strlen(buf), qos, retain, mqtt_publish_request_cb, NULL); if (res != ERR_OK) { McuLog_error("Failed sensor values mqtt_publish: %d", res); (void)MqttClient_Disconnect(); /* try disconnect and connect again */ @@ -304,7 +304,7 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len } else if (McuUtility_strcmp(topic, TOPIC_NAME_CHARGER_CHARGING_POWER)==0) { mqtt.in_pub_ID = Topic_ID_Charging_Power; #elif MQTT_CLIENT_IS_SENSOR - if (McuUtility_strcmp(topic, TOPIC_NAME_SENSORS_UPDATE)==0) { + if (McuUtility_strcmp(topic, mqtt.sensors_update_topic)==0) { mqtt.in_pub_ID = Topic_ID_Sensor_Update; #endif } else { /* unknown */ @@ -405,6 +405,7 @@ uint8_t MqttClient_Connect(void) { McuMinINI_ini_gets(NVMC_MININI_SECTION_MQTT, NVMC_MININI_KEY_MQTT_CLIENT, MQTT_DEFAULT_CLIENT, mqtt.client_id, sizeof(mqtt.client_id), NVMC_MININI_FILE_NAME); McuMinINI_ini_gets(NVMC_MININI_SECTION_MQTT, NVMC_MININI_KEY_MQTT_USER, MQTT_DEFAULT_USER, mqtt.client_user, sizeof(mqtt.client_user), NVMC_MININI_FILE_NAME); McuMinINI_ini_gets(NVMC_MININI_SECTION_MQTT, NVMC_MININI_KEY_MQTT_PASS, MQTT_DEFAULT_PASS, mqtt.client_pass, sizeof(mqtt.client_pass), NVMC_MININI_FILE_NAME); + McuMinINI_ini_gets(NVMC_MININI_SECTION_MQTT, NVMC_TOPIC_NAME_SENSORS_UPDATE, DEFAULT_TOPIC_NAME_SENSORS_UPDATE, mqtt.sensors_update_topic, sizeof(mqtt.sensors_update_topic), NVMC_MININI_FILE_NAME); mqtt.doPublishing = McuMinINI_ini_getbool(NVMC_MININI_SECTION_MQTT, NVMC_MININI_KEY_MQTT_PASS, MQTT_DEFAULT_PUBLISH, NVMC_MININI_FILE_NAME); #else McuUtility_strcpy(mqtt.broker, sizeof(mqtt.broker), MQTT_DEFAULT_BROKER);