Soft AP and DHCP Client IP Address Range

kidproquo
Posts: 1
Joined: Mon Apr 03, 2017 10:49 pm

Soft AP and DHCP Client IP Address Range

Postby kidproquo » Thu May 12, 2022 6:39 am

Hello,

I have an ESP-32 in Soft AP mode. I am trying to stream some data via UDP to a station and have hard-coded the recipient's IP address to 192.168.4.2.

I have set the max connections to 1. This works i.e. only one station can connect at a time. However, the first station that connects gets IP address 192.168.4.2 and the second one gets 192.168.4.3.

How do I configure the DHCP server to always assign 192.168.4.2 to the any station that connects i.e. limit the start and stop of the client IP address range to 192.168.4.2 - 192.168.4.2?

On a related note, I see these info message in the monitor console:
  1. I (20683) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2
How do I register an event handler for this? If I can register an event handler, I could then track the station IP address and send data to the latest assigned IP address.

On another related note, how do I send a broadcast UDP message to all stations ? If I try "192.168.4.255" or "255.255.255.255" as the recipient's IP address, this locks up the ESP32. My guess is that it's going into some kind of infinite loop because the broadcast mask includes its own IP address (192.168.4.1).

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: Soft AP and DHCP Client IP Address Range

Postby ESP_YJM » Fri May 20, 2022 10:42 am

Hi kidproquo
1. For the question that range the DHCP server IP from 192.168.4.2-192.168.4.2, you can call API esp_netif_dhcps_option.

Code: Select all

dhcps_lease_t dhcps_poll = {
    .enable = true
};
uint32_t len = sizeof(dhcps_poll);
IP4_ADDR(&dhcps_poll.start_ip, 192, 168, 4, 2);
IP4_ADDR(&dhcps_poll.end_ip, 192, 168, 4, 2);
esp_netif_dhcps_option(esp_netif, ESP_NETIF_OP_SET, REQUESTED_IP_ADDRESS, &dhcps_poll, len);
2. For the event register, you can regist IP_EVENT and event_id is IP_EVENT_AP_STAIPASSIGNED.

3. For the broadcast, you can follow this code

Code: Select all

esp_err_t esp_send_broadcast(void)
{
   int opt_val = 1;
   esp_err_t err = ESP_FAIL;
   struct sockaddr_in from_addr = {0};
   socklen_t from_addr_len = sizeof(struct sockaddr_in);

   // 创建 IPv4 UDP 套接字
   int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
   if (sockfd == -1) {
      ESP_LOGE(TAG, "Create UDP socket fail");
      return err;
   }

   // 设置 SO_BROADCAST 套接字选项, 使能该套接字支持广播发送
   int ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt_val, sizeof(int));
   if (ret < 0) {
      ESP_LOGE(TAG, "Set SO_BROADCAST option fail");
      goto exit;
   }

   // 设置广播目的地址和端口
   struct sockaddr_in dest_addr = {
      .sin_family      = AF_INET,
      .sin_port        = htons(3333),
      .sin_addr.s_addr = htonl(INADDR_BROADCAST),
   };

   char *broadcast_msg_buf = "TEST";

   // 调用 sendto 接口发送广播数据
   ret = sendto(sockfd, broadcast_msg_buf, strlen(broadcast_msg_buf), 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
   if (ret < 0) {
      ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
   } else {
      ESP_LOGI(TAG, "Message sent successfully");
      err = ESP_OK;
      }
exit:
   close(sockfd);
   return err;
}

Who is online

Users browsing this forum: No registered users and 121 guests