Developer Portal Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    SOLVED

    send sensor data over http - convert buffer

    Highlighted
    Member

    send sensor data over http - convert buffer

    Hello together,

    i used the MQTT example from Workbench for sending sensor data over http. It worked fine if i simple send a string like in the http example.

    If i want to send my sensor data i get a bad request "400".  I think its because of a wron data type. 

    How can i format my Buffer to a string?

    Full code:

    Spoiler

    #include "XdkAppInfo.h"

    #undef BCDS_MODULE_ID /* Module ID define before including Basics package*/
    #define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

    /* own header files */
    #include "AppController.h"

    /* system header files */
    #include <stdio.h>

    /* additional interface header files */
    #include "XDK_Sensor.h"
    #include "XDK_WLAN.h"
    #include "XDK_ServalPAL.h"
    #include "XDK_HTTPRestClient.h"
    #include "XDK_SNTP.h"
    #include "BCDS_BSP_Board.h"

    #include "BCDS_WlanNetworkConfig.h"
    #include "BCDS_WlanNetworkConnect.h"
    #include "BCDS_CmdProcessor.h"
    #include "BCDS_Assert.h"
    #include "XDK_Utils.h"
    #include "FreeRTOS.h"
    #include "task.h"
    #include "timers.h"
    #include "string.h"

    /* constant definitions ***************************************************** */

    #if HTTP_SECURE_ENABLE
    #define APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT UINT32_C(10000)/**< Timeout for SNTP server time sync */
    #endif /* HTTP_SECURE_ENABLE */

    #define APP_RESPONSE_FROM_HTTP_SERVER_POST_TIMEOUT UINT32_C(25000)/**< Timeout for completion of HTTP rest client POST */

    #define APP_RESPONSE_FROM_HTTP_SERVER_GET_TIMEOUT UINT32_C(25000)/**< Timeout for completion of HTTP rest client GET */

    #define APP_TEMPERATURE_OFFSET_CORRECTION (-3459)
    #define APP_MQTT_DATA_BUFFER_SIZE UINT32_C(256)/**< macro for data size of incoming subscribed and published messages */

    /* local variables ********************************************************** */

    static WLAN_Setup_T WLANSetupInfo =
    {
    .IsEnterprise = false,
    .IsHostPgmEnabled = false,
    .SSID = WLAN_SSID,
    .Username = WLAN_PSK, /* Unused for Personal WPA2 connection */
    .Password = WLAN_PSK,
    .IsStatic = WLAN_STATIC_IP,
    .IpAddr = WLAN_IP_ADDR,
    .GwAddr = WLAN_GW_ADDR,
    .DnsAddr = WLAN_DNS_ADDR,
    .Mask = WLAN_MASK,
    };/**< WLAN setup parameters */

    static Sensor_Setup_T SensorSetup =
    {
    .CmdProcessorHandle = NULL,
    .Enable =
    {
    .Accel = true,
    .Mag = true,
    .Gyro = true,
    .Humidity = true,
    .Temp = true,
    .Pressure = true,
    .Light = true,
    .Noise = false,
    },
    .Config =
    {
    .Accel =
    {
    .Type = SENSOR_ACCEL_BMA280,
    .IsRawData = false,
    .IsInteruptEnabled = false,

    },
    .Gyro =
    {
    .Type = SENSOR_GYRO_BMG160,
    .IsRawData = false,
    },
    .Mag =
    {
    .IsRawData = false,
    },
    .Light =
    {
    .IsInteruptEnabled = false,

    },
    .Temp =
    {
    .OffsetCorrection = APP_TEMPERATURE_OFFSET_CORRECTION,
    },
    },
    };/**< Sensor setup parameters */

    #if HTTP_SECURE_ENABLE
    static SNTP_Setup_T SNTPSetupInfo =
    {
    .ServerUrl = SNTP_SERVER_URL,
    .ServerPort = SNTP_SERVER_PORT,
    };/**< SNTP setup parameters */
    #endif /* HTTP_SECURE_ENABLE */

    static HTTPRestClient_Setup_T HTTPRestClientSetupInfo =
    {
    .IsSecure = HTTP_SECURE_ENABLE,
    };/**< HTTP rest client setup parameters */

    /*
    * @todo - Use only DEST_SERVER_PORT in the header for secure and non-secure.
    * And remove the below 6 lines of the macros
    */
    #if HTTP_SECURE_ENABLE
    #ifdef DEST_SERVER_PORT
    #undef DEST_SERVER_PORT
    #endif /* #ifdef DEST_SERVER_PORT */
    #define DEST_SERVER_PORT DEST_SERVER_PORT_SECURE
    #endif /* if HTTP_SECURE_ENABLE */

    static HTTPRestClient_Config_T HTTPRestClientConfigInfo =
    {
    .IsSecure = HTTP_SECURE_ENABLE,
    .DestinationServerUrl = DEST_SERVER_HOST,
    .DestinationServerPort = DEST_SERVER_PORT,
    .RequestMaxDownloadSize = REQUEST_MAX_DOWNLOAD_SIZE,
    }; /**< HTTP rest client configuration parameters */

    static HTTPRestClient_Post_T HTTPRestClientPostInfo =
    {
    .Payload = POST_REQUEST_BODY,
    .PayloadLength = (sizeof(POST_REQUEST_BODY) - 1U),
    .Url = DEST_POST_PATH,
    .RequestCustomHeader0 = POST_REQUEST_CUSTOM_HEADER_0,
    .RequestCustomHeader1 = POST_REQUEST_CUSTOM_HEADER_1,
    }; /**< HTTP rest client POST parameters */

    /**
    * @brief The HTTP GET request response callback.
    *
    * @param [in] responseContent
    * Pointer to the GET request response
    *
    * @param [in] responseContentLen
    * Length of the GET request response
    *
    * @param [in] isLastMessage
    * Boolean to represent if it is the last part of the ongoing message
    */
    static void HTTPRestClientGetCB(const char* responseContent, uint32_t responseContentLen, bool isLastMessage);

    static HTTPRestClient_Get_T HTTPRestClientGetInfo =
    {
    .Url = DEST_GET_PATH,
    .GetCB = HTTPRestClientGetCB,
    .GetOffset = 0UL,
    }; /**< HTTP rest client GET parameters */

    static xTaskHandle AppControllerHandle = NULL; /**< OS thread handle for Application controller */

    static CmdProcessor_T * AppCmdProcessor; /**< Handle to store the main Command processor handle to be reused by ServalPAL thread */

    /* global variables ********************************************************* */

    /* inline functions ********************************************************* */

    /* local functions ********************************************************** */

    /** Refer function definition for description */
    static void HTTPRestClientGetCB(const char* responseContent, uint32_t responseContentLen, bool isLastMessage)
    {
    BCDS_UNUSED(isLastMessage);

    if ((NULL != responseContent) && (0UL != responseContentLen))
    {
    printf("HTTPRestClientGetCB: HTTP GET response: %.*s\r\n", (int) responseContentLen, responseContent);
    }
    }

    /**
    * @brief This will validate the WLAN network connectivity
    *
    * If there is no connectivity then it will scan for the given network and try to reconnect
    *
    * @return RETCODE_OK on success, or an error code otherwise.
    */
    static Retcode_T AppControllerValidateWLANConnectivity(void)
    {
    Retcode_T retcode = RETCODE_OK;
    WlanNetworkConnect_IpStatus_T nwStatus;
    nwStatus = WlanNetworkConnect_GetIpStatus();

    if (WLANNWCT_IPSTATUS_CT_AQRD != nwStatus)
    {
    #if HTTP_SECURE_ENABLE
    static bool isSntpDisabled = false;
    if (false == isSntpDisabled)
    {
    retcode = SNTP_Disable();
    }
    if (RETCODE_OK == retcode)
    {
    isSntpDisabled = true;
    retcode = WLAN_Reconnect();
    }
    if (RETCODE_OK == retcode)
    {
    retcode = SNTP_Enable();
    }
    #else
    retcode = WLAN_Reconnect();
    #endif /* HTTP_SECURE_ENABLE */

    }
    return retcode;

    }

    /**
    * @brief Responsible for controlling the HTTP Example application control flow.
    *
    * - Synchronize the node with the SNTP server for time-stamp (if HTTPS)
    * - Check whether the WLAN network connection is available
    * - Do a HTTP rest client POST
    * - Wait for INTER_REQUEST_INTERVAL if POST was successful
    * - Do a HTTP rest client GET if POST was successful
    * - Wait for INTER_REQUEST_INTERVAL if GET was successful
    * - Redo the last 5 steps
    *
    * @param[in] pvParameters
    * Unused
    */
    static void AppControllerFire(void* pvParameters)
    {
    BCDS_UNUSED(pvParameters);

    Retcode_T retcode = RETCODE_OK;
    Sensor_Value_T sensorValue;
    char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
    const char *publishDataFormat = "{\"xdk_2\":{\"Environment\":{\"temperature\": %ld, \"humidity\": %ld, \"light\": %d}}}";

    memset(&sensorValue, 0x00, sizeof(sensorValue));
    #if HTTP_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 /* HTTP_SECURE_ENABLE */

    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.Temp, (long int) sensorValue.RH, (unsigned int) sensorValue.Light);


    HTTPRestClientPostInfo.Payload = publishBuffer;
    printf(HTTPRestClientPostInfo.Payload);
    /**HTTPRestClientPostInfo.Payload = "{ \"device\": \"XDK110\", \"ping\": \"pong\" }";*/
    /**printf(HTTPRestClientPostInfo.Payload);*/
    }
    /* Do a HTTP rest client POST */
    if (RETCODE_OK == retcode)
    {
    retcode = HTTPRestClient_Post(&HTTPRestClientConfigInfo, &HTTPRestClientPostInfo, APP_RESPONSE_FROM_HTTP_SERVER_POST_TIMEOUT);
    }
    if (RETCODE_OK == retcode)
    {
    /* Wait for INTER_REQUEST_INTERVAL */
    vTaskDelay(pdMS_TO_TICKS(INTER_REQUEST_INTERVAL));
    }
    if (RETCODE_OK != retcode)
    {
    printf("Error in Post/get request: Will trigger another post/get after INTER_REQUEST_INTERVAL\r\n");
    vTaskDelay(pdMS_TO_TICKS(INTER_REQUEST_INTERVAL));
    /* Report error and continue */
    Retcode_RaiseError(retcode);
    }
    }
    }

    /**
    * @brief To enable the necessary modules for the application
    * - WLAN
    * - ServalPAL
    * - SNTP (if HTTPS)
    * - HTTP rest client
    *
    * @param[in] param1
    * Unused
    *
    * @param[in] param2
    * Unused
    */
    static void AppControllerEnable(void * param1, uint32_t param2)
    {
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    Retcode_T retcode = WLAN_Enable();
    if (RETCODE_OK == retcode)
    {
    retcode = ServalPAL_Enable();
    }
    #if HTTP_SECURE_ENABLE
    if (RETCODE_OK == retcode)
    {
    retcode = SNTP_Enable();
    }
    #endif /* HTTP_SECURE_ENABLE */
    if (RETCODE_OK == retcode)
    {
    retcode = HTTPRestClient_Enable();
    }
    if (RETCODE_OK == retcode)
    {
    retcode = Sensor_Enable();
    }
    if (RETCODE_OK == retcode)
    {
    if (pdPASS != xTaskCreate(AppControllerFire, (const char * const ) "AppController", TASK_STACK_SIZE_APP_CONTROLLER, NULL, TASK_PRIO_APP_CONTROLLER, &AppControllerHandle))
    {
    retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_OUT_OF_RESOURCES);
    }
    }
    if (RETCODE_OK != retcode)
    {
    printf("AppControllerEnable : Failed \r\n");
    Retcode_RaiseError(retcode);
    assert(0); /* To provide LED indication for the user */
    }

    Utils_PrintResetCause();
    }

    /**
    * @brief To setup the necessary modules for the application
    * - WLAN
    * - ServalPAL
    * - SNTP (if HTTPS)
    * - HTTP rest client
    *
    * @param[in] param1
    * Unused
    *
    * @param[in] param2
    * Unused
    */
    static void AppControllerSetup(void * param1, uint32_t param2)
    {
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    Retcode_T retcode = WLAN_Setup(&WLANSetupInfo);
    if (RETCODE_OK == retcode)
    {
    retcode = ServalPAL_Setup(AppCmdProcessor);
    }
    #if HTTP_SECURE_ENABLE
    if (RETCODE_OK == retcode)
    {
    retcode = SNTP_Setup(&SNTPSetupInfo);
    }
    #endif /* HTTP_SECURE_ENABLE */
    if (RETCODE_OK == retcode)
    {
    retcode = HTTPRestClient_Setup(&HTTPRestClientSetupInfo);
    }
    if (RETCODE_OK == retcode)
    {
    SensorSetup.CmdProcessorHandle = AppCmdProcessor;
    retcode = Sensor_Setup(&SensorSetup);
    }
    if (RETCODE_OK == retcode)
    {
    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    }
    if (RETCODE_OK != retcode)
    {
    printf("AppControllerSetup : Failed \r\n");
    Retcode_RaiseError(retcode);
    assert(0); /* To provide LED indication for the user */
    }
    }

    /* global functions ********************************************************** */

    /** Refer interface header for description */
    void AppController_Init(void * cmdProcessorHandle, uint32_t param2)
    {
    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    if (cmdProcessorHandle == NULL)
    {
    printf("AppController_Init : Command processor handle is NULL \r\n");
    retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
    }
    else
    {
    AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
    }

    if (RETCODE_OK != retcode)
    {
    Retcode_RaiseError(retcode);
    assert(0); /* To provide LED indication for the user */
    }
    }

    /**@} */
    /** ************************************************************************* */

    Payload:

    char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
    const char *publishDataFormat = "{\"xdk_2\":{\"Environment\":{\"temperature\": %ld, \"humidity\": %ld, \"light\": %d}}}";

    int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormat,
    (long int) sensorValue.Temp, (long int) sensorValue.RH, (unsigned int) sensorValue.Light);

    HTTPRestClientPostInfo.Payload = publishBuffer;

     

    Best regards,

    Felix

    3 REPLIES 3
    Highlighted
    Established Member

    Re: send sensor data over http - convert buffer

    Hiello FelixGo,

    The type of HTTP paylaod can be set using the following API in HTTPRestClient.c file.

    HttpMsg_setContentType(httpMessage, Http_ContentType_App_Json);

     

    In the workbench, by default it is set as Http_ContentType_App_Json for HTTP_POST requests.

    Example application is of json format - { \"device\": \"XDK110\", \"ping\": \"pong\" }

    If we pass only a string in the POST request, it can be set as Http_ContentType_Text_Plain.

    In case of your payload format, ensure if it is in valid json format. Else try setting the type as Http_ContentType_Text_Plain.

     

    Regards,

    Shanmathi P

     

     

     

    Highlighted
    Member

    Re: send sensor data over http - convert buffer

    Hello Shanmathi,

    thanks for the hint with the 

    HttpMsg_setContentType

    i set it to "Text/Plain" and could succesfully send the message. But in my App i recieved just an empty  object {}.

    If i print my  "HTTPRestClientPostInfo.Payload"(publishBuffer) in Console before i do the post request, i get the following : {"xdk_2":{"Environment":{"temperature": 20941, "humidity": 34, "light": 0}}}

    This should be in correct JSON or isn´t it?

    Best regards

    Felix

    Highlighted
    Member

    Re: send sensor data over http - convert buffer

    Finaly found the problem:

    I changed HttpMsg_setContentType back to 

    HttpMsg_setContentType(httpMessage, Http_ContentType_App_Json);

    I just forgot to set the new "HTTPRestClientPostInfo.PayloadLength", in "AppController.c" file

    so after "HTTPRestClientPostInfo.Payload = publishBuffer;"

    i did "HTTPRestClientPostInfo.PayloadLength = (strlen(publishBuffer));"

     

     Full code example:

    Spoiler

    #include "XdkAppInfo.h"

    #undef BCDS_MODULE_ID /* Module ID define before including Basics package*/
    #define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

    /* own header files */
    #include "AppController.h"

    /* system header files */
    #include <stdio.h>

    /* additional interface header files */
    #include "XDK_Sensor.h"
    #include "XDK_WLAN.h"
    #include "XDK_ServalPAL.h"
    #include "XDK_HTTPRestClient.h"
    #include "XDK_SNTP.h"
    #include "BCDS_BSP_Board.h"

    #include "BCDS_WlanNetworkConfig.h"
    #include "BCDS_WlanNetworkConnect.h"
    #include "BCDS_CmdProcessor.h"
    #include "BCDS_Assert.h"
    #include "XDK_Utils.h"
    #include "FreeRTOS.h"
    #include "task.h"
    #include "timers.h"
    #include "string.h"

    /* constant definitions ***************************************************** */

    #if HTTP_SECURE_ENABLE
    #define APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT UINT32_C(10000)/**< Timeout for SNTP server time sync */
    #endif /* HTTP_SECURE_ENABLE */

    #define APP_RESPONSE_FROM_HTTP_SERVER_POST_TIMEOUT UINT32_C(25000)/**< Timeout for completion of HTTP rest client POST */

    #define APP_RESPONSE_FROM_HTTP_SERVER_GET_TIMEOUT UINT32_C(25000)/**< Timeout for completion of HTTP rest client GET */

    #define APP_TEMPERATURE_OFFSET_CORRECTION (-3459)
    #define APP_MQTT_DATA_BUFFER_SIZE UINT32_C(256)/**< macro for data size of incoming subscribed and published messages */

    /* local variables ********************************************************** */

    static WLAN_Setup_T WLANSetupInfo =
    {
    .IsEnterprise = false,
    .IsHostPgmEnabled = false,
    .SSID = WLAN_SSID,
    .Username = WLAN_PSK, /* Unused for Personal WPA2 connection */
    .Password = WLAN_PSK,
    .IsStatic = WLAN_STATIC_IP,
    .IpAddr = WLAN_IP_ADDR,
    .GwAddr = WLAN_GW_ADDR,
    .DnsAddr = WLAN_DNS_ADDR,
    .Mask = WLAN_MASK,
    };/**< WLAN setup parameters */

    static Sensor_Setup_T SensorSetup =
    {
    .CmdProcessorHandle = NULL,
    .Enable =
    {
    .Accel = true,
    .Mag = true,
    .Gyro = true,
    .Humidity = true,
    .Temp = true,
    .Pressure = true,
    .Light = true,
    .Noise = false,
    },
    .Config =
    {
    .Accel =
    {
    .Type = SENSOR_ACCEL_BMA280,
    .IsRawData = false,
    .IsInteruptEnabled = false,

    },
    .Gyro =
    {
    .Type = SENSOR_GYRO_BMG160,
    .IsRawData = false,
    },
    .Mag =
    {
    .IsRawData = false,
    },
    .Light =
    {
    .IsInteruptEnabled = false,

    },
    .Temp =
    {
    .OffsetCorrection = APP_TEMPERATURE_OFFSET_CORRECTION,
    },
    },
    };/**< Sensor setup parameters */

    #if HTTP_SECURE_ENABLE
    static SNTP_Setup_T SNTPSetupInfo =
    {
    .ServerUrl = SNTP_SERVER_URL,
    .ServerPort = SNTP_SERVER_PORT,
    };/**< SNTP setup parameters */
    #endif /* HTTP_SECURE_ENABLE */

    static HTTPRestClient_Setup_T HTTPRestClientSetupInfo =
    {
    .IsSecure = HTTP_SECURE_ENABLE,
    };/**< HTTP rest client setup parameters */

    /*
    * @todo - Use only DEST_SERVER_PORT in the header for secure and non-secure.
    * And remove the below 6 lines of the macros
    */
    #if HTTP_SECURE_ENABLE
    #ifdef DEST_SERVER_PORT
    #undef DEST_SERVER_PORT
    #endif /* #ifdef DEST_SERVER_PORT */
    #define DEST_SERVER_PORT DEST_SERVER_PORT_SECURE
    #endif /* if HTTP_SECURE_ENABLE */

    static HTTPRestClient_Config_T HTTPRestClientConfigInfo =
    {
    .IsSecure = HTTP_SECURE_ENABLE,
    .DestinationServerUrl = DEST_SERVER_HOST,
    .DestinationServerPort = DEST_SERVER_PORT,
    .RequestMaxDownloadSize = REQUEST_MAX_DOWNLOAD_SIZE,
    }; /**< HTTP rest client configuration parameters */

    static HTTPRestClient_Post_T HTTPRestClientPostInfo =
    {
    .Payload = POST_REQUEST_BODY,
    .PayloadLength = (sizeof(POST_REQUEST_BODY) - 1U),
    .Url = DEST_POST_PATH,
    .RequestCustomHeader0 = POST_REQUEST_CUSTOM_HEADER_0,
    .RequestCustomHeader1 = POST_REQUEST_CUSTOM_HEADER_1,
    }; /**< HTTP rest client POST parameters */

    /**
    * @brief The HTTP GET request response callback.
    *
    * @param [in] responseContent
    * Pointer to the GET request response
    *
    * @param [in] responseContentLen
    * Length of the GET request response
    *
    * @param [in] isLastMessage
    * Boolean to represent if it is the last part of the ongoing message
    */
    static void HTTPRestClientGetCB(const char* responseContent, uint32_t responseContentLen, bool isLastMessage);

    static HTTPRestClient_Get_T HTTPRestClientGetInfo =
    {
    .Url = DEST_GET_PATH,
    .GetCB = HTTPRestClientGetCB,
    .GetOffset = 0UL,
    }; /**< HTTP rest client GET parameters */

    static xTaskHandle AppControllerHandle = NULL; /**< OS thread handle for Application controller */

    static CmdProcessor_T * AppCmdProcessor; /**< Handle to store the main Command processor handle to be reused by ServalPAL thread */

    /* global variables ********************************************************* */

    /* inline functions ********************************************************* */

    /* local functions ********************************************************** */

    /** Refer function definition for description */
    static void HTTPRestClientGetCB(const char* responseContent, uint32_t responseContentLen, bool isLastMessage)
    {
    BCDS_UNUSED(isLastMessage);

    if ((NULL != responseContent) && (0UL != responseContentLen))
    {
    printf("HTTPRestClientGetCB: HTTP GET response: %.*s\r\n", (int) responseContentLen, responseContent);
    }
    }

    /**
    * @brief This will validate the WLAN network connectivity
    *
    * If there is no connectivity then it will scan for the given network and try to reconnect
    *
    * @return RETCODE_OK on success, or an error code otherwise.
    */
    static Retcode_T AppControllerValidateWLANConnectivity(void)
    {
    Retcode_T retcode = RETCODE_OK;
    WlanNetworkConnect_IpStatus_T nwStatus;
    nwStatus = WlanNetworkConnect_GetIpStatus();

    if (WLANNWCT_IPSTATUS_CT_AQRD != nwStatus)
    {
    #if HTTP_SECURE_ENABLE
    static bool isSntpDisabled = false;
    if (false == isSntpDisabled)
    {
    retcode = SNTP_Disable();
    }
    if (RETCODE_OK == retcode)
    {
    isSntpDisabled = true;
    retcode = WLAN_Reconnect();
    }
    if (RETCODE_OK == retcode)
    {
    retcode = SNTP_Enable();
    }
    #else
    retcode = WLAN_Reconnect();
    #endif /* HTTP_SECURE_ENABLE */

    }
    return retcode;

    }

    /**
    * @brief Responsible for controlling the HTTP Example application control flow.
    *
    * - Synchronize the node with the SNTP server for time-stamp (if HTTPS)
    * - Check whether the WLAN network connection is available
    * - Do a HTTP rest client POST
    * - Wait for INTER_REQUEST_INTERVAL if POST was successful
    * - Do a HTTP rest client GET if POST was successful
    * - Wait for INTER_REQUEST_INTERVAL if GET was successful
    * - Redo the last 5 steps
    *
    * @param[in] pvParameters
    * Unused
    */
    static void AppControllerFire(void* pvParameters)
    {
    BCDS_UNUSED(pvParameters);

    Retcode_T retcode = RETCODE_OK;
    Sensor_Value_T sensorValue;
    char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
    const char *publishDataFormat ={ "{\"xdk_2\":{\"Environment\":{\"temperature\": %ld, \"humidity\": %ld, \"light\": %d}}}" };

    memset(&sensorValue, 0x00, sizeof(sensorValue));
    #if HTTP_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 /* HTTP_SECURE_ENABLE */

    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.Temp, (long int) sensorValue.RH, (unsigned int) sensorValue.Light);

    HTTPRestClientPostInfo.Payload = publishBuffer;
    HTTPRestClientPostInfo.PayloadLength = (strlen(publishBuffer));
    printf(HTTPRestClientPostInfo.Payload);

    }
    /* Do a HTTP rest client POST */
    if (RETCODE_OK == retcode)
    {
    retcode = HTTPRestClient_Post(&HTTPRestClientConfigInfo, &HTTPRestClientPostInfo, APP_RESPONSE_FROM_HTTP_SERVER_POST_TIMEOUT);
    }
    if (RETCODE_OK == retcode)
    {
    /* Wait for INTER_REQUEST_INTERVAL */
    vTaskDelay(pdMS_TO_TICKS(INTER_REQUEST_INTERVAL));
    }
    if (RETCODE_OK != retcode)
    {
    printf("Error in Post/get request: Will trigger another post/get after INTER_REQUEST_INTERVAL\r\n");
    vTaskDelay(pdMS_TO_TICKS(INTER_REQUEST_INTERVAL));
    /* Report error and continue */
    Retcode_RaiseError(retcode);
    }
    }
    }

    /**
    * @brief To enable the necessary modules for the application
    * - WLAN
    * - ServalPAL
    * - SNTP (if HTTPS)
    * - HTTP rest client
    *
    * @param[in] param1
    * Unused
    *
    * @param[in] param2
    * Unused
    */
    static void AppControllerEnable(void * param1, uint32_t param2)
    {
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    Retcode_T retcode = WLAN_Enable();
    if (RETCODE_OK == retcode)
    {
    retcode = ServalPAL_Enable();
    }
    #if HTTP_SECURE_ENABLE
    if (RETCODE_OK == retcode)
    {
    retcode = SNTP_Enable();
    }
    #endif /* HTTP_SECURE_ENABLE */
    if (RETCODE_OK == retcode)
    {
    retcode = HTTPRestClient_Enable();
    }
    if (RETCODE_OK == retcode)
    {
    retcode = Sensor_Enable();
    }
    if (RETCODE_OK == retcode)
    {
    if (pdPASS != xTaskCreate(AppControllerFire, (const char * const ) "AppController", TASK_STACK_SIZE_APP_CONTROLLER, NULL, TASK_PRIO_APP_CONTROLLER, &AppControllerHandle))
    {
    retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_OUT_OF_RESOURCES);
    }
    }
    if (RETCODE_OK != retcode)
    {
    printf("AppControllerEnable : Failed \r\n");
    Retcode_RaiseError(retcode);
    assert(0); /* To provide LED indication for the user */
    }

    Utils_PrintResetCause();
    }

    /**
    * @brief To setup the necessary modules for the application
    * - WLAN
    * - ServalPAL
    * - SNTP (if HTTPS)
    * - HTTP rest client
    *
    * @param[in] param1
    * Unused
    *
    * @param[in] param2
    * Unused
    */
    static void AppControllerSetup(void * param1, uint32_t param2)
    {
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    Retcode_T retcode = WLAN_Setup(&WLANSetupInfo);
    if (RETCODE_OK == retcode)
    {
    retcode = ServalPAL_Setup(AppCmdProcessor);
    }
    #if HTTP_SECURE_ENABLE
    if (RETCODE_OK == retcode)
    {
    retcode = SNTP_Setup(&SNTPSetupInfo);
    }
    #endif /* HTTP_SECURE_ENABLE */
    if (RETCODE_OK == retcode)
    {
    retcode = HTTPRestClient_Setup(&HTTPRestClientSetupInfo);
    }
    if (RETCODE_OK == retcode)
    {
    SensorSetup.CmdProcessorHandle = AppCmdProcessor;
    retcode = Sensor_Setup(&SensorSetup);
    }
    if (RETCODE_OK == retcode)
    {
    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    }
    if (RETCODE_OK != retcode)
    {
    printf("AppControllerSetup : Failed \r\n");
    Retcode_RaiseError(retcode);
    assert(0); /* To provide LED indication for the user */
    }
    }

    /* global functions ********************************************************** */

    /** Refer interface header for description */
    void AppController_Init(void * cmdProcessorHandle, uint32_t param2)
    {
    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    if (cmdProcessorHandle == NULL)
    {
    printf("AppController_Init : Command processor handle is NULL \r\n");
    retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
    }
    else
    {
    AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
    }

    if (RETCODE_OK != retcode)
    {
    Retcode_RaiseError(retcode);
    assert(0); /* To provide LED indication for the user */
    }
    }

    /**@} */
    /** ************************************************************************* */

    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