This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

MQTT Example - Settings for publishing while subscribing?

Hi, I use the following.

  • Raspberry Pi 3 (RPi3 with Raspbian Jessie 4.4) as a router,

RPi3 is connected to a Wi-Fi router to access the Internet by using the built-in Wi-Fi BLE BCM43438 chip.

  • IoT SDK 0.9

  • MQTT Publisher Example

  • PCA10040 v1.1

  • Keil MDK 5.20

  • MQTT Broker = Mosquitto 1.4.9, hosted from Amazon EC2 instance which uses Ubuntu 14.

As far as I know, it is possible to publish while subscribing to topics.

I'm trying the IoT SDK's MQTT publish example.

Is it possible to publish while subscribing to topics with PCA10040?

If so, what settings should I add in the publish example?

/+++++++++++++++++++++++++++++++++++++++++++/

Edited : 2016.08.04

I tried the following code.

breakpoint

#define APP_MQTT_SUBSCRIPTION_PKT_ID        10                                                      /**< Unique identification of subscription, can be any unsigned 16 bit integer value. */
#define APP_MQTT_SUBSCRIPTION_TOPIC         "led/state"                                             /**< MQTT topic to which this application subscribes. */

static void button_init(void){
uint32_t err_code;

static app_button_cfg_t buttons[] =
{
    {BSP_BUTTON_0,        false, BUTTON_PULL, button_event_handler},
    {BSP_BUTTON_1,        false, BUTTON_PULL, button_event_handler},
    {BSP_BUTTON_2,        false, BUTTON_PULL, button_event_handler},
    {BSP_BUTTON_3,        false, BUTTON_PULL, button_event_handler}, // for testing subscribe
#ifdef COMMISSIONING_ENABLED // I didn't enabled it, the handler for BSP_BUTTON_3 is shown above
    {ERASE_BUTTON_PIN_NO, false, BUTTON_PULL, button_event_handler}
#endif // COMMISSIONING_ENABLED
}; // omitted other parts
}

void app_mqtt_evt_handler(mqtt_client_t * const p_client, const mqtt_evt_t * p_evt){
switch(p_evt->id){
   case MQTT_EVT_PUBLISH: // Publish event received when message is published on a topic client is subscribed to
   {
     if (p_evt->param.pub_message.message.payload.bin_strlen == 1){}
     if (p_evt->param.pub_message.message.topic.qos == MQTT_QoS_1_ATLEAST_ONCE){}
     break;
   }

-Best Regards, Mango

Parents
  • FormerMember
    0 FormerMember

    Yes, it is possible to combine a MQTT publisher and subscriber in one application. In order to do so, the publisher and subscriber functionality has to be "merged" into one application. The settings that you need to include in the publisher example are the subscriber specific settings like APP_MQTT_SUBSCRIPTION_PKT_ID and APP_MQTT_SUBSCRIPTION_TOPIC for instance.

Reply
  • FormerMember
    0 FormerMember

    Yes, it is possible to combine a MQTT publisher and subscriber in one application. In order to do so, the publisher and subscriber functionality has to be "merged" into one application. The settings that you need to include in the publisher example are the subscriber specific settings like APP_MQTT_SUBSCRIPTION_PKT_ID and APP_MQTT_SUBSCRIPTION_TOPIC for instance.

Children
  • Great, while trying your suggestion I have an additional question.

    Does the IoT SDK support subscribing MQTT multiple topics?

    I'm curious whether the handler can handle when the application subscribes topic "A", "B", and "C".

    -Best Regards, Mango

  • FormerMember
    0 FormerMember in reply to FormerMember

    Yes, the IoT SDK supports subscribing to multiple topics.

  • Well, I added the code by referring the subscriber example.

    However, even though the error code is 0 when calling mqtt_subscribe,

    the breakpoint didn't hit the case MQTT_EVT_PUBLISH: when I publish data to the topic "led/state".

    What seems to be the problem?

    -Best Regards, Mango

  • FormerMember
    0 FormerMember in reply to FormerMember

    Does your subscriber code works in a standalone subscriber project?

    Just to double check, the publisher is publishing on the topic "led/state"?

    Does the publisher in you subscriber&publisher project work?

    The publisher that is publishing on "led/state", does it have the same broker port? "1883" for non secure port, for instance?

  • Hi, Kristin.

    I haven't tried the standalone subscriber project. I will do it soon.

    About the "Just to double check, the publisher is publishing on the topic "led/state"?" and "The publisher that is publishing on "led/state", does it have the same broker port? "1883" for non-secure port, for instance?" ,

    yes, it is.

    The merged application (publish + subscribe) is both using port 1883.

    Thanks to Stian's help, the Raspberry Pi (RPi) is using the 6tunnel to forward packets to IPv4.

    The application is publishing to topic "led/state".

    Come to think of it, the application subscribed to the same topic.

    So I thought MQTT_EVT_PUBLISH event will be triggered after publishing.

    While trying the standalone subscriber project, are you recommending me to subscribe to a different topic?

    For instance, publish to "led/state" but subscribe to "test".

    Should I try it this way and see how it goes?

    -Best Regards, Mango

Related