Skip to content

Commit

Permalink
2018-06-27 Add TCP Client or Server Sample Code
Browse files Browse the repository at this point in the history
2018-06-27 Add TCP Client or Server Sample Code
  • Loading branch information
xuhongNewPC authored and xuhongNewPC committed Jun 27, 2018
1 parent 2ff1bc3 commit f9702ec
Show file tree
Hide file tree
Showing 7 changed files with 772 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 9_tcp_server_client/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := tcp_perf

include $(IDF_PATH)/make/project.mk

1 change: 1 addition & 0 deletions 9_tcp_server_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- esp as a tcp server or client
119 changes: 119 additions & 0 deletions 9_tcp_server_client/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
menu "Example Configuration"

#choice
# prompt "TCP_PERF_MODE "
# default MODE_TCP_SHIELDBOX
# help
# This option set performance mode.
#
# - Testing in shieldbox for "Performance in shieldbox" setting.
#
# - Testing in air for "Performance in air" setting.
#
# - Testing in long distance for "Performance in long distance" setting.
#
#
#config MODE_TCP_SHIELDBOX
# bool "Performance in shieldbox"
#config MODE_TCP_AIR
# bool "Performance in air"
#config MODE_TCP_LONG_DISTANCE
# bool "Performance in long distance"
#endchoice

choice TCP_PERF_WIFI_MODE
prompt "AP or STA"
default TCP_PERF_ESP_IS_STATION
help
Whether the esp32 is softAP or station.

config TCP_PERF_ESP_IS_SOFTAP
bool "SoftAP"
config TCP_PERF_ESP_IS_STATION
bool "Station"
endchoice

config TCP_PERF_WIFI_MODE_AP
bool
default y if TCP_PERF_ESP_IS_SOFTAP
default n if TCP_PERF_ESP_IS_STATION

choice TCP_PERF_SERVER_CLIENT
prompt "server or client"
default TCP_PERF_ESP_IS_CLIENT
help
Whether the esp32 is tcp server or client.

We suggest to choose "client" if you choose "station" in "wifi mode".

config TCP_PERF_ESP_IS_SERVER
bool "server"
config TCP_PERF_ESP_IS_CLIENT
bool "client"
endchoice

config TCP_PERF_SERVER
bool
default y if TCP_PERF_ESP_IS_SERVER
default n if TCP_PERF_ESP_IS_CLIENT

choice TCP_PERF_TX_RX
prompt "send or receive"
default TCP_PERF_ESP_RECV
help
Whether the esp32 will send or receive.

config TCP_PERF_ESP_SEND
bool "send"
config TCP_PERF_ESP_RECV
bool "receive"
endchoice

config TCP_PERF_TX
bool
default y if TCP_PERF_ESP_SEND
default n if TCP_PERF_ESP_RECV

config TCP_PERF_DELAY_DEBUG
bool "TCP performance delay info enable"
depends on TCP_PERF_TX
default n
help
Show TCP performance delay info.

Ignore in TCP RX.

config TCP_PERF_WIFI_SSID
string "WiFi SSID"
default "esp_wifi_test1"
help
SSID (network name) for the example to connect to.

config TCP_PERF_WIFI_PASSWORD
string "WiFi Password"
default "1234567890"
help
WiFi password (WPA or WPA2) for the example to use.

config TCP_PERF_SERVER_PORT
int "TCP server port"
default 4567
help
Which will the tcp server use.

config TCP_PERF_SERVER_IP
string "TCP server ip"
depends on TCP_PERF_ESP_IS_CLIENT
default "192.168.4.1"
help
IP of TCP server.

Ignore in TCP server.

config TCP_PERF_PKT_SIZE
int "Size of TCP packet"
default 1460
help
the data send&recv packet size.

endmenu
8 changes: 8 additions & 0 deletions 9_tcp_server_client/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
135 changes: 135 additions & 0 deletions 9_tcp_server_client/main/tcp_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@

#include <errno.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "xuhong_Tcp.h"

//this task establish a TCP connection and receive data from TCP
static void tcp_conn(void *pvParameters)
{
while (1)
{

g_rxtx_need_restart = false;

ESP_LOGI(TAG, "task tcp_conn...");

/*wating for connecting to AP*/
xEventGroupWaitBits(tcp_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
TaskHandle_t tx_rx_task = NULL;

#if TCP_SERVER_CLIENT_OPTION

ESP_LOGI(TAG, "tcp_server will start after 3s...");
vTaskDelay(3000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "create_tcp_server.");
int socket_ret = create_tcp_server(true);
#else
ESP_LOGI(TAG, "tcp_client will start after 3s...");
vTaskDelay(3000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "create_tcp_Client.");
int socket_ret = create_tcp_client();
#endif
if (socket_ret == ESP_FAIL)
{
ESP_LOGI(TAG, "create tcp socket error,stop...");
continue;
}
else
{
ESP_LOGI(TAG, "create tcp socket succeed...");
}

if (pdPASS != xTaskCreate(&recv_data, "recv_data", 4096, NULL, 4, &tx_rx_task))
{
ESP_LOGI(TAG, "Recv task create fail!");
}
else
{
ESP_LOGI(TAG, "Recv task create succeed!");
}

double bps;

while (1)
{

vTaskDelay(3000 / portTICK_RATE_MS);

#if TCP_SERVER_CLIENT_OPTION

if (g_rxtx_need_restart)
{
ESP_LOGE(TAG, "tcp server send or receive task encoutner error, need to restart...");

if (ESP_FAIL != create_tcp_server(false))
{
if (pdPASS != xTaskCreate(&recv_data, "recv_data", 4096, NULL, 4, &tx_rx_task))
{
ESP_LOGE(TAG, "tcp server Recv task create fail!");
}
else
{
ESP_LOGE(TAG, "tcp server Recv task create succeed!");
}
}
}
#else
if (g_rxtx_need_restart)
{
ESP_LOGI(TAG, "tcp_client will reStart after 3s...");
vTaskDelay(3000 / portTICK_RATE_MS);
ESP_LOGI(TAG, "create_tcp_Client...");
int socket_ret = create_tcp_client();

if (socket_ret == ESP_FAIL)
{
ESP_LOGE(TAG, "create tcp socket error,stop...");
continue;
}
else
{
ESP_LOGI(TAG, "create tcp socket succeed...");
g_rxtx_need_restart = false;
}

if (pdPASS != xTaskCreate(&recv_data, "recv_data", 4096, NULL, 4, &tx_rx_task))
{
ESP_LOGE(TAG, "Recv task create fail!");
}
else
{
ESP_LOGI(TAG, "Recv task create succeed!");
}
}
#endif
}
}

vTaskDelete(NULL);
}

void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

#if TCP_SERVER_CLIENT_OPTION
ESP_LOGI(TAG, "As a Tcp Server , will start wifi_init_softap...");
wifi_init_softap();
#else

ESP_LOGI(TAG, "As a Tcp Client , will start wifi_init_sta...");
wifi_init_sta();
#endif
xTaskCreate(&tcp_conn, "tcp_conn", 4096, NULL, 5, NULL);
}
Loading

0 comments on commit f9702ec

Please sign in to comment.