BOSCH XDK THINGSPEAK
Answer
5/21/19 9:10 AM

Hi I am trying to send Bosch XDK Sensor Data to ThingSpeak to get live monitoring of the data.

As a start I try to send Light data, I manage to GET response successful. However, the data receive at ThingSpeak is always 0. Please could someone assist me with this issue. Below are my code. Thank You!

 

#include <stdio.h>
#include "BCDS_Basics.h"

/* additional interface header files */
#include <FreeRTOS.h>
#include <timers.h>

#include "PAL_initialize_ih.h"
#include "PAL_socketMonitor_ih.h"
#include "BCDS_WlanConnect.h"
#include "BCDS_NetworkConfig.h"
#include <Serval_HttpClient.h>
#include <Serval_Network.h>
#include "PAL_initialize_ih.h"
#include "BCDS_LightSensor.h"
#include "XdkSensorHandle.h"

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

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

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

/* global variables ********************************************************* */
static xTimerHandle connectTimerHandle; /**< variable to store timer handle*/
Ip_Address_T destAddr = UINT32_C(0);/*< variable to store the Ip address of the server */


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

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

/* The description is in the header file. */
static retcode_t wlanConnect(void)
{
    NetworkConfig_IpSettings_T myIpSettings;
    char ipAddress[PAL_IP_ADDRESS_SIZE] = { 0 };
    Ip_Address_T* IpaddressHex = Ip_getMyIpAddr();
    WlanConnect_SSID_T connectSSID;
    WlanConnect_PassPhrase_T connectPassPhrase;
    Retcode_T ReturnValue = (Retcode_T)RETCODE_FAILURE;
    int32_t Result = INT32_C(-1);

    if (RETCODE_OK != WlanConnect_Init())
    {
        return (RC_PLATFORM_ERROR);
    }

    printf("Connecting to %s \r\n ", WLAN_CONNECT_WPA_SSID);

    connectSSID = (WlanConnect_SSID_T) WLAN_CONNECT_WPA_SSID;
    connectPassPhrase = (WlanConnect_PassPhrase_T) WLAN_CONNECT_WPA_PASS;
    ReturnValue = NetworkConfig_SetIpDhcp(NULL);
    if (RETCODE_OK != ReturnValue)
    {
        printf("Error in setting IP to DHCP\n\r");
        return (RC_PLATFORM_ERROR);
    }
    if (RETCODE_OK == WlanConnect_WPA(connectSSID, connectPassPhrase, NULL))
    {
        ReturnValue = NetworkConfig_GetIpSettings(&myIpSettings);
        if (RETCODE_OK == ReturnValue)
        {
            *IpaddressHex = Basics_htonl(myIpSettings.ipV4);
            Result = Ip_convertAddrToString(IpaddressHex, ipAddress);
            if (Result < 0)
            {
                printf("Couldn't convert the IP address to string format \r\n ");
                return (RC_PLATFORM_ERROR);
            }
            printf("Connected to WPA network successfully \r\n ");
            printf(" Ip address of the device %s \r\n ", ipAddress);
            return (RC_OK);
        }
        else
        {
            printf("Error in getting IP settings\n\r");
            return (RC_PLATFORM_ERROR);
        }
    }
    else
    {
        return (RC_PLATFORM_ERROR);
    }

}

/* The description is in the configuration header file. */
static retcode_t callbackOnSent(Callable_T *callfunc, retcode_t status)
{
    BCDS_UNUSED(callfunc);

    if (status != RC_OK)
    {
        printf("error occurred in connecting server \r\n");
    }
    return (RC_OK);
}

/* The description is in the configuration header file. */
static retcode_t httpClientResponseCallback(HttpSession_T *httpSession,
        Msg_T *msg_ptr, retcode_t status)
{
    BCDS_UNUSED(httpSession);
    retcode_t rc = status;

    if (status != RC_OK)
    {
        /* Error occurred in downloading the page */
    }
    else if (msg_ptr == NULL)
    {
        rc = RC_HTTP_PARSER_INVALID_CONTENT_TYPE;
    }
    else
    {
        if (HttpMsg_getStatusCode(msg_ptr) != Http_StatusCode_OK)
        {
            rc = RC_HTTP_INVALID_RESPONSE;
        }
        else
        {
            if (HttpMsg_getContentType(msg_ptr) != Http_ContentType_Text_Html)
            {
                rc = RC_HTTP_INVALID_RESPONSE;
            }
            else
            {
                char const *content_ptr;
                unsigned int len = UINT32_C(0);

                HttpMsg_getContent(msg_ptr, &content_ptr, &len);
                printf("GET Response Content %s length %d \r\n", content_ptr, len);
            }
        }
    }

    if (rc != RC_OK)
    {
        printf("error occurred in downloading HTML \r\n");
    }
    return (rc);
}


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

static void initSensor(void)
{
    Retcode_T returnValue = (Retcode_T) RETCODE_FAILURE;
    returnValue = Environmental_init(xdkEnvironmental_BME280_Handle);
    returnValue = LightSensor_init(xdkLightSensor_MAX44009_Handle);
    returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);
    if(RETCODE_OK != returnValue)
    {
        //printf("Light Sensor initialization Failed\n\r");
    }
}

uint32_t luxData = UINT32_C(0);
Accelerometer_XyzData_T aData = { INT32_C(0), INT32_C(0), INT32_C(0) };
Environmental_Data_T bme280 = { INT32_C(0), UINT32_C(0), UINT32_C(0) };

static void readSensor(void *pvParameters)
{

    BCDS_UNUSED(pvParameters);
    //uint32_t milliLuxData = UINT32_C(0);
    Retcode_T returnValue = (Retcode_T)RETCODE_FAILURE;

    returnValue = Environmental_readTemperature(xdkEnvironmental_BME280_Handle,
    &bme280);

    if ( RETCODE_OK == returnValue)
        {
            /*print chip id and Environmental data of BME280 on serialport */
            printf("BME280 Environmental Conversion Data :\n\rp =%ld Pa\n\rt =%ld mDeg\n\rh =%ld %%rh\n\r", (long int) bme280.pressure, (long int) bme280.temperature, (long int) bme280.humidity);
        }
        else
        {
            printf("Environmental Read actual Data Failed\n\r");
        }

    returnValue = LightSensor_readLuxData(xdkLightSensor_MAX44009_Handle,
    &luxData);

    if (returnValue != RETCODE_OK)
        {
            printf("lightsensorReadInMilliLux Failed\n\r");
        }
        else
        {
            printf("Light sensor data obtained in milli lux :%d \n\r", (unsigned int) luxData);
        }

    returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,
    &aData);

}

/* The description is in the configuration header file. */
void connectServer(xTimerHandle xTimer)
{
    BCDS_UNUSED(xTimer);

    retcode_t rc = RC_OK;
    Msg_T* msg_ptr;
    Ip_Port_T destPort = (Ip_Port_T) DEST_PORT_NUMBER;
    static Callable_T SentCallable;
    const char url_ptr[100]; /* URL string for the http website */

    int n = sprintf(url_ptr,"/update?key=FLERRQZY4C3U8MRK&field1=%d", luxData);

    Callable_T * Callable_pointer;
    Callable_pointer = Callable_assign(&SentCallable, callbackOnSent);
    if (Callable_pointer == NULL)
    {
        printf("Failed Callable_assign\r\n ");
        return;
    }
    rc = HttpClient_initRequest(&destAddr, Ip_convertIntToPort(destPort), &msg_ptr);

    if (rc != RC_OK || msg_ptr == NULL)
    {
        printf("Failed HttpClient_initRequest\r\n ");
        return;
    }

    HttpMsg_setReqMethod(msg_ptr, Http_Method_Post);
    rc = HttpMsg_setReqUrl(msg_ptr, url_ptr);
    if (rc != RC_OK)
    {
        printf("Failed to fill message \r\n ");
        return;
    }

    rc = HttpClient_pushRequest(msg_ptr, &SentCallable,
            httpClientResponseCallback);
    if (rc != RC_OK)
    {
        printf("Failed HttpClient_pushRequest \r\n  ");
        return;
    }
}

/* The description is in the interface header file. */
void init(void)
{
    retcode_t rc = RC_OK;

    rc = wlanConnect();
    if (RC_OK != rc)
    {
        printf("Network init/connection failed %i \r\n", rc);
        return;
    }

    rc = PAL_initialize();
    if (RC_OK != rc)
    {
        printf("PAL and network initialize %i \r\n", rc);
        return;
    }

    PAL_socketMonitorInit();

    /* start client */
    rc = HttpClient_initialize();
    if (rc != RC_OK)
    {
        printf("Failed to initialize http client\r\n ");
        return;
    }

    if (RC_OK != PAL_getIpaddress((uint8_t*) "api.thingspeak.com", &destAddr))
    {
        return;
    }
    else
    {
        uint32_t Ticks = CONNECT_TIME_INTERVAL;

        if (Ticks != UINT32_MAX) /* Validated for portMAX_DELAY to assist the task to wait Infinitely (without timing out) */
        {
            Ticks /= portTICK_RATE_MS;
        }
        if (UINT32_C(0) == Ticks) /* ticks cannot be 0 in FreeRTOS timer. So ticks is assigned to 1 */
        {
            Ticks = UINT32_C(1);
        }
        connectTimerHandle = xTimerCreate(
                (const char * const ) "connectServer", Ticks,
                TIMER_AUTORELOAD_ON, NULL, connectServer);

        if (connectTimerHandle != NULL)
        {
            if (xTimerStart(connectTimerHandle, TIMERBLOCKTIME) != pdTRUE)
            {
                assert(false);
            }

        }
    }
}

/**
 * @brief This is a template function where the user can write his custom application.
 *
 */
void appInitSystem(xTimerHandle xTimer)
{
    BCDS_UNUSED(xTimer);

    uint32_t timerBlockTime = UINT32_MAX;
    uint32_t OneSecondDelay = UINT32_C(2000);
    uint32_t timerAutoReloadOn = pdTRUE;

    xTimerHandle SensorHandle= NULL;
    initSensor();

    SensorHandle = xTimerCreate((const char *) "readTempSensor", OneSecondDelay, timerAutoReloadOn, NULL, readSensor);

    /*Call the RHC init API */
    init();
}
/** ************************************************************************* */
 

0 (0 Votes)
RE: BOSCH XDK THINGSPEAK
Answer
5/23/19 6:55 AM as a reply to ng John.

Hi ng John,

Workbench has example application BoschXDKCloudConnectivity which sends the sensors data to ThingSpeak. Kindly refer this example application.

Thanks and Regards,

Vijaya Kumar B

0 (0 Votes)