Example of how to subscribe to custom MQTT topic

Hi,
does anyone have some example of subscribing of custom MQTT mesage? I suppose it is necessary to set radio module to listening bc_radio_init(BC_RADIO_MODE_NODE_LISTENING); but I am not sure what to do after that. I am also not sure if it is also possible to send MQTT messages in listening mode.

I know that there is some example for power module, but it is not used for listening custom message. I would like to receive some values (intervals how frequently measure some values) and use these values instead of constants.

Thank you very much

Hello,

we did not documented that yet, but its fully functional.

Here I register topic for servo


And then you call this structure in init:

Here’s more topic for 1-wire relays

This functionality was added later and we would like to rewrite older projects to use this dynamic radio subscriptions, then the Dongle does not need to have these topics hard-coded.

Martin

Thank you very much! Until today I tried to get it work, but without success.

I used your example, with just some modifications:

void servo_angle_set(uint64_t *id, const char *topic, void *value, void *param)
{
bc_log_debug("Ziskano pomoci MQTT - hodnota %d", *(int*)value);
//(char*)value
}

// subscribe table, format: topic, expect payload type, callback, user param
static const bc_radio_sub_t subs[] = {
// state/set
{"test", BC_RADIO_SUB_PT_INT, servo_angle_set, NULL }
};

Then I called this structure in the init:

bc_radio_init(BC_RADIO_MODE_NODE_LISTENING);
bc_radio_set_subs((bc_radio_sub_t *) subs, sizeof(subs)/sizeof(bc_radio_sub_t));

I tried to send some messages using Bigclown HUB in more different formats:

 test
 node/#id/test
 node/alias/test

But withnout succes and I didn’t see log message from the function. Can you help me, what I am doing wrong, please? Or what I missing to do?

My guess is that you have to keep the MQTT topic topic “tree-depth”:
denkovi-relay/q4/state/set

So the format should be
x/y/z/Ĺľ :wink:

I think that bcg gateway is using some wildcards ? in the MQTT topics. We did this probably so it is similar to existing style of topics.

let me know the result, send me application.c if you could not get it work.
Also try to update the dongle firmware. I think that anything above 1.8.0 is fine, but please test the latest version as the last option.

Martin

OK, I will try it at home at the evening. If I understand correctly, it is needed to keep same structure as using other devices in opposite way (publishing). In my case for getting pressure it is: “greenhouse_climate/barometer/0:0/pressure”. I will try to publish “greenhouse_climate/barometer/0:0/update_interval”

But I am not sure if it is correct that is not possible to subscribe message without this tree. For example when I would like to use some attribute of whole climate kit (core module) It should not be necessary to use this structure. Similar case is, when I would like to subscribe all topics “#”…

In this time, the structure of the subject is solid. Wildcard as # or + doesn’t supported.
You register a/b/c/d for example denkovi-relay/q4/state/set and node added gateway and id added dongle. Result subscibe topic is node//denkovi-relay/q4/state/set .

But we are planning to support a different structure of the topic. However, it should not be forgotten that 868Mhz is not wifi, there is a limited number of messages.

Thank you, for your help, but I it is still not working. I tried to subscribe topic in recommended format. But it doesn’t help. I also tried to update dongle firmware with this command: bcf flash --device /dev/ttyUSB0 bigclownlabs/bcf-gateway-usb-dongle:latest but it still doesn’t work. I am attaching link to file with my code with my examples. Can you please check, what I am doing wrong? Thank you very much.

I tried these payload messages:
node//greenhouse_climate/barometer/update/interval
node/greenhouse_climate/barometer/update/interval
greenhouse_climate/barometer/update/interval

application.c (9.4 KB)

You have to initializate radio in LISTENING mode:

bc_radio_init(BC_RADIO_MODE_NODE_LISTENING);

You have to call bc_radio_init everytime to start and initialize radio.

Martin

I thought that I have it as you mention. (also in attached file)

I’m sorry, I overlooked that. I was focused on that commented line and searching for other BC_RADIO_MODE_NODE_SLEEPING in the code, which was stupid.

Now that I have the code I try to test it. I’m not sure if I can do it today. I’ll keep you informed.

Martin

It is okay, thank you very much for your help.

It must be something related to the topic length maybe.

This works

static const bc_radio_sub_t subs[] = {
// state/set
{"servo/p14/angle/set", BC_RADIO_SUB_PT_INT, servo_angle_set, NULL }
};

ANd from console

bcf-dynamic-topic-forum$ bch pub node/climate-monitor:0/servo/p14/angle/set 10

There’s also other thing.
The module itself pairs as a climate-monitor:0, I guess that you renamed the module alias to the “greenhouse_climate”, but this name cannot be in the C code. This alias is appended automatically by gateway.

So if your code would work (there’s that topic length bug maybe) then you would need to add another greenhouse_climate to the topic:

node/greenhouse_climate/greenhouse_climate/barometer/update/interval
NODE ALIAS

I’ll try to debug the code and let you know later today.

I would suggest this topic to change interval which works, please try it:

{"barometer/-/interval/set", BC_RADIO_SUB_PT_INT, servo_angle_set, NULL }

Also if you run bcg manually with --debug, and after it start you reset the climate module, you will see the registration of the dynamic topic (the last line in the debug output below)

hub@hpnix:~/Desktop/Bigclown/temp/bcf-dynamic-topic-forum$ bcg --device /dev/tUSB1 --debug
2018-10-24 16:50:12,702 INFO: Start
2018-10-24 16:50:12,702 INFO: Serial port: /dev/ttyUSB1
2018-10-24 16:50:12,702 INFO: MQTT broker host: localhost, port: 1883, use tls: False
2018-10-24 16:50:12,705 INFO: Connected to MQTT broker with code 0
2018-10-24 16:50:12,705 DEBUG: subscribe gateway/ping
2018-10-24 16:50:12,705 DEBUG: subscribe gateway/all/info/get
2018-10-24 16:50:14,828 INFO: Opened serial port: /dev/ttyUSB1
2018-10-24 16:50:14,828 DEBUG: Exclusive lock on file descriptor: 3
2018-10-24 16:50:14,829 DEBUG: write b'["/info/get", null]\n'
2018-10-24 16:50:14,852 DEBUG: read b'["/info", {"id": "836d19831c4a", "firmware": "bcf-gateway-usb-dongle", "version": "v1.11.0"}]\n'
2018-10-24 16:50:14,852 DEBUG: subscribe gateway/usb-dongle/+/+
2018-10-24 16:50:14,853 DEBUG: write b'["$eeprom/alias/list", 0]\n'
2018-10-24 16:50:14,878 DEBUG: read b'["$eeprom/alias/list/0", {"78d1430b42a4": "climate-monitor:0"}]\n'
2018-10-24 16:50:14,878 DEBUG: alias_list: {'78d1430b42a4': 'climate-monitor:0'}
2018-10-24 16:50:14,878 DEBUG: node_rename 78d1430b42a4 to climate-monitor:0
2018-10-24 16:50:14,879 DEBUG: write b'["/nodes/get", null]\n'
2018-10-24 16:50:14,893 DEBUG: read b'["/nodes", ["78d1430b42a4"]]\n'
2018-10-24 16:50:14,893 DEBUG: node_add 78d1430b42a4
2018-10-24 16:50:14,894 DEBUG: subscribe node/78d1430b42a4/+/+/+/+
2018-10-24 16:50:14,894 DEBUG: subscribe node/climate-monitor:0/+/+/+/+
2018-10-24 16:50:36,259 DEBUG: read b'["78d1430b42a4/info", {"firmware": "climate-monitor", "version": "vdev", "mode": 2}]\n'
2018-10-24 16:50:36,329 DEBUG: read b'["$sub", {"topic": "78d1430b42a4/barometer/-/interval/set", "pt": 1}]\n'

GREAT, GREAT, GREAT!!! Thanks a lot! It really works! :tada::tada::tada:

I will experiment with topic length, but when I know that it is restricted to some format or length, it is no problem!

1 Like

Nice. :+1:
Max subs topic length is 50 chars.

Hi, I have one more question about payloads types. Which types of payload are supported? I tried to use same as for publishing, but UINT32 looks that doesn’t work. Or is it needed to use some different format then “BC_RADIO_SUB_PT_UINT32”?
Thank you

1 Like

I have written this code and ran into the same problem with topic subscription. With topic “barometer/-/interval/set” it works great and for example with topic a/b/c/d, or owar/lcd/temperature/set it does not.

I had similar issues with the topic name, but in my case it doesn’t work too long topics (more than ± 55 characters) or when the topic was too short (or in shorten structure - for example xx/yy).

It is strange that “barometer/-/interval/set” works and “owar/lcd/temperature/set” not… (same structure, similar number of characters)

for me works for example:
“core/barometer/-/val-change/set” (the longest one which I am using)
“core/-/sleep-period/set” (the shortest one which I am using)

It should also work in your code… If not, problem is probably not in the topic name.

Yeah, I know it is fishy. I will try to investigate it this weekend.