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

This is an example about how to create a TCP client using the wiznet device.

/* @file main.c
* @author TST
* @version 0.1
* @date 18/11/2013
* @brief This is an example of how to create a TCP client using the
* wiznet device.
*
* The Wiznet chip is a Hardwired TCP/IP embedded Ethernet controller that
* enables easier internet connection using SPI (Serial Peripheral
* Interface).
*
* This example show the user how to create and manage a TCP client working
* in a Ethernet network. It downloads a header form "google" website. This
* application uses a HTTP protocol(HEAD method) to do it.
*
* @note To run this application it's necessary to set the following parameters:
* - MAC address.
* - IP device.
* - Subnet mask.
* - IP gateway.
* - DNS address.
*
* Users shall change the specific defines located below: MACx, IP_ADDRx, NET_MASK_x, IPGWx
* and DNSx.
*
* @note This application uses the debug UART to display debug information.
* To use the debug UART, 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
*
*/
/* Platform headers */
#include "tsmart.h"
#include "wiznet\W5200\include\w5200.h"
#include "wiznet\W5200\include\socket.h"
#include "wiznet\W5200\include\spi.h"
#include "wiznet\W5200\include\config.h"
/* Google website address */
#define GOOGLE_IP0 173
#define GOOGLE_IP1 194
#define GOOGLE_IP2 41
#define GOOGLE_IP3 208
#define GOOGLE_PORT 80
/* This is the specific configuration for Wiznet, Set them */
/* Set MAC address */
#define MAC0 0x00
#define MAC1 0x11
#define MAC2 0x22
#define MAC3 0x33
#define MAC4 0x44
#define MAC5 0x55
/* Set IP address */
#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 1
#define IP_ADDR3 158
/* Set Netmask */
#define NET_MASK0 255
#define NET_MASK1 255
#define NET_MASK2 255
#define NET_MASK3 0
/* Set IP Gateway */
#define IP_GW0 192
#define IP_GW1 168
#define IP_GW2 1
#define IP_GW3 1
/* Set DNS */
#define DNS0 8
#define DNS1 8
#define DNS2 8
#define DNS3 8
/* Number of socket for the TCP client */
#define TCP_C_SOCKET 5
/* Maximum reception buffer size */
#define MAX_BUF_SIZE (2*1024)
#define MAX_RETRIES_TO_CONNECT 3
/* @brief Wiznet config
*
* This function configures wiznet the user network
* parameters
*
*/
void WIZ_Config(void){
/* Variables */
wiz_NetInfo netinfo;
/* HW Reset */
/* Initialize Wiznet */
/* Enable IRQ socket to work in IRQ mode */
/* Set MAC address */
netinfo.Mac[0] = MAC0;
netinfo.Mac[1] = MAC1;
netinfo.Mac[2] = MAC2;
netinfo.Mac[3] = MAC3;
netinfo.Mac[4] = MAC4;
netinfo.Mac[5] = MAC5;
/* Set IP address */
netinfo.IP[0] = IP_ADDR0;
netinfo.IP[1] = IP_ADDR1;
netinfo.IP[2] = IP_ADDR2;
netinfo.IP[3] = IP_ADDR3;
/* Set Subnet mask */
netinfo.Subnet[0] = NET_MASK0;
netinfo.Subnet[1] = NET_MASK1;
netinfo.Subnet[2] = NET_MASK2;
netinfo.Subnet[3] = NET_MASK3;
/* Set IP Gateway */
netinfo.Gateway[0] = IP_GW0;
netinfo.Gateway[1] = IP_GW1;
netinfo.Gateway[2] = IP_GW2;
netinfo.Gateway[3] = IP_GW3;
/* Set DNS */
netinfo.DNSServerIP[0] = DNS0;
netinfo.DNSServerIP[1] = DNS1;
netinfo.DNSServerIP[2] = DNS2;
netinfo.DNSServerIP[3] = DNS3;
/* Set network info */
SetNetInfo(&netinfo);
/* Show netowrk info */
ts_printf("\r\n--------------------------------------- \r\n");
ts_printf("Network Configuration Information \r\n");
ts_printf("--------------------------------------- ");
ts_printf("\r\nMAC : %02X.%02X.%02X.%02X.%02X.%02X", netinfo.Mac[0],netinfo.Mac[1],netinfo.Mac[2],netinfo.Mac[3],netinfo.Mac[4],netinfo.Mac[5]);
ts_printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]);
ts_printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]);
ts_printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]);
ts_printf("\r\nDNS server : %d.%d.%d.%d \r\n", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]);
}
/* @brief vW_TCP_Client task
*
* This task turns on the wiznet device and it sets the network parameters.
* After that it create a TCP client. Then it uses a HTTP protocol to request
* the header of the google website using a HEAD method.
*
* @param pvParameters
*/
void vW_TCP_Client(void *pvParameters) {
/* Variables */
uint8 HTTPs_IP[4]= {GOOGLE_IP0, GOOGLE_IP1, GOOGLE_IP2, GOOGLE_IP3}; /* Google address */
uint16_t port = GOOGLE_PORT; /* Google port */
uint16_t local_port = 5000; /* Local port */
int16_t data_len; /* Data length received */
tsmart_w5200_socket_events_t event; /* Event */
static uint8_t http_head[]="HEAD / HTTP/1.0\r\nHost:www.google.es\r\n\r\n\r\n\0"; /* HTTP method: HEAD */
static uint8_t data[MAX_BUF_SIZE]; /* Data buffer */
uint8_t retries = 0; /* Connection retries */
/* Debug message */
ts_printf("---> WIZNET: TCP CLIENT EXAMPLE <---\r\n");
/* Configure network */
WIZ_Config();
/* Time to stabilize */
vTaskDelay(1000/portTICK_RATE_MS);
do{
/* Open TCP client */
if( TCPClientOpen(TCP_C_SOCKET, local_port++, (uint8*)HTTPs_IP, port) == 1){
/* Wait for being connected */
if (xQueueReceive(socket_queue[TCP_C_SOCKET], &event, 60000/portTICK_RATE_MS) == pdPASS){
if (event == TSMART_W5200_CONNECT){
/* Correct event */
break;
}else{
ts_printf("FAILURE: event received: %d >\r\n", event);
retries++;
}
}else{
ts_printf("< FAIL -NO MSG- CONNECTING >\r\n");
retries++;
}
}
}while(retries <= MAX_RETRIES_TO_CONNECT);
if(retries == MAX_RETRIES_TO_CONNECT){
ts_printf("Expired retries to connect\r\n");
for(;;);
}
ts_printf("< Connect OK >\r\n");
/* Request HTTP: HEAD */
TCPSend(TCP_C_SOCKET, http_head, strlen((char *)http_head));
/* Wait for SEND OK */
if (xQueueReceive(socket_queue[TCP_C_SOCKET], &event, 1000/portTICK_RATE_MS) != pdPASS){
ts_printf("< FAIL -NO MSG- SEND OK >\r\n");
for(;;);
}
if (event != TSMART_W5200_SEND_OK){
ts_printf("< FAIL SEND OK >\r\n");
for(;;);
}
/* Wait for DATA */
if (xQueueReceive(socket_queue[TCP_C_SOCKET], &event, 1000/portTICK_RATE_MS) != pdPASS){
ts_printf("< FAIL -NO MSG- RECEIVE >\r\n");
for(;;);
}
if (event != TSMART_W5200_RECEIVE){
ts_printf("< FAIL RECEIVE >\r\n");
for(;;);
}
/* Get number of bytes received */
data_len = GetSocketRxRecvBufferSize(TCP_C_SOCKET);
if (data_len > 0){
/* Get bytes received */
data_len = TCPRecv(TCP_C_SOCKET, data, 800);
if (data_len > 0){
ts_printf("< Receive(%d byes)--->\r\n\r\n %s \r\n >\r\n", data_len, data);
}
}
/* Close socket */
if(TCPClose(TCP_C_SOCKET) != 1){
for(;;);
}
/* Wait for Disconnect */
if (xQueueReceive(socket_queue[TCP_C_SOCKET], &event, 1000/portTICK_RATE_MS) != pdPASS){
ts_printf("< FAIL -NO MSG- DISCONNECTING >\r\n");
for(;;);
}
if (event != TSMART_W5200_DISCONNECT){
ts_printf("< FAIL DISCONNECTING >\r\n");
for(;;);
}
ts_printf("< Disconnected >");
/* End task */
while(1);
}
/* @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_uart_config_t uart_config;
uint8_t uart_buf[1];
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: Wiznet and UART
*/
/* Initialize wiznet */
/* UART configuration */
uart_config.baud_rate = 115200;
uart_config.flow_control = TSMART_UART_FC_NONE;
uart_config.parity_bit = TSMART_UART_PARITY_NONE;
uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
uart_config.stop_bit = TSMART_UART_STOPBITS_1;
uart_config.rx_buf_sz = 1;
uart_config.rx_buf = uart_buf;
uart_config.use_dma = TSMART_UART_DMA_RX_TX;
uart_config.use_tim = TSMART_UART_TIM_SW;
/* Initialize UART */
TSMART_UART_Init(&tsmart_uart1, &uart_config);
/* Use ts_printf for debugging. This utility is vinculated to an UART. To use it,
* it is necessary to initilialize an UART */
ts_printf_init(&tsmart_uart1);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - W_TCP_Client task: This task turns on the wiznet, configures the device
* and download the header of "google" website using
* HTTP protocol.
*/
/* Create W_TCP_Client Task */
if (xTaskCreate(vW_TCP_Client, "W_TCP_Client", 512, NULL, 7, NULL) != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}