Help with wifi Station mode please

orbitcoms
Posts: 141
Joined: Fri Aug 03, 2018 10:08 pm
Location: Sydney, Australia

Help with wifi Station mode please

Postby orbitcoms » Tue Jun 22, 2021 9:52 pm

Hi,

I have a project that is having an issue in wifi "station mode". To test the wifi and my web server I set up as soft AP and it works fine. When I then run as Station, I can see it attaches to my Wifi AP and seems to allocate an ip ok but when I browse that IP I get no response (as if the ip address is not right). Below is monitor output where you can see it has attached ok to "DrayTek_2G" wifi and assigned ip 192.168.1.17 but this url is not responsive at all.

Below this is the code I use for setting up wifi station.

I (7739) wifi_init: WiFi IRAM OP enabled
I (7749) wifi_init: WiFi RX IRAM OP enabled
I (7789) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (7879) wifi:mode : sta (ac:67:b2:cb:c4:b0)
I (7879) wifi:enable tsf
I (8499) wifi:new:<6,1>, old:<1,0>, ap:<255,255>, sta:<6,1>, prof:1
I (9249) wifi:state: init -> auth (b0)
I (9269) wifi:state: auth -> assoc (0)
I (9339) wifi:state: assoc -> run (10)
W (9379) wifi:<ba-add>idx:0 (ifx:0, 16:49:bc:42:f6:a0), tid:5, ssn:0, winSize:64
I (9419) wifi:connected with DrayTek_2G, aid = 7, channel 6, 40U, bssid = 16:49:bc:42:f6:a0
I (9419) wifi:security: WPA2-PSK, phy: bgn, rssi: -53
I (9429) wifi:pm start, type: 1

W (9479) wifi:<ba-add>idx:1 (ifx:0, 16:49:bc:42:f6:a0), tid:0, ssn:4, winSize:64
I (9509) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (10149) esp_netif_handlers: sta ip: 192.168.1.17, mask: 255.255.255.0, gw: 192.168.1.1
I (10149) wifi station: got ip:192.168.1.17
I (10149) wifi station: connected to AP:DrayTek_2G
I (10159) WEB SERVER: Web Server Running

[Codebox=c file=Untitled.c]
static int s_retry_num = 0;

static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
esp_wifi_connect();
}

else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (s_retry_num < 5) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");

}

else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
xSemaphoreGive(connectionSemaphore);
}

else if (event_id == WIFI_EVENT_AP_STADISCONNECTED)
{
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}

else if (event_id == WIFI_EVENT_AP_STACONNECTED)
{
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",MAC2STR(event->mac), event->aid);
}
}

void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();

ESP_ERROR_CHECK(esp_netif_init());

ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();

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 wifi_config = {
.sta = {
.ssid = MYSSID,
.password = MYPW,
/* 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
},
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );

/* 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:%s",MYSSID);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, PW: %s",
MYSSID, MYPW);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}

/* 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(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(s_wifi_event_group);
}

void wifi_init_softap(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
NULL));

wifi_config_t wifi_config =
{
.ap =
{
.ssid = STATION_SSID,
.ssid_len = strlen(STATION_SSID),
.channel = 1,
.password = "",
.max_connection = WIFI_MAX_STA_CONN,
.authmode = WIFI_AUTH_OPEN
},
};

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

ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s", STATION_SSID);
xSemaphoreGive(connectionSemaphore);
}
[/Codebox]

User avatar
mbratch
Posts: 298
Joined: Fri Jun 11, 2021 1:51 pm

Re: Help with wifi Station mode please

Postby mbratch » Thu Jul 01, 2021 2:31 am

I'm not exactly sure what you mean by, "...I browse that IP". Do you mean you attempt to connect to it from a browser with HTTP?

What happens if you ping the address, do you get a response?

Who is online

Users browsing this forum: No registered users and 97 guests