Open ghost opened 3 years ago
I have the same issue. Is there a solution for this?
Hi, I also wanted to use@martin-ger great work but with a static IP on the STA. I think the original code is not working because when a static ip is defined, the DNS is not properly configured.
I had success by changing a piece of code in file: esp32_nat_router.c, function: wifi_event_handler, for the switch/case of: SYSTEM_EVENT_STA_GOT_IP
This piece of original code:
if (esp_netif_get_dns_info(wifiSTA, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK) {
dhcps_dns_setserver((const ip_addr_t *)&dns.ip);
ESP_LOGI(TAG, "DHCP: set dns to:" IPSTR, IP2STR(&dns.ip.u_addr.ip4));
}
I changed it to:
if (has_static_ip) {
// Configure static DNS from DEFAULT DNS
ip_addr_t dnsserver;
dnsserver.u_addr.ip4.addr = ipaddr_addr(DEFAULT_DNS);
dnsserver.type = IPADDR_TYPE_V4;
dhcps_dns_setserver(&dnsserver);
ESP_LOGI(TAG, "STATIC IP: set dns to:" IPSTR, IP2STR(&dnsserver.u_addr.ip4));
}
else {
// Configure DNS from STA DHCP
if (esp_netif_get_dns_info(wifiSTA, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK) {
dhcps_dns_setserver((const ip_addr_t *)&dns.ip);
ESP_LOGI(TAG, "DHCP: set dns to:" IPSTR, IP2STR(&dns.ip.u_addr.ip4));
}
}
In my case, I also updated the DEFAULT_DNS define, as I wanted to use my router ip address. I am not using any of the portmap features, so I have not tested this change outside of my usage scope.
PS: The "new code" can probably be simplified to make the setserver and ESP_LOG instructions generic, and outside/after the if has_static_ip
Here's a patch with the above code. Three sections:
First: Change default DNS to opennic dns server instead of Google Second: Add (modified) above code to git source Third: Update web page to hide STA password
Change those lines of code: esp_netif_dhcpc_stop(ESP_IF_WIFI_STA); // Don't run a DHCP client esp_netif_set_ip_info(ESP_IF_WIFI_STA, &ipInfo_sta);
To this: esp_netif_dhcpc_stop(wifiSTA); // Don't run a DHCP client esp_netif_set_ip_info(wifiSTA, &ipInfo_sta);
Work for me
The esp32 setup connects to the internet without setting a static ip. But as soon as I set it, connection to the internet stops. It is connected to my router, yes I can access my router gateway. LAN works, but not internet.
I checked my dhcp clients, and the device "expressif" ip is not set to "192.168.0.9". On my computer wifi, setting static ip works, but just not on the esp32 for some reason.
Did I do something wrong? I have tried both 192.168.4.1 config page, and through serial monitor console set_sta_static.
Here is the serial monitor log of me setting via set_sta_static and restart to apply changes:
Click to expand log
``` 00:22:27.240 -> esp32> set_sta_static 192.168.0.9 255.255.255.0 192.168.0.1 00:23:12.993 -> [0;32mI (251407) cmd_router: STA Static IP settings 192.168.0.9/255.255.255.0/192.168.0.1 stored.[0m 00:23:12.993 -> esp32> [0;32mI (265727) HTTPServer: Found header => Host: 192.168.4.1[0m 00:23:29.873 -> [0;32mI (268307) HTTPServer: Found header => Host: 192.168.4.1[0m 00:23:33.075 -> restart 00:23:33.406 -> [0;32mI (271857) cmd_system: Restarting[0m 00:23:33.406 -> I (271857) wifi:state: run -> init (0) 00:23:33.452 -> I (271857) wifi:pm stop, total sleep time: 108057005 us / 268982778 us 00:23:33.452 -> 00:23:33.452 -> I (271857) wifi:new:<1,0>, old:<1,0>, ap:<1,1>, sta:<1,0>, prof:1 00:23:33.452 -> I (271867) wifi:station: [redacted] leave, AID = 1, bss_flags is 658531, bss:0x3ffd7da4 00:23:33.452 -> I (271877) wifi:new:<1,0>, old:<1,0>, ap:<1,1>, sta:<1,0>, prof:1 00:23:33.452 -> W (271877) wifi:W (271887) wifi:hmac tx: ifx0 stop, discard 00:23:33.452 ->