diff options
Diffstat (limited to 'nrfdemo/src/mqtt_connection.c')
| -rw-r--r-- | nrfdemo/src/mqtt_connection.c | 30 |
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, ¶m); +} + /**@brief Function to publish data on the configured topic */ /* Define the function data_publish() to publish data */ |
