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
  • 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

Reply
  • 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

Children
Related