在去年的项目开发中,遗留了一个比较难解决的问题,需要udp传图,当时的解决办法是将图片在应用层切片依次传输到wifi芯片,而且还用的是定长的明文传输。
一直在想有什么好办法可以优化,首先想到寻找比较高效的编码算法做简单的编码也基本可以接收了,但经过各种查找还是base64用得最多最普遍,经过base64编码后的图片长度大概增加了原来长度的1/3,1200+1200/3 = 1600个字节。问题来了那么esp8266一次能传输1600个字节吗?,马上测试了一下8266直接收不到发的数据。UDP>1472则收不到,注:MTU指的是链路层的数据区,不包含IP数据报头(20)和UDP数据报头(8),在网络层数据报的数据区最大长度为1472。请参考TCP、UDP数据包大小的限制
beforeEncode为Encode之前的字符串
那么Encode后的字符串长度为:
1、如果beforeEncode.length()是3的整数倍,那么长度为
(beforeEncode.length()/3)*4
2、如果beforeEncode.length()不是3的整数倍,那么长度为
(beforeEncode.length()/3+1)*4
上述base64编码的想法受限了,请教了乐鑫原厂大佬,同时也查询了LWIP突破MTU限制的方法,网上均没有好的解决方法,于是最近研究了一下LWIP内部相关的概念之后,直接咨询乐鑫原厂当前SDK Master分支是否支持IP层分片重组。原厂给的建议是分片最好在应用层处理,以防丢包;IP层分片重组配置方法见下文(issues/596)。
刚开始按照乐鑫给的方法配置之后测试,根本没有作用。又看了下lwip可配置的选项,正确的方法如下:
测试用例是基于udp_server更改,注意用例中socket是阻塞的。测试了多次,IP层分片重组完美运行!
#include
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
#define PORT CONFIG_EXAMPLE_PORT
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;
const int IPV4_GOTIP_BIT = BIT0;
static const char *TAG = "UDP";
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
break;
case SYSTEM_EVENT_STA_CONNECTED:
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
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) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
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() );
}
static void wait_for_ip()
{
uint32_t bits = IPV4_GOTIP_BIT;
ESP_LOGI(TAG, "Waiting for AP connection...");
xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);
ESP_LOGI(TAG, "Connected to AP");
}
static void udp_server_task(void *pvParameters)
{
char rx_buffer[4000];
char addr_str[128];
int addr_family;
int ip_protocol;
while (1) {
struct sockaddr_in destAddr;
destAddr.sin_addr.s_addr = htonl(INADDR_ANY);
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(PORT);
addr_family = AF_INET;
ip_protocol = IPPROTO_IP;
inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1);
int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
if (sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
break;
}
ESP_LOGI(TAG, "Socket created");
int err = bind(sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
if (err < 0) {
ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
}
ESP_LOGI(TAG, "Socket binded");
while (1) {
ESP_LOGI(TAG, "Waiting for data");
struct sockaddr_in sourceAddr;
socklen_t socklen = sizeof(sourceAddr);
int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&sourceAddr, &socklen);
// Error occured during receiving
if (len < 0) {
ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
break;
}
// Data received
else {
// Get the sender's ip address as string
inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...
ESP_LOGI(TAG, "Received %d bytes from %s Free memory %d bytes", len, addr_str,esp_get_free_heap_size());
}
}
if (sock != -1) {
ESP_LOGE(TAG, "Shutting down socket and restarting...");
shutdown(sock, 0);
close(sock);
}
}
vTaskDelete(NULL);
}
void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_LOGI(TAG, "Free memory %d bytes", esp_get_free_heap_size());
initialise_wifi();
wait_for_ip();
xTaskCreate(udp_server_task, "udp_server", 8192, NULL, 5, NULL);
}
当前版本经过测试了较稳定,如果大家有想法可以将项目移到最新的SDK,但一定要测试产品稳定性!相信解决了IP层分片重组,有的产品又可以进行代码优化了,对于发送或接收一端数据量大的应用可以减少拆包大小进而提高通信效率。IP层分片重组,更适用于传图、文件等数据量大的应用。