Setting Static IP for AP Mode not working

Pratikm78
Posts: 13
Joined: Wed Jun 26, 2019 6:03 pm

Setting Static IP for AP Mode not working

Postby Pratikm78 » Wed Mar 02, 2022 7:28 am

I am trying to perform a wifi scan to get the available wifi networks. Then switch to AP mode to host a web page from spiffs. However require the static IP address to be set. However this is not working. Followed the documentation on netif. Please assist this seems like it should be simple
  1. void WEBCONFIG_Init()
  2. {
  3.     esp_err_t ret = nvs_flash_init();
  4.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  5.     {
  6.         ESP_ERROR_CHECK(nvs_flash_erase());
  7.         ret = nvs_flash_init();
  8.     }
  9.     ESP_ERROR_CHECK(ret);
  10.     ESP_ERROR_CHECK(esp_netif_init());
  11.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  12.     esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
  13.     assert(ap_netif);
  14.      
  15.  
  16.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  17.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  18.     uint16_t number = 10;
  19.     wifi_ap_record_t ap_info[10];
  20.     uint16_t ap_count = 0;
  21.     memset(ap_info, 0, sizeof(ap_info));
  22.  
  23.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
  24.     ESP_ERROR_CHECK(esp_wifi_start());
  25.     esp_wifi_scan_start(NULL, true);
  26.     ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
  27.     ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
  28.     ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
  29.     cJSON *ssidArray, *root;
  30.     root = cJSON_CreateObject();
  31.     cJSON_AddItemToObject(root, "ssid", ssidArray = cJSON_CreateArray());
  32.  
  33.     for (int i = 0; (i < 10) && (i < ap_count); i++)
  34.     {
  35.         cJSON_AddStringToObject(ssidArray, "ssid", (char *)ap_info[i].ssid);
  36.     }
  37.  
  38.     char *string = cJSON_PrintUnformatted(root);
  39.     printf("%s\n", string);
  40.     memset(WifiJsonBuffer, 0x00, sizeof(WifiJsonBuffer));
  41.     strcpy(WifiJsonBuffer, string);
  42.     cJSON_Delete(root);
  43.     cJSON_free(string);
  44.  
  45.     esp_wifi_stop();
  46.  
  47.     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  48.                                                         ESP_EVENT_ANY_ID,
  49.                                                         &wifi_event_handler,
  50.                                                         NULL,
  51.                                                         NULL));
  52.  
  53.     wifi_config_t wifi_config = {
  54.         .ap = {
  55.             .ssid = "smart1",
  56.             .max_connection = 3,
  57.             .authmode = WIFI_AUTH_OPEN},
  58.     };
  59.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  60.  
  61.  
  62.     ESP_ERROR_CHECK(esp_netif_dhcpc_stop(ap_netif));
  63.  
  64.     esp_netif_ip_info_t ip_info;
  65.  
  66.     IP4_ADDR(&ip_info.ip, 10, 0, 10, 5);
  67.     IP4_ADDR(&ip_info.gw, 10, 0, 10, 1);
  68.     IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
  69.  
  70.     ESP_ERROR_CHECK(esp_netif_set_ip_info(ap_netif, &ip_info));
  71.     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
  72.     ESP_ERROR_CHECK(esp_wifi_start());
  73. }

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

Re: Setting Static IP for AP Mode not working

Postby ESP_YJM » Thu Mar 03, 2022 2:21 am

Maybe you need change ESP_ERROR_CHECK(esp_netif_dhcpc_stop(ap_netif)); to ESP_ERROR_CHECK(esp_netif_dhcps_stop(ap_netif));

Pratikm78
Posts: 13
Joined: Wed Jun 26, 2019 6:03 pm

Re: Setting Static IP for AP Mode not working

Postby Pratikm78 » Thu Mar 03, 2022 1:47 pm

@ESP_YJM your suggestion did stop the dchp however it is not setting a static IP

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

Re: Setting Static IP for AP Mode not working

Postby ESP_YJM » Fri Mar 04, 2022 6:20 am

Which error you got. Could you please describe it.

Pratikm78
Posts: 13
Joined: Wed Jun 26, 2019 6:03 pm

Re: Setting Static IP for AP Mode not working

Postby Pratikm78 » Fri Mar 04, 2022 9:26 am

No IP is assigned now.
  1. I (10505) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
  2. I (10515) wifi:station: f8:5e:a0:df:e5:cb join, AID=1, bgn, 40U
  3. I (10515) web config: station f8:5e:a0:df:e5:cb join, AID=1
  4. I (10515) web config: starting server
  5. W (10665) wifi:<ba-add>idx:4 (ifx:1, f8:5e:a0:df:e5:cb), tid:0, ssn:9, winSize:64
  6. I (44735) wifi:station: f8:5e:a0:df:e5:cb leave, AID = 1, bss_flags is 134243, bss:0x3ffd0270
  7. I (44735) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
  8. W (44735) wifi:<ba-del>idx
  9. I (44735) web config: station f8:5e:a0:df:e5:cb leave, AID=1

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

Re: Setting Static IP for AP Mode not working

Postby ESP_YJM » Fri Mar 04, 2022 12:17 pm

You can add esp_netif_dhcps_start(netif); after ESP_ERROR_CHECK(esp_netif_set_ip_info(ap_netif, &ip_info));

Who is online

Users browsing this forum: No registered users and 309 guests