Publish/Subscribe System

Hello, I am trying to understand the publish/subscribe system through BM nodes. Currently, I just want to send the on board temperature and power data that i receive from one Dev Kit to another Dev Kit.

I have found the source code for the bm_pub function, but I am having trouble implementing it and getting it compiled. I have added the following lines of code to the main loop within the hello_world_guide_5 app from the Dev Kit guides:

void setup(void) {
/* USER ONE-TIME SETUP CODE GOES HERE */

static const char topic = “temperaturetopic”;
static constexpr uint8_t temperaturetopictype = 1;

}

void loop(void) {
/* USER LOOP CODE GOES HERE */

float temperature, humidity = 0.0;
if (htuSamplerGetLatest(temperature, humidity)) {
printf(“hum-temp from user code: H: %f, T:%f\n”, humidity, temperature);
size_t msg_len = sizeof(temperature);
bm_pub(topic, temperature, msg_len, temperaturetopictype);
}

Is this the correct format for publishing the temperature data from the Dev Kit to a topic called temperature data?

Welcome @ggrinn1! :wave: You’ve got the right idea; your code is mostly correct. There are some small changes required though.

One issue I see is that you need to move the initial declaration of the topic and temperaturetopictype constants out of the setup function so that code inside the loop function can access them.

Also the data — the second argument to bm_pub — needs to be a pointer. You can see this in the source code for bm_pub because there is a * before the data argument. One simple change to your code that achieves this is to prepend & to the temperature variable, so that you pass the address of the temperature variable instead of the temperature value.

For a more detailed payload than a simple number, you’ll want to encode your data into some byte buffer and pass a pointer to the buffer. I’m looking for an example in the source code that I could point you to, but they’re all overly complicated. I’ll add to my task list to post a simplified example this week.

Let us know if that helps, and don’t hesitate to ask more questions.

Cheers!

Thank you so much! The publish app successfully built and now I am onto the subscribe portion. I am following the same approach and using the hello_world_guide_5 app for the receiving Dev Kit’s ‘subscribe’ source code. I have the following:

void callback_function() {
printf("successfully subscribed ");
}

void setup(void) {
/* USER ONE-TIME SETUP CODE GOES HERE */
}

void loop(void) {
/* USER LOOP CODE GOES HERE */

const char *topic = “temperaturetopic”;
bm_sub(topic, bm_cb_t callback_function);

I am getting an error when I build:

error: expected primary-expression before ‘callback_function’
35 | bm_sub(topic, bm_cb_t callback_function);

I looked on the internet and I am thinking this is because of the bm_cb_t signature, but I could be wrong. Also, I am not too sure how to handle the data, but I am assuming it comes out of the second argument of bm_sub and I can process it in the callback_function.

1 Like

Excellent! Happy to help. Here are next steps.

The call to bm_sub needs to run only once, so it should be in setup, not in loop. This call basically says, “When a message arrives in the future with the given topic, please pass it to the given callback.”

The callback function needs a particular signature, defined in bm_pubsub.h here:

It’s a little complicated looking, but basically your callback function needs to accept 7 arguments. You can copy the exact contents of the argument list from that header file, like this:

void callback_function(uint64_t node_id, const char* topic, uint16_t topic_len, const uint8_t* data, uint16_t data_len, uint8_t type, uint8_t version) {
  printf("Received a published message! Topic: %.*s. Data: %.*s\n", topic_len, topic, data_len, data);
}

Last small thing — per the error you posted — remove the bm_cb_t from before callback_function in your call to bm_sub.

Hey! Update! @russ put together a quick pair of apps to serve as pub-sub examples! :raised_hands: :clap: Check out Feature/pub sub examples by russelldeguzman ¡ Pull Request #148 ¡ bristlemouth/bm_protocol ¡ GitHub