How to receive published data using MQTT subscriber?


Brenton Chetty
 

Hi devel team

May you please assist me with this issue.

I managed to use the "mqtt_publisher" example in the sample folder, as well as the "mqtt_pubsub" and "mqtt_subscriber" in "zephyr/tests/net/lib".

From the subscriber examples I could not find code to receive messages which are published to the subscribed topic.

- Is there any example code available for receiving the messages from the subscribed topic?
- If not, how do you suggest I proceed?

With thanks
Brenton


Lubos, Robert
 

Hi Brenton,

 

I’m afraid we don’t have an out-of-the box example for the MQTT subscriber in the Zephyr tree. However the aforementioned “mqtt_pubsub” test is a good reference on how to handle subscribtions. There are basically three things to do in order to receive data for a particular topic:

  1. Subscribe to a topic with `mqtt_subscribe()` function,
  2. Handle `MQTT_EVT_PUBLISH` event in the MQTT event handler function,
  3. In the `MQTT_EVT_PUBLISH` handler, make sure to consume all of the payload with `mqtt_readall_publish_payload()` for instance.

 

You can also check the Nordic MQTT sample as a reference (https://github.com/nrfconnect/sdk-nrf/blob/master/samples/nrf9160/mqtt_simple/src/main.c), note however this example is nRF91 specific and contains some other stuff unrelated to MQTT per se.

 

Regards,

Robert

 

From: devel@... <devel@...> On Behalf Of Brenton Chetty via lists.zephyrproject.org
Sent: wtorek, 3 sierpnia 2021 21:02
To: devel@...
Subject: [Zephyr-devel] How to receive published data using MQTT subscriber?

 

Hi devel team

 

May you please assist me with this issue.

 

I managed to use the "mqtt_publisher" example in the sample folder, as well as the "mqtt_pubsub" and "mqtt_subscriber" in "zephyr/tests/net/lib".

 

From the subscriber examples I could not find code to receive messages which are published to the subscribed topic.

 

- Is there any example code available for receiving the messages from the subscribed topic?

- If not, how do you suggest I proceed?

 

With thanks

Brenton