Page 1 of 1

DNS lookup fails when assigning a static IP to AP

Posted: Tue Nov 03, 2020 7:01 pm
by daniel.kjellstrom
Hi,

I'm not able to get my esp to connect to a desired server, it always returns error "HTTP_CLIENT: Connection failed, sock < 0". By doing some research on the esp forums etc I found that people were having problems to make their DNS to work when running the ESP32 on either STA or AP mode.
Now I'm trying to connect to my server to make some REST requests but it always fails. So I commented the part of the code where I assigned a static IP to my AP (static IP only in AP mode) and then it started to work.

I tried to set the DNS by hand. Tried with most common ones like 8.8.8.8 and 1.1.1.1 but didn't work.

This is the part of the code where I assign it a static IP and a specific DNS server.

Code: Select all

	my_ap = esp_netif_create_default_wifi_ap();

    	esp_netif_ip_info_t ip_info = {0};
    	esp_netif_dns_info_t dns_info = {0};
    	IP4_ADDR(&ip_info.ip, 192,168,1,1);
	IP4_ADDR(&ip_info.gw, 192,168,1,1);
	IP4_ADDR(&ip_info.netmask, 255,255,255,0);
	IP_ADDR4(&dns_info.ip, 8, 8, 8, 8);

    	esp_netif_dhcps_stop(my_ap);
    	esp_netif_set_ip_info(my_ap, &ip_info);
    	esp_netif_set_dns_info(my_ap, ESP_NETIF_DNS_MAIN, &dns_info);
    	esp_netif_dhcps_start(my_ap);
Any ideas?

Re: DNS lookup fails when assigning a static IP to AP

Posted: Thu Oct 28, 2021 8:03 am
by Tagima
Good morning,

This is a late reply, but based on your code I got it to work in my ESP:

Code: Select all

    esp_netif_init();

    esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();

    esp_netif_ip_info_t ip_info = {0};
    esp_netif_dns_info_t dns_info = {0};

    IP4_ADDR(&ip_info.ip, 192, 168, 43, 90);
    IP4_ADDR(&ip_info.gw, 192, 168, 43, 1);
    IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);

    IP_ADDR4(&dns_info.ip, 8, 8, 8, 8);

    esp_netif_dhcpc_stop(my_sta);

    esp_netif_set_ip_info(my_sta, &ip_info);
    esp_netif_set_dns_info(my_sta, ESP_NETIF_DNS_MAIN, &dns_info);
Beyond the difference in esp_netif_create_default_wifi_sta(), I also stopped the DHCP Client (esp_netif_dhcpc_stop()) instead of the server.

I hope it helps someone.