Is there anyone who can send sensor data via HTTPS POST?
Who could give me sample code ?
I can only send fixed data through HTTPS POST, but I don't know how to get sensor data as a variable value and send it through HTTPS..
here is my sample C language code:
/* own header files */
#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_WLAN.h"
#include "XDK_Sensor.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 "XdkSensorHandle.h"
#include "timers.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)/**< Macro for static temperature offset correction. Self heating, temperature correction factor */
/* 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 */
#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;
#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();
/* 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));
/* Do a HTTP rest client GET */
retcode = HTTPRestClient_Get(&HTTPRestClientConfigInfo, &HTTPRestClientGetInfo, APP_RESPONSE_FROM_HTTP_SERVER_GET_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)
{
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)
{
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 */
}
}
Solved! Go to Solution.