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

  • 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

  • FormerMember
    0 FormerMember in reply to FormerMember

    If you first try the subscriber and publisher examples separately, and test them like described in the documentation, and know that they work, then you know that the setup is correct and it will be easier to debug the subscriber&publisher project.

  • Well, I tested both the standalone subscriber project and standalone publisher project.

    The standalone subscriber project triggered the breakpoint at case MQTT_EVT_PUBLISH:.

    However, my merged project does not.

    Then how about using "diff"? Can you upload the merged application, the main.c file?

    Then I can "diff" (compare codes) together and check the problem.

    -Best Regards, Mango

  • Hi,

    I made an example for both publishing and subscribing in the same example. It is based on mqtt publisher example and merged with the mqtt subscriber example. I added button 4 as the subscribing button, and LED 4 is toggling only if it receives a publish message from the broker.

    1. LED1 lights up after connection
    2. Push Button1 to connect to broker. LED2 turns on.
    3. Push Button2 to publish. No activity on LEDs
    4. Push Button4 to subscribe. LED3 turns on.
    5. Push Button2 to publish. You should now see activity on LED4. This means that the kit is both publishing and subscribing to the same topic "led/state".

    Replace the content of the main.c file in the mqtt publisher example with this one: main.c

  • Hey copied the code min main file but i have few issues:

    1. In step 3 when i push button 2 LED 4 blinks instead of no activity.

    2. In step 4 when i push button 4 it doesn´t subscribe and LD3 is off all the while. I can add the code if want.

Related