XDK110
Last updated
Was this helpful?
Last updated
Was this helpful?
First we need to open Workbench 3.4.0, then we can use the example provided by Bosch named "SendDataOverMQTT".
We need to edit ""
*Basically this file help us to define each component of WIFI, MQTT, Topics. We need to add two lines more that is user and password.
...
#define APP_MQTT_USER "XDK110"
/**
* APP_MQTT_PASSWORD is the password for the connection
*/
#define APP_MQTT_PASSWORD "XDK110"
..
Then we need to open and modify "". We need to define multiple things in this file.
Add user and Password of the MQTT connection.
Set up what sensors of the XDK we want to retrive.
Create the JSON structure that we are going to use and the send throug mqtt.
/* module includes ********************************************************** */
/* 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 "BCDS_BSP_Board.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_NetworkConfig.h"
#include "BCDS_Assert.h"
#include "XDK_WLAN.h"
#include "XDK_MQTT.h"
#include "XDK_Sensor.h"
#include "XDK_SNTP.h"
#include "XDK_ServalPAL.h"
#include "FreeRTOS.h"
#include "task.h"
/* constant definitions ***************************************************** */
#define MQTT_CONNECT_TIMEOUT_IN_MS UINT32_C(60000)/**< Macro for MQTT connection timeout in milli-second */
#define MQTT_SUBSCRIBE_TIMEOUT_IN_MS UINT32_C(5000)/**< Macro for MQTT subscription timeout in milli-second */
#define MQTT_PUBLISH_TIMEOUT_IN_MS UINT32_C(5000)/**< Macro for MQTT publication timeout in milli-second */
#define APP_TEMPERATURE_OFFSET_CORRECTION (-3459)/**< Macro for static temperature offset correction. Self heating, temperature correction factor */
#define APP_MQTT_DATA_BUFFER_SIZE UINT32_C(256)/**< macro for data size of incoming subscribed and published messages */
#if APP_MQTT_SECURE_ENABLE
#define APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT UINT32_C(10000)/**< Timeout for SNTP server time sync */
#endif /* APP_MQTT_SECURE_ENABLE */
/* local variables ********************************************************** */
static WLAN_Setup_T WLANSetupInfo =
{
.IsEnterprise = false,
.IsHostPgmEnabled = false,
.SSID = WLAN_SSID,
.Username = WLAN_PSK,
.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 = false,
.Mag = false,
.Gyro = false,
.Humidity = true,
.Temp = true,
.Pressure = true,
.Light = true,
.Noise = true,
},
.Config =
{
.Accel =
{
.Type = SENSOR_ACCEL_BMA280,
.IsRawData = false,
.IsInteruptEnabled = false,
.Callback = NULL,
},
.Gyro =
{
.Type = SENSOR_GYRO_BMG160,
.IsRawData = false,
},
.Mag =
{
.IsRawData = false
},
.Light =
{
.IsInteruptEnabled = false,
.Callback = NULL,
},
.Temp =
{
.OffsetCorrection = APP_TEMPERATURE_OFFSET_CORRECTION,
},
},
};/**< Sensor setup parameters */
#if APP_MQTT_SECURE_ENABLE
static SNTP_Setup_T SNTPSetupInfo =
{
.ServerUrl = SNTP_SERVER_URL,
.ServerPort = SNTP_SERVER_PORT,
};/**< SNTP setup parameters */
#endif /* APP_MQTT_SECURE_ENABLE */
static MQTT_Setup_T MqttSetupInfo =
{
.MqttType = MQTT_TYPE_SERVALSTACK,
.IsSecure = APP_MQTT_SECURE_ENABLE,
};/**< MQTT setup parameters */
static MQTT_Connect_T MqttConnectInfo =
{ .User = APP_MQTT_USER,
.Password = APP_MQTT_PASSWORD,
.ClientId = APP_MQTT_CLIENT_ID,
.BrokerURL = APP_MQTT_BROKER_HOST_URL,
.BrokerPort = APP_MQTT_BROKER_HOST_PORT,
.CleanSession = true,
.KeepAliveInterval = 100,
};/**< MQTT connect parameters */
static void AppMQTTSubscribeCB(MQTT_SubscribeCBParam_T param);
static MQTT_Subscribe_T MqttSubscribeInfo =
{
.Topic = APP_MQTT_TOPIC,
.QoS = 1UL,
.IncomingPublishNotificationCB = AppMQTTSubscribeCB,
};/**< MQTT subscribe parameters */
static MQTT_Publish_T MqttPublishInfo =
{
.Topic = APP_MQTT_TOPIC,
.QoS = 1UL,
.Payload = NULL,
.PayloadLength = 0UL,
};/**< MQTT publish parameters */
static uint32_t AppIncomingMsgCount = 0UL;/**< Incoming message count */
static char AppIncomingMsgTopicBuffer[APP_MQTT_DATA_BUFFER_SIZE];/**< Incoming message topic buffer */
static char AppIncomingMsgPayloadBuffer[APP_MQTT_DATA_BUFFER_SIZE];/**< Incoming message payload buffer */
static CmdProcessor_T * AppCmdProcessor;/**< Handle to store the main Command processor handle to be used by run-time event driven threads */
static xTaskHandle AppControllerHandle = NULL;/**< OS thread handle for Application controller to be used by run-time blocking threads */
/* global variables ********************************************************* */
/* inline functions ********************************************************* */
/* local functions ********************************************************** */
static void AppMQTTSubscribeCB(MQTT_SubscribeCBParam_T param)
{
AppIncomingMsgCount++;
memset(AppIncomingMsgTopicBuffer, 0, sizeof(AppIncomingMsgTopicBuffer));
memset(AppIncomingMsgPayloadBuffer, 0, sizeof(AppIncomingMsgPayloadBuffer));
if (param.PayloadLength < sizeof(AppIncomingMsgPayloadBuffer))
{
strncpy(AppIncomingMsgPayloadBuffer, (const char *) param.Payload, param.PayloadLength);
}
else
{
strncpy(AppIncomingMsgPayloadBuffer, (const char *) param.Payload, (sizeof(AppIncomingMsgPayloadBuffer) - 1U));
}
if (param.TopicLength < (int) sizeof(AppIncomingMsgTopicBuffer))
{
strncpy(AppIncomingMsgTopicBuffer, param.Topic, param.TopicLength);
}
else
{
strncpy(AppIncomingMsgTopicBuffer, param.Topic, (sizeof(AppIncomingMsgTopicBuffer) - 1U));
}
printf("AppMQTTSubscribeCB : #%d, Incoming Message:\r\n"
"\tTopic: %s\r\n"
"\tPayload: \r\n\"\"\"\r\n%s\r\n\"\"\"\r\n", (int) AppIncomingMsgCount,
AppIncomingMsgTopicBuffer, AppIncomingMsgPayloadBuffer);
}
...
/**
* @brief Responsible for controlling the send data over MQTT application control flow.
*
* - Synchronize SNTP time stamp for the system if MQTT communication is secure
* - Connect to MQTT broker
* - Subscribe to MQTT topic
* - Read environmental sensor data
* - Publish data periodically for a MQTT topic
*
* @param[in] pvParameters
* Unused
*/
static void AppControllerFire(void* pvParameters)
{
BCDS_UNUSED(pvParameters);
//Create the JSON structure
Retcode_T retcode = RETCODE_OK;
Sensor_Value_T sensorValue;
char publishBuffer[APP_MQTT_DATA_BUFFER_SIZE];
const char *publishDataFormat = "{\n"
"\r\t\"Ident\" : \"xdk110-4\","
"\r\t\"Humidity\" : \"%ld\",\n"
"\r\t\"Pressure\" : \"%ld\",\n"
"\r\t\"Temperature\" : \"%f\",\n"
"\r\t\"Light\" : \"%ld\",\n"
"\r\t\"Noise\" : \"%f\"\n"
"}";
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)
{
retcode = MQTT_SubsribeToTopic(&MqttSubscribeInfo, MQTT_SUBSCRIBE_TIMEOUT_IN_MS);
}
if (RETCODE_OK != retcode)
{
/* We raise error and still proceed to publish data periodically */
Retcode_RaiseError(retcode);
}
/* 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 */
AppControllerValidateWLANConnectivity();
retcode = Sensor_GetData(&sensorValue);
if (RETCODE_OK == retcode)
{
int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormat,
(long int) sensorValue.RH, (long int) sensorValue.Pressure,
(sensorValue.Temp /= 1000),sensorValue.Light,sensorValue.Noise);
MqttPublishInfo.Payload = publishBuffer;
MqttPublishInfo.PayloadLength = length;
retcode = MQTT_PublishToTopic(&MqttPublishInfo, MQTT_PUBLISH_TIMEOUT_IN_MS);
}
if (RETCODE_OK != retcode)
{
Retcode_RaiseError(retcode);
}
vTaskDelay(APP_MQTT_DATA_PUBLISH_PERIODICITY);
}
}
....
This new add on the file would cause and error, that why we are going to modify another files on the project.
This changes are going to be on the part of the mqtt part, adding user and password.
We need to modifie the next libraries:
...
switch (MqttSetupInfo.MqttType)
{
case MQTT_TYPE_SERVALSTACK:
{
Ip_Address_T brokerIpAddress = 0UL;
StringDescr_T clientID;
StringDescr_T Username;//Add
StringDescr_T Password;//Add
char mqttBrokerURL[30] = { 0 };
char serverIpStringBuffer[16] = { 0 };
if (RC_OK != Mqtt_initialize())
{
retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_INIT_FAILED);
}
if (RETCODE_OK == retcode)
{
if (RC_OK != Mqtt_initializeInternalSession(&MqttSession))
{
retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_INIT_INTERNAL_SESSION_FAILED);
}
}
if (RETCODE_OK == retcode)
{
retcode = NetworkConfig_GetIpAddress((uint8_t *) connect->BrokerURL, &brokerIpAddress);
}
if (RETCODE_OK == retcode)
{
if (0 > Ip_convertAddrToString(&brokerIpAddress, serverIpStringBuffer))
{
retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_IPCONIG_FAIL);
}
}
if (RETCODE_OK == retcode)
{
MqttSession.MQTTVersion = 3;
MqttSession.keepAliveInterval = connect->KeepAliveInterval;
MqttSession.cleanSession = connect->CleanSession;
MqttSession.will.haveWill = false;
MqttSession.onMqttEvent = MqttEventHandler;
StringDescr_wrap(&Username, connect->User); // Add
MqttSession.username = Username; //Add
StringDescr_wrap(&Password, connect->Password);
MqttSession.password = Password;
StringDescr_wrap(&clientID, connect->ClientId);
MqttSession.clientID = clientID;
...
....
struct MQTT_Connect_S
{
const char * User;
const char * Password;
const char * ClientId; /**< The client identifier which is a identifier of each MQTT client connecting to a MQTT broker. It needs to be unique for the broker to know the state of the client. */
const char * BrokerURL; /**< The URL pointing to the MQTT broker */
uint16_t BrokerPort; /**< The port number of the MQTT broker */
bool CleanSession; /**< The clean session flag indicates to the broker whether the client wants to establish a clean session or a persistent session where all subscriptions and messages (QoS 1 & 2) are stored for the client. */
uint32_t KeepAliveInterval; /**< The keep alive interval (in seconds) is the time the client commits to for when sending regular pings to the broker. The broker responds to the pings enabling both sides to determine if the other one is still alive and reachable */
};
...