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

This is an example about how to receive an API frame using the XBee wifi device creating a ad-hoc network.

/* @file wifi_receiver.c
* @author TST
* @version 0.1
* @date 27/08/2012
* @brief This is an example about how to receive a frame using the XBee wifi
* device creating an ad-hoc network.
*
* This example uses TCP protocol to receive a frame from other XBee.
*
* @note to run this application it's necessary to set the other XBee wifi as joiner with
* the following parameters:
* - SSID = "SANTANDER"
* - Network type = IBSS JOINER
* - Protocol = TCP
* - DHCP = Disable;
* - Destination IP = 0.0.0.2
* - Local Port = 0x2616
* - Destination Port = 0x2616
* - Local IP = 0.0.0.1
* - Mask IP address = 255.255.255.0
* - Security key Size = 0
* - API = Enable
*
* The rest value can be default values.
*
* @note to use the debug UART, user must set the next configuration in the serial communication program:
* - Baud rate: 19200
* - Word length: 8 bits
* - Parity: none
* - Stop bit: 1
* - Flow control: none
*
*/
/* Platform headers */
#include "tsmart.h"
/* @brief vXBeewifi_receiver task
*
* This task configures the XBee wifi and wait for new API frame from other XBee.
*
* @param pvParameters
*/
void vXBeewifi_receiver(void *pvParameters) {
/* Variables */
tsmart_xbee_wifi_config_device_t xbee_wifi_config_device;
/* bytes received */
uint8_t bytes_rcv = 0;
/* Debug buffer */
uint8_t debug_buffer[50];
/* Resets module */
/* Sets default values */
TSMART_XBEE_WIFI_InitStruct(&xbee_wifi_config_device);
/* Specific configuration for this application, not all parameter are
* interesting for this applications
*/
memset(xbee_wifi_config_device.ssid, 0x00, PR_TSMART_XBEE_WIFI_MAX_SSID_SIZE);
memcpy(xbee_wifi_config_device.ssid, "SANTANDER", PR_TSMART_XBEE_WIFI_MAX_SSID_SIZE);
xbee_wifi_config_device.ssid_size = strlen("SANTANDER");
xbee_wifi_config_device.network_type = TSMART_XBEE_WIFI_IBSS_CREATOR;
xbee_wifi_config_device.dhcp = TSMART_XBEE_WIFI_DISABLE;
xbee_wifi_config_device.dest_ip.ip0 = 0;
xbee_wifi_config_device.dest_ip.ip1 = 0;
xbee_wifi_config_device.dest_ip.ip2 = 0;
xbee_wifi_config_device.dest_ip.ip3 = 1;
xbee_wifi_config_device.local_port = 0x2616;
xbee_wifi_config_device.dest_port = 0x2616;
xbee_wifi_config_device.local_ip.ip0 = 0;
xbee_wifi_config_device.local_ip.ip1 = 0;
xbee_wifi_config_device.local_ip.ip2 = 0;
xbee_wifi_config_device.local_ip.ip3 = 2;
xbee_wifi_config_device.mask.mask0 = 255;
xbee_wifi_config_device.mask.mask1 = 255;
xbee_wifi_config_device.mask.mask2 = 255;
xbee_wifi_config_device.mask.mask3 = 0;
xbee_wifi_config_device.security_key_size = 0;
xbee_wifi_config_device.api = TSMART_XBEE_WIFI_ENABLE;
if( TSMART_XBEE_WIFI_Config(&tsmart_xbee_wifi, &xbee_wifi_config_device) != TSMART_PASS){
for(;;);
}
/* Wait for creating the network */
for(;;){
status_message = TSMART_XBEE_WIFI_ReadSatus(&tsmart_xbee_wifi, 5000/portTICK_RATE_MS);
if(status_message == TSMART_XBEE_WIFI_JOINED_NETWORK){
break;
}
}
TSMART_UART_Send(&tsmart_uart1, "Ready\r\n", strlen("Ready\r\n"), 1000/portTICK_RATE_MS);
for(;;){
/* Reset buffer */
memset(ipv4_data.payload, 0x00, sizeof(ipv4_data.payload));
/* Wait for new API frame */
bytes_rcv = TSMART_XBEE_WIFI_ReciveIpv4Frame(&tsmart_xbee_wifi, &ipv4_data, portMAX_DELAY);
if(bytes_rcv != 0){
/* Debug UART */
sprintf(debug_buffer, "%s from %d.%d.%d.%d\r\n", ipv4_data.payload, ipv4_data.remote_ip.ip0, ipv4_data.remote_ip.ip1,
ipv4_data.remote_ip.ip2, ipv4_data.remote_ip.ip3);
TSMART_UART_Send(&tsmart_uart1, debug_buffer, strlen(debug_buffer), 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 */
tsmart_xbee_wifi_config_t xbee_wifi_config;
tsmart_uart_config_t tsmart_uart_config;
static uint8_t uart_buf[50];
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: XBee Digimesh and UART.
* The baud rate and flow control for the XBee UART will be set. The priority
* for the reception task will be set to 14, avoiding lose packet.
*/
xbee_wifi_config.baud_rate = 9600;
xbee_wifi_config.priority = 14;
if (TSMART_XBEE_WIFI_Init(&tsmart_xbee_wifi, &xbee_wifi_config) != TSMART_PASS){
return TSMART_FAIL;
}
/* UART configuration. */
/* Baud rate */
tsmart_uart_config.baud_rate = 19200;
/* Flow control */
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
/* Parity bit */
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
/* Word length */
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
/* Stop bit */
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
/* Buffer size for our UART */
tsmart_uart_config.rx_buf_sz = 50;
/* Buffer for our UART */
tsmart_uart_config.rx_buf = uart_buf;
/* UART DMA */
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
/* UART time out */
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
/* UART initialization. UART 1 is selected */
TSMART_UART_Init(&tsmart_uart1, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - XBeewifi_receiver task: This task configures the device and wait for new
* message, this message will be showed using a debug UART.
*/
xReturn = xTaskCreate(vXBeewifi_receiver, "XBeewifi_receiver", 512, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}