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

This example shows the capabilities of the Carriots middleware using the wifi interface.

/* @file carriots_wifi.c
* @author TST
* @version 0.1
* @date 03/05/2013
* @brief This is an example of how to send/receive data from Carriots middleware
* using the wifi interface.
*
* This example shows the user how to manage the main functions 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(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 This example is enabled to work in a LAN network. Then it's necessary to known and set
* the public IP in the definitions below. Carrtiots will send the trigger message to a public IP.
* In addition the server port will be open in your router device.
*
*
*/
/*-------------------------------------------------------- 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%%%%%% wifi"
#define TSMART_CARRIOTS_ALARM_NAME "wifi_Alarm"
#define TSMART_CARRIOTS_ALARM_DESCRIPTION "Trigger working"
/*-------------------------------------------------------- Trigger message ------------------------------------------------*/
#define LOCAL_SERVER_PORT 1237
/*-------------------------------------------------------- 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 SSID>"
/* Network key */
#define KEY "<SET NETWORK KEY>"
/* Type network key: select one of them */
#define KEY_TYPE TSMART_CARRIOTS_WIFI_UNSEC
//#define KEY_TYPE TSMART_CARRIOTS_WIFI_WEP
//#define KEY_TYPE TSMART_CARRIOTS_WIFI_WPA
//#define KEY_TYPE TSMART_CARRIOTS_WIFI_WPA2
/* Set Public IP address */
#define PUBLIC_IP0 0
#define PUBLIC_IP1 0
#define PUBLIC_IP2 0
#define PUBLIC_IP3 0
/*-------------------------------------------------------- Interface variables --------------------------------------------*/
static tsmart_carriots_wifi_wlan_t tsmart_carriots_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,
};
/*-------------------------------------------------------- Carriots variables ---------------------------------------------*/
static tsmart_carriots_data_t data[2];
static uint8_t url_string[18];
/* Task synchronization */
xQueueHandle application_mutex;
/*-------------------------------------------------------- Application tasks -----------------------------------------------*/
/* @brief CARRIOTS task.
*
* This task Start the wifi 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;
uint8_t i = 0;
/* 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 wifi interface */
if(TSMART_CARRIOTS_WIFI_Start() != TSMART_PASS){
if(TSMART_CARRIOTS_WIFI_Start() != TSMART_PASS){
for(;;);
}
}
/* Connect to the network access point */
if(TSMART_CARRIOTS_WIFI_ConnectToAP(&tsmart_carriots_wifi_wlan) != TSMART_PASS){
for(;;);
}
/* Start server */
if(TSMART_CARRIOTS_StartServer(LOCAL_SERVER_PORT) != TSMART_PASS){
for(;;);
}
/* Read IP */
/* Set public IP address */
ipv4.v0 = PUBLIC_IP0;
ipv4.v1 = PUBLIC_IP1;
ipv4.v2 = PUBLIC_IP2;
ipv4.v3 = PUBLIC_IP3;
/* Create URL */
sprintf(url_string, "%d.%d.%d.%d", ipv4.v0, ipv4.v1, ipv4.v2, ipv4.v3);
/* Task synchronization */
xQueueSend(application_mutex, &i, 0);
/* 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 datastream */
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;
uint8_t v;
/* Task synchronization */
xQueueReceive(application_mutex, &v, portMAX_DELAY);
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;
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 wifi interface for Carriots */
if (TSMART_CARRIOTS_WIFI_Init() != TSMART_PASS){
return TSMART_FAIL;
}
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Task synchronization */
application_mutex = xQueueCreate(1, sizeof(uint8_t));
if (application_mutex == NULL){
return TSMART_FAIL;
}
/* 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;
}