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

Example of how to communicate with Lhings middleware using the WIFI interface.

/* @file lhings_wifi.c
* @author TST
* @version 0.1
* @date 03/06/2014
* @brief This is an example of how to communicate with Lhings middleware
* using the WIFI interface.
*
* This example shows the user how to manage the functions of the Lhings API
* for TSmarT platforms:: register devices, send data, events or manage rules.
*
* To run this example is necessary to have an Lhings API-KEY. Once you have
* one, you shall fill out in the following definition: LHINGS_APIKEY.
*
* The Lhings API functions is able to work with different interfaces:
* - Cellular 2g
* - WIFI
*
* Users can manage these interfaces using the following functions for each
* interface to comunicate with Lhings(only one of them is supported at the
* same time):
* - Cellular 2G:
* - TSMART_LHINGS_WIFI_Init();
* - TSMART_LHINGS_WIFI_DisconnectfromAP();
* - TSMART_LHINGS_WIFI_ConnectToAP();
* - TSMART_LHINGS_WIFI_Stop();
* - TSMART_LHINGS_WIFI_Start();
* - WIFI:
* - TSMART_LHINGS_CELLULAR_2G_Init();
* - TSMART_LHINGS_CELLULAR_2G_NetworkDeAtachment();
* - TSMART_LHINGS_CELLULAR_2G_NetworkAttachment();
* - TSMART_LHINGS_CELLULAR_2G_Stop();
* - TSMART_LHINGS_CELLULAR_2G_Start();
*
*
* @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 (IP_ADDR, NET_MASK, IP_GW, DNS, SSID, KEY, KEY_TYPE).
*
*
* @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
*
*/
#include "tsmart.h"
/*-------------------------------------------------------- Debug parameters -----------------------------------------------*/
#define DEBUG_UART tsmart_uart2
#define APP_RX_BUFFER_SZ 100
static uint8_t app_rx_buf[APP_RX_BUFFER_SZ];
static uint8_t debug_buffer[APP_RX_BUFFER_SZ];
/* ------------------------------------------------------- Interface variables --------------------------------------------*/
/* IP address */
#define IP_ADDR0 0
#define IP_ADDR1 0
#define IP_ADDR2 0
#define IP_ADDR3 0
/* Netmask */
#define NET_MASK0 0
#define NET_MASK1 0
#define NET_MASK2 0
#define NET_MASK3 0
/* IP Gateway */
#define IP_GW0 0
#define IP_GW1 0
#define IP_GW2 0
#define IP_GW3 0
/* DNS */
#define DNS 0
#define DNS 0
#define DNS 0
#define DNS 0
/* SSID */
#define SSID "TSMART_TST\0"//"<SET YOUR SSID\0>"
/* SSID */
#define KEY "tsmart78\0"//"<SET YOUR NETWORK KEY\0>"
/* Type network key: select one of them */
//#define KEY_TYPE TSMART_LHINGS_WIFI_UNSEC
//#define KEY_TYPE TSMART_LHINGS_WIFI_WEP
//#define KEY_TYPE TSMART_LHINGS_WIFI_WPA
#define KEY_TYPE TSMART_LHINGS_WIFI_WPA2
static tsmart_lhings_wifi_wlan_t tsmart_lhings_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,
};
/* ------------------------------------------------------- LHINGS definitions ---------------------------------------------*/
#define DEVICE_FRIENDLY_NAME "TS_Cloud\0"
#define DEVICE_MODEL_NAME "TSmoTe\0"
#define DEVICE_MANUFACTURER "TST\0"
#define DEVICE_TYPE "TSmarTe\0"
#define DEVICE_SERIAL_NUMBER "123456AB\0"
#define DEVICE_VERSION 1
#define LHINGS_DEVICE_NAME "TSmoTe\0"
#define LHINGS_APIKEY "ed2e5096-4926-459b-9c84-85cfeaca86da\0"//"<SET YOUR LHINGS API-KEY>\0"
#define MAX_DEVICES_SUPPORTED 6
/* Event list */
#define MAX_EVENTS 2
#define DOOR_EVENT "DOOR\0"
#define TV_EVENT "TV\0"
/* Action List */
#define MAX_ACTIONS 2
#define ACTION_1 "action_1\0"
#define ACTION_1_INPUT_1 "a1i1\0"
#define ACTION_2 "action_2\0"
#define ACTION_2_INPUT_1 "a2i1\0"
#define ACTION_2_INPUT_2 "a2i2\0"
/* State Variables */
#define MAX_STATE_VARIABLES 5
#define STATE_VARIABLE_TEMPERATURE "Temperature\0"
#define STATE_VARIABLE_HUMIDITY "Humidity\0"
#define STATE_VARIABLE_MOVEMENT "Movement\0"
#define TARGET_ACTION_ARG_1 ACTION_2_INPUT_1
#define TARGET_ACTION_ARG_2 ACTION_2_INPUT_2
/*-------------------------------------------------------- Appliction Variables -------------------------------------------*/
tsmart_lhings_event_t event_list[MAX_EVENTS];
tsmart_lhings_action_t action_list[MAX_ACTIONS];
tsmart_lhings_inputs_t action_input_1[1];
tsmart_lhings_inputs_t action_input_2[2];
tsmart_lhings_state_variable_t state_variable_list[MAX_STATE_VARIABLES];
tsmart_lhings_action_arg_t target_action_args[2];
tsmart_lhings_device_t device_list[MAX_DEVICES_SUPPORTED];
tsmart_lhings_buffer_t lhings_buffer;
/* Event payloads buffers */
uint8_t door_buf[50];
uint8_t tv_buf[50];
int8_t socket_server_id;
/*-------------------------------------------------------- Application task -----------------------------------------------*/
void vLHINGS(void *pvParameters){
/* Variables */
uint8_t device_is_installed;
int8_t n;
int8_t i;
uint8_t temperature_buf[10];
uint8_t humidity_buf[10];
uint8_t movement_buf[10];
/* Set event default values */
sprintf(door_buf, "The door is opened\0");
sprintf(tv_buf, "TV is working properly\0");
/* Default Data sensor values */
sprintf(temperature_buf, "25.3\0");
sprintf(humidity_buf, "38\0");
sprintf(movement_buf, "enable\0");
TSMART_UART_Send(&DEBUG_UART, "LHINGS TEST\r\n", strlen("LHINGS TEST\r\n"), 1000/portTICK_RATE_MS);
/* ****************************************************** */
/* Device */
/* ****************************************************** */
sprintf(device.name, LHINGS_DEVICE_NAME);
/* ****************************************************** */
/* Device description */
/* ****************************************************** */
device_descriptor.device_type = DEVICE_TYPE;
device_descriptor.friendly_name = DEVICE_FRIENDLY_NAME;
device_descriptor.manufacturer = DEVICE_MANUFACTURER;
device_descriptor.model_name = DEVICE_MODEL_NAME;
device_descriptor.serial_number = DEVICE_SERIAL_NUMBER;
device_descriptor.version = DEVICE_VERSION;
/* ****************************************************** */
/* Event list */
/* ****************************************************** */
event_list[0].name = DOOR_EVENT;
event_list[0].payload = door_buf;
event_list[1].name = TV_EVENT;
event_list[1].payload = tv_buf;
device_descriptor.n_events = MAX_EVENTS;
device_descriptor.events = event_list;
/* ****************************************************** */
/* Action list */
/* ****************************************************** */
/* Action 1 */
action_list[0].name = ACTION_1;
action_list[0].n_inputs = 1;
action_list[0].inputs = action_input_1;
/* Action 1, input 1 */
action_input_1[0].name = ACTION_1_INPUT_1;
action_input_1[0].type = "integer";
/* Action 2 */
action_list[1].name = ACTION_2;
action_list[1].n_inputs = 2;
action_list[1].inputs = action_input_2;
/* Action 2, input 1 */
action_input_2[0].name = ACTION_2_INPUT_1;
action_input_2[0].type = "integer";
/* Action 2, input 2 */
action_input_2[1].name = ACTION_2_INPUT_2;
action_input_2[1].type = "integer";
device_descriptor.n_actions = MAX_ACTIONS;
device_descriptor.actions = action_list;
/* ****************************************************** */
/* Variable list */
/* ****************************************************** */
/* State Variable list */
state_variable_list[0].name = STATE_VARIABLE_TEMPERATURE;
state_variable_list[0].type = "float\0";
state_variable_list[0].value = temperature_buf;
state_variable_list[1].name = STATE_VARIABLE_HUMIDITY;
state_variable_list[1].type = "integer\0";
state_variable_list[1].value = humidity_buf;
state_variable_list[2].name = STATE_VARIABLE_MOVEMENT;
state_variable_list[2].type = "string\0";
state_variable_list[2].value = movement_buf;
device_descriptor.state_variables = state_variable_list;
device_descriptor.n_state_variables = MAX_STATE_VARIABLES;
/* ****************************************************** */
/* rule */
/* ****************************************************** */
rule.name = "ts_rule\0";
rule.target_device = device.uuid;
rule.source_app = "null\0";
rule.target_app = "null\0";
rule.source_event = TV_EVENT;
rule.target_action = ACTION_2;
rule.target_action_args = target_action_args;
/* Fill out target action argument */
target_action_args[0].name = TARGET_ACTION_ARG_1;
target_action_args[0].value = "123\0";
target_action_args[1].name = TARGET_ACTION_ARG_2;
target_action_args[1].value = "456\0";
/* **************************************************************************************** */
/* Starting 2G interface */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Starting wifi device...", strlen("Starting wifi device..."), 1000/portTICK_RATE_MS);
/* Start WIFI module */
if(TSMART_LHINGS_WIFI_Start() != TSMART_PASS){
if(TSMART_LHINGS_WIFI_Start() != TSMART_PASS){
for(;;);
}
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Attach to the Access point */
TSMART_UART_Send(&DEBUG_UART, "Attaching to AP...", strlen("Attaching to AP..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_WIFI_ConnectToAP(&tsmart_lhings_wifi_wlan) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Read IP address */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "IP: ", strlen("IP: "), 1000/portTICK_RATE_MS);
if( TSMART_LHINGS_ReadIP(&local_ip) != TSMART_PASS){
for(;;);
}
sprintf(debug_buffer, "%d.%d.%d.%d\r\n", local_ip.v0, local_ip.v1, local_ip.v2, local_ip.v3);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Register device */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Registering device...", strlen("Registering device..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceRegister(LHINGS_APIKEY, device.name, device.uuid, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Load device descriptor */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Loading device descriptor...", strlen("Loading device descriptor..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceSetDescriptor(LHINGS_APIKEY, device.uuid, &device_descriptor, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Device is install */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Is device install? ---> ", strlen("Is device install? ---> "), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceIsInstall(LHINGS_APIKEY, device.uuid, &device_is_installed, &lhings_buffer) != TSMART_PASS){
for(;;);
}
if(device_is_installed == 1){
TSMART_UART_Send(&DEBUG_UART, "YES\r\n", strlen("YES\r\n"), 1000/portTICK_RATE_MS);
}else{
TSMART_UART_Send(&DEBUG_UART, "NO\r\n", strlen("NO\r\n"), 1000/portTICK_RATE_MS);
}
/* **************************************************************************************** */
/* Get device list */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Getting device list...\r\n", strlen("Getting device list...\r\n"), 1000/portTICK_RATE_MS);
n = TSMART_LHINGS_DeviceList(LHINGS_APIKEY, device_list, &lhings_buffer);
if(n < 0){
for(;;);
}
for(i=0; i<n; i++){
sprintf(debug_buffer, " %d/%d- %s: %s\r\n", (i+1), n, device_list[i].name, device_list[i].uuid);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
}
/* **************************************************************************************** */
/* Uninstalling device */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Uninstalling device...", strlen("Uninstalling device..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceUnistall(LHINGS_APIKEY, device.uuid, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Reinstalling device */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Reinstalling device...", strlen("Reinstalling device..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceReinstall(LHINGS_APIKEY, device.uuid, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Device Start session */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Starting session...", strlen("Starting session..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceStartSession(LHINGS_APIKEY, device.uuid, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Send Events */
/* **************************************************************************************** */
for(i=0; i< MAX_EVENTS; i++){
TSMART_UART_Send(&DEBUG_UART, "Sending event...", strlen("Sending Event..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceSendEvent(LHINGS_APIKEY, device.uuid, &event_list[i], &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
}
/* **************************************************************************************** */
/* Status Store */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Storing status...", strlen("Storing status..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_StatusStore(LHINGS_APIKEY, device.uuid, state_variable_list, MAX_STATE_VARIABLES, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Create rule */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Creating rule...", strlen("Creating rule..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_RuleCreate (LHINGS_APIKEY, device.uuid, &rule, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Disable rule */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Disabling rule...", strlen("Disabling rule..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_RuleDisable(LHINGS_APIKEY, rule.rule_id, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Enable rule */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Enabling rule...", strlen("Enabling rule..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_RuleEnable(LHINGS_APIKEY, rule.rule_id, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Rule is enabled */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Is rule enabled? ---> ", strlen("Is rule enabled? ---> "), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_RuleIsEnable(LHINGS_APIKEY, rule.rule_id, &n, &lhings_buffer) != TSMART_PASS){
for(;;);
}
if(n == 1){
TSMART_UART_Send(&DEBUG_UART, "YES\r\n", strlen("YES\r\n"), 1000/portTICK_RATE_MS);
}else{
TSMART_UART_Send(&DEBUG_UART, "NO\r\n", strlen("NO\r\n"), 1000/portTICK_RATE_MS);
}
/* **************************************************************************************** */
/* Rule is broken */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Is rule broken? ---> ", strlen("Is rule broken? ---> "), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_RuleIsBroken(LHINGS_APIKEY, rule.rule_id, &n, &lhings_buffer) != TSMART_PASS){
for(;;);
}
if(n == 1){
TSMART_UART_Send(&DEBUG_UART, "YES\r\n", strlen("YES\r\n"), 1000/portTICK_RATE_MS);
}else{
TSMART_UART_Send(&DEBUG_UART, "NO\r\n", strlen("NO\r\n"), 1000/portTICK_RATE_MS);
}
/* **************************************************************************************** */
/* Delete rule */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Deleting rule...", strlen("Deleting rule..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_RuleDelete(LHINGS_APIKEY, rule.rule_id, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Device End session */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Ending session...", strlen("Ending session..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceEndSession(LHINGS_APIKEY, device.uuid, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* **************************************************************************************** */
/* Delete device */
/* **************************************************************************************** */
TSMART_UART_Send(&DEBUG_UART, "Deleting device...", strlen("Deleting device..."), 1000/portTICK_RATE_MS);
if(TSMART_LHINGS_DeviceDelete(LHINGS_APIKEY, device.uuid, &lhings_buffer) != TSMART_PASS){
for(;;);
}
TSMART_UART_Send(&DEBUG_UART, "OK\r\n", strlen("OK\r\n"), 1000/portTICK_RATE_MS);
/* Wait forever */
for(;;);
}
int32_t init() {
/* Variables */
tsmart_uart_config_t tsmart_uart_config;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize hardware resources */
/* ************************************************************************* */
/* Initialize wifi interface for Lhings */
if(TSMART_LHINGS_WIFI_Init() != TSMART_PASS){
return TSMART_FAIL;
}
/* 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(&tsmart_uart2, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create Lhings task */
if(xTaskCreate(vLHINGS, "LHINGS", 2048, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}