API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
cumulocity_nfc_wifi_xbee.c

Example showing the capabilities of the Cumulocity middleware using the wifi interface and managing a NFC reader and XBee device.

/* @file cumulocity_nfc_wifi_xbee.c
* @author TST
* @version 0.1
* @date 16/08/2013
* @brief This is an example of how to use the TSmarT API to send data to Cumulocity middleware
* using the wifi interface.
*
* The example application shows the user how to register a device in Cumulocity before sending the NFC ID
* card detected and the data received from a remote XBee.
*
* The example application is based on three task:
* - CM task --------> It's the main task because it configures the WIFI communication interface, the NFC reader and the
* XBee device.
* In addition, it registers a new device in Cumulocity.
*
* - CM_NFC task ----> It's constantly checking the presence of a NFC card or not. If a NFC card is detected, its ID
* card will be sent to Cumulocity.
*
* - CM_XBee task ---> It's waiting for a new data from a remote XBee(Digimesh). When new data are received it sends
* those data to Cumulocity. In addition it sends a confirmation to the remote XBee.
*
* To run this example is necessary to be previously registered in the Cumulcoty middleware.
* http://cumulocity.com
*
* Once you have access to Cumulocity, users shall set their account details in the definitions below:
* - TENANT_ACCOUNT
* - USER_ACCOUNT
* - PASSWORD_ACCOUNT
* - API_KEY_ACCOUNT
*
*
* The interface selection is really done during compiling process. So you shall select
* the adequate interface for the application(WIFI for this example) enabling or
* disabling the following definitions located in the App.defs file in the application
* folder:
*
* MIDDELWARE_INTERFACE = ETHERNET
* MIDDELWARE_INTERFACE = WIFI
* MIDDELWARE_INTERFACE = CELLULAR_2G
*
* After selecting one of them, users only have to use the functions of the interface selected
* besides of the common API functions to program their applications.
*
* @note To run this application it's necessary to set some wireless network parameters:
* - SSID ------------------> Service Set IDentifier. It is also the network name.
* - Network key -----------> Wifi network key.
* - Type of network key ---> type of the network key: open, WEP, WPA, WPA2.
* - Local IP --------------> Set 0.0.0.0 for dynamic IP address.
* - Subnet mask -----------> Set 0.0.0.0 for dynamic IP address.
* - IP gateway ------------> Set 0.0.0.0 for dynamic IP address.
* - DNS -------------------> Set 0.0.0.0 for dynamic IP address.
* User shall set them in the definitions below.
*
* @note to run this application it's necessary to set some XBee parameters in both XBee devices, local and remote:
*
* - Operating channel ----------> i.e.: 0x0E.
* - PAN id: --------------------> i.e.: 0x3322.
* - Destiantion address high ---> It's the high part of the address destination.
* - Destination address low ----> It's the low part of the address destination.
* - The rest values shall be default values.
* User shall set them in the definitions below.
*
* @note User shall use the X-CTU terminal form DIGI to receive/send information to the XBee located in the
* TSmarT platform
*
*
*/
/*-------------------------------------------------------- System headers -------------------------------------------------*/
#include "tsmart.h"
/*-------------------------------------------------------- UART Debug -----------------------------------------------------*/
#define DEBUG_UART tsmart_uart3
#define APP_RX_BUFFER_SZ 10
static uint8_t app_rx_buf[APP_RX_BUFFER_SZ];
/* ---------------------------------------- Wifi Interface Variables/Definitions ------------------------------------------*/
/* IP address (0.0.0.0 for dynamic IP address) */
#define IP_ADDR0 0
#define IP_ADDR1 0
#define IP_ADDR2 0
#define IP_ADDR3 0
/* Subnet mask (0.0.0.0 for dynamic IP address) */
#define NET_MASK0 0
#define NET_MASK1 0
#define NET_MASK2 0
#define NET_MASK3 0
/* Gateway address (0.0.0.0 for dynamic IP address) */
#define IP_GW0 0
#define IP_GW1 0
#define IP_GW2 0
#define IP_GW3 0
/* DNS (0.0.0.0 for dynamic IP address) */
#define DNS 0
#define DNS 0
#define DNS 0
#define DNS 0
/* SSID */
#define SSID "<SET YOUR SSID>"
/* Network key */
#define KEY "<SET YOUR NETWORK KEY>"
/* Type network key: select one of them */
#define KEY_TYPE TSMART_CUMULOCITY_WIFI_UNSEC
//#define KEY_TYPE TSMART_CUMULOCITY_WIFI_WEP
//#define KEY_TYPE TSMART_CUMULOCITY_WIFI_WPA
//#define KEY_TYPE TSMART_CUMULOCITY_WIFI_WPA2
static tsmart_cumulocity_wifi_wlan_t tsmart_cumulocity_wifi_wlan = {
.ip = {
.v0 = IP_ADDR0,
.v0 = IP_ADDR0,
.v0 = IP_ADDR0,
.v0 = IP_ADDR0,
},
.mask = {
.v0 = NET_MASK0,
.v0 = NET_MASK0,
.v0 = NET_MASK0,
.v0 = NET_MASK0,
},
.ip_gw = {
.v0 = NET_MASK0,
.v0 = NET_MASK0,
.v0 = NET_MASK0,
.v0 = NET_MASK0,
},
.dns = {
.v0 = DNS,
.v0 = DNS,
.v0 = DNS,
.v0 = DNS,
},
.ssid = SSID,
.key = KEY,
.key_length = sizeof(KEY)-1,
.key_type = KEY_TYPE,
};
/* ---------------------------------------- Common variables --------------------------------------------------------------*/
static xSemaphoreHandle start_nfc;
static xSemaphoreHandle start_xbee;
static uint8_t nfc_id[20];
static uint8_t data[85];
/* ---------------------------------------- XBee variables/definitions ----------------------------------------------------*/
#define XBEE_CHANNEL 0x0E;
#define XBEE_PAN_ID 0x3322;
#define XBEE_DEST_ADDR_HIGH 0x0013a200
#define XBEE_DEST_ADDR_LOW 0x40927FC2
static tsmart_xbee_dm_config_device_t xbee_dm_config_device;
static uint8_t xbee_buffer[100];
/* ---------------------------------------- Cumulocity variables/definitions ----------------------------------------------*/
/* Account details */
#define TENANT_ACCOUNT "<SET YOUR TENANT>\0"
#define USER_ACCOUNT "<SET YPUR USER>\0"
#define PASSWORD_ACCOUNT "<SET YOUR PASSWORD>\0"
#define API_KEY_ACCOUNT "<SET YOUR API-KEY>\0"
/* Device parameters */
#define DEVICE_NAME "TSMART001\0"
#define DEVICE_TYPE "NFC_WIFI_XBEE\0"
#define HW_MODEL "TSmoTe\0"
#define HW_REVISION "2.2\0"
#define HW_SERIALNUMBER "00051\0"
static uint8_t details_account_base64encoded[100];
static tsmart_cumulocity_buffer_t cumulocity_buffer1;
static tsmart_cumulocity_buffer_t cumulocity_buffer2;
/* Account details */
static tsmart_cumulocity_account_t account = {
/* Account */
.tenant = TENANT_ACCOUNT,
.username = USER_ACCOUNT,
.password = PASSWORD_ACCOUNT,
.api_key = API_KEY_ACCOUNT,
.authorization = details_account_base64encoded,
};
/* Device description */
static tsmart_cumulocity_device_t device = {
.name = DEVICE_NAME,
.type = DEVICE_TYPE,
.model = HW_MODEL,
.serial_number = HW_SERIALNUMBER,
.revision = HW_REVISION,
};
/* Sensor structure */
static tsmart_cumulocity_sensor_t sensors[2] = {
{ /* NFC ID */
.name = "NFC ID\0",
.unit = "",
.value.str_value = {
.value_type_id = TSMART_CUMULOCITY_STRING,
.value = nfc_id,
}
},{
/* User data */
.name = "USER_DATA\0",
.unit = "",
.value.str_value = {
.value_type_id = TSMART_CUMULOCITY_STRING,
.value = data,
}
}
};
/* ---------------------------------------- NFC variables -----------------------------------------------------------------*/
static uint8_t key[] = {0xff,0xff,0xff,0xff,0xff,0xff}; /* Tag key */
/* ---------------------------------------- Application tasks -------------------------------------------------------------*/
/* @brief CM task.
*
* This task starts and congirues the wifi interface. After that,
* it configures the XBee device and the NFC reader. Finally it
* registers a new device in cumulocity middleware with two
* datastreams called "NFC_ID" and "USER_DATA". When everything
* is OK, it sends the start conditions to the other tasks.
*
* @param pvParameters
*/
void vCM(void *pvParameters){
/* Variables */
static uint8_t xbee_message_device_id[40];
/* Set Time */
time.year = 2013;
time.month = 8;
time.day = 16;
time.hour = 10;
time.minute = 54;
time.second = 5;
/* Set the time */
/* Start WIFI module */
if(TSMART_CUMULOCITY_WIFI_Start() != TSMART_PASS){
if(TSMART_CUMULOCITY_WIFI_Start() != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
}
/* Connect to network access point */
if(TSMART_CUMULOCITY_WIFI_ConnectToAP(&tsmart_cumulocity_wifi_wlan) != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Read IP */
if( TSMART_CUMULOCITY_ReadIP(&ipv4) != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Set default configuration */
TSMART_XBEE_DM_InitStruct(&xbee_dm_config_device);
/* Specific configuration for this application, not all parameter are
* interesting for this applications
*/
xbee_dm_config_device.kind_dev = TSMART_XBEE_DM_ROUTER;
xbee_dm_config_device.channel = XBEE_CHANNEL;
xbee_dm_config_device.pan_id = XBEE_PAN_ID;
xbee_dm_config_device.dest_addr_h = XBEE_DEST_ADDR_HIGH;
xbee_dm_config_device.dest_addr_l = XBEE_DEST_ADDR_LOW;
if (TSMART_XBEE_DM_Config(&tsmart_xbee_dm, &xbee_dm_config_device) != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Enter in Command mode and change operation mode to transparent mode */
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Send AT command: AP */
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, "ATAP 0\r", strlen("ATAP 0\r"), 1000/portTICK_RATE_MS);
TSMART_XBEE_DM_Receive(&tsmart_xbee_dm, xbee_buffer, 0, 3500/portTICK_RATE_MS);
/* Apply Changes */
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, "ATAC\r", strlen("ATAC\r"), 1000/portTICK_RATE_MS);
TSMART_XBEE_DM_Receive(&tsmart_xbee_dm, xbee_buffer, 0, 3500/portTICK_RATE_MS);
/* Exit in command mode and change operation mode to tranparent mode */
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
vTaskDelay(1000/portTICK_RATE_MS);
/* Enter to receive data in transparent mode */
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Wake up the NFC device */
TSMART_NFC_WakeUp(&tsmart_nfc, 100/portTICK_RATE_MS);
/* Configure device */
if( TSMART_NFC_SamConfiguration(&tsmart_nfc, TSMART_NFC_API_NORMAL_MODE, 0, TSMART_NFC_DISABLE, 100/portTICK_RATE_MS) != TSMART_NFC_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Encoder credentials */
TSMART_CUMULOCITY_Base64Encoder(&account, &cumulocity_buffer1, details_account_base64encoded);
/* Register device */
if(TSMART_CUMULOCITY_RegiserDevice(&account, &device, &cumulocity_buffer1) != TSMART_PASS){
for(;;);
}
/* Register mesurements */
if(TSMART_CUMULOCITY_RegisterMesurment(&account, device.id, sensors, 2, &cumulocity_buffer1) != TSMART_PASS){
for(;;);
}
/* Send device ID to the remote XBee */
sprintf(xbee_message_device_id, "Device registered - %d\r\n", device.id);
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, xbee_message_device_id, strlen(xbee_message_device_id), 1000/portTICK_RATE_MS);
/* Send Start conditions */
xSemaphoreGive(start_nfc);
xSemaphoreGive(start_xbee);
/* Autodelete task */
vTaskDelete(NULL);
}
/* @brief CM NFC task.
*
* This task sends the NFC card identifier to cumulocity when a NFC reader
* detetecs a NFC card.
*
* @param pvParameters
*/
void vCM_NFC(void *pvParameters){
/* Variables */
int8_t founded_nfc_card;
/* Wait for start condition */
xSemaphoreTake(start_nfc, portMAX_DELAY);
/* Send init OK message */
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, "NFC OK ", strlen("NFC OK "), 1000/portTICK_RATE_MS);
while(1){
do{
/* Check NFC card */
founded_nfc_card = TSMART_NFC_GetId(&tsmart_nfc, &id, 100/portTICK_RATE_MS);
vTaskDelay(500/portTICK_RATE_MS);
}while(founded_nfc_card != 1);
/* Select NFC card */
if( TSMART_NFC_InSelect(&tsmart_nfc, id.tg, 100/portTICK_RATE_MS) == TSMART_NFC_PASS){
/* Print id in data variable */
sprintf(nfc_id, "%x%x%x%x", id.id[0],id.id[1],id.id[2],id.id[3]);
/* Get Time */
sensors[0].time.year = time.year;
sensors[0].time.month = time.month;
sensors[0].time.day = time.day;
sensors[0].time.hour = time.hour;
sensors[0].time.minute = time.minute;
sensors[0].time.second = time.second;
/* Send NFC id */
TSMART_CUMULOCITY_SendMeasurement(&account, device.id, "NFC\0", &sensors[0], 1, &cumulocity_buffer1);
}
}
}
/* @brief CM_XBEE task.
*
* This task is waiting for incomming XBee data.
* When it receives a new data, they are sent to Cumulocity.
*
* @param pvParameters
*/
void vCM_XBEE(void *pvParameters){
/* Variables */
uint8_t i;
/* Wait for start condition */
xSemaphoreTake(start_xbee, portMAX_DELAY);
/* Send init OK message */
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, "XBee OK ", strlen("XBee OK "), 1000/portTICK_RATE_MS);
for(;;){
/* Reset buffers */
memset(xbee_buffer, 0x00, sizeof(xbee_buffer));
memset(data, 0x00, sizeof(data));
i = 0;
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, "Enter data and press \"enter\" to send(max. 80 bytes): ", strlen("Enter data and press \"enter\" to send(max. 80 bytes): "), 1000/portTICK_RATE_MS);
/* Wait for new data */
do{
TSMART_XBEE_DM_Receive(&tsmart_xbee_dm, &xbee_buffer[i], 0, portMAX_DELAY);
i++;
}while((xbee_buffer[i-1] != 0x0d) && (i < 80));
/* Print User data */
memcpy(data, xbee_buffer, i-1);
/* Get Time */
sensors[1].time.year = time.year;
sensors[1].time.month = time.month;
sensors[1].time.day = time.day;
sensors[1].time.hour = time.hour;
sensors[1].time.minute = time.minute;
sensors[1].time.second = time.second;
/* Send User Data */
TSMART_CUMULOCITY_SendMeasurement(&account, device.id, "XBEE\0", &sensors[1], 1, &cumulocity_buffer2);
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, "Your data was sent to Cumulocity. ", strlen("Your data was sent to Cumulocity. "), 1000/portTICK_RATE_MS);
}
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
static tsmart_uart_config_t tsmart_uart_config;
static tsmart_xbee_dm_config_t xbee_dm_config;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* Initialize Real-time clock */
/* Initialize WIFI interface for Cumulocity */
if (TSMART_CUMULOCITY_WIFI_Init() != TSMART_PASS){
return TSMART_FAIL;
}
/* Initialize NFC */
/* Initilize XBee */
xbee_dm_config.baud_rate = 9600;
xbee_dm_config.priority = 14;
if (TSMART_XBEE_DM_Init(&tsmart_xbee_dm, &xbee_dm_config) != TSMART_PASS){
return TSMART_FAIL;
}
/* Exit XBee from sleep mode */
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create mutex for NFC */
start_nfc = xSemaphoreCreateMutex();
if (start_nfc == NULL){
return TSMART_FAIL;
}
xSemaphoreTake(start_nfc, portMAX_DELAY);
/* Create mutex for XBee */
start_xbee = xSemaphoreCreateMutex();
if (start_xbee == NULL){
return TSMART_FAIL;
}
xSemaphoreTake(start_xbee, portMAX_DELAY);
/* Create Cumulocity task */
if(xTaskCreate(vCM, "CM", 1024, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create Cumulocity-NFC task */
if(xTaskCreate(vCM_NFC, "CM_NFC", 1024, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create Cumulocity-XBee task */
if(xTaskCreate(vCM_XBEE, "CM_XBEE", 512, NULL, 6, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}