summaryrefslogtreecommitdiff
path: root/nrfdemo/src/mqtt_connection.c
diff options
context:
space:
mode:
authorMichal Hanus <hanus.michal@divelit.cz>2025-05-21 20:58:52 +0200
committerMichal Hanus <hanus.michal@divelit.cz>2025-05-21 20:58:52 +0200
commita407db2d45286f37eff10710c9a8e43755cedfc6 (patch)
treeb2451cf76bad9d2ee8f80df9f9df32f176c1e5eb /nrfdemo/src/mqtt_connection.c
parent85f4aadd7382eec843a02f513f6933abba6e5d02 (diff)
bck
Diffstat (limited to 'nrfdemo/src/mqtt_connection.c')
-rw-r--r--nrfdemo/src/mqtt_connection.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/nrfdemo/src/mqtt_connection.c b/nrfdemo/src/mqtt_connection.c
index ef4e69f..1d884b7 100644
--- a/nrfdemo/src/mqtt_connection.c
+++ b/nrfdemo/src/mqtt_connection.c
@@ -98,6 +98,36 @@ static void data_print(uint8_t *prefix, uint8_t *data, size_t len)
LOG_INF("%s%s", (char *)prefix, (char *)buf);
}
+int publish_sensor(struct mqtt_client *c, uint8_t* topic, int32_t val1, int32_t val2)
+{
+ uint8_t buf[20], sgn = (val1 < 0 && val2 < 0) ? '-' : '+';
+ snprintf(buf, 20, "%c%i.%03i", labs(val1), labs(val2)/1000);
+ publish_wrap(c, topic, buf, strlen(buf));
+}
+
+uint16_t mqtt_cycle = 0;
+
+int publish_wrap(struct mqtt_client *c, uint8_t* topic, uint8_t *data, size_t len)
+{
+ struct mqtt_publish_param param;
+
+ param.message.topic.qos = MQTT_QOS_1_AT_LEAST_ONCE;
+ param.message.topic.topic.utf8 = topic;
+ param.message.topic.topic.size = strlen(topic);
+ param.message.payload.data = data;
+ param.message.payload.len = len;
+ param.message_id = mqtt_cycle++;
+ param.dup_flag = 0;
+ param.retain_flag = 0;
+
+ data_print("Publishing: ", data, len);
+ LOG_INF("to topic: %s len: %u",
+ topic,
+ (unsigned int)strlen(topic));
+
+ return mqtt_publish(c, &param);
+}
+
/**@brief Function to publish data on the configured topic
*/
/* Define the function data_publish() to publish data */