APSTA MODE issue

sathish
Posts: 7
Joined: Sat Jan 11, 2020 7:23 am

APSTA MODE issue

Postby sathish » Tue Jan 14, 2020 9:43 am

Hi,
I am trying to connect with internet by esp32 AP-STA mode.But it connect to wifi and shows it has no internet connection.if there is any solution to connect esp32 with internet by using AP or AP-STA mode.

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());

Is this code is possible to connect it to the internet or what change I want to do in this code.


Thank you

sathish
Posts: 7
Joined: Sat Jan 11, 2020 7:23 am

Re: APSTA MODE issue

Postby sathish » Mon Jan 20, 2020 1:20 pm

Hi,

I am using this code and try to connect internet.

#define EXAMPLE_ESP_WIFI_SSID "Esp32"
#define EXAMPLE_ESP_WIFI_PASS "123456789"
#define EXAMPLE_MAX_STA_CONN 1

#define EXAMPLE_WIFI_SSID "Test"
#define EXAMPLE_WIFI_PASS "test1234"

const int CONNECTED_BIT = BIT0;
static EventGroupHandle_t s_wifi_event_group;

static const char *TAG = "wifi softAP";

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d",
MAC2STR(event->event_info.sta_connected.mac),
event->event_info.sta_connected.aid);
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d",
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:

esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}

void wifi_init_softap()
{
s_wifi_event_group = xEventGroupCreate();

tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
.password = EXAMPLE_ESP_WIFI_PASS,
.max_connection = EXAMPLE_MAX_STA_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
wifi_config_t wifi_config1 = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS,
},
};

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config1.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config1));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}

void app_main()
{
//Initialize NVS
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_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
}

I am using the above code. It is act as both station mode and wifi mode but Not able to connect internet in APSTA mode. What i want to do ?To access the internet connection by using Esp32.

rsimpsonbusa
Posts: 124
Joined: Tue May 17, 2016 8:12 pm

Re: APSTA MODE issue

Postby rsimpsonbusa » Tue Jan 21, 2020 6:16 pm

Hi @satish.

I had the same problem. Solution is to memset(& wifi_config,0, sizeof( wifi_config)) before EACH mode setting, AP and STA.
Example

memset(& wifi_config,0, sizeof( wifi_config)); // set the wifi_config for AP ....
wifi_config.ap.ssid = EXAMPLE_ESP_WIFI_SSID;
wifi_config.ap.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID);
wifi_config.ap.password = EXAMPLE_ESP_WIFI_PASS;
wifi_config.ap.max_connection = EXAMPLE_MAX_STA_CONN;
wifi_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));

//now for the STA
memset(& wifi_config,0, sizeof( wifi_config)); //set the wifi_config for STA ....
wifi_config.sta .ssid = EXAMPLE_WIFI_SSID,
wifi_config.sta .password = EXAMPLE_WIFI_PASS,
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));

I have a faint idea the reason we cannot set both AP and STA is the SAME wifi_config variable is because the Struct is a Union and some parameters are left dangling or not initialized to 0.

Enjoy.

Robert

sathish
Posts: 7
Joined: Sat Jan 11, 2020 7:23 am

Re: APSTA MODE issue

Postby sathish » Wed Jan 22, 2020 7:46 am

Hi

I need to act my esp32 as AP and my mobile device as Station. i need to access internet on my mobile using esp32 AP network, is this possible.


Help me out to solve this issue


Thank you,

Who is online

Users browsing this forum: Bing [Bot], chriskuku, Google [Bot] and 74 guests