XDK110
...
#define APP_MQTT_USER "XDK110"
/**
* APP_MQTT_PASSWORD is the password for the connection
*/
#define APP_MQTT_PASSWORD "XDK110"
../* 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);
}
}
....Last updated