How to configure static ip in ap mode

FirstUser
Posts: 3
Joined: Wed Jan 25, 2023 6:33 pm

How to configure static ip in ap mode

Postby FirstUser » Wed Jan 25, 2023 7:10 pm

I am trying to configure the esp32 devkit to work with the static ip in AP MODE, but everytime that any device is connected in acess point created, it break down de connection.

The firmware read the nvs data to restore the last wifi configuration, if it is clean, them, it init the ap mode to receive new credentials from user.
  1. void wifi_init_softap(void)
  2. {
  3.     esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
  4.     assert(ap_netif);
  5.  
  6.     ESP_ERROR_CHECK(esp_wifi_stop());
  7.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
  8.  
  9.     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  10.                                                 ESP_EVENT_ANY_ID,
  11.                                                 &wifi_event_handler,
  12.                                                 NULL,
  13.                                                 NULL));
  14.  
  15.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT,
  16.                                                 IP_EVENT_AP_STAIPASSIGNED,
  17.                                                 &connect_handler,
  18.                                                 &server));
  19.  
  20.     wifi_config_t wifi_config =
  21.     {
  22.         .ap = {
  23.             .ssid = ESP_AP_WIFI_SSID,
  24.             .ssid_len = strlen(ESP_AP_WIFI_SSID),
  25.             .channel = ESP_AP_WIFI_CHANNEL,
  26.             .password = ESP_AP_WIFI_PASS,
  27.             .max_connection = ESP_AP_MAX_STA_CONN,
  28.             .authmode = WIFI_AUTH_WPA_WPA2_PSK
  29.         },
  30.     };
  31.     if (strlen(ESP_AP_WIFI_PASS) == 0) {
  32.         wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  33.     }
  34.  
  35.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  36.  
  37.     /* STATIC IP BEGIN*/
  38.     ESP_ERROR_CHECK(esp_netif_dhcps_stop(ap_netif));
  39.  
  40.     esp_netif_ip_info_t ip_info;
  41.  
  42.     IP4_ADDR(&ip_info.ip, 10, 34, 46, 5);
  43.     IP4_ADDR(&ip_info.gw, 10, 34, 46, 1);
  44.     IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
  45.  
  46.     ESP_ERROR_CHECK(esp_netif_set_ip_info(ap_netif, &ip_info));
  47.     esp_netif_dhcps_start(ap_netif);
  48.     /* STATIC IP ENDS*/
  49.  
  50.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
  51.     ESP_ERROR_CHECK(esp_wifi_start());
  52.    
  53.     ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
  54.             ESP_AP_WIFI_SSID, ESP_AP_WIFI_PASS, ESP_AP_WIFI_CHANNEL);  
  55. }
  56.  
  57. void wifi_init(void)
  58. {
  59.     wifi_config_t conf;
  60.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  61.  
  62.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  63.     ESP_ERROR_CHECK(esp_netif_init() );
  64.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  65.  
  66.     // esp_netif_create_default_wifi_ap();
  67.     esp_netif_create_default_wifi_sta();
  68.  
  69.     esp_err_t ret = esp_wifi_get_config(ESP_IF_WIFI_STA, &conf);
  70.  
  71.     if (ret == ESP_OK)
  72.     {
  73.         ESP_LOGI(TAG, "Wifi configuration already stored in flash partition called NVS");
  74.         ESP_LOGI(TAG, "%s" ,conf.sta.ssid);
  75.         ESP_LOGI(TAG, "%s" ,conf.sta.password);
  76.  
  77.         // int isDiff = strcmp((char *)conf.sta.ssid, ESP_STA_WIFI_SSID);
  78.         int hasData = strcmp((char *)conf.sta.ssid, "");
  79.        
  80.         if(hasData)
  81.             wifi_init_sta((char*)conf.sta.ssid, (char*)conf.sta.password);
  82.         else wifi_init_softap();
  83.     }
  84. }
  85.  
  86. void app_main(void)
  87. {
  88.     //Initialize NVS
  89.     esp_err_t ret = nvs_flash_init();
  90.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  91.       ESP_ERROR_CHECK(nvs_flash_erase());
  92.       ret = nvs_flash_init();
  93.     }
  94.     ESP_ERROR_CHECK(ret);
  95.  
  96.     wifi_init();
  97. }

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

Re: How to configure static ip in ap mode

Postby ESP_YJM » Sat Jan 28, 2023 3:46 am

Your AP IP address is class A, and the netmask can't be 255.255.255.0, you can change

Code: Select all

IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
to

Code: Select all

IP4_ADDR(&ip_info.netmask, 255, 0, 0, 0);

FirstUser
Posts: 3
Joined: Wed Jan 25, 2023 6:33 pm

Re: How to configure static ip in ap mode

Postby FirstUser » Mon Jan 30, 2023 6:05 pm

Thank you so much @ESP_YJM, but the problem continue.

This is the output, after make the change. It join and leave in loop, like before.
  1. I (14807) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 10.34.46.6
  2. I (14807) Webserver: Starting webserver
  3. I (14807) Webserver: Starting server on port: '80'
  4. I (14817) Webserver: Registering URI handlers
  5. W (15247) wifi:<ba-add>idx:2 (ifx:1, 64:a2:00:1e:18:e6), tid:0, ssn:18, winSize:64
  6. I (17457) wifi:station: 64:a2:00:1e:18:e6 leave, AID = 1, bss_flags is 658547, bss:0x3ffb994c
  7. I (17467) wifi:new:<1,0>, old:<1,0>, ap:<1,0>, sta:<255,255>, prof:1
  8. W (17467) wifi:<ba-del>idx
  9. I (17467) Webserver: station 64:a2:00:1e:18:e6 leave, AID=1
  10. I (18787) wifi:new:<1,0>, old:<1,0>, ap:<1,0>, sta:<255,255>, prof:1
  11. I (18787) wifi:station: 64:a2:00:1e:18:e6 join, AID=1, bgn, 20
  12. I (18807) Webserver: station 64:a2:00:1e:18:e6 join, AID=1
  13. I (22707) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 10.34.46.6
  14. W (23167) wifi:<ba-add>idx:2 (ifx:1, 64:a2:00:1e:18:e6), tid:0, ssn:16, winSize:64
  15. I (25377) wifi:station: 64:a2:00:1e:18:e6 leave, AID = 1, bss_flags is 658531, bss:0x3ffb994c

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

Re: How to configure static ip in ap mode

Postby ESP_YJM » Tue Jan 31, 2023 6:15 am

It seems the station connected to the AP and get the IP 10.34.46.6. But the station still leave the connection from AP. I run your example and the station not auto leave the AP. Could you please try other station.

FirstUser
Posts: 3
Joined: Wed Jan 25, 2023 6:33 pm

Re: How to configure static ip in ap mode

Postby FirstUser » Mon Feb 06, 2023 1:09 pm

Thank you for take time to help me. I Try to connect with other station and it's realy working. I will try understand because it's heppening. Thank you so much.

Who is online

Users browsing this forum: Google [Bot] and 113 guests