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

Example showing the capabilities of the Xively middleware using the cellular_2g interface.

/* @file xv_2g.c
* @author TST
* @version 0.1
* @date 12/03/2013
* @brief This is an example of how to send/recive data from Xively middleware
* using the cellular 2g interface.
*
* This example shows the user how to manage the main functions of the Xively API
* for TSmarT platforms: creating a new product, a new device and activating it,
* creating, updating and deleting a trigger and writing/reading datastreams.
*
* 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 variable located below.
*
*
* The interface selection is really done during compiling process. So you shall select
* the adequate interface for the application(CELLULAR_2G 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 network parameters:
* - PIN code -------> Personal identification number.
* - APN ------------> Access Point Name.
* - Login ----------> Network user name.
* - Password -------> Network password.
*
* @note This application uses the debug UART to display debug information like ip address asigned.
* According to use it, users should set the next configuration in their serial communication program:
* - Baud rate -----> 115200
* - Word length ---> 8 bits
* - Parity --------> none
* - Stop bit ------> 1
* - Flow control --> none
*
*/
/*-------------------------------------------------------- System headers -------------------------------------------------*/
#include "tsmart.h"
/*-------------------------------------------------------- Debug parameters -----------------------------------------------*/
#define DEBUG_UART tsmart_uart2
#define APP_RX_BUFFER_SZ 200
static uint8_t app_rx_buf[APP_RX_BUFFER_SZ];
static uint8_t debug_buffer[APP_RX_BUFFER_SZ];
/*-------------------------------------------------------- Port server ----------------------------------------------------*/
#define LOCAL_SERVER_PORT 1236
/*-------------------------------------------------------- Cellular 2G network parameters ---------------------------------*/
#define PIN_CODE "<SET PIN CODE>"
#define APN "<SET APN>"
#define LOGIN "<SET LOGIN>"
#define PASSWORD "<SET PASSWORD>"
static tsmart_xv_cellular_2g_config_t cellular_config = {
.pin_code = PIN_CODE,
.ip_ttl = 64,
.ip_frag_timeo = 60,
.tcp_maxinitwin = 0,
.tcp_rexmt_max = 64,
.tcp_rexmt_maxcnt = 12
};
static tsmart_xv_cellular_2g_network_t cellular_2g_network= {
.apn = APN,
.login = LOGIN,
.password = PASSWORD,
.time_to_connect = 1200
};
/*-------------------------------------------------------- Xively definitions ---------------------------------------------*/
#define PRODUCT_NAME "TSmeTer\0"
#define PRODUCT_DESCRIPTION "This product can get several mesures from the different sensors.\0"
#define PRODUCT_TITLE "Electric monitoring\0"
#define PRODUCT_TYPE "TSMART\0"
#define PRODUCT_PRIVATE "true\0"
#define PRODUCT_USER "TST\0"
#define API_KEY "<SET YOUR XIVELY APY-KEY>"
/*-------------------------------------------------------- Xively Parameters ----------------------------------------------*/
static double temperature = 0.0;
static int32_t acc_y = 0;
static int32_t acc_x = 0;
static int32_t humidity = 0;
static int32_t feed_id;
static uint8_t serial_number[] = "tst000001";
static uint8_t status[10] = "Active";
static uint8_t product_id[25];
static uint8_t device_api_key[60];
static uint8_t activation_code[50];
/*-------------------------------------------------------- DataStreams ----------------------------------------------------*/
static tsmart_xv_datastream_t datastreams[5] = {
{
.id = "humidity",
.value.integer_value = {
.value_type_id = TSMART_XV_INTEGER,
.value = &humidity,
}
},{
.id = "temperature",
.value.double_value = {
.value_type_id = TSMART_XV_DOUBLE,
.value = &temperature,
}
},{
.id = "status",
.value.str_value = {
.value_type_id = TSMART_XV_STRING,
.value = status,
}
},{
.id = "acceleration_x",
.value.integer_value = {
.value_type_id = TSMART_XV_INTEGER,
.value = &acc_x,
}
},{
.id = "acceleration_y",
.value.integer_value = {
.value_type_id = TSMART_XV_INTEGER,
.value = &acc_y,
}
}
};
/*-------------------------------------------------------- Product --------------------------------------------------------*/
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 = 6,
.user = PRODUCT_USER,
};
/*-------------------------------------------------------- Location board -------------------------------------------------*/
static tsmart_xv_location_t location = {
.disposition = "mobile",
.name = "Monuriki Island\0",
.exposure = "outdoor\0",
.domain = "physical\0",
.elevation = 20,
.latitude = 43.453569,
.longitude = -3.869719,
};
/*-------------------------------------------------------- Read DataStreams -----------------------------------------------*/
static int32_t humidity_r;
static int32_t humidity_max_r;
static int32_t humidity_min_r;
static tsmart_xv_datastream_read_t datastream_read[4] = {
{ /* Read humidity: integer */
.value = &humidity_r
},
.max_value.integer_value = {
.value_type_id = TSMART_XV_INTEGER,
.value = &humidity_max_r
},
.min_value.integer_value = {
.value_type_id = TSMART_XV_INTEGER,
.value = &humidity_min_r
}
}
};
/*-------------------------------------------------------- Tigger ID ------------------------------------------------------*/
int32_t trigger_id;
/*-------------------------------------------------------- Appliction Variables -------------------------------------------*/
xQueueHandle application_mutex;
/*-------------------------------------------------------- Application tasks -----------------------------------------------*/
/* @brief XV task.
*
* This task starts the cellular 2g interface, turning on and setting the cellular 2g
* interface. After that, it creates a new product and a new device.
* A trigger is created with the next condition:
* temperature value < 25
* In addtion a writing and reading datastream operation will be done.
* When all these operations are done the task waits one minute for
* deleting the device and the product created.
*
*
* @param pvParameters
*/
void vXV(void *pvParameters){
/* Variables */
static tsmart_xv_buffer_t xv_buffer;
int32_t trigger_value = 25;
uint8_t i = 0;
TSMART_UART_Send(&DEBUG_UART, "---> Xively-cellular 2g example <---\r\n", strlen("---> Xively-cellular 2g example <---\r\n"), 1000/portTICK_RATE_MS);
/* Start module 2G */
if(TSMART_XV_CELLULAR_2G_Start(&cellular_config) != TSMART_PASS){
if(TSMART_XV_CELLULAR_2G_Start(&cellular_config) != TSMART_PASS){
for(;;);
}
}
/* Attach to network */
if(TSMART_XV_CELLULAR_2G_NetworkAttachment(&cellular_2g_network) != TSMART_PASS){
for(;;);
}
/* Start Server parameters */
if( TSMART_XV_StartServer(LOCAL_SERVER_PORT) != TSMART_PASS){
for(;;);
}
/* Read IP */
if( TSMART_XV_ReadIP(&ipv4) != TSMART_PASS){
for(;;);
}
sprintf(debug_buffer, "Public IP: %d.%d.%d.%d\r\n", ipv4.v0, ipv4.v1, ipv4.v2, ipv4.v3);
TSMART_UART_Send(&tsmart_uart2, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Task synchronization */
xQueueSend(application_mutex, &i, 0);
/* Create product */
if(TSMART_XV_CreateProduct(API_KEY,product_id, &product, &xv_buffer) != TSMART_PASS){
for(;;);
}
sprintf(debug_buffer, "Product id: %s\r\n", product_id);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Create device */
if(TSMART_XV_CreateDevice(API_KEY, product_id, serial_number, &xv_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "Device created\r\n", strlen("Device created\r\n"), 1000/portTICK_RATE_MS);
/* Get activation code */
if(TSMART_XV_GetActivationCode(API_KEY, product_id, serial_number, activation_code, &xv_buffer) != TSMART_PASS){
for(;;);
}
sprintf(debug_buffer, "Activation code id: %s\r\n", activation_code);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Activate device */
feed_id = TSMART_XV_ActivateDevice(API_KEY, activation_code, device_api_key, &xv_buffer);
if(feed_id < 0){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "Device Activated\r\n", strlen("Device Activated\r\n"), 1000/portTICK_RATE_MS);
/* Set location */
if(TSMART_XV_SetDeviceLocation(device_api_key, feed_id, &location, &xv_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "Set location\r\n", strlen("Set location\r\n"), 1000/portTICK_RATE_MS);
/* Create trigger */
trigger.ip = &ipv4;
trigger.port = LOCAL_SERVER_PORT;
trigger.feed_id = feed_id;
trigger.threshold_value.integer_value.value = &trigger_value;
trigger.datastream_id = "humidity";
trigger_id = TSMART_XV_CreateTrigger(API_KEY, &trigger, &xv_buffer);
if(trigger_id < 0){
for(;;);
}
sprintf(debug_buffer, "Trigger created with ID: %d\r\n", trigger_id);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Set values to write */
acc_y = 100;
acc_x = 101;
humidity = 102;
temperature = 103.5;
sprintf(status, "enable");
/* Write DataStreams */
if(TSMART_XV_WriteDataStream(device_api_key, feed_id, datastreams, 5, &xv_buffer) != TSMART_PASS){
for(;;);
}
sprintf(debug_buffer, "Datastream written\r\n", feed_id);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Get Data */
TSMART_XV_ReadDataStream(device_api_key, feed_id, "humidity", TSMART_XV_INTEGER, &datastream_read[0], &xv_buffer);
sprintf(debug_buffer, "humidity ------------> %d %d %d\r\n",
*datastream_read[0].current_value.integer_value.value,
*datastream_read[0].min_value.integer_value.value,
*datastream_read[0].max_value.integer_value.value);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
sprintf(debug_buffer, "Data from datastream \"temperature\":\r\n"
"- current value: %d\r\n"
"- max value: %d\r\n"
"- min value: %d\r\n"
"- at: %dh:%dmin:%dsec of %d/%d/%d\r\n",
*datastream_read[0].current_value.integer_value.value,
*datastream_read[0].min_value.integer_value.value,
*datastream_read[0].max_value.integer_value.value,
datastream_read[0].time.hour,
datastream_read[0].time.min,
datastream_read[0].time.sec,
datastream_read[0].time.day,
datastream_read[0].time.month,
datastream_read[0].time.year);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* After 1 minutes the feed will be deleted */
vTaskDelay(60000/portTICK_RATE_MS);
/* Delete device */
if(TSMART_XV_DeleteDevice(API_KEY, product_id, serial_number, &xv_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "Device deleted\r\n", strlen("Device deleted\r\n"), 1000/portTICK_RATE_MS);
/* Delete product */
if(TSMART_XV_DeleteProduct(API_KEY, product_id, &xv_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "Product deleted\r\n", strlen("Product deleted\r\n"), 1000/portTICK_RATE_MS);
/* Wait forever */
vTaskDelay(portMAX_DELAY);
}
/* @brief Notifications task.
*
* This task is waiting for notificacions. If the data received match with a
* trigger notification, the trigger ID is correct and the operator condition
* is "less to". The trigger will be updated with the condition:
* "temperature == 20"
* Otherwise the trigger will be remove.
*
* @param pvParameters
*/
void vXV_NOTIF(void *pvParameters){
/* Variables */
static tsmart_xv_buffer_t xv_notif_buffer;
int32_t trigger_value = 20;
uint8_t v;
/* Task synchronization */
xQueueReceive(application_mutex, &v, portMAX_DELAY);
for(;;){
/* Wait for new notification */
TSMART_XV_ReceiveNotification(&notif, &xv_notif_buffer);
switch(notif.notif_type.notif_type){
if(notif.trigger_alarm.trigger_id == trigger_id){
/* Delete trigger */
if(TSMART_XV_DeleteTrigger(API_KEY, trigger_id, &xv_notif_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "TRIGGER-----> Deleted\r\n", strlen("TRIGGER-----> Deleted\r\n"), 1000/portTICK_RATE_MS);
}else{
/* Update trigger---> integer */
trigger.ip = &ipv4;
trigger.port = LOCAL_SERVER_PORT;
trigger.threshold_value.integer_value.value = &trigger_value;
trigger.datastream_id = "temperature";
trigger.feed_id = feed_id;
if(TSMART_XV_UpdateTrigger (API_KEY, trigger_id, &trigger, &xv_notif_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "TRIGGER-----> Updated\r\n", strlen("TRIGGER-----> Updated\r\n"), 1000/portTICK_RATE_MS);
}
}/* end if trigger_id */
}/* end if notif_type */
break;
TSMART_UART_Send(&DEBUG_UART, "\tCellular2G notification\r\n", strlen("\tCellular2G notification\r\n"), 1000/portTICK_RATE_MS);
/* Parse cellular notification */
switch(notif.notification_2g.code){
/* GPRS network failure:de-attach form GPRS network */
for(;;);
}
break;
/* Stop module */
/* Start module */
if(TSMART_XV_CELLULAR_2G_Start(&cellular_config) != TSMART_PASS){
for(;;);
}
break;
default:
break;
}
/* Attach to network */
if(TSMART_XV_CELLULAR_2G_NetworkAttachment(&cellular_2g_network) != TSMART_PASS){
for(;;);
}
/* Re-start Server parameters */
if( TSMART_XV_StartServer(LOCAL_SERVER_PORT) != TSMART_PASS){
for(;;);
}
/* Read new IP */
if(TSMART_XV_ReadIP(&ipv4) != TSMART_PASS){
for(;;);
}
/* Register New IP address updating all triggers */
/* Update trigger */
trigger.ip = &ipv4;
trigger.port = LOCAL_SERVER_PORT;
trigger.threshold_value.integer_value.value = &trigger_value;
trigger.datastream_id = "temperature";
trigger.feed_id = feed_id;
if(TSMART_XV_UpdateTrigger (API_KEY, trigger_id, &trigger, &xv_notif_buffer) != TSMART_PASS){
for(;;);
}
default:
break;
}/* end notif_type switch */
}/* end for */
}
/* @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() {
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize hardware resources */
/* ************************************************************************* */
/* Initialize cellular 2G interface for Xively */
if (TSMART_XV_CELLULAR_2G_Init() != TSMART_PASS){
return TSMART_FAIL;
}
static tsmart_uart_config_t tsmart_uart_config;
/* Initialize Debug UART */
tsmart_uart_config.baud_rate = 115200;
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
tsmart_uart_config.rx_buf_sz = APP_RX_BUFFER_SZ;
tsmart_uart_config.rx_buf = app_rx_buf;
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
TSMART_UART_Init(&DEBUG_UART, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Task synchronization */
application_mutex = xQueueCreate(1, sizeof(uint8_t));
if (application_mutex == NULL){
return TSMART_FAIL;
}
/* Create Xively task */
if(xTaskCreate(vXV, "XV", 1024, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create notification Xively task */
if(xTaskCreate(vXV_NOTIF, "XV_NOTIF", 512, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}