Developer Portal Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    XDK MQTT issue connecting two different tasks to the Broker

    AngeloMiele94
    New Poster

    XDK MQTT issue connecting two different tasks to the Broker

    When I try to connect two different tasks to the same Broker ("Mosquitto"), the connection of the first task launched collapse when the program activate the second task.

    Both tasks are able to establish a connection with the Broker and run properly if launched alone.

    In the console appears the following code:

    INFO | XDK DEVICE 1: WLANPersonalWPA2Connect : Connected to WPA network successfully.
    INFO | XDK DEVICE 1: MQTT_ConnectToBroker : Failed to connect MQTT
    INFO | XDK DEVICE 1: AppControllerFire : MQTT connection to the broker failed Env
    INFO | XDK DEVICE 1: retcode: 2569478261
    INFO | XDK DEVICE 1: Error in XDK110 Application package.
    INFO | XDK DEVICE 1: Package ID: 153
    INFO | XDK DEVICE 1: Module ID: 39
    INFO | XDK DEVICE 1: Severity code: 2
    INFO | XDK DEVICE 1: Error code: 117

    Could you please help me.

     

     

    3 REPLIES 3
    Padmapriya
    Long-established Member

    Re: XDK MQTT issue connecting two different tasks to the Broker

    Hello Angelo,

    Sorry that I do not understand your question clearly. What do you mean by connection from 2 task ?

    In MQTT communication, XDK device acts as client ; whereas MQTT broker acts as server. Connection is initiated from client and accepted by broker. After successful connection,  XDK sensor data can be sent to the broker.

    AngeloMiele94
    New Poster

    Re: XDK MQTT issue connecting two different tasks to the Broker

    The 2 Tasks in the Project are:

    • AppControllerFire_Env
    • AppControllerFire_Mov

     

    They sent to the broker 2 different messages with different topics.

    static void AppControllerFire_Env(void* pvParameters)
    {
        BCDS_UNUSED(pvParameters);
    
        Retcode_T retcode = RETCODE_OK;
        Sensor_Value_T sensorValue;
        char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
        /* const char *publishDataFormat = "Environmental Data -\n"
                "\r\tHumidity : %ld [%%] \n"
                "\r\tPressure : %ld [bar] \n"
                "\r\tTemperature : %f\xf8 [°C] \n"
        		"\r\tAcceleration X : %ld [m/s^2] \n"
        		"\r\tAcceleration Y : %ld [m/s^2] \n"
        		"\r\tAcceleration Z : %ld [m/s^2] \n";
    	*/
    
        char *publishDataFormat = "{ "
        		"\"Humidity\":%ld,"
        		"\"Pressure\":%ld,"
        		"\"Temperature\":%f,"
        		"}";
    
        memset(&sensorValue, 0x00, sizeof(sensorValue));
    #if APP_MQTT_SECURE_ENABLE
    
        uint64_t sntpTimeStampFromServer = 0UL;
    
        /* We Synchronize the node with the SNTP server for time-stamp.
         * Since there is no point in doing a HTTPS communication without a valid time */
        do
        {
            retcode = SNTP_GetTimeFromServer(&sntpTimeStampFromServer, APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT);
            if ((RETCODE_OK != retcode) || (0UL == sntpTimeStampFromServer))
            {
                printf("AppControllerFire : SNTP server time was not synchronized. Retrying...\r\n");
            }
        } while (0UL == sntpTimeStampFromServer);
    
        BCDS_UNUSED(sntpTimeStampFromServer); /* Copy of sntpTimeStampFromServer will be used be HTTPS for TLS handshake */
    #endif /* APP_MQTT_SECURE_ENABLE */
    
        if (RETCODE_OK == retcode)
        {
            retcode = MQTT_ConnectToBroker(&MqttConnectInfo, MQTT_CONNECT_TIMEOUT_IN_MS);
        	if (RETCODE_OK != retcode)
            {
                printf("AppControllerFire : MQTT connection to the broker failed \n\r");
            }
        }
    
        if (RETCODE_OK == retcode)
        {
            // retcode = MQTT_SubsribeToTopic(&MqttSubscribeInfo_Env, MQTT_SUBSCRIBE_TIMEOUT_IN_MS);
        	retcode = RETCODE_OK;
            if (RETCODE_OK != retcode)
            {
                 printf("AppControllerFire : MQTT subscribe failed \n\r");
            }
        }
    
        if (RETCODE_OK != retcode)
        {
            /* We raise error and still proceed to publish data periodically */
            Retcode_RaiseError(retcode);
            vTaskSuspend(NULL);
        }
    
        /* A function that implements a task must not exit or attempt to return to
         its caller function as there is nothing to return to. */
        while (1)
        {
            /* Resetting / clearing the necessary buffers / variables for re-use */
            retcode = RETCODE_OK;
    
            /* Check whether the WLAN network connection is available */
            retcode = AppControllerValidateWLANConnectivity();
            if (RETCODE_OK == retcode)
            {
                retcode = Sensor_GetData(&sensorValue);
            }
            if (RETCODE_OK == retcode)
            {
                int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormat,
                        (long int) sensorValue.RH, (sensorValue.Pressure /= 1000), (sensorValue.Temp /= 1000));
    
                MqttPublishInfo_Env.Payload = publishBuffer;
                MqttPublishInfo_Env.PayloadLength = length;
    
                retcode = MQTT_PublishToTopic(&MqttPublishInfo_Env, MQTT_PUBLISH_TIMEOUT_IN_MS);
                if (RETCODE_OK != retcode)
                {
                    printf("AppControllerFire : MQTT publish failed \n\r");
                }
            }
            if (RETCODE_OK != retcode)
            {
                Retcode_RaiseError(retcode);
            }
            vTaskDelay(APP_MQTT_DATA_PUBLISH_PERIODICITY_Env);
        }
    }
    
    static void AppControllerFire_Mov(void* pvParameters)
    {
        BCDS_UNUSED(pvParameters);
    
        Retcode_T retcode = RETCODE_OK;
        Sensor_Value_T sensorValue;
        char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
        /* const char *publishDataFormat = "Environmental Data -\n"
                "\r\tHumidity : %ld [%%] \n"
                "\r\tPressure : %ld [bar] \n"
                "\r\tTemperature : %f\xf8 [°C] \n"
        		"\r\tAcceleration X : %ld [m/s^2] \n"
        		"\r\tAcceleration Y : %ld [m/s^2] \n"
        		"\r\tAcceleration Z : %ld [m/s^2] \n";
    	*/
    
        char *publishDataFormat = "{ "
        		"\"AccelerationX\":%ld,"
        		"\"AccelerationY\":%ld,"
        		"\"AccelerationZ\":%ld"
        		"}";
    
        memset(&sensorValue, 0x00, sizeof(sensorValue));
    #if APP_MQTT_SECURE_ENABLE
    
        uint64_t sntpTimeStampFromServer = 0UL;
    
        /* We Synchronize the node with the SNTP server for time-stamp.
         * Since there is no point in doing a HTTPS communication without a valid time */
        do
        {
            retcode = SNTP_GetTimeFromServer(&sntpTimeStampFromServer, APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT);
            if ((RETCODE_OK != retcode) || (0UL == sntpTimeStampFromServer))
            {
                printf("AppControllerFire : SNTP server time was not synchronized. Retrying...\r\n");
            }
        } while (0UL == sntpTimeStampFromServer);
    
        BCDS_UNUSED(sntpTimeStampFromServer); /* Copy of sntpTimeStampFromServer will be used be HTTPS for TLS handshake */
    #endif /* APP_MQTT_SECURE_ENABLE */
    
        if (RETCODE_OK == retcode)
        {
            retcode = MQTT_ConnectToBroker(&MqttConnectInfo, MQTT_CONNECT_TIMEOUT_IN_MS);
            if (RETCODE_OK != retcode)
            {
                printf("AppControllerFire : MQTT connection to the broker failed \n\r");
            }
        }
    
        if (RETCODE_OK == retcode)
        {
            // retcode = MQTT_SubsribeToTopic(&MqttSubscribeInfo_Mov, MQTT_SUBSCRIBE_TIMEOUT_IN_MS);
        	retcode = RETCODE_OK;
            if (RETCODE_OK != retcode)
            {
                printf("AppControllerFire : MQTT subscribe failed \n\r");
            }
        }
    
        if (RETCODE_OK != retcode)
        {
            /* We raise error and still proceed to publish data periodically */
            Retcode_RaiseError(retcode);
            vTaskSuspend(NULL);
        }
    
        /* A function that implements a task must not exit or attempt to return to
         its caller function as there is nothing to return to. */
        while (1)
        {
            /* Resetting / clearing the necessary buffers / variables for re-use */
            retcode = RETCODE_OK;
    
            /* Check whether the WLAN network connection is available */
            retcode = AppControllerValidateWLANConnectivity();
            if (RETCODE_OK == retcode)
            {
                retcode = Sensor_GetData(&sensorValue);
            }
            if (RETCODE_OK == retcode)
            {
    
                int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormat,
                        sensorValue.Accel.X, sensorValue.Accel.Y, sensorValue.Accel.Z);
    
                MqttPublishInfo_Mov.Payload = publishBuffer;
                MqttPublishInfo_Mov.PayloadLength = length;
    
                retcode = MQTT_PublishToTopic(&MqttPublishInfo_Mov, MQTT_PUBLISH_TIMEOUT_IN_MS);
                if (RETCODE_OK != retcode)
                {
                    printf("AppControllerFire : MQTT publish failed \n\r");
                }
            }
            if (RETCODE_OK != retcode)
            {
                Retcode_RaiseError(retcode);
            }
            vTaskDelay(APP_MQTT_DATA_PUBLISH_PERIODICITY_Mov);
        }
    }

     

     

    Padmapriya
    Long-established Member

    Re: XDK MQTT issue connecting two different tasks to the Broker

    Hello AngeloMiele94,

    Appreciate your interest in using XDK MQTT application.

    From your console output,

    • XDK is successfully connected to WiFi network only
    • XDK is not connected to MQTT broker any time

    More information is required to support you further,

    1. What are you trying to acheive with 2 MQTT broker connections ?
    2. What is the purpose of 2 tasks ?
      1. From your comment, you are trying to connect XDK with Mosquitto broker. In that case on task with initiates connection with MQTT broker is enough.
      2. Request you to refer/use SendDataOverMQTT application, which will connect with MQTT broker and sent XDK sensor data to broker continously

     

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist