added AP+STA mode to wifi
This commit is contained in:
parent
b1d0da6d2c
commit
0cf14154b3
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright 2022 Bogdan Pilyugin
|
/* Copyright 2022 Bogdan Pilyugin
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -162,7 +162,7 @@ static void wifi_init_softap(void *pvParameter)
|
||||||
wifi_config_t wifi_config = {
|
wifi_config_t wifi_config = {
|
||||||
.ap = {
|
.ap = {
|
||||||
|
|
||||||
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
||||||
.max_connection = EXAMPLE_MAX_STA_CONN,
|
.max_connection = EXAMPLE_MAX_STA_CONN,
|
||||||
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
||||||
},
|
},
|
||||||
|
|
@ -291,15 +291,192 @@ static void wifi_init_sta(void *pvParameter)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The event will not be processed after unregister */
|
/* The event will not be processed after unregister */
|
||||||
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
||||||
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister( WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
// ESP_ERROR_CHECK(esp_event_handler_instance_unregister( WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
||||||
// vEventGroupDelete(s_wifi_event_group);
|
// vEventGroupDelete(s_wifi_event_group);
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wifi_init_apsta(void *pvParameter)
|
||||||
|
{
|
||||||
|
s_wifi_event_group = xEventGroupCreate();
|
||||||
|
//BEGIN AP MODE IF
|
||||||
|
char ap_if_key_str[24];
|
||||||
|
esp_netif_inherent_config_t ap_esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
|
||||||
|
;
|
||||||
|
|
||||||
|
strcpy(ap_if_key_str, "WIFI_AP_USER");
|
||||||
|
ap_esp_netif_conf.if_key = ap_if_key_str;
|
||||||
|
ap_esp_netif_conf.route_prio = AP_PRIO;
|
||||||
|
|
||||||
|
esp_netif_config_t cfg_netif = {
|
||||||
|
.base = &ap_esp_netif_conf,
|
||||||
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
|
||||||
|
};
|
||||||
|
|
||||||
|
ap_netif = esp_netif_new(&cfg_netif);
|
||||||
|
assert(ap_netif);
|
||||||
|
//END AP MODE IF
|
||||||
|
|
||||||
|
//BEGIN STA MODE IF
|
||||||
|
char sta_if_key_str[24];
|
||||||
|
esp_netif_inherent_config_t staesp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
|
||||||
|
strcpy(sta_if_key_str, "WIFI_STA_USER");
|
||||||
|
staesp_netif_conf.if_key = sta_if_key_str;
|
||||||
|
staesp_netif_conf.route_prio = STA_PRIO;
|
||||||
|
esp_netif_config_t sta_cfg_netif = {
|
||||||
|
.base = &staesp_netif_conf,
|
||||||
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
|
||||||
|
};
|
||||||
|
sta_netif = esp_netif_new(&sta_cfg_netif);
|
||||||
|
assert(sta_netif);
|
||||||
|
//END STA MODE IF
|
||||||
|
|
||||||
|
//BEGIN AP MODE CONFIGURATION
|
||||||
|
esp_netif_ip_info_t ip_info;
|
||||||
|
memcpy(&ip_info.ip, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||||
|
memcpy(&ip_info.gw, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||||
|
memcpy(&ip_info.netmask, &GetSysConf()->wifiSettings.InfMask, 4);
|
||||||
|
|
||||||
|
esp_netif_dns_info_t dns_info;
|
||||||
|
memcpy(&dns_info, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||||
|
|
||||||
|
esp_netif_dhcps_stop(ap_netif);
|
||||||
|
esp_netif_set_ip_info(ap_netif, &ip_info);
|
||||||
|
esp_netif_set_dns_info(ap_netif, ESP_NETIF_DNS_MAIN, &dns_info);
|
||||||
|
esp_netif_dhcps_start(ap_netif);
|
||||||
|
|
||||||
|
esp_netif_attach_wifi_ap(ap_netif);
|
||||||
|
esp_wifi_set_default_wifi_ap_handlers();
|
||||||
|
|
||||||
|
wifi_init_config_t ap_cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||||
|
;
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_init(&ap_cfg));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||||
|
ESP_EVENT_ANY_ID,
|
||||||
|
&event_handler,
|
||||||
|
NULL,
|
||||||
|
NULL));
|
||||||
|
|
||||||
|
wifi_config_t ap_wifi_config = {
|
||||||
|
.ap = {
|
||||||
|
|
||||||
|
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
||||||
|
.max_connection = EXAMPLE_MAX_STA_CONN,
|
||||||
|
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (strlen(DEFAULT_WIFI_SSID_AP_KEY) == 0)
|
||||||
|
{
|
||||||
|
ap_wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(ap_wifi_config.ap.ssid, GetSysConf()->wifiSettings.ApSSID, strlen(GetSysConf()->wifiSettings.ApSSID));
|
||||||
|
memcpy(ap_wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey,
|
||||||
|
strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||||
|
ap_wifi_config.ap.ssid_len = strlen(GetSysConf()->wifiSettings.ApSSID);
|
||||||
|
//END AP MODE CONFIGURATION
|
||||||
|
|
||||||
|
//BEGIN STA MODE CONFIGURATION
|
||||||
|
//esp_netif_ip_info_t ip_info;
|
||||||
|
memcpy(&ip_info.ip, &GetSysConf()->wifiSettings.InfIPAddr, 4);
|
||||||
|
memcpy(&ip_info.gw, &GetSysConf()->wifiSettings.InfGateway, 4);
|
||||||
|
memcpy(&ip_info.netmask, &GetSysConf()->wifiSettings.InfMask, 4);
|
||||||
|
esp_netif_dns_info_t sta_dns_info;
|
||||||
|
memcpy(&sta_dns_info, &GetSysConf()->wifiSettings.DNSAddr1, 4);
|
||||||
|
|
||||||
|
esp_netif_dhcpc_stop(sta_netif);
|
||||||
|
esp_netif_set_ip_info(sta_netif, &ip_info);
|
||||||
|
esp_netif_set_dns_info(sta_netif, ESP_NETIF_DNS_MAIN, &sta_dns_info);
|
||||||
|
|
||||||
|
//esp_netif_str_to_ip4(&GetSysConf()->wifiSettings.DNSAddr3, (esp_ip4_addr_t*)(&dns_info.ip));
|
||||||
|
memcpy(&sta_dns_info.ip, &GetSysConf()->wifiSettings.DNSAddr3, sizeof(esp_ip4_addr_t));
|
||||||
|
|
||||||
|
esp_netif_set_dns_info(sta_netif, ESP_NETIF_DNS_FALLBACK, &sta_dns_info);
|
||||||
|
|
||||||
|
if (GetSysConf()->wifiSettings.Flags1.bIsDHCPEnabled)
|
||||||
|
esp_netif_dhcpc_start(sta_netif);
|
||||||
|
|
||||||
|
esp_netif_attach_wifi_station(sta_netif);
|
||||||
|
esp_wifi_set_default_wifi_sta_handlers();
|
||||||
|
|
||||||
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||||
|
;
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||||
|
|
||||||
|
esp_event_handler_instance_t instance_any_id;
|
||||||
|
esp_event_handler_instance_t instance_got_ip;
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||||
|
ESP_EVENT_ANY_ID,
|
||||||
|
&event_handler,
|
||||||
|
NULL,
|
||||||
|
&instance_any_id));
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||||
|
IP_EVENT_STA_GOT_IP,
|
||||||
|
&event_handler,
|
||||||
|
NULL,
|
||||||
|
&instance_got_ip));
|
||||||
|
|
||||||
|
wifi_config_t sta_wifi_config = {
|
||||||
|
.sta = {
|
||||||
|
/* Setting a password implies station will connect to all security modes including WEP/WPA.
|
||||||
|
* However these modes are deprecated and not advisable to be used. Incase your Access point
|
||||||
|
* doesn't support WPA2, these mode can be enabled by commenting below line */
|
||||||
|
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||||
|
|
||||||
|
.pmf_cfg = {
|
||||||
|
.capable = true,
|
||||||
|
.required = false
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
memcpy(sta_wifi_config.sta.ssid, GetSysConf()->wifiSettings.InfSSID, strlen(GetSysConf()->wifiSettings.InfSSID));
|
||||||
|
memcpy(sta_wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey,
|
||||||
|
strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||||
|
//END STA MODE CONFIGURATION
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_wifi_config));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_wifi_config));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "wifi_init_softap_sta finished");
|
||||||
|
|
||||||
|
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
||||||
|
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||||
|
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||||
|
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||||
|
pdFALSE,
|
||||||
|
pdFALSE,
|
||||||
|
portMAX_DELAY);
|
||||||
|
|
||||||
|
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||||
|
* happened. */
|
||||||
|
if (bits & WIFI_CONNECTED_BIT)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
|
||||||
|
GetSysConf()->wifiSettings.InfSSID,
|
||||||
|
GetSysConf()->wifiSettings.InfSecurityKey);
|
||||||
|
}
|
||||||
|
else if (bits & WIFI_FAIL_BIT)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
|
||||||
|
GetSysConf()->wifiSettings.InfSSID,
|
||||||
|
GetSysConf()->wifiSettings.InfSecurityKey);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||||
|
}
|
||||||
|
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiAPStart(void)
|
void WiFiAPStart(void)
|
||||||
{
|
{
|
||||||
xTaskCreate(wifi_init_softap, "InitSoftAPTask", 1024 * 4, (void*) 0, 3, NULL);
|
//xTaskCreate(wifi_init_softap, "InitSoftAPTask", 1024 * 4, (void*) 0, 3, NULL);
|
||||||
|
xTaskCreate(wifi_init_apsta, "InitSoftAPTask", 1024 * 4, (void*) 0, 3, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiSTAStart(void)
|
void WiFiSTAStart(void)
|
||||||
|
|
@ -307,4 +484,3 @@ void WiFiSTAStart(void)
|
||||||
xTaskCreate(wifi_init_sta, "InitStationTask", 1024 * 4, (void*) 0, 3, NULL);
|
xTaskCreate(wifi_init_sta, "InitStationTask", 1024 * 4, (void*) 0, 3, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user