• AVR Freaks

Hot!MQTT Library v2.0.0

Author
combesthomas
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2020/06/03 03:03:06
  • Location: 0
  • Status: offline
2020/06/04 23:32:09 (permalink)
0

MQTT Library v2.0.0

Hello guys, this is my first post on this forum.
 
The last 2 weeks I've been working on an MQTT application. I'm using the microchip AVR IoT development board with the ATMega4808 mcu. Although this board was developed for an cloud mqtt connection I'm using the MQTT Library v2.0.0 to build my own application in my local home network. I have an MQTT broker installed on an Raspberry Pi also running Openhab.
 
I am able to make a connection to the Broker and publish messages on a specific topic. This all works fine. However when I try to subscribe to a topic things go wrong. After sending the subscribe message I never get a subscribe acknowledgement from the Broker. Because connection and publishing is working fine I'm guessing the problem is with my mcu applicaiton.
 
Below my code to build the subscribe packet:
 
static void app_buildSubscribePacket(void)
{
    mqttSubscribePacket appSubscribePacket;
    uint8_t topicCount = 0;
    //mqttSubscribePayload appSubscribePayload;
    
    //memset(&appSubscribePacket, 0, sizeof(mqttSubscribePacket));
    
    //Variable header
    appSubscribePacket.packetIdentifierLSB = 11;
    appSubscribePacket.packetIdentifierMSB = 0;
    
    //Payload
    for(topicCount = 0; topicCount < NUM_TOPICS_SUBSCRIBE; topicCount++)
    {
  //sprintf(mqttSubscribeTopic, "/SwitchControl/LightOn");
        strcpy(mqttSubscribeTopic, "SwitchControl/LightOff");
        uint16_t topicLen = strlen(mqttSubscribeTopic);
  appSubscribePacket.subscribePayload[topicCount].topic = (uint8_t *)mqttSubscribeTopic;
  appSubscribePacket.subscribePayload[topicCount].topicLength = topicLen;
  appSubscribePacket.subscribePayload[topicCount].requestedQoS = 0;

        MQTT_SetPublishReceptionCallback(&app_mqttPublishReceptionHandler);
    }
    
    if(MQTT_CreateSubscribePacket(&appSubscribePacket) == true)
    {
        subscribed = true;
    }
}

 
Is there anybody already using the MQTT Library v2.0.0. There are no posts on this forum about it. I'm currently troubleshooting the Library code but so for haven't found any problems. The only unusual thing I came up on was that the topic lengths were very long. But this is also the case for the publish packets which work fine...
 
All help is very welcome...!
 
Thanks in advance,
 
Thomas
#1

11 Replies Related Threads

    combesthomas
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/06/03 03:03:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/06 10:52:57 (permalink)
    0
    Ok, so after consulting the Mosquitto log on the Raspberry PI I can safely say the Subscribe command is being received at the Broker end. I can also see the broker returning the SUBACK command. But then the MCU doens't react to the message. So I think there's something wrong in the detection of de SUBACK commands...
    #2
    combesthomas
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/06/03 03:03:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/22 03:07:05 (permalink)
    5 (1)
    So, nobody using the mqtt library...???
    #3
    Jacob the dane
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2020/05/12 06:44:23
    • Location: Trondheim, Norway
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/23 11:57:10 (permalink)
    0
    Hi Thomas,
     
    Our IoT firmware team has noticed your post and are looking into it. Thanks for sharing your observations.
     
    -Jacob
    #4
    mmujumdar
    Starting Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/04/23 16:27:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/23 14:08:58 (permalink)
    5 (1)
    The MQTT library sends the messages published over the subscribed topics to the application through a callback function defined in the application. This callback needs to be set by the application using the following API:
    void MQTT_SetPublishReceptionCallback(imqttHandlePublishDataFuncPtr appPublishReceptionCallback);
    where imqttHandlePublishDataFuncPtr is a function pointer of type
    typedef void (*imqttHandlePublishDataFuncPtr)(uint8_t *topic, uint8_t *payload);
    (defined in mqtt_packetTransfer_interface.h file).
     
    It seems like this callback is being set using: 
    MQTT_SetPublishReceptionCallback(&app_mqttPublishReceptionHandler);
    in the application code.
    Where is the app_mqttPublishReceptionHandler() function defined? Does control reach inside this function after reception of SUBACK?
    #5
    combesthomas
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/06/03 03:03:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/24 10:18:58 (permalink)
    0
    Hello mmujumdar
     
    Thank you for responding. I can see the application generating a subscription and the broker is sending a suback. But the application never reaches the suback reception in the receptionhandler in mqtt_core.h. After that it just generates a timeout. So the broker never gets to publish anything because the connection is terminated due to response timeout.
    #6
    mmujumdar
    Starting Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/04/23 16:27:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/24 17:44:19 (permalink)
    0
    Hi Thomas,
     
    Since suback is sent by the broker, there might be an issue in processing the packet. If MCC MQTT v2.0.0 generated code is being used, MQTT subscribe implementation was not a part of this release. Please find mqtt_example.c file attached herewith in which the application subscribes to a single topic (defined by the macro CFG_SUBTOPIC, which will essentially be 'SwitchControl/LightOff' in your application).
     
    Please compare the void app_mqttScheduler(void) function (in particular the APP_STATE_TLS_CONNECTED state in mqtt_example.c with the one in your application. This function handles the transmission and reception of the publish and subscribe packets in the application, so there might be some differences here. The void app_mqttExampleInit(void) and void app_mqttScheduler(void) functions will have to be called in main.c file for MQTT packet exchange.

    int main(void)
    {
        /* Initializes MCU, drivers and middleware */
        SYSTEM_Initialize();
        app_mqttExampleInit();
     
     
     
        while (1)
        {
            app_mqttScheduler();
        }
    }

    The avr-iot-aws-sensor-node application on GitHub is another example of subscribing to topics in AWS IoT Core. Please refer to the static void handleSocketState(socketState_t socketState, int32_t thisAge) function in cloud_service.c file for more details.
    post edited by mmujumdar - 2020/06/24 17:51:44
    #7
    mmujumdar
    Starting Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/04/23 16:27:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/24 17:50:37 (permalink)
    0
    There is some issue with links in the above post.
    GitHub link for avr-iot-aws-sensor-node application: https://github.com/microchip-pic-avr-solutions/avr-iot-aws-sensor-node-mplab/
    GitHub link for cloud_service.c file: https://github.com/microchip-pic-avr-solutions/avr-iot-aws-sensor-node-mplab/blob/master/AVRIoT.X/mcc_generated_files/cloud/cloud_service.c
    #8
    combesthomas
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/06/03 03:03:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/26 08:27:51 (permalink)
    0
    Hey mmujumdar
     
    I checked your mqtt_example.c and changed mine accordingly. I've attached my mqtt_example.c file to this post. However the results are the same.
    My mosquitto broker receives the subscription request and responds with a suback.
     
    I've added a breakpoint inside the MQTT_ReceptionHandler function in the mqtt_core.c file but this is never reached.
     
    As far as I know something is going wrong on the reception side of this code...
     
    Regards, Thomas
     
    PS: I wanted to add some screenshots but this forum is giving me trouble about it...
     
    #9
    combesthomas
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/06/03 03:03:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/26 08:29:30 (permalink)
    #10
    mmujumdar
    Starting Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/04/23 16:27:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/06/30 15:41:07 (permalink)
    4 (1)
    Hi Thomas,
     
    There was a missing line of code related to the recv() function call in the APP_STATE_TLS_CONNECTED state. On adding the recv() function (to flush the MQTT buffer), your mqtt_example.c file is working as expected. 
    Please find the modified mqtt_example.c file attached herewith. 
    #11
    combesthomas
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2020/06/03 03:03:06
    • Location: 0
    • Status: offline
    Re: MQTT Library v2.0.0 2020/07/31 21:14:16 (permalink)
    0
    Hi mmujumdar, just wanted to post here that the code is now running smoothly. Thanks to your help!
    #12
    Jump to:
    © 2020 APG vNext Commercial Version 4.5