Subscription to MQTT topic with string payload

I’m trying to subscribe to MQTT topic with BC_RADIO_SUB_PT_STRING payload type, but callback function is never called. When I send same message, but change type to _INT, it is called just fine. And all other topics I use (int and float) are working fine. Am I doing something wrong?

Excerpt from code:

// topic callback headers
void led_trigger_set(uint64_t *id, const char *topic, void *value, void *param);
void led_brightness_set(uint64_t *id, const char *topic, void *value, void *param);
void led_config_set(uint64_t *id, const char *topic, void *value, void *param);

static const bc_radio_sub_t mqtt_subs[] = {
    {"led-pwm/-/config/set", BC_RADIO_SUB_PT_STRING, led_config_set, NULL },
    {"led-pwm/-/triger/set", BC_RADIO_SUB_PT_INT, led_trigger_set, NULL },
    {"led-pwm/-/brightness/set", BC_RADIO_SUB_PT_FLOAT, led_brightness_set, NULL },
    // ...

// ...

void led_config_set(uint64_t *id, const char *topic, void *value, void *param) {
    bc_log_debug("config: led_config_set() called");
    // ...

// ...

void application_init(void)
    // ...
    // Initialize radio
    bc_radio_set_subs((bc_radio_sub_t *) mqtt_subs, sizeof(mqtt_subs)/sizeof(bc_radio_sub_t));
    // ...

Thanks for pointing me in right direction :o)

all the payload must be json valid, I assume you are sending data like this

mosquitto_pub -t node/lable/aaa/bbb/set -m TEXT

try adding quotation marks

mosquitto_pub -t node/lable/aaa/bbb/set -m ‘“TEXT”’

or use bch

sudo pip3 install -U bch
bch pub node/lable/aaa/bbb/set TEXT

Thanks for hint, when I try:

mosquitto_pub -t node/led-pwm:terasa:0/led-pwm/-/config/set -m '"1"'

callback function gets called, but what I do need is to send actual JSON, for example:

mosquitto_pub -t node/led-pwm:terasa:0/led-pwm/-/config/set -m '{"timeoutBase":"3000"}'

This for some reason doesn’t work even though it is valid JSON payload.

PS: I would really appreciate some SDK documentation, where I could just read such critical details along with some examples… it would save me hours of experimentation, reverse engineering SDK code, googling and banging my head on desk with frustration

For BC_RADIO_SUB_PT_STRING must be json valid, but still type string not object.

I recommend using BC_RADIO_SUB_PT_INT and change topic to

or you must send as ‘"{\“timeoutBase\”:\“3000\”}"’

bcg or playground after receiving a message from mqtt applies json.parse to the payload and then checks the type

Using individual topic endpoints for all values is not really an option for me, as there will be like 10+ possible values in JSON payload and 10 configuration endpoints (I have 9 PWM channels and using “0”-“8” instead of “-” in topic name configures individual channel, while “-” propagates configuration to all channels). That would require 100+ topic subscriptions just for configuration and more complex configuration provisioning implementation. And some values are arrays to complicate things even more.

I would like to be able to use standard JSON libraries for preparing message payload, so if I got it right, I have following options:

  1. tunnel JSON payload through JSON string value by having some translation service forwarding messages from lets say “node-json/#” to “node/#” using base64 or escaping and create library for decoding it in firmware,

  2. patch bcg to skip JSON validation for string values,

  3. patch bcg and SDK to add support for BC_RADIO_SUB_PT_JSON payload type,

I guess I have to go with option 2 (even though I don’t like it), as option 1 would increase text+bss memory usage in firmware and is more error prone. And I don’t have neither knowledge nor time for implementing option 3.

BTW, is there some official way to request implementation of option 3?