gpt4 book ai didi

c - 在 esp8266 上发送多播?

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:25 25 4
gpt4 key购买 nike

我正在研究 esp8266,nonos sdk v 2.0.0_16_08_10,native c。我正在通过 wireshark 监控网络。

我正在尝试通过 udp 发送多播消息。

接收 udp 多播有效。发送 udp 单播有效。发送 udp 多播不起作用。

在我的udp send回调函数中,显示消息已发送,但我无法通过wireshark捕获它。

多播IP地址:224.0.1.187组播端口:5683

加入多播组:

uint32_t mip = wifi_get_ip();
if(mip == 0){
os_printf("ERROR MULTICAST JOIN mip==0\n");
return;
}

ip_addr_t local, remote;
remote.addr = ocf_mgroup.ip.full;
local.addr = mip;

os_printf("multicast result = %d\n", espconn_igmp_join(&local, &remote));

打开一个 UDP channel :

uint8_t ICACHE_FLASH_ATTR udp_open(uint8_t ch_no, uint8_t ch_id, uint32_t src_addr, uint16_t src_port, uint32_t dst_addr, uint16_t dst_port){
//ALLOC MEM
udp_conn[ch_no] = (struct espconn*) os_malloc(sizeof(struct espconn));
udp_info[ch_no] = (esp_udp*) os_malloc(sizeof(esp_udp));

//CHANNEL
//ports
udp_info[ch_no]->remote_port = dst_port;
udp_info[ch_no]->local_port = src_port;
//ips
udp_info[ch_no]->remote_ip[0] = (dst_addr) & 0xff;
udp_info[ch_no]->remote_ip[1] = (dst_addr >> 8) & 0xff;
udp_info[ch_no]->remote_ip[2] = (dst_addr >> 16) & 0xff;
udp_info[ch_no]->remote_ip[3] = (dst_addr >> 24) & 0xff;
udp_info[ch_no]->local_ip[0] = (src_addr) & 0xff;
udp_info[ch_no]->local_ip[1] = (src_addr >> 8) & 0xff;
udp_info[ch_no]->local_ip[2] = (src_addr >> 16) & 0xff;
udp_info[ch_no]->local_ip[3] = (src_addr >> 24) & 0xff;

//connection
udp_conn[ch_no]->type = ESPCONN_UDP;
udp_conn[ch_no]->state = ESPCONN_NONE;
udp_conn[ch_no]->proto.udp = udp_info[ch_no];
udp_conn[ch_no]->link_cnt = ch_id;

//HANDLERS
espconn_regist_recvcb(udp_conn[ch_no], udp_receive_handler);
//on send successfull
espconn_regist_sentcb(udp_conn[ch_no], udp_send_handler);

//CRAETE
return espconn_create(udp_conn[ch_no]);
}

发送消息到 ip:port:

uint8_t ICACHE_FLASH_ATTR udp_send_cfg(uint8_t ch_no, uint8_t* data, uint16_t len, uint32_t dst_ip, uint16_t dst_port){
udp_conn[ch_no]->proto.udp->remote_port = dst_port;

udp_conn[ch_no]->proto.udp->remote_ip[0] = dst_ip & 0xFF;
udp_conn[ch_no]->proto.udp->remote_ip[1] = (dst_ip >> 8) & 0xFF;
udp_conn[ch_no]->proto.udp->remote_ip[2] = (dst_ip >> 16) & 0xFF;
udp_conn[ch_no]->proto.udp->remote_ip[3] = (dst_ip >> 24) & 0xFF;

return espconn_sent(udp_conn[ch_no], data, len);
}

最佳答案

在互联网上广泛搜索后,我发现 soft-ap 导致组播发送出现问题。

我的代码从闪存读取配置,所以我硬编码 ap 配置为 NULL,现在可以毫无问题地发送多播。

因此,请避免使用以下函数:

wifi_set_opmode(STATIONAP_MODE)
wifi_set_opmode(SOFTAP_MODE)
wifi_softap_foo

我还发现是soft-ap dhcp导致了这个问题,有时我可以在没有打开的情况下发送多播消息,但有时我不能。

我找到的信息是针对 Arduino 的,但似乎也适用于原生 c。

关于c - 在 esp8266 上发送多播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42761524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com