Skip to main content

How to: Custom MQTT topics

A node talks over the radio to the gateway (the Radio Dongle with the gateway service), which turns the messages into MQTT topics under node/{id}/..., where {id} is the ID or the alias of the node. Besides the built-in topics, your firmware can publish and receive its own topics.

References​

Publishing values​

Each of these functions publishes the value as node/{id}/<subtopic>:

FunctionPayload in MQTT
twr_radio_pub_bool(subtopic, &value)true or false
twr_radio_pub_int(subtopic, &value)number
twr_radio_pub_uint32(subtopic, &value)number
twr_radio_pub_float(subtopic, &value)number
twr_radio_pub_string(subtopic, value)text in quotes
twr_radio_pub_buffer(buffer, length)raw bytes

For example, twr_radio_pub_float("outdoor/temperature", &temperature) appears as node/{id}/outdoor/temperature 21.5. The functions return false and send nothing when the subtopic or the value is too long (see limits).

tip

Every radio message is acknowledged, so many separate messages in quick succession are slow and can get lost. Send values that belong together in one message, for example twr_radio_pub_acceleration() for the three axes of the accelerometer, a string or a buffer.

Receiving values​

The node keeps a table of its own topics and registers them with the gateway; the gateway then forwards the MQTT messages of these topics to the node, which calls the callback of the topic.

  • The topic in the table has exactly four levels, e.g. report/-/interval/set: the gateway forwards only node/{id}/+/+/+/+. Do not write node/, the ID or the alias into it, and do not use the wildcards + and #.
  • The payload type is one of TWR_RADIO_SUB_PT_BOOL, TWR_RADIO_SUB_PT_INT, TWR_RADIO_SUB_PT_FLOAT, TWR_RADIO_SUB_PT_STRING and TWR_RADIO_SUB_PT_NULL. The callback gets a pointer to the value: *(int *) value, *(float *) value, *(bool *) value or (char *) value.
  • The node must be able to receive: initialize the radio with TWR_RADIO_MODE_NODE_LISTENING on a powered node, or with TWR_RADIO_MODE_NODE_SLEEPING and twr_radio_set_rx_timeout_for_sleeping_node(), so that the node listens for a moment after each of its messages.
  • The node registers the topics after its first acknowledged message, so send something after the start, for example the pairing request. The table must stay in memory for the whole run (static const). If the gateway does not forward a topic after you changed the table, remove the node and pair it again.

Custom Topics Code Example

#include <application.h>

static int report_interval = 60;

static void interval_set(uint64_t *id, const char *topic, void *value, void *param)
{
(void) id;
(void) param;

report_interval = *(int *) value;
twr_log_info("%s = %d", topic, report_interval);
}

static void text_set(uint64_t *id, const char *topic, void *value, void *param)
{
(void) id;
(void) topic;
(void) param;

twr_log_info("text: %s", (char *) value);
}

// Topic (exactly four levels), payload type, callback, parameter of the callback
static const twr_radio_sub_t subs[] = {
{"report/-/interval/set", TWR_RADIO_SUB_PT_INT, interval_set, NULL},
{"display/-/text/set", TWR_RADIO_SUB_PT_STRING, text_set, NULL},
};

void application_init(void)
{
twr_log_init(TWR_LOG_LEVEL_DUMP, TWR_LOG_TIMESTAMP_ABS);

twr_radio_init(TWR_RADIO_MODE_NODE_LISTENING);
twr_radio_set_subs((twr_radio_sub_t *) subs, sizeof(subs) / sizeof(twr_radio_sub_t));
twr_radio_pairing_request("custom-topics", FW_VERSION);
}

With the alias custom-topics:0, which the gateway service makes from the name in the pairing request, you then send:

mosquitto_pub -t 'node/custom-topics:0/report/-/interval/set' -m 300
mosquitto_pub -t 'node/custom-topics:0/display/-/text/set' -m '"Hello"'
caution

The payload must be valid JSON: a text goes in double quotes ('"Hello"'). The gateway service drops a message whose payload is not valid JSON.

Built-in commands in your firmware​

The built-in set and get topics of the topics reference reach the node as calls of weak functions of the SDK. Define a function of the same name in your application, and the linker uses yours:

MQTT topicstate_id
led/-/state/setTWR_RADIO_NODE_STATE_LED
relay/-/state/setTWR_RADIO_NODE_STATE_POWER_MODULE_RELAY
relay/0:0/state/setTWR_RADIO_NODE_STATE_RELAY_MODULE_0
relay/0:1/state/setTWR_RADIO_NODE_STATE_RELAY_MODULE_1

The set topics call twr_radio_node_on_state_set(id, state_id, state), the matching get topics twr_radio_node_on_state_get(id, state_id). The LED strip topics have their own functions:

MQTT topicFunction
led-strip/-/color/settwr_radio_node_on_led_strip_color_set()
led-strip/-/brightness/settwr_radio_node_on_led_strip_brightness_set()
led-strip/-/compound/settwr_radio_node_on_led_strip_compound_set()
led-strip/-/effect/settwr_radio_node_on_led_strip_effect_set()
led-strip/-/thermometer/settwr_radio_node_on_led_strip_thermometer_set()

The radio of the node must listen here as well. After a state change, confirm the new state with twr_radio_pub_state(), which publishes, for example, node/{id}/relay/-/state.

Power Module Relay Code Example

void twr_radio_node_on_state_set(uint64_t *id, uint8_t state_id, bool *state)
{
(void) id;

if (state_id == TWR_RADIO_NODE_STATE_POWER_MODULE_RELAY)
{
twr_module_power_relay_set_state(*state);
twr_radio_pub_state(TWR_RADIO_PUB_STATE_POWER_MODULE_RELAY, state);
}
}

Limits​

A radio packet has 64 bytes; after the headers, these limits apply:

WhatLimit
Subtopic of pub_bool, pub_int, pub_uint32, pub_float50 characters
Subtopic and text of pub_string together53 characters
pub_buffer55 bytes
Topic in the subscription table50 characters
Text sent to the node (TWR_RADIO_SUB_PT_STRING)47 characters

Debugging​

To see what the gateway receives, stop the gateway service (on a Raspberry Pi pm2 stop bcg-ud), run it by hand with --debug and restart the node:

bcg --device /dev/ttyUSB0 --debug

A line with $sub confirms that the node has registered a topic; pt is the payload type (0 bool, 1 int, 2 float, 3 string, 4 null):

DEBUG: read b'["$sub", {"topic": "836d19833c33/report/-/interval/set", "pt": 1}]\n'

A message with a payload that is not valid JSON shows up as an error starting with parse json.