From 80df2dfc52156467829a1e80fd11322dd3a20c34 Mon Sep 17 00:00:00 2001 From: Reimar Date: Mon, 24 Mar 2025 10:40:23 +0100 Subject: [PATCH] Send JSON string to MQTT --- iot/main.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/iot/main.c b/iot/main.c index 51f7f07..fb1571c 100644 --- a/iot/main.c +++ b/iot/main.c @@ -22,9 +22,16 @@ void *watch_temperature(void *arg) while (true) { double temperature = get_temperature(temp_handle); + size_t timestamp = time(NULL); - char *str = malloc(snprintf(NULL, 0, "%lf", temperature) + 1); - sprintf(str, "%lf", temperature); + char *format = "{" + "\"temperature\": %lf," + "\"device_id\": \"%s\"," + "\"timestamp\": %zu" + "}"; + + char *str = malloc(snprintf(NULL, 0, format, temperature, device_id, timestamp) + 1); + sprintf(str, format, temperature, device_id, timestamp); mqtt_send_message("temperature", str);