Environmental Sensor
11/3/17 4:01 PM

Good afternoon Franjo,

I've discoverd a XDK tutorial on my laptop called "Sensor Guide XDK 110". To make sure that we're looking at the same document I've sended you the pdf Version via Mail.

In this tutorial it is described how to initalize and later read the sensor data of different single sensor or for the environmental sensors.

I'm running a bit into trouble when I try to determine the correct place for implementing the code snippets in my current project.

E.g. chapter 6: 'Envrionmental Sensor'

In which Project folders i exactly need to copy the given codesnippets ( Code 19-24 in the pdf) ?

Furthermore, when I add a code snippet e.g. to my ApplicationTemplate.c i receive a error like:

'Invalid declaration of 'returnOverSamplingValue'.

However, the variable returnOverSamplingValue is given in that way within the training (see code e.g. snippet 21 within the pdf) ?


Thank you very much in advance & have a nice weekend

Best regards





0 (0 Votes)
RE: Environmental Sensor
11/6/17 6:12 PM as a reply to Nils Hilser.

Hello Nils,

I have split your request in your old thread in a new one since the old initial issue is already solved. This will give other users with similar questions the benefit to find the thread more easily and to benefit from the proposed solutions as well.

I took a look a the guide as well and I will give you a short introduction about how to use the code.

The code should be used with a XdkApplicationTemplate for starters. All code needs to be pasted into XdkApplicationTemplate.c, here you can declare functions for initializing the sensor and reading it. As an example for initializing, declare:

void initializeSensor(void) {
  // paste code from guide here

Afterwards, you can use initializeSensor() in appInitSystem() after BCDS_UNUSED(param2)

Furthermore, Code 19 is used to display the sensor handle you which is neccessary to initialize the used sensor with the API of the environmental sensor. The code snippets 20 to 22 provde a simple implementation using the environmental sensor API Code 23 and 24 provide an overview on how the data can be read from the environmental sensor.

Additionally please note that Code 21 already makes an initialization of the environmental sensor with pre-settings. Code 22 also initializes the sensor (without changing the settings).

If you use Code 21, remove the call of Environmental_init(), and the following if-statement from Code 22.

Regarding your error, I have tried the tutorial to confirm that it works correctly, and it does. I assume that this error will be solved if you follow my instructions.

Please let me know if that was helpful and do not hesitate to ask if you have further questions.

Kind regards,

0 (0 Votes)
RE: Environmental Sensor
11/20/17 12:36 PM as a reply to Franjo Stjepandic.

Hello Franjo,

i apated my Application template from the previous post (https://xdk.bosch-connectivity.com/community/-/message_boards/message/306413). But still I'm running in some uncertainities.

As you advised I opened a new function called 

void initializeSensor(void) {}

Later in my Application Template within the void appInitSystem(void * CmdProcessorHandle, uint32_t param2) function I 'recalled' the function below the command BCDS_UNUSED(param2).

The only code snippet from the guide I was able to use without receiving an error was code 22.

So I copied the code snippet of code 22 to my application Template. I assume that this script is capable to read my environmental sensor data. 

Within the function static void clientTask(void*pvParameters) I removed/uncommented the msg.payload = "Hello" and replaced it with msg.payloadlen= sizeof(str(returnValue)). In my thinking returnValue is the variable which carries the desired sensor data ?! So the str() function will give me the string of it and sizeof() the required length. Am I correct with this assumption?

However, I do not really understand the difference between code snippet 22 and 23 and 24. Is it correct that code 21 (I'm not able to add this snippet without an error to my application template)and 22 (this one I can copy without a problem to my application template) are basically doing exactly the same thing?

In regard to snippet 23 and 24.. what is the difference to code 21 and 22. I'm asking this because from my understanding all 4 codes have a common denominator --> they read sensor data. Could please explain me more detailed the difference and between the four snippets, as well in regard to their field of application?

In the following you can find my most recent application template. The rest of the files within the project were not changed. They remained the same as in the previous post (see link above).

/* system header files */
#include <stdio.h>
#include "BCDS_Basics.h"
#include "BCDS_WlanConnect.h"
#include "BCDS_NetworkConfig.h"
#include "MQTTClient.h"
#include "MQTTConnect.h"
/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"

/* own header files */
#include "MQTT_3.1_new.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

/* Interface for the Gyroscope */
//#include "BCDS_Environmental.h" SETTING FROM GUIDE
#include "EnvironmentalSensor.h"
/* Interface for all sensors on the XDK */
#include "XdkSensorHandle.h"

/* constant definitions ***************************************************** */
#define CLIENT_BUFF_SIZE 1000
/* local variables ********************************************************** */
#define SECONDS(x) ((portTickType) (x * 1000) / portTICK_RATE_MS)

Network n;
Client c;

returnValue = Environmental_init(xdkEnvironmental_BME280_Handle);

static unsigned char buf[CLIENT_BUFF_SIZE];
static unsigned char readbuf[CLIENT_BUFF_SIZE];


static uint32_t clientMessageId = 0;

void initializeSensor(void){
    /* Sensor Handler for the BME280 Sensor */

    extern Environmental_HandlePtr_T xdkEnvironmental_BME280_Handle;

    /* initialize environmental sensor */
    if (RETCODE_OK != returnValue) {
    // do something
    /* read and print BME280 environmental sensor data */
    Environmental_Data_T bme280 = { INT32_C(0), UINT32_C(0), UINT32_C(0) };
    returnValue = Environmental_readData(xdkEnvironmental_BME280_Handle, &bme280);
    if ( RETCODE_OK == returnValue) {
    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);


static void clientTask(void*pvParameters)
{MQTTMessage msg;
msg.id = clientMessageId++;
msg.qos = 2;
msg.payload = str(returnValue);
msg.payloadlen= sizeof(str(returnValue));
//msg.payload = "Hello";
//msg.payloadlen = sizeof("Hello")-1;
MQTTPublish(&c, "xdk, &msg); //--> I changed the topic to only "xdk" only

void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
    if (CmdProcessorHandle == NULL)
        printf("Command processor handle is null \n\r");
    WlanConnect_SSID_T connectSSID = (WlanConnect_SSID_T) "xxxx";
    WlanConnect_PassPhrase_T connectPassPhrase = (WlanConnect_PassPhrase_T)
    Retcode_T rc_wlan=WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);
    printf("rc_wlan: %d\n\r", (int) Retcode_getCode(rc_wlan));
    int rc_connect=ConnectNetwork(&n,"", 1883); //1883
    printf("rc_connect: %d\n\r", rc_connect);
    MQTTClient(&c, &n, 1000, buf, CLIENT_BUFF_SIZE, readbuf,

    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.willFlag = 0;
    data.MQTTVersion = 3;
    data.clientID.cstring = "xdk123";
    data.keepAliveInterval = 100;
    data.cleansession = 1;
    MQTTConnect(&c, &data);

       xTaskCreate(clientTask, (const char * const) "Mqtt Client App",

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

I would be very glad for any further help. My aim is to receive sensor data via mqtt on my broker. 

Currently i do not receive any string or as desired sensor data on my broker.


 I will send you the entire project as well via mail.

Kind regards








0 (0 Votes)
RE: Environmental Sensor
11/20/17 12:41 PM as a reply to Nils Hilser.

Hello Franjo,

small adaption:

//#include "BCDS_Environmental.h" SETTING FROM GUIDE
#include "EnvironmentalSensor.h"

should be the other way around:

include "BCDS_Environmental.h" SETTING FROM GUIDE
//#include "EnvironmentalSensor.h"

However, this seems not to be the issue.  Btw. what is the differenve between both statements ?


Thank you very much in advance

Looking forward to hear from you


Kind regards




0 (0 Votes)
RE: Environmental Sensor
11/20/17 3:45 PM as a reply to Nils Hilser.

Good evening Franjo,


while I was working on my piece of code I stumbeld upon the MQTT paho guide. For some reason I forgot the existence of this training... I think this one is exactly doing what i would like to do (sending sensor data via Mqtt). However I was following the instructions of a relatively new version of this Mqtt training. So far the newest I was able to find:
(XDK110: Getting Started Guide with MQTT (Paho Demo)
Document revision 1.5
Document release date July 17, 2017

However, it seems that the paho files are not adapted to the need of applying it under the latest workbench version (3.0) ? 

I adapted the paho Mqtt files with files from a folder eclispse paho folder I still had on my harddrive. Name of this file is org.eclipse.paho.mqtt.embedded-c-1.0.0. The adaption of the MQTT paho files with files of the eclipse paho folder is as well described within the guide. But I was not able to the paho eclispe file which was stored on my harddrive within the internet.

My questions would be the following:

- Is there a complete MQTT paho training for the latest version of the workbench available?
Cause there are many functions delievering an error while creating the project. I was only able to find the new name for a few functions (from workbench 2 to workbench 3) within the XDK forum..

- What makes more sense ? to continue with the reconstruction of my Hello string approach or to 
   spend the time into the MQTT paho training ?


Would be glad for any help


Thank you in advance


Best regards and have a nice evening





0 (0 Votes)
RE: Environmental Sensor
11/20/17 7:48 PM as a reply to Nils Hilser.

Hello Nils,

I will gladly answer your questions and give you the necessary hints to get your code working.

Regarding your errors, it is good to implement a function called initializeSensor() to paste the guide code into it. Unfortunately you missed to paste the line returnValue = Environmental_init(xdkEnvironmental_BME280_Handle); in it as well. Without that line, the environmental sensor will not be initialized properly and shows an unintended behaviour.

Furthermore, the line extern Environmental_HandlePtr_T xdkEnvironmental_BME280_Handle; can be deleted, since the handle is provided by the interface XdkSensorHandle.h.

Regarding your issue about the guide codes. Code 21 and 22 are not exactly the same. Code 21 shows how to initialize the environmental sensor with pre-settings.

Code 22 shows how to simply implement the environmental sensor and read data from it with less configuration. I would recommend to use this code for your application as a starting point.

The codes 23 and 24 display the differences when reading data from the environmental sensor. Code 23 shows how to get the sensor data in its physical representation. This means that the digitized sensor values are converted into their physical values.

On the opposite, code 24 shows how to get the sensor data in its digital representation.

For more information about these values, you can take a look at the related section in the sensor guide.

Regarding the include EnvironmentalSensor.h, you only need to include the interface XdkSensorHandle.h which provides all interfaces and sensor variables for every sensor the XDK integrates.

The interface Environmental.h is the interface which provides the functionality for the environmental sensor and where you can find all necessary functions. Furthermore, it is included in XdkSensorHandle.h and does not need to be included in your implementation file.

The interface EnvironmentalSensor.h in that case is an application header file from an example of the XDK-Workbench and offers no direct functionality to use the environmental sensor.

Regarding your use case, I would recommend splitting the code for the environmental sensor into two parts as follows.

Retcode_T initializeSensor(void){ returnValue = Environmental_init(xdkEnvironmental_BME280_Handle); return returnValue; } Environmental_Data_T ReadSensor(void){ Environmental_Data_T bme280 = { INT32_C(0), UINT32_C(0), UINT32_C(0) }; returnValue = Environmental_readData(xdkEnvironmental_BME280_Handle, &bme280); if ( RETCODE_OK == returnValue) { 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); } return bme280; }

With this you will have a function for initializing the environmental sensor properly and one to read data from it. The function initializeSensor() can stay in appInitSystem() and readSensor() should be passed into the function clientTask(). Please note that currently, the function readSensor() returns a struct, which holds the data of the environmental sensor. To send it as a string via MQTT, I recommend to convert the data into a string, by using the function snprintf(). The function gives you the benefit, that you additionally receive the length of the string as return value. The approach you described should fit too, but nevertheless I recommend the use of snprintf().

Regarding the MQTT paho demo, there is no updated documentation for the XDK-Workbench 3.1.0, since the demo is no longer available for download due to incompatibilities with the latest version of the MQTT paho demo. For more information about that, please refer to this thread here. Regarding your question, what would make sense to continue? I would recommend to continue implementing your own application to send sensor data to your MQTT broker, since you gain a lot of experience regarding implementing applications for the XDK.

Besides that a short recommendation, since you asked some questions which are mostly in detail described in the Environmental section of the sensor guide, I would recommend read through the section to get familiar with the functionality of the environmental sensor.

Furthermore, I would also recommend to going through an C-Tutorial since C programming is absolutely necessary when developing with the XDK. If needed I can recommend a guide that covers most of the needed basics.

Please let me know if that was helpful and do not hesitate to ask if you have further questions.

Kind regards,

0 (0 Votes)