写点什么

esp8266~lwip 突破 MTU 最大 1500 限制,apt 编译时期自动生成代码 & 动态类加载

用户头像
Android架构
关注
发布于: 13 小时前

#include "lwip/sys.h"


#include <lwip/netdb.h>


#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, IPV


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


4_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 {

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
esp8266~lwip突破MTU最大1500限制,apt编译时期自动生成代码&动态类加载