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

MQTT Subscriber support Multiple subscriber?

[Dev Env]

nRF51 DK

IoT SDK 0.8

IoT SoftDevice

MQTT Subscriber Example

[Question]

  1. i tested multi subscriber, but error returned. IoT SDK not Support multi subscriber?

    then received data contain all setting value?

    i try to subscriber multiple

      1) MacAddress/beacon/txpower
    
      2) MacAddress/beacon/broadcasting Interval
    
      .....
    
  2. if i want one beacon receive message, how can i do?

    like above , contain MacAddress to topic?

thank you.

  • my tested code like this.. case BSP_BUTTON_1: { uint32_t err_code; if (m_connection_state == APP_MQTT_STATE_CONNECTED) {

                    const char * topic_str = APP_MQTT_SUBSCRIPTION_TOPIC;
                    const mqtt_topic_t topic = {.p_topic = (uint8_t *)topic_str, strlen(topic_str)};
    
                    err_code = mqtt_subscribe(&m_app_mqtt_id, &topic, APP_MQTT_SUBSCRIPTION_PKT_ID);
                    if(err_code == MQTT_SUCCESS)
                    {
                        m_connection_state = APP_MQTT_STATE_SUBSCRIBED;
                        m_display_state = LEDS_SUBSCRIBED_TO_TOPIC;
                        err_code = app_timer_start(m_led_blink_timer, LED_BLINK_INTERVAL, NULL);
                        APP_ERROR_CHECK(err_code);
                    }
    				
    				 const char * topic_str2 = APP_MQTT_SUBSCRIPTION_TOPIC2;
                    const mqtt_topic_t topic2 = {.p_topic = (uint8_t *)topic_str2, strlen(topic_str2)};
    
                    err_code = mqtt_subscribe(&m_app_mqtt_id, &topic2, 20);
    				APPL_LOG("mqtt_subscribe:[err:%d]\n", err_code);
                }
                else if (m_connection_state == APP_MQTT_STATE_SUBSCRIBED)
                {
                    //Unsubscribe with the broker.
                    err_code = mqtt_unsubscribe(&m_app_mqtt_id, APP_MQTT_SUBSCRIPTION_PKT_ID);
                    if(err_code == MQTT_SUCCESS)
                    {
                        m_connection_state = APP_MQTT_STATE_CONNECTED;
                        m_display_state = LEDS_CONNECTED_TO_BROKER;
                        err_code = app_timer_start(m_led_blink_timer, LED_BLINK_INTERVAL, NULL);
                        APP_ERROR_CHECK(err_code);
                    }
                }
                break;
            }
    
  • error code #define MQTT_ERR_BUSY 0x00000007 /**< Could not process a request as it was busy. */

    mqtt_subscribe function if((p_client->state & MQTT_STATE_PENDING_WRITE) == MQTT_STATE_PENDING_WRITE) { err_code = MQTT_ERR_BUSY; }

  • I believe the correct way of doing this is to wait for the MQTT_PKT_TYPE_SUBACK control packet before you subscribe to a new topic.

    This event is not handled in the mqtt library.

    I would try to modify the library so it handles the event, and sends an event to the application.

    Add a case for MQTT_PKT_TYPE_SUBACK in recv_callback().

    Add a new event in the mqtt_evt_identifier_t struct. For example MQTT_EVT_SUBACK. Send this event to the application when you receive MQTT_PKT_TYPE_SUBACK.

    Add a case for MQTT_EVT_SUBACK in the app_mqtt_evt_handler(). Here you add your second subscription.

    I haven't tested this myself, just a suggestion.

Related