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

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

/* @file carriots_2g.c
* @author TST
* @version 0.1
* @date 22/03/2013
* @brief This is an example of how to send/receive data from Carriots middleware
* using the cellular 2g interface.
*
*
* This example shows the user how to manage the main function of the Carriots API
* for TSmarT platforms: create a device, create a trigger, create an alarm,
* create dataStreams and receive data form Carriots.
*
* To run this example is necessary to be previously registered in the Carriots middleware.
* https://www.carriots.com/
*
* Once you have access to Carriots, users shall set the following definitions located below.
* - User
* - API key
* - Project
* - Service
* - Group
*
* 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.
* User shall set them in the definitions below.
*/
/*-------------------------------------------------------- System headers -------------------------------------------------*/
#include "tsmart.h"
/*-------------------------------------------------------- Carriots Parameters --------------------------------------------*/
#define TSMART_CARRIOTS_USER "<SET USER>"
#define TSMART_CARRIOTS_API_KEY "<SET CARRIOTS API-KEY>"
#define TSMART_CARRIOTS_PROJECT_NAME "<SET PROJECT NAME>"
#define TSMART_CARRIOTS_SERVICE_NAME "<SET SERVICE NAME>"
#define TSMART_CARRIOTS_GROUP_NAME "<SET GROUP NAME>"
#define TSMART_CARRIOTS_DEVICE_NAME "<SET DEVICE NAME>"
#define TSMART_CARRIOTS_TRIGGER_NAME "trigger"
#define TIME_ZONE_EU_MADRID "Europe/Madrid"
/*-------------------------------------------------------- Sensors --------------------------------------------------------*/
#define TSMART_CARRIOTS_TEMPERATURE "temperature"
#define TSMART_CARRIOTS_HUMIDITY "humidity"
/*-------------------------------------------------------- Trigger/Alarms -------------------------------------------------*/
#define TSMART_CARRIOTS_TRIGGER_MESSAGE "Current temperature=%%%%%%temperature%%%%%% TSmarT"
#define TSMART_CARRIOTS_ALARM_NAME "TSmarT_Alarm"
#define TSMART_CARRIOTS_ALARM_DESCRIPTION "Trigger working"
/*-------------------------------------------------------- Trigger message ------------------------------------------------*/
#define LOCAL_SERVER_PORT 1236
/*-------------------------------------------------------- Cellular 2G network parameters ---------------------------------*/
/* This is the specific configuration for cellullar 2g, Set them */
/* Set SIM configuration */
#define PIN_CODE "<SET PIN CODE>"
#define APN "<SET APN>"
#define LOGIN "<SET LOGIN>"
#define PASSWORD "<SET PASSWORD>"
/* ****************************************************** */
/* Interface variables */
/* ****************************************************** */
static tsmart_carriots_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_carriots_cellular_2g_network_t cellular_2g_network= {
.apn = APN,
.login = LOGIN,
.password = PASSWORD,
.time_to_connect = 1200,
};
/*--------------------------------------------------------Carriots variables-----------------------------------------------*/
static tsmart_carriots_data_t data[2];
static uint8_t url_string[18];
/*-------------------------------------------------------- Applications Task ----------------------------------------------*/
/* @brief CARRIOTS task.
*
* This task Start the cellular 2g interface, turning
* on and setting it. After that, it creates a device
* and a trigger in Carriots middleware. Once this is
* done, it sends the sensor values to Carriots
* middleware.
*
* @param pvParameters
*/
void vCARRIOTS(void *pvParameters){
/* Variables */
static tsmart_carriots_buffer_t carriots_buffer;
int16_t code;
/* Account parameters */
sprintf(account.api_key,TSMART_CARRIOTS_API_KEY);
sprintf(account.user, TSMART_CARRIOTS_USER);
sprintf(account.project,TSMART_CARRIOTS_PROJECT_NAME);
sprintf(account.service,TSMART_CARRIOTS_SERVICE_NAME);
sprintf(account.group, TSMART_CARRIOTS_GROUP_NAME);
sprintf(account.device, TSMART_CARRIOTS_DEVICE_NAME);
/* Start module 2G */
if(TSMART_CARRIOTS_CELLULAR_2G_Start(&cellular_config) != TSMART_PASS){
if(TSMART_CARRIOTS_CELLULAR_2G_Start(&cellular_config) != TSMART_PASS){
for(;;);
}
}
/* Attach to cellular 2g network */
if(TSMART_CARRIOTS_CELLULAR_2G_NetworkAttachment(&cellular_2g_network) != TSMART_PASS){
for(;;);
}
/* Start server */
if(TSMART_CARRIOTS_StartServer(LOCAL_SERVER_PORT) != TSMART_PASS){
for(;;);
}
/* Read IP */
/* Create URL */
sprintf(url_string, "%d.%d.%d.%d", ipv4.v0, ipv4.v1, ipv4.v2, ipv4.v3);
/* Device parameters */
sprintf(device.time_zone, TIME_ZONE_EU_MADRID);
device.frequency_stream = 1440;
device.frequency_status = 1440;
device.enabled = 1;
/* Create a new device */
code = TSMART_CARRIOTS_CreateDevice(&account, &device, &carriots_buffer);
if(code != 201){
for(;;);
}
/* Create trigger */
trigger.max_retries = 10;
trigger.push_frequency = 1;
trigger.port = LOCAL_SERVER_PORT;
trigger.external_config.url = url_string;
trigger.enabled = 1;
sprintf(trigger.external_config.verb, "POST");
sprintf(trigger.external_config.payload, TSMART_CARRIOTS_TRIGGER_MESSAGE);
trigger.external_config.add_header = NULL;
code = TSMART_CARRIOTS_CreateTrigger(&account, TSMART_CARRIOTS_TRIGGER_NAME, &trigger, &carriots_buffer);
if(code != 201){
for(;;);
}
/* Temperature */
sprintf(data[0].id, TSMART_CARRIOTS_TEMPERATURE);
data[0].value.integer_value.value = 19;
/* Humidity */
sprintf(data[1].id, TSMART_CARRIOTS_HUMIDITY);
data[1].value.double_value.value = 40.38;
/* Send data */
code = TSMART_CARRIOTS_CreateDataStream(&account, data, 2, &carriots_buffer);
if(code != 200){
for(;;);
}
/* Wait forever */
vTaskDelay(portMAX_DELAY);
}
/* @brief Notification task.
*
* This task is waiting for notificacions. If the data received match with a
* trigger notification, an alarm will be created in Carriots.
*
* @param pvParameters
*/
void vCARRIOTS_NOTIF(void *pvParameters){
/* Variables */
static tsmart_carriots_buffer_t buffer_notif;
static tsmart_carriots_notification_t notification;
int16_t code;
for(;;){
/* Wait for notification */
TSMART_CARRIOTS_Notifications(&notification, &buffer_notif);
switch(notification.notif.notif_type){
/* Create Alarm */
sprintf(alarm.description, TSMART_CARRIOTS_ALARM_DESCRIPTION);
alarm.severity = 1;
alarm.enabled = 1;
code = TSMART_CARRIOTS_CreateAlarm(&account, TSMART_CARRIOTS_ALARM_NAME, &alarm, &buffer_notif);
if(code != 201){
for(;;);
}
break;
/* Parse cellular notification */
switch(notification.notification_2g.code){
/* GPRS network failure:de-attach form GPRS network */
for(;;);
}
break;
/* Stop module */
/* Start module */
if(TSMART_CARRIOTS_CELLULAR_2G_Start(&cellular_config) != TSMART_PASS){
for(;;);
}
break;
default:
break;
}
/* Attach to network */
if(TSMART_CARRIOTS_CELLULAR_2G_NetworkAttachment(&cellular_2g_network) != TSMART_PASS){
for(;;);
}
/* Re-start Server parameters */
if( TSMART_CARRIOTS_StartServer(LOCAL_SERVER_PORT) != TSMART_PASS){
for(;;);
}
/* Read new IP */
if(TSMART_CARRIOTS_ReadIP(&ipv4) != TSMART_PASS){
for(;;);
}
/* Create URL */
sprintf(url_string, "%d.%d.%d.%d", ipv4.v0, ipv4.v1, ipv4.v2, ipv4.v3);
/* Update trigger: new IP */
trigger.external_config.url = url_string;
code = TSMART_CARRIOTS_UpdateTrigger(&account, TSMART_CARRIOTS_TRIGGER_NAME, &trigger, &buffer_notif);
if(code != 200){
for(;;);
}
break;
default:
break;
}
}
}
/*
* @brief init() function
*
* This is the first thing that the user must do for using TSmarT.
* It initializes the specific hardware resources (CELLULAR, 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 resources */
/* ************************************************************************* */
/* Initialize cellular 2G interface for Carriots */
if (TSMART_CARRIOTS_CELLULAR_2G_Init() != TSMART_PASS){
return TSMART_FAIL;
}
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create carriots task */
if(xTaskCreate(vCARRIOTS, "CARRIOTS", 2048, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create notification task */
if(xTaskCreate(vCARRIOTS_NOTIF, "CARRIOTS_NOTIF", 1024, NULL, 8, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}