martin-ger / esp32_nat_router

A simple NAT Router for the ESP32
1.38k stars 288 forks source link

STA Static IP not working #55

Open ghost opened 3 years ago

ghost commented 3 years ago

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 -> I (251407) cmd_router: STA Static IP settings 192.168.0.9/255.255.255.0/192.168.0.1 stored. 00:23:12.993 -> esp32> I (265727) HTTPServer: Found header => Host: 192.168.4.1 00:23:29.873 -> I (268307) HTTPServer: Found header => Host: 192.168.4.1 00:23:33.075 -> restart 00:23:33.406 -> I (271857) cmd_system: Restarting 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 -> idx 00:23:33.452 -> I (271887) ESP32 NAT router: disconnected - retry to connect to the AP 00:23:33.452 -> I (271897) ESP32 NAT router: station disconnected - 0 remain 00:23:33.500 -> W (271907) httpd_txrx: httpd_sock_err: error in recv : 113 00:23:33.546 -> I (271997) wifi:flush txq 00:23:33.546 -> I (271997) wifi:stop sw txq 00:23:33.546 -> I (271997) wifi:lmac stop hw txq 00:23:33.546 -> ets Jun 8 2016 00:22:57 00:23:33.594 -> 00:23:33.594 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) 00:23:33.594 -> configsip: 0, SPIWP:0xee 00:23:33.594 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 00:23:33.594 -> mode:DIO, clock div:2 00:23:33.594 -> load:0x3fff0030,len:5356 00:23:33.594 -> boss 0 tail 12 room 4 00:23:33.594 -> load:0x40078000,len:13732 00:23:33.594 -> load:0x40080400,len:3412 00:23:33.594 -> entry 0x40080634 00:23:33.921 -> I (354) cpu_start: Pro cpu up. 00:23:33.921 -> I (355) cpu_start: Starting app cpu, entry point is 0x40081538 00:23:33.921 -> I (341) cpu_start: App cpu up. 00:23:33.921 -> I (369) cpu_start: Pro cpu start user code 00:23:33.967 -> I (369) cpu_start: cpu freq: 160000000 00:23:33.967 -> I (369) cpu_start: Application information: 00:23:33.967 -> I (373) cpu_start: Project name: esp32_nat_router 00:23:33.967 -> I (379) cpu_start: App version: 5d490e2-dirty 00:23:33.967 -> I (385) cpu_start: Compile time: Apr 13 2021 18:01:34 00:23:33.967 -> I (391) cpu_start: ELF file SHA256: 4d6fb183f7eaecc5... 00:23:33.967 -> I (397) cpu_start: ESP-IDF: v4.4-dev-744-g1cb31e509 00:23:33.967 -> I (403) heap_init: Initializing. RAM available for dynamic allocation: 00:23:33.967 -> I (410) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM 00:23:34.013 -> I (416) heap_init: At 3FFB8118 len 00027EE8 (159 KiB): DRAM 00:23:34.013 -> I (423) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM 00:23:34.013 -> I (429) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM 00:23:34.013 -> I (435) heap_init: At 40097A00 len 00008600 (33 KiB): IRAM 00:23:34.013 -> I (443) spi_flash: detected chip: generic 00:23:34.013 -> I (446) spi_flash: flash io: dio 00:23:34.013 -> I (451) cpu_start: Starting scheduler on PRO CPU. 00:23:34.013 -> I (0) cpu_start: Starting scheduler on APP CPU. 00:23:34.060 -> I (538) ESP32 NAT router: Command history enabled 00:23:34.060 -> I (538) cmd_router: ssid [redacted] 00:23:34.060 -> I (538) cmd_router: passwd [redacted] 00:23:34.060 -> I (538) cmd_router: static_ip 192.168.0.9 00:23:34.106 -> I (548) cmd_router: subnet_mask 255.255.255.0 00:23:34.106 -> I (548) cmd_router: gateway_addr 192.168.0.1 00:23:34.106 -> I (558) cmd_router: ap_ssid [redacted] 00:23:34.106 -> I (558) cmd_router: ap_passwd [redacted] 00:23:34.106 -> I (578) wifi:wifi driver task: 3ffc79e8, prio:23, stack:3584, core=0 00:23:34.106 -> I (578) system_api: Base MAC address is not set 00:23:34.106 -> I (578) system_api: read default base MAC address from EFUSE 00:23:34.153 -> I (598) wifi:wifi firmware version: 4809a2e 00:23:34.153 -> I (598) wifi:wifi certification version: v7.0 00:23:34.153 -> I (598) wifi:config NVS flash: enabled 00:23:34.153 -> I (598) wifi:config nano formating: disabled 00:23:34.153 -> I (608) wifi:Init data frame dynamic rx buffer num: 32 00:23:34.153 -> I (608) wifi:Init management frame dynamic rx buffer num: 32 00:23:34.153 -> I (618) wifi:Init management short buffer num: 32 00:23:34.153 -> I (618) wifi:Init dynamic tx buffer num: 32 00:23:34.153 -> I (628) wifi:Init static rx buffer size: 1600 00:23:34.153 -> I (628) wifi:Init static rx buffer num: 10 00:23:34.153 -> I (628) wifi:Init dynamic rx buffer num: 32 00:23:34.200 -> I (638) wifi_init: rx ba win: 6 00:23:34.200 -> I (638) wifi_init: tcpip mbox: 32 00:23:34.200 -> I (648) wifi_init: udp mbox: 6 00:23:34.200 -> I (648) wifi_init: tcp mbox: 6 00:23:34.200 -> I (648) wifi_init: tcp tx win: 5744 00:23:34.200 -> I (658) wifi_init: tcp rx win: 5744 00:23:34.200 -> I (658) wifi_init: tcp mss: 1440 00:23:34.200 -> I (658) wifi_init: WiFi IRAM OP enabled 00:23:34.200 -> I (668) wifi_init: WiFi RX IRAM OP enabled 00:23:36.226 -> I (2678) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07 00:23:36.320 -> I (2778) wifi:mode : sta ([redacted]) + softAP ([redacted]) 00:23:36.320 -> I (2778) wifi:enable tsf 00:23:36.320 -> I (2778) wifi:Total power save buffer number: 16 00:23:36.320 -> I (2788) wifi:Init max length of beacon: 752/752 00:23:36.320 -> I (2788) wifi:Init max length of beacon: 752/752 00:23:36.320 -> I (2788) ESP32 NAT router: wifi_init_apsta finished. 00:23:36.320 -> I (2798) ESP32 NAT router: connect to ap SSID: [redacted]  00:23:36.367 -> I (2798) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<1,0>, prof:1 00:23:36.367 -> I (2808) wifi:state: init -> auth (b0) 00:23:36.367 -> I (2818) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0  00:23:36.367 -> I (2818) wifi:state: auth -> assoc (0) 00:23:36.367 -> I (2818) ESP32 NAT router: NAT is enabled 00:23:36.367 -> I (2828) wifi:state: assoc -> run (10) 00:23:36.367 -> I (2828) ESP32 NAT router: Starting config web server 00:23:36.367 -> W (2838) wifi:idx:0 (ifx:0, [redacted]), tid:0, ssn:0, winSize:64 00:23:36.367 -> I (2848) HTTPServer: Starting server on port: '80' 00:23:36.414 -> I (2848) HTTPServer: Registering URI handlers 00:23:36.414 -> I (2858) wifi:connected with [redacted], aid = 1, channel 1, BW20, bssid = [redacted] 00:23:36.414 -> I (2868) wifi:security: [redacted], phy: bgn, rssi: -61 00:23:36.414 -> I (2878) wifi:pm start, type: 1 00:23:36.414 -> 00:23:36.414 -> I (2878) esp_netif_handlers: sta ip: 192.168.0.9, mask: 255.255.255.0, gw: 192.168.0.1 00:23:36.414 -> I (2878 00:23:36.414 -> ESP32 NAT ROUTER 00:23:36.414 -> Type 'help' to get the list of commands. 00:23:36.462 -> Use UP/DOWN arrows to navigate through command history. 00:23:36.462 -> Press TAB when typing command name to auto-complete. 00:23:36.462 -> I (2958) wifi:AP's beacon interval = 102400 us, DTIM period = 1 00:23:36.651 -> 00:23:36.651 -> Your terminal application does not support escape sequences. 00:23:36.651 -> Line editing and history features are disabled. 00:23:36.651 -> On Windows, try using Putty instead. 00:23:36.651 -> esp32> W (5328) wifi:idx 00:23:42.566 -> I (9058) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<1,0>, prof:1 00:23:42.612 -> I (9058) wifi:station: [redacted] join, AID=1, bgn, 20 00:23:42.659 -> I (9128) ESP32 NAT router: 1. station connected 00:23:42.799 -> I (9288) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2 00:23:45.871 -> W (12318) wifi:idx:0 (ifx:0, [redacted]), tid:0, ssn:3, winSize:64 00:23:46.583 -> W (13068) wifi:idx:4 (ifx:1, [redacted]), tid:0, ssn:13, winSize:64 00:25:02.784 -> show 00:25:03.116 -> I (89588) cmd_router: ssid [redacted] 00:25:03.116 -> I (89588) cmd_router: passwd [redacted] 00:25:03.116 -> I (89588) cmd_router: static_ip 192.168.0.9 00:25:03.116 -> I (89598) cmd_router: subnet_mask 255.255.255.0 00:25:03.116 -> I (89608) cmd_router: gateway_addr 192.168.0.1 00:25:03.163 -> I (89608) cmd_router: ap_ssid [redacted] 00:25:03.163 -> I (89608) cmd_router: ap_passwd [redacted] 00:25:03.163 -> STA SSID: [redacted] Password: [redacted] 00:25:03.163 -> AP SSID: [redacted] Password: [redacted] 00:25:03.163 -> AP IP address: 192.168.4.1 00:25:03.163 -> Uplink AP connected 00:25:03.163 -> IP: 192.168.0.9 00:25:03.163 -> 1 Stations connected 00:25:03.163 -> esp32> ```
Stasfe commented 2 years ago

I have the same issue. Is there a solution for this?

maps2002 commented 2 years ago

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

Fretwire commented 2 years ago

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

esp32_nr_patch.txt

phucnguyentrong commented 4 months ago

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