esp32 udp组播接收程序
时间: 2023-08-04 19:07:13 浏览: 289
以下是基于ESP-IDF的ESP32 UDP组播接收程序示例:
```c
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#define EXAMPLE_WIFI_SSID "YOUR_WIFI_SSID"
#define EXAMPLE_WIFI_PASS "YOUR_WIFI_PASSWORD"
#define EXAMPLE_MULTICAST_IPV4_ADDR "239.255.255.250"
#define EXAMPLE_MULTICAST_PORT 1900
static const char *TAG = "udp_multicast";
static void wifi_init_sta(void)
{
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
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, "Connecting to %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());
ESP_LOGI(TAG, "Connected to %s", wifi_config.sta.ssid);
}
void udp_receiver_task(void *pvParameters)
{
int sockfd;
struct sockaddr_in local_addr;
struct sockaddr_in multicast_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
ESP_LOGE(TAG, "Failed to create socket");
vTaskDelete(NULL);
}
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(EXAMPLE_MULTICAST_PORT);
if (bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
ESP_LOGE(TAG, "Failed to bind socket");
vTaskDelete(NULL);
}
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = inet_addr(EXAMPLE_MULTICAST_IPV4_ADDR);
multicast_addr.sin_port = htons(EXAMPLE_MULTICAST_PORT);
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(EXAMPLE_MULTICAST_IPV4_ADDR);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
ESP_LOGE(TAG, "Failed to join multicast group");
vTaskDelete(NULL);
}
char rx_buffer[128];
while (1) {
struct sockaddr_in source_addr;
socklen_t socklen = sizeof(source_addr);
int len = recvfrom(sockfd, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
if (len < 0) {
ESP_LOGE(TAG, "Failed to receive data");
break;
} else {
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
ESP_LOGI(TAG, "Received %d bytes from %s:%d", len, inet_ntoa(source_addr.sin_addr), ntohs(source_addr.sin_port));
ESP_LOGI(TAG, "%s", rx_buffer);
}
}
close(sockfd);
vTaskDelete(NULL);
}
void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
wifi_init_sta();
xTaskCreate(udp_receiver_task, "udp_receiver_task", 4096, NULL, 5, NULL);
}
```
在这个示例中,我们首先初始化WiFi连接,然后创建一个UDP套接字并将其绑定到本地地址和端口。接下来,我们加入多播组并等待接收数据。当数据到达时,我们打印接收到的数据和来源地址和端口。
阅读全文