添加esp32 wifi ap tcpserver的例子
parent
ac2841a24b
commit
d3054962ed
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
|
||||
<storageModule moduleId="org.eclipse.cdt.core.settings">
|
||||
<cconfiguration id="org.eclipse.cdt.core.default.config.1169930517">
|
||||
<storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.1169930517" moduleId="org.eclipse.cdt.core.settings" name="Configuration">
|
||||
<externalSettings/>
|
||||
<extensions/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
|
||||
</cconfiguration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.pathentry">
|
||||
<pathentry kind="src" path=""/>
|
||||
<pathentry excluding="**/CMakeFiles/**" kind="out" path="build"/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
</cproject>
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>esp32</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.core.cBuilder</name>
|
||||
<triggers>clean,full,incremental,</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.cdt.core.cnature</nature>
|
||||
<nature>org.eclipse.cdt.core.ccnature</nature>
|
||||
<nature>com.espressif.idf.core.idfNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,3 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding//main/xuhong_Tcp.c=UTF-8
|
||||
encoding//main/xuhong_Tcp.h=UTF-8
|
|
@ -0,0 +1,6 @@
|
|||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(app-template)
|
|
@ -0,0 +1,5 @@
|
|||
Code in this repository is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
|
@ -0,0 +1,11 @@
|
|||
ESP-IDF template app
|
||||
====================
|
||||
|
||||
This is a template application to be used with [Espressif IoT Development Framework](https://github.com/espressif/esp-idf).
|
||||
|
||||
Please check [ESP-IDF docs](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for getting started instructions.
|
||||
|
||||
*Code in this repository is in the Public Domain (or CC0 licensed, at your option.)
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.*
|
|
@ -0,0 +1,8 @@
|
|||
# Edit following two lines to set component requirements (see docs)
|
||||
set(COMPONENT_REQUIRES )
|
||||
set(COMPONENT_PRIV_REQUIRES )
|
||||
|
||||
set(COMPONENT_SRCS "main.c xuhong_Tcp.c")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS "")
|
||||
|
||||
register_component()
|
|
@ -0,0 +1,14 @@
|
|||
# put here your custom config value
|
||||
menu "Example Configuration"
|
||||
config ESP_WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
config ESP_WIFI_PASSWORD
|
||||
string "WiFi Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the example to use.
|
||||
endmenu
|
|
@ -0,0 +1,133 @@
|
|||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_err.h"
|
||||
#include "driver/uart.h"
|
||||
#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 "xuhong_Tcp.h"
|
||||
|
||||
#define TCP_SERVER_CLIENT_OPTION 1
|
||||
//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!");
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,377 @@
|
|||
/* tcp_perf Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "xuhong_Tcp.h"
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected to wifi */
|
||||
EventGroupHandle_t tcp_event_group;
|
||||
|
||||
/*socket*/
|
||||
static int server_socket = 0;
|
||||
static struct sockaddr_in server_addr;
|
||||
static struct sockaddr_in client_addr;
|
||||
static unsigned int socklen = sizeof(client_addr);
|
||||
static int connect_socket = 0;
|
||||
bool g_rxtx_need_restart = false;
|
||||
|
||||
int g_total_data = 0;
|
||||
|
||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
||||
|
||||
int g_total_pack = 0;
|
||||
int g_send_success = 0;
|
||||
int g_send_fail = 0;
|
||||
int g_delay_classify[5] = {0};
|
||||
|
||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
||||
|
||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
switch (event->event_id)
|
||||
{
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
ESP_LOGI(TAG, "got ip:%s\n",
|
||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
||||
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STACONNECTED:
|
||||
ESP_LOGI(TAG, "station:" MACSTR " join,AID=%d\n",
|
||||
MAC2STR(event->event_info.sta_connected.mac),
|
||||
event->event_info.sta_connected.aid);
|
||||
xEventGroupSetBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STADISCONNECTED:
|
||||
ESP_LOGI(TAG, "station:" MACSTR "leave,AID=%d\n",
|
||||
MAC2STR(event->event_info.sta_disconnected.mac),
|
||||
event->event_info.sta_disconnected.aid);
|
||||
g_rxtx_need_restart = true;
|
||||
xEventGroupClearBits(tcp_event_group, WIFI_CONNECTED_BIT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//send data
|
||||
void send_data(void *pvParameters)
|
||||
{
|
||||
int len = 0;
|
||||
char *databuff = (char *)malloc(EXAMPLE_DEFAULT_PKTSIZE * sizeof(char));
|
||||
memset(databuff, EXAMPLE_PACK_BYTE_IS, EXAMPLE_DEFAULT_PKTSIZE);
|
||||
vTaskDelay(100 / portTICK_RATE_MS);
|
||||
ESP_LOGI(TAG, "start sending...");
|
||||
|
||||
while (1)
|
||||
{
|
||||
int to_write = EXAMPLE_DEFAULT_PKTSIZE;
|
||||
|
||||
while (to_write > 0)
|
||||
{
|
||||
len = send(connect_socket, databuff + (EXAMPLE_DEFAULT_PKTSIZE - to_write), to_write, 0);
|
||||
if (len > 0)
|
||||
{
|
||||
g_total_data += len;
|
||||
to_write -= len;
|
||||
}
|
||||
else
|
||||
{
|
||||
int err = get_socket_error_code(connect_socket);
|
||||
|
||||
if (err != ENOMEM)
|
||||
{
|
||||
show_socket_error_reason("send_data", connect_socket);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
||||
gettimeofday(&tv_finish, NULL);
|
||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
||||
if (g_total_data > 0)
|
||||
{
|
||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
||||
g_send_success++;
|
||||
send_delay_ms = (tv_finish.tv_sec - tv_start.tv_sec) * 1000 + (tv_finish.tv_usec - tv_start.tv_usec) / 1000;
|
||||
if (send_delay_ms < 30)
|
||||
{
|
||||
g_delay_classify[0]++;
|
||||
}
|
||||
else if (send_delay_ms < 100)
|
||||
{
|
||||
g_delay_classify[1]++;
|
||||
}
|
||||
else if (send_delay_ms < 300)
|
||||
{
|
||||
g_delay_classify[2]++;
|
||||
}
|
||||
else if (send_delay_ms < 1000)
|
||||
{
|
||||
g_delay_classify[3]++;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_delay_classify[4]++;
|
||||
}
|
||||
#endif /*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_rxtx_need_restart = true;
|
||||
free(databuff);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
//receive data
|
||||
void recv_data(void *pvParameters)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
char databuff[1024];
|
||||
|
||||
while (1)
|
||||
{
|
||||
//姣忔鎺ユ敹閮借娓呯┖鎺ユ敹鏁扮粍
|
||||
memset(databuff, 0x00, sizeof(databuff));
|
||||
len = recv(connect_socket, databuff, sizeof(databuff), 0);
|
||||
g_rxtx_need_restart = false;
|
||||
if (len > 0)
|
||||
{
|
||||
g_total_data += len;
|
||||
//鎵撳嵃鎺ユ敹鍒扮殑鏁扮粍
|
||||
ESP_LOGI(TAG, "recvData: %s\n", databuff);
|
||||
//鍘熻矾杩斿洖锛屼笉鎸囧畾鏌愪釜瀹㈡埛绔<E59F9B>
|
||||
send(connect_socket, databuff, sizeof(databuff), 0);
|
||||
//sendto(connect_socket, databuff , sizeof(databuff), 0, (struct sockaddr *) &remote_addr,sizeof(remote_addr));
|
||||
}
|
||||
else
|
||||
{
|
||||
show_socket_error_reason("recv_data", connect_socket);
|
||||
g_rxtx_need_restart = true;
|
||||
#if !TCP_SERVER_CLIENT_OPTION
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
close_socket();
|
||||
g_rxtx_need_restart = true;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
esp_err_t create_tcp_server(bool isCreatServer)
|
||||
{
|
||||
|
||||
if (isCreatServer)
|
||||
{
|
||||
ESP_LOGI(TAG, "server socket....,port=%d", TCP_PORT);
|
||||
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
if (server_socket < 0)
|
||||
{
|
||||
show_socket_error_reason("create_server", server_socket);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(TCP_PORT);
|
||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
|
||||
{
|
||||
show_socket_error_reason("bind_server", server_socket);
|
||||
close(server_socket);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
if (listen(server_socket, 5) < 0)
|
||||
{
|
||||
show_socket_error_reason("listen_server", server_socket);
|
||||
close(server_socket);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
connect_socket = accept(server_socket, (struct sockaddr *)&client_addr, &socklen);
|
||||
|
||||
if (connect_socket < 0)
|
||||
{
|
||||
show_socket_error_reason("accept_server", connect_socket);
|
||||
close(server_socket);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/*connection established锛宯ow can send/recv*/
|
||||
ESP_LOGI(TAG, "tcp connection established!");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//鍒涘缓TCP瀹㈡埛绔繛鎺ュ埌鎸囧畾鐨勬湇鍔″櫒
|
||||
esp_err_t create_tcp_client()
|
||||
{
|
||||
ESP_LOGI(TAG, "will connect gateway ssid : %s port:%d\n",
|
||||
TCP_SERVER_ADRESS, TCP_PORT);
|
||||
connect_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
if (connect_socket < 0)
|
||||
{
|
||||
show_socket_error_reason("create client", connect_socket);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(TCP_PORT);
|
||||
server_addr.sin_addr.s_addr = inet_addr(TCP_SERVER_ADRESS);
|
||||
ESP_LOGI(TAG, "connectting server...");
|
||||
if (connect(connect_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
|
||||
{
|
||||
show_socket_error_reason("client connect", connect_socket);
|
||||
ESP_LOGE(TAG, "connect failed!");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGI(TAG, "connect success!");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//wifi_init_sta
|
||||
void wifi_init_sta()
|
||||
{
|
||||
tcp_event_group = xEventGroupCreate();
|
||||
|
||||
tcpip_adapter_init();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = GATEWAY_SSID,
|
||||
.password = GATEWAY_PAS},
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
|
||||
GATEWAY_SSID, GATEWAY_PAS);
|
||||
}
|
||||
|
||||
//wifi_init_softap
|
||||
void wifi_init_softap()
|
||||
{
|
||||
tcp_event_group = xEventGroupCreate();
|
||||
|
||||
tcpip_adapter_init();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
.ssid_len = 0,
|
||||
.ssid = SOFT_AP_SSID,
|
||||
.max_connection = SOFT_AP_MAX_CONNECT,
|
||||
.password = SOFT_AP_PAS,
|
||||
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
|
||||
};
|
||||
if (strlen(EXAMPLE_DEFAULT_PWD) == 0)
|
||||
{
|
||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "SoftAP set finish:%s pas:%s \n",
|
||||
EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD);
|
||||
}
|
||||
|
||||
int get_socket_error_code(int socket)
|
||||
{
|
||||
int result;
|
||||
u32_t optlen = sizeof(int);
|
||||
int err = getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen);
|
||||
if (err == -1)
|
||||
{
|
||||
ESP_LOGE(TAG, "getsockopt failed:%s", strerror(err));
|
||||
return -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int show_socket_error_reason(const char *str, int socket)
|
||||
{
|
||||
int err = get_socket_error_code(socket);
|
||||
|
||||
if (err != 0)
|
||||
{
|
||||
ESP_LOGW(TAG, "%s socket error %d %s", str, err, strerror(err));
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int check_working_socket()
|
||||
{
|
||||
int ret;
|
||||
#if EXAMPLE_ESP_TCP_MODE_SERVER
|
||||
ESP_LOGD(TAG, "check server_socket");
|
||||
ret = get_socket_error_code(server_socket);
|
||||
if (ret != 0)
|
||||
{
|
||||
ESP_LOGW(TAG, "server socket error %d %s", ret, strerror(ret));
|
||||
}
|
||||
if (ret == ECONNRESET)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
ESP_LOGD(TAG, "check connect_socket");
|
||||
ret = get_socket_error_code(connect_socket);
|
||||
if (ret != 0)
|
||||
{
|
||||
ESP_LOGW(TAG, "connect socket error %d %s", ret, strerror(ret));
|
||||
}
|
||||
if (ret != 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void close_socket()
|
||||
{
|
||||
close(connect_socket);
|
||||
close(server_socket);
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
|
||||
#ifndef __TCP_PERF_H__
|
||||
#define __TCP_PERF_H__
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*test options*/
|
||||
#define EXAMPLE_ESP_WIFI_MODE_AP CONFIG_TCP_PERF_WIFI_MODE_AP
|
||||
#define EXAMPLE_ESP_TCP_MODE_SERVER CONFIG_TCP_PERF_SERVER
|
||||
#define EXAMPLE_ESP_TCP_PERF_TX CONFIG_TCP_PERF_TX
|
||||
#define EXAMPLE_ESP_TCP_DELAY_INFO CONFIG_TCP_PERF_DELAY_DEBUG
|
||||
|
||||
/*AP info and tcp_server info*/
|
||||
#define EXAMPLE_DEFAULT_SSID "test"
|
||||
#define EXAMPLE_DEFAULT_PWD "123456789"
|
||||
#define EXAMPLE_DEFAULT_PORT 8900
|
||||
#define EXAMPLE_DEFAULT_PKTSIZE 200
|
||||
#define EXAMPLE_MAX_STA_CONN 1
|
||||
|
||||
#ifdef CONFIG_TCP_PERF_SERVER_IP
|
||||
#define EXAMPLE_DEFAULT_SERVER_IP CONFIG_TCP_PERF_SERVER_IP
|
||||
#else
|
||||
#define EXAMPLE_DEFAULT_SERVER_IP "192.168.4.1"
|
||||
#endif
|
||||
#define EXAMPLE_PACK_BYTE_IS 97 //'a'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define TCP_SERVER_CLIENT_OPTION FALSE //true涓哄紑鍚儹鐐瑰苟涓斿垱寤簍cp鏈嶅姟鍣紝fasle涓鸿繛鎺ュ埌鎸囧畾鐨勮矾鐢卞櫒骞朵笖杩炴帴鍒版寚瀹氱殑tcp鏈嶅姟鍣<E5A79F>
|
||||
#define TAG "XuHongTCP-->" //鎵撳嵃鐨則ag
|
||||
|
||||
//浠ヤ笅鏄痵oftAP鐑偣妯″紡鐨勯厤缃俊鎭<E4BF8A>
|
||||
#define SOFT_AP_SSID "XuHongTCP2018"
|
||||
|
||||
#define SOFT_AP_PAS "xuhong123456" //濡傛灉瀵嗙爜璁剧疆涓虹┖锛屽垯閰嶇疆鐨勭儹鐐规槸寮<E6A7B8>鏀剧殑锛屾病鏈夊瘑鐮佺殑銆<E6AE91>
|
||||
|
||||
#define SOFT_AP_MAX_CONNECT 1 //浣滀负AP鐑偣鏃跺<E98F83>欙紝鏈<E7B49D>澶х殑杩炴帴鏁扮洰
|
||||
|
||||
|
||||
//浠ヤ笅鏄痵tation妯″紡閰嶇疆淇℃伅,鏄偍瀹堕噷鐨勮矾鐢卞櫒鐨勪俊鎭<E4BF8A>
|
||||
|
||||
#define GATEWAY_SSID "AliyunOnlyTest"
|
||||
|
||||
#define GATEWAY_PAS "aliyun#123456"
|
||||
|
||||
#define TCP_SERVER_ADRESS "192.168.1.104" //瑕佽繛鎺CP鏈嶅姟鍣ㄥ湴鍧<E6B9B4>
|
||||
|
||||
|
||||
//缁熶竴鐨勭鍙e彿锛屽寘鎷琓CP瀹㈡埛绔垨鑰呮湇鍔$
|
||||
#define TCP_PORT 8266
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected to wifi*/
|
||||
extern EventGroupHandle_t tcp_event_group;
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
|
||||
extern int g_total_data;
|
||||
extern bool g_rxtx_need_restart;
|
||||
|
||||
#if EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO
|
||||
extern int g_total_pack;
|
||||
extern int g_send_success;
|
||||
extern int g_send_fail;
|
||||
extern int g_delay_classify[5];
|
||||
#endif/*EXAMPLE_ESP_TCP_PERF_TX && EXAMPLE_ESP_TCP_DELAY_INFO*/
|
||||
|
||||
|
||||
//using esp as station
|
||||
void wifi_init_sta();
|
||||
//using esp as softap
|
||||
void wifi_init_softap();
|
||||
|
||||
//create a tcp server socket. return ESP_OK:success ESP_FAIL:error
|
||||
esp_err_t create_tcp_server(bool isCreatServer);
|
||||
//create a tcp client socket. return ESP_OK:success ESP_FAIL:error
|
||||
esp_err_t create_tcp_client();
|
||||
|
||||
//send data task
|
||||
void send_data(void *pvParameters);
|
||||
//receive data task
|
||||
void recv_data(void *pvParameters);
|
||||
|
||||
//close all socket
|
||||
void close_socket();
|
||||
|
||||
//get socket error code. return: error code
|
||||
int get_socket_error_code(int socket);
|
||||
|
||||
//show socket error code. return: error code
|
||||
int show_socket_error_reason(const char* str, int socket);
|
||||
|
||||
//check working socket
|
||||
int check_working_socket();
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /*#ifndef __TCP_PERF_H__*/
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue