Enabling DHCP server on WiFi station mode

shelving
Posts: 1
Joined: Sat Aug 06, 2022 10:29 pm

Enabling DHCP server on WiFi station mode

Postby shelving » Mon Aug 08, 2022 9:09 pm

I've been trying to use a ESP32 device as a DHCP server.
I'm able to enable it when on Access Point mode but when switching to Station mode I haveno success.
Steps I'm taking:
  1. esp_netif_init();
  2. esp_event_loop_create_default();
  3.  
  4. // Station mode
  5. esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
  6.  
  7. // stop dhcp client and set a static IP address
  8. esp_netif_dhcpc_stop(my_sta);
  9. esp_netif_ip_info_t ip_info;
  10. // [...]
  11. esp_netif_set_ip_info(my_sta, &ip_info);
  12.  
  13. // start dhcp server
  14. esp_netif_dhcps_start(my_sta);
  15.  
  16. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  17. esp_wifi_init(&cfg);
  18. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);
  19. esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL);
  20.  
  21. // connect to WiFi
  22. wifi_config_t wifi_config = {
  23.     .sta = {
  24.         .ssid = MY_SSID,
  25.         .password = MY_PASSWORD,
  26.     },
  27. };
  28. esp_wifi_set_mode(WIFI_MODE_STA);
  29. esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
  30. esp_wifi_start();
From this I'm able to connect to the wireless network and get an IP address (the static one), but the DHCP server doesn't start and stays at the INIT state.

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

Re: Enabling DHCP server on WiFi station mode

Postby ESP_YJM » Mon Aug 15, 2022 6:16 am

STA netif not support DHCP server. You should create a AP netif and run DHCP server on it.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot] and 76 guests