This commit is contained in:
@@ -776,69 +776,32 @@ esp_err_t web_server_start(void)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* AP netif must exist before wifi start */
|
||||
esp_netif_t *ap = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
|
||||
if (ap == NULL) {
|
||||
ap = esp_netif_create_default_wifi_ap();
|
||||
/* If Wi-Fi is not yet started, configure AP and start it */
|
||||
wifi_mode_t mode;
|
||||
if (esp_wifi_get_mode(&mode) != ESP_OK) {
|
||||
esp_netif_t *ap = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
|
||||
if (ap == NULL) {
|
||||
ESP_LOGE(TAG, "failed to create AP netif");
|
||||
return ESP_FAIL;
|
||||
ap = esp_netif_create_default_wifi_ap();
|
||||
if (ap != NULL) {
|
||||
esp_netif_set_hostname(ap, "mtg-companion");
|
||||
}
|
||||
}
|
||||
esp_netif_set_hostname(ap, "mtg-companion");
|
||||
(void)esp_wifi_set_mode(WIFI_MODE_APSTA);
|
||||
wifi_config_t wifi_cfg = {
|
||||
.ap = {
|
||||
.ssid_len = sizeof(AP_SSID) - 1,
|
||||
.max_connection = 4,
|
||||
.authmode = (AP_PASS[0] == '\0') ? WIFI_AUTH_OPEN : WIFI_AUTH_WPA2_PSK,
|
||||
},
|
||||
};
|
||||
strncpy((char *)wifi_cfg.ap.ssid, AP_SSID, sizeof(wifi_cfg.ap.ssid) - 1);
|
||||
if (AP_PASS[0] != '\0') {
|
||||
strncpy((char *)wifi_cfg.ap.password, AP_PASS, sizeof(wifi_cfg.ap.password) - 1);
|
||||
}
|
||||
(void)esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg);
|
||||
(void)esp_wifi_start();
|
||||
}
|
||||
|
||||
esp_err_t ret = esp_wifi_stop(); /* safe even if not started */
|
||||
if (ret != ESP_OK && ret != ESP_ERR_WIFI_NOT_STARTED) {
|
||||
ESP_LOGE(TAG, "wifi stop failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_wifi_set_mode(WIFI_MODE_APSTA);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "set mode APSTA failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
wifi_config_t wifi_cfg = {
|
||||
.ap = {
|
||||
.ssid_len = sizeof(AP_SSID) - 1,
|
||||
.max_connection = 4,
|
||||
.authmode = (AP_PASS[0] == '\0') ? WIFI_AUTH_OPEN : WIFI_AUTH_WPA2_PSK,
|
||||
},
|
||||
};
|
||||
strncpy((char *)wifi_cfg.ap.ssid, AP_SSID, sizeof(wifi_cfg.ap.ssid) - 1);
|
||||
if (AP_PASS[0] != '\0') {
|
||||
strncpy((char *)wifi_cfg.ap.password, AP_PASS, sizeof(wifi_cfg.ap.password) - 1);
|
||||
}
|
||||
ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "set AP config failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_netif_ip_info_t ip_info = {
|
||||
.ip = { .addr = ESP_IP4TOADDR(192, 168, 4, 1) },
|
||||
.netmask = { .addr = ESP_IP4TOADDR(255, 255, 255, 0) },
|
||||
.gw = { .addr = ESP_IP4TOADDR(192, 168, 4, 1) },
|
||||
};
|
||||
/* The AP netif has DHCP running by default; stop it before assigning a
|
||||
* static IP, otherwise esp_netif_set_ip_info fails with
|
||||
* ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED. */
|
||||
esp_netif_dhcps_stop(ap);
|
||||
ret = esp_netif_set_ip_info(ap, &ip_info);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "set AP IP failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = esp_wifi_start();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "wifi start (APSTA) failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
(void)esp_netif_dhcps_start(ap); /* harmless if someone else started it */
|
||||
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.stack_size = 8 * 1024;
|
||||
config.max_uri_handlers = 24;
|
||||
@@ -846,7 +809,7 @@ esp_err_t web_server_start(void)
|
||||
config.keep_alive_enable = true;
|
||||
config.max_open_sockets = 4;
|
||||
|
||||
ret = httpd_start(&s_server, &config);
|
||||
esp_err_t ret = httpd_start(&s_server, &config);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "httpd_start failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user