Setting Static IP for AP Mode not working
Posted: 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
- void WEBCONFIG_Init()
- {
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
- {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
- ESP_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
- assert(ap_netif);
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- uint16_t number = 10;
- wifi_ap_record_t ap_info[10];
- uint16_t ap_count = 0;
- memset(ap_info, 0, sizeof(ap_info));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
- ESP_ERROR_CHECK(esp_wifi_start());
- esp_wifi_scan_start(NULL, true);
- ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
- ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
- ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
- cJSON *ssidArray, *root;
- root = cJSON_CreateObject();
- cJSON_AddItemToObject(root, "ssid", ssidArray = cJSON_CreateArray());
- for (int i = 0; (i < 10) && (i < ap_count); i++)
- {
- cJSON_AddStringToObject(ssidArray, "ssid", (char *)ap_info[i].ssid);
- }
- char *string = cJSON_PrintUnformatted(root);
- printf("%s\n", string);
- memset(WifiJsonBuffer, 0x00, sizeof(WifiJsonBuffer));
- strcpy(WifiJsonBuffer, string);
- cJSON_Delete(root);
- cJSON_free(string);
- esp_wifi_stop();
- ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
- ESP_EVENT_ANY_ID,
- &wifi_event_handler,
- NULL,
- NULL));
- wifi_config_t wifi_config = {
- .ap = {
- .ssid = "smart1",
- .max_connection = 3,
- .authmode = WIFI_AUTH_OPEN},
- };
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
- ESP_ERROR_CHECK(esp_netif_dhcpc_stop(ap_netif));
- esp_netif_ip_info_t ip_info;
- IP4_ADDR(&ip_info.ip, 10, 0, 10, 5);
- IP4_ADDR(&ip_info.gw, 10, 0, 10, 1);
- IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
- ESP_ERROR_CHECK(esp_netif_set_ip_info(ap_netif, &ip_info));
- ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
- ESP_ERROR_CHECK(esp_wifi_start());
- }