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

Example showing the capabilities of the Xively middleware using the wifi interface with NFC and XBee technologies.

/* @file xv_nfc_wifi_xbee.c
* @author TST
* @version 0.1
* @date 31/07/2013
* @brief This is an example of how to use the TSmarT API to send data to Xively middleware
* using the wifi interface.
*
* The example application shows the user how to create a product and a device in Xively middleware which it is able to save information
* provided by a NFC reader and data received from a remote XBee.
*
* It is based on three task:
* - XV task --------> It's the main task because it configures the WIFI communication interface, the NFC reader and the
* XBee device.
* In addition, it creates a new product and a new device in Xively with two datastreams:
* - Id_NFC_Card: it's the NFC card identifier.
* - XBee_Data: user can send to xively any data using a remote XBee.
*
* - XV_NFC task ----> It's constinuosly checking for the presence of a NFC card or not. If there is a NFC card, its ID
* card will be sent to Xively.
*
* - XV_XBee task ---> It's waiting for a new data from a remote XBee(Digimesh). When new data are received it sends
* those data to Xively. In addition it sends a confirmation message to the remote XBee.
*
* To run this example is necessary to be previously registered in the Xively middleware.
* https://xively.com/
*
* Once you have access to Xively, users shall set API key definition located below: API_KEY.
*
*
* 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 these XBee parameters in both XBee, local and remote:
*
* - 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"
/*-------------------------------------------------------- Wireless network parameters ------------------------------------*/
/* 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
/*-------------------------------------------------------- XBee parameters ------------------------------------------------*/
#define XBEE_CHANNEL 0x0E;
#define XBEE_PAN_ID 0x3322;
#define XBEE_DEST_ADDR_HIGH 0x0013a200
#define XBEE_DEST_ADDR_LOW 0x40927FC2
/*-------------------------------------------------------- Xively definitions ---------------------------------------------*/
#define PRODUCT_NAME "TSmarT\0"
#define PRODUCT_DESCRIPTION "This product can manage different technologies like XBee or NFC.\0"
#define PRODUCT_TITLE "NFC_WIFI_XBEE\0"
#define PRODUCT_TYPE "TSMART\0"
#define PRODUCT_PRIVATE "true\0"
#define PRODUCT_USER "TST\0"
#define API_KEY "<SET YOUR API KEY>"
/*-------------------------------------------------------- Wifi interface variables ---------------------------------------*/
static tsmart_xv_wifi_wlan_t tsmart_xv_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;
/*-------------------------------------------------------- NFC variables -------------------------------------------------*/
static uint8_t key[] = {0xff,0xff,0xff,0xff,0xff,0xff}; /* Tag key */
static uint8_t id_nfc_card[9];
/*-------------------------------------------------------- XBee variables ------------------------------------------------*/
static tsmart_xbee_dm_config_device_t xbee_dm_config_device;
static uint8_t xbee_buffer[100];
/*-------------------------------------------------------- Xively variables -----------------------------------------------*/
static uint32_t feed_id;
static uint8_t serial_number[] = "tst000001";
static uint8_t product_id[25];
static uint8_t device_api_key[60];
static uint8_t activation_code[50];
static tsmart_xv_buffer_t xv_buffer1;
static tsmart_xv_buffer_t xv_buffer2;
static tsmart_xv_datastream_t datastreams[2] = {
{
.id = "XBee_Data\0",
.value.str_value = {
.value_type_id = TSMART_XV_STRING,
.value = xbee_buffer,
}
},{
.id = "Id_NFC_Card\0",
.value.str_value = {
.value_type_id = TSMART_XV_STRING,
.value = id_nfc_card,
}
}
};
static tsmart_xv_product_t product = {
.name = PRODUCT_NAME,
.description = PRODUCT_DESCRIPTION,
.title = PRODUCT_TITLE,
.type = PRODUCT_TYPE,
.private = PRODUCT_PRIVATE,
.datastream = datastreams,
.n_datastreams = 2,
.user = PRODUCT_USER,
};
/*-------------------------------------------------------- Appliction Test ------------------------------------------------*/
/* @brief XV task.
*
* Starts the wifi interface, turning on and setting it.
* After that, it configure the XBee device and the NFC.
* After that, it creates a new product and a new device
* with two datastream called "Id_NFC_Card" and "XBee_Data".
* When everything is OK, it sends the start conditions to
* the other tasks.
*
* @param pvParameters
*/
void vXV(void *pvParameters){
/* Start WIFI module */
if(TSMART_XV_WIFI_Start() != TSMART_PASS){
if(TSMART_XV_WIFI_Start() != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
}
/* Attach to network */
if(TSMART_XV_WIFI_ConnectToAP(&tsmart_xv_wifi_wlan) != 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();
}
/* Create product */
if(TSMART_XV_CreateProduct(API_KEY, product_id, &product, &xv_buffer1) != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Create device */
if(TSMART_XV_CreateDevice(API_KEY, product_id, serial_number, &xv_buffer1) != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Get activation code */
if(TSMART_XV_GetActivationCode(API_KEY, product_id, serial_number, activation_code, &xv_buffer1) != TSMART_PASS){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Activate device */
feed_id = TSMART_XV_ActivateDevice(API_KEY, activation_code, device_api_key, &xv_buffer1);
if(feed_id < 0){
/* Bad state ==> Reboot system */
NVIC_SystemReset();
}
/* Send Start conditions */
xSemaphoreGive(start_nfc);
xSemaphoreGive(start_xbee);
/* Autodelete task */
vTaskDelete(NULL);
}
/* @brief XV NFC task.
*
* This task updates a datastream when a new NFC card is
* detected by the NFC reader.
*
* @param pvParameters
*/
void vXV_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){
/* Save ID NFC card */
sprintf(id_nfc_card, "%x%x%x%x\0", id.id[0],id.id[1],id.id[2],id.id[3]);
/* Write feed */
TSMART_XV_WriteDataStream(device_api_key, feed_id, &datastreams[1], 1, &xv_buffer1);
}
}
}
/* @brief XV_XBEE task.
*
* This task is waiting for incomming XBee data.
* When it receives a new data, they are sent to Xively.
*
* @param pvParameters
*/
void vXV_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));
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));
/* Remove invalid character for Xively: "\r" */
xbee_buffer[i-1] = 0x00;
/* Write feed */
TSMART_XV_WriteDataStream(device_api_key, feed_id, &datastreams[0], 1, &xv_buffer2);
TSMART_XBEE_DM_Send(&tsmart_xbee_dm, "Your data was sent to Xively. ", strlen("Your data was sent to Xively. "), 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 hardware resources */
/*---------------------------------------------------------------------------*/
/* Initialize WIFI interface for Xively */
if (TSMART_XV_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 Xively-NFC task */
if(xTaskCreate(vXV, "XV", 2048, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create Xively-NFC task */
if(xTaskCreate(vXV_NFC, "XV_NFC", 1024, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create Xively-XBee task */
if(xTaskCreate(vXV_XBEE, "XV_XBEE", 512, NULL, 6, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}