Skip to content

Commit

Permalink
2018-06-20 Add UDP Client or Server Sample Code
Browse files Browse the repository at this point in the history
2018-06-20 Add UDP Client or Server Sample Code
  • Loading branch information
xuhongv committed Jun 20, 2018
1 parent 0ba794a commit 2ff1bc3
Show file tree
Hide file tree
Showing 10 changed files with 586 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 8_udp_client_server/.settings/language.settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
<configuration id="cdt.managedbuild.toolchain.gnu.cross.base.1283170695" name="Default">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/>
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-311626720601977250" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
</extension>
</configuration>
</project>
9 changes: 9 additions & 0 deletions 8_udp_client_server/.settings/org.eclipse.cdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/IDF_PATH/delimiter=;
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/IDF_PATH/operation=append
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/IDF_PATH/value=E\:\\Esp32Study\\ESP32IDE\\msys32\\home\\Administrator\\esp-idf
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/PATH/delimiter=;
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/PATH/operation=replace
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/PATH/value=E\:\\Esp32Study\\ESP32IDE\\msys32\\usr\\bin;E\:\\Esp32Study\\ESP32IDE\\msys32\\opt\\xtensa-esp32-elf\\bin;E\:\\Esp32Study\\ESP32IDE\\msys32\\mingw32\\bin;C\:/Program Files/Java/jre1.8.0_161/bin/server;C\:/Program Files/Java/jre1.8.0_161/bin;C\:/Program Files/Java/jre1.8.0_161/lib/amd64;E\:\\203CSARM_SDK\\bin;G\:\\203CS_CODE\\bin;C\:\\ProgramData\\Oracle\\Java\\javapath;C\:\\windows\\system32;C\:\\windows;C\:\\windows\\System32\\Wbem;C\:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C\:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C\:\\Program Files\\Java\\jdk1.8.0_161\\bin;G\:\\Git\\cmd;C\:\\WINDOWS\\system32;C\:\\WINDOWS;C\:\\WINDOWS\\System32\\Wbem;C\:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C\:\\Program Files\\SourceGear\\Common\\DiffMerge\\;E\:\\Esp32Study\\ESP8266IDE\\cywin\\cygwin\\bin\\;E\:\\Esp32Study\\ESP8266IDE\\xtensa-lx106-elf\\bin\\;E\:\\Esp32Study\\ESP32IDE\\msys32\\mingw32\\bin\\;E\:\\Esp32Study\\ESP32IDE\\msys32\\opt\\xtensa-esp32-elf\\bin\\;E\:\\Esp32Study\\ESP32IDE\\msys32\\usr\\bin\\;D\:\\SDK\\platform-tools;D\:\\SDK\\tools;C\:\\Users\\Administrator\\AppData\\Local\\Microsoft\\WindowsApps;C\:\\Users\\Administrator\\AppData\\Local\\GitHubDesktop\\bin;E\:\\Esp32Study\\ESP32IDE\\eclipse
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/append=true
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.1283170695/appendContributed=true
9 changes: 9 additions & 0 deletions 8_udp_client_server/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 := udp_perf

include $(IDF_PATH)/make/project.mk

6 changes: 6 additions & 0 deletions 8_udp_client_server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


实现UDP客户端和服务器端!



112 changes: 112 additions & 0 deletions 8_udp_client_server/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
menu "Example Configuration"

#choice
# prompt "UDP_PERF_MODE "
# default MODE_UDP_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_UDP_SHIELDBOX
# bool "Performance in shieldbox"
#config MODE_UDP_AIR
# bool "Performance in air"
#config MODE_UDP_LONG_DISTANCE
# bool "Performance in long distance"
#endchoice
#

choice UDP_PERF_WIFI_MODE
prompt "AP or STA"
default UDP_PERF_ESP_IS_STATION
help
Whether the esp32 is softAP or station.

config UDP_PERF_ESP_IS_SOFTAP
bool "SoftAP"
config UDP_PERF_ESP_IS_STATION
bool "Station"
endchoice

config UDP_PERF_WIFI_MODE_AP
bool
default y if UDP_PERF_ESP_IS_SOFTAP
default n if UDP_PERF_ESP_IS_STATION

choice UDP_PERF_SERVER_CLIENT
prompt "server or client"
default UDP_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 UDP_PERF_ESP_IS_SERVER
bool "server"
config UDP_PERF_ESP_IS_CLIENT
bool "client"
endchoice

config UDP_PERF_SERVER
bool
default y if UDP_PERF_ESP_IS_SERVER
default n if UDP_PERF_ESP_IS_CLIENT

choice UDP_PERF_TX_RX
prompt "send or receive"
default UDP_PERF_ESP_RECV
help
Whether the esp32 will send or receive.

config UDP_PERF_ESP_SEND
bool "send"
config UDP_PERF_ESP_RECV
bool "receive"
endchoice

config UDP_PERF_TX
bool
default y if UDP_PERF_ESP_SEND
default n if UDP_PERF_ESP_RECV


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

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

config UDP_PERF_SERVER_PORT
int "UDP server port"
default 4567
help
Which will the udp server use.

config UDP_PERF_SERVER_IP
string "UDP server ip"
depends on UDP_PERF_ESP_IS_CLIENT
default "192.168.4.1"
help
IP of UDP server.

Ignore in UDP server.

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

endmenu
240 changes: 240 additions & 0 deletions 8_udp_client_server/main/XuHongUdp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
#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 "XuHongUdp.h"

/* FreeRTOS event group to signal when we are connected to WiFi and ready to start UDP test*/
EventGroupHandle_t udp_event_group;

static int mysocket;

static struct sockaddr_in remote_addr;
static unsigned int socklen;

int total_data = 0;
int success_pack = 0;

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(udp_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_CONNECTED:
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!");
ESP_LOGI(TAG, "got ip:%s\n",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
xEventGroupSetBits(udp_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(udp_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);
xEventGroupSetBits(udp_event_group, UDP_CONNCETED_SUCCESS);
xEventGroupClearBits(udp_event_group, WIFI_CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}

//wifi初始化,连接路由器
void wifi_init_sta() {

udp_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_PASSWORD }, };

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_PASSWORD);
}

//wifi的softap初始化
void wifi_init_softap() {
udp_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 = AP_SSID, .ssid_len = 0,
.max_connection = EXAMPLE_MAX_STA_CONN, .password = AP_PAW,
.authmode = WIFI_AUTH_WPA_WPA2_PSK }, };
if (strlen(AP_SSID) == 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, " Wifi_init_softap finished the SSID: %s password:%s \n",
AP_SSID, AP_PAW);
}

//create a udp server socket. return ESP_OK:success ESP_FAIL:error
esp_err_t create_udp_server() {

ESP_LOGI(TAG, "Create Udp Server succeed port : %d \n", SERVICE_PORT);

mysocket = socket(AF_INET, SOCK_DGRAM, 0);

if (mysocket < 0) {
show_socket_error_reason(mysocket);
return ESP_FAIL;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVICE_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(mysocket, (struct sockaddr *) &server_addr, sizeof(server_addr))
< 0) {
show_socket_error_reason(mysocket);
close(mysocket);
return ESP_FAIL;
}
return ESP_OK;
}

//create a udp client socket. return ESP_OK:success ESP_FAIL:error
esp_err_t create_udp_client() {

ESP_LOGI(TAG, "create_udp_client()");
ESP_LOGI(TAG, "connecting to %s:%d",
SERVER_IP, SERVICE_PORT);

mysocket = socket(AF_INET, SOCK_DGRAM, 0);

if (mysocket < 0) {
show_socket_error_reason(mysocket);
return ESP_FAIL;
}
/*for client remote_addr is also server_addr*/
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(SERVICE_PORT);
remote_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

return ESP_OK;
}

void send_recv_data(void *pvParameters) {

ESP_LOGI(TAG, "task send_recv_data start! \n");

int len;
char databuff[1024];

socklen = sizeof(remote_addr);
memset(databuff, EXAMPLE_PACK_BYTE_IS, sizeof(databuff));

//作为UDP Client时候,等待服务器响应再通讯
#if !Server_Station_Option

char sendBuff[1024] = "hello xuhong , this is first message ...";

len = sendto(mysocket, sendBuff, 1024, 0, (struct sockaddr *) &remote_addr,
sizeof(remote_addr));
if (len > 0) {
ESP_LOGI(TAG, "succeed transfer data to %s:%u\n",
inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
xEventGroupSetBits(udp_event_group, UDP_CONNCETED_SUCCESS);

} else {
show_socket_error_reason(mysocket);
close(mysocket);
vTaskDelete(NULL);
}
#endif

ESP_LOGI(TAG, "start Recieve!\n");

while (1) {
//每次接收都要清空接收数组
memset(databuff, 0x00, sizeof(databuff));
//开始接收
len = recvfrom(mysocket, databuff, sizeof(databuff), 0,
(struct sockaddr *) &remote_addr, &socklen);
//打印接收到的数组
ESP_LOGI(TAG, "recvData: %s\n", databuff);
if (len > 0) {
total_data += len;
success_pack++;
} else {
if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) {
show_socket_error_reason(mysocket);
}
}
}
}

int send_Buff_with_UDP(char *databuff, int length) {

int result;
result = sendto(mysocket, databuff, length, 0,
(struct sockaddr *) &remote_addr, sizeof(remote_addr));

return result;
}

int get_socket_error_code(int socket) {
int result;
u32_t optlen = sizeof(int);
if (getsockopt(socket, SOL_SOCKET, SO_ERROR, &result, &optlen) == -1) {
ESP_LOGE(TAG, "getsockopt failed");
return -1;
}
return result;
}

int show_socket_error_reason(int socket) {
int err = get_socket_error_code(socket);
ESP_LOGW(TAG, "socket error %d %s", err, strerror(err));
return err;
}

int check_connected_socket() {
int ret;
ESP_LOGD(TAG, "check connect_socket");
ret = get_socket_error_code(mysocket);
if (ret != 0) {
ESP_LOGW(TAG, "socket error %d %s", ret, strerror(ret));
}
return ret;
}

void close_socket() {
close(mysocket);
}
Loading

0 comments on commit 2ff1bc3

Please sign in to comment.