Merge branch 'feat/pico-sensor/request-measurement'

feat(pico-sensor): request-measurement

Closes #28

See merge request team-raclette/project-softweng!13
This commit is contained in:
Sylvan Arnold
2025-05-23 07:01:46 +00:00
9 changed files with 70 additions and 25 deletions

View File

@@ -11,7 +11,7 @@ import (
// Command represents the structure of the command to be published
// @Description Command structure for publishing
type Command struct {
Command string `json:"command" example:"MEASURE_NEW" binding:"required"`
Command string `json:"command" example:"measure" binding:"required"`
}
// @Summary Publish command
@@ -55,7 +55,7 @@ func (gh *Gateway) publishCommand(c *gin.Context) error {
}
// Publish the command to the MQTT broker
topic := user + "/" + room + "/" + device + "/command/" + json.Command
topic := user + "/" + room + "/" + device + "/cmd/" + json.Command
token := gh.mqtt.Publish(topic, 1, false, "")
if token.Wait() && token.Error() != nil {
return token.Error()

View File

@@ -184,7 +184,7 @@ const docTemplate = `{
"properties": {
"command": {
"type": "string",
"example": "MEASURE_NEW"
"example": "measure"
}
}
}

View File

@@ -178,7 +178,7 @@
"properties": {
"command": {
"type": "string",
"example": "MEASURE_NEW"
"example": "measure"
}
}
}

View File

@@ -7,7 +7,7 @@ definitions:
description: Command structure for publishing
properties:
command:
example: MEASURE_NEW
example: measure
type: string
required:
- command

View File

@@ -21,9 +21,10 @@ McuMinINI write settings.ini MQTT user "USERNAME"
McuMinINI write settings.ini MQTT pass "PASSWORD"
```
Topic name:
Topic names:
```shell
McuMinINI write settings.ini MQTT topic_sensor_update "<user>/<room>/<device>/update"
McuMinINI write settings.ini MQTT topic_send_measurement "<user>/<room>/<device>/cmd/measure"
```
## Build

View File

@@ -35,6 +35,7 @@
#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" /*<user>/<room>/<device>/update*/
#define NVMC_TOPIC_NAME_SEND_MEASUREMENT "topic_send_measurement" /*<user>/<room>/<device>/cmd/measure*/
#endif
#if PL_CONFIG_USE_NTP_CLIENT

View File

@@ -132,10 +132,19 @@ static void MqttTask(void *pv) {
McuLog_error("failed publishing sensor values");
}
}
vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(10000));
} /* for */
}
static void MyMqttMessageCallback(topic_ID_e topic, const unsigned char *payload, size_t len) {
McuLog_info("MQTT message received, topic: %d, payload: %.*s", topic, (int)len, payload);
if (topic==Topic_ID_Send_Measurement) {
if (MqttClient_Publish_SensorValues(Sensor_GetTemperature(), Sensor_GetHumidity())!=ERR_OK) {
McuLog_error("failed publishing sensor values");
}
}
}
#endif
void App_Init(void) {
@@ -154,6 +163,7 @@ void App_Init(void) {
}
#endif
#if PL_CONFIG_USE_MQTT_CLIENT
MqttClient_RegisterMessageCallback(MyMqttMessageCallback); // register the callback for incoming messages
if (xTaskCreate(
MqttTask, /* pointer to the task */
"mqtt", /* task name for kernel awareness debugging */

View File

@@ -39,27 +39,14 @@
#define TOPIC_NAME_CHARGER_CHARGING_POWER "home/charger/power"
#endif
typedef enum topic_ID_e {
Topic_ID_None,
#if MQTT_CLIENT_IS_EV_CHARGER
Topic_ID_Solar_Power, /* power from PV panels */
Topic_ID_Site_Power, /* power to the house/site */
Topic_ID_Grid_Power, /* power from/to grid */
Topic_ID_Battery_Power, /* power from/to battery */
Topic_ID_Battery_Percentage,/* battery level percentage */
Topic_ID_Charging_Power, /* actual charging power */
#elif MQTT_CLIENT_IS_SENSOR
Topic_ID_Sensor_Update,
#endif
} topic_ID_e;
/* default entries for the MQTT broker connection */
#define MQTT_DEFAULT_BROKER "homeassistant"
#define MQTT_DEFAULT_CLIENT "client"
#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" /*<user>/<room>/<device>/update*/
#define DEFAULT_TOPIC_NAME_SENSORS_UPDATE "user/room/device/update"
#define DEFAULT_TOPIC_NAME_SEND_MEASUREMENT "user/room/device/cmd/measure"
typedef struct mqtt_t {
mqtt_client_t *mqtt_client; /* lwIP MQTT client handle */
@@ -69,6 +56,7 @@ typedef struct mqtt_t {
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 */
unsigned char send_measurement_topic[64]; /* topic name for sending measurement */
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 */
@@ -91,6 +79,13 @@ static const struct mqtt_connect_client_info_t mqtt_client_info = {
#endif
};
// Static variable to hold the callback pointer
static MqttClient_MessageCallback_t app_message_callback = NULL;
void MqttClient_RegisterMessageCallback(MqttClient_MessageCallback_t cb) {
app_message_callback = cb;
}
static void mqtt_publish_request_cb(void *arg, err_t err) {
#if 0 && MQTT_EXTRA_LOGS
const struct mqtt_connect_client_info_t *client_info = (const struct mqtt_connect_client_info_t*)arg;
@@ -275,8 +270,8 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f
McuLog_trace("bat%%: %s%%", buf);
}
#elif MQTT_CLIENT_IS_SENSOR
if (mqtt.in_pub_ID == Topic_ID_Sensor_Update) {
McuLog_trace("Sensor update");
if (app_message_callback != NULL) {
app_message_callback(mqtt.in_pub_ID, data, len);
#endif
} else {
McuLog_trace("mqtt_incoming_data_cb: Ignoring payload...");
@@ -306,6 +301,8 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len
#elif MQTT_CLIENT_IS_SENSOR
if (McuUtility_strcmp(topic, mqtt.sensors_update_topic)==0) {
mqtt.in_pub_ID = Topic_ID_Sensor_Update;
} else if (McuUtility_strcmp(topic, mqtt.send_measurement_topic)==0) {
mqtt.in_pub_ID = Topic_ID_Send_Measurement;
#endif
} else { /* unknown */
McuLog_trace("MQTT client \"%s\" publish cb: topic %s, len %d", client_info->client_id, topic, (int)tot_len);
@@ -381,7 +378,15 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection
McuLog_error("failed subscribing, err %d", err);
}
#elif MQTT_CLIENT_IS_SENSOR
/* no subscriptions */
err = mqtt_sub_unsub(client,
mqtt.send_measurement_topic, /* topic: send measurement command */
1, /* quality of service */
mqtt_request_cb, // Callback to call when subscribe/unsubscribe response is received
LWIP_CONST_CAST(void*, client_info),
1); // 1 for subscribe, 0 for unsubscribe
if (err!=ERR_OK) {
McuLog_error("failed subscribing, err %d", err);
}
#endif
} else if (status==MQTT_CONNECT_DISCONNECTED) {
McuLog_trace("MQTT connect disconnect");
@@ -406,6 +411,7 @@ uint8_t MqttClient_Connect(void) {
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);
McuMinINI_ini_gets(NVMC_MININI_SECTION_MQTT, NVMC_TOPIC_NAME_SEND_MEASUREMENT, DEFAULT_TOPIC_NAME_SEND_MEASUREMENT, mqtt.send_measurement_topic, sizeof(mqtt.send_measurement_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);

View File

@@ -70,6 +70,33 @@ void MqttClient_Deinit(void);
*/
void MqttClient_Init(void);
typedef enum topic_ID_e {
Topic_ID_None,
#if MQTT_CLIENT_IS_EV_CHARGER
Topic_ID_Solar_Power, /* power from PV panels */
Topic_ID_Site_Power, /* power to the house/site */
Topic_ID_Grid_Power, /* power from/to grid */
Topic_ID_Battery_Power, /* power from/to battery */
Topic_ID_Battery_Percentage,/* battery level percentage */
Topic_ID_Charging_Power, /* actual charging power */
#elif MQTT_CLIENT_IS_SENSOR
Topic_ID_Sensor_Update,
Topic_ID_Send_Measurement
#endif
} topic_ID_e;
/*!
* \brief Callback function for incoming messages
*/
typedef void (*MqttClient_MessageCallback_t)(topic_ID_e topic, const unsigned char *payload, size_t len);
/*!
* \brief Register a callback function for incoming messages
* \param cb Callback function to be registered
*/
void MqttClient_RegisterMessageCallback(MqttClient_MessageCallback_t cb);
#ifdef __cplusplus
} /* extern "C" */
#endif