What is the proper way to init and deinit SoftAP + Https server with DHCP?

wwll123
Posts: 4
Joined: Mon Jul 27, 2020 6:44 am

What is the proper way to init and deinit SoftAP + Https server with DHCP?

Postby wwll123 » Tue Feb 09, 2021 9:51 am

Hi,

Module: esp32-wrover-b
ESP-IDF: ESP-IDF v4.1

Here is the init flow for SoftAp + Https server with DHCP.

Code: Select all

ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());

// Init ap mode and dhcp
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &EvtHandler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &EvtHandler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &EvtHandler, NULL));

psApHandler = esp_netif_create_default_wifi_ap();
esp_netif_dhcps_stop(psApHandler);
esp_netif_ip_info_t IP;
    IP4_ADDR(
        &IP.ip,
		DEFAULT_WIFI_AP_IP_1,
		DEFAULT_WIFI_AP_IP_2,
		DEFAULT_WIFI_AP_IP_3,
		DEFAULT_WIFI_AP_IP_4);
    IP4_ADDR(
        &IP.gw,
		DEFAULT_WIFI_AP_GW_1,
		DEFAULT_WIFI_AP_GW_2,
		DEFAULT_WIFI_AP_GW_3,
		DEFAULT_WIFI_AP_GW_4);
    IP4_ADDR(
        &IP.netmask,
		DEFAULT_WIFI_AP_NM_1,
		DEFAULT_WIFI_AP_NM_2,
		DEFAULT_WIFI_AP_NM_3,
		DEFAULT_WIFI_AP_NM_4);

esp_netif_set_ip_info(psApHandler, &IP);
esp_netif_dhcps_start(psApHandler);
esp_wifi_set_mode(WIFI_MODE_AP);
esp_wifi_set_config(ESP_IF_WIFI_AP, &WifiApConfig);
esp_wifi_start();

// Init https server
httpd_ssl_config_t Config = HTTPD_SSL_CONFIG_DEFAULT();
Config.transport_mode = HTTPD_SSL_TRANSPORT_SECURE;
Config.httpd.max_uri_handlers = HTTPS_SERVER_MAX_URI_HANDLERS;
Config.cacert_pem = https_server_cert_pem_start;
Config.cacert_len = https_server_cert_pem_end - https_server_cert_pem_start;
Config.prvtkey_pem = https_server_prvt_key_pem_start;
Config.prvtkey_len = https_server_prvt_key_pem_end - https_server_prvt_key_pem_start;
httpd_ssl_start(&HttpsServerHandler, &Config);
httpd_register_uri_handler(HttpsServerHandler, &GetSysUri);

After using notebook to connect to above ap, using Postman can communicate with its RESTful API with no issue.
However, there is error during exiting server and ap mode.



After esp32 receiving specific string, it would perform following code to stop https server and ap mode. Is this correct way to stop https server, dhcp and ap mode?

Code: Select all

httpd_ssl_stop(HttpsServerHandler);
esp_netif_dhcps_stop(psApHandler);
esp_netif_destroy(psApHandler);
esp_wifi_stop();
esp_wifi_deinit();
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &EvtHandler));
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &EvtHandler));
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &EvtHandler));
Sometimes above code would trigger following error code.

Code: Select all

E (457925) esp-netif_lwip-ppp: esp_netif_stop_ppp not supported, please enable PPP in lwIP component configuration
I (457926) Wifi: Wifi event: WIFI_EVENT_AP_STADISCONNECTED
I (457926) Wifi: Wifi event: WIFI_EVENT_AP_STOP
E (457927) esp-netif_lwip-ppp: esp_netif_stop_ppp not supported, please enable PPP in lwIP component configuration

Who is online

Users browsing this forum: hdsjulian2 and 128 guests