Hello,
first of:
I'm using an ESP32 DevKit C V4 development board and the Arduino IDE 2.2.1.
My problem:
I want to integrate WiFi into a project at work but I have problems connecting to a specific WiFi AP (ESP32 in Station Mode).
My project consists of two ESP32 on custom PCBs that communicate over a CAN-Bus with each other. They're collecting measurements data and control some peripherals. Everything works fine but now I want to be able to control both ESP32 via a MODBUS TCP protocol using WiFi.
I started adding the WiFi functionality using the example from the espressif github (https://github.com/espressif/esp-idf/tr ... ng_started).
Apart from a few minor changes my code was functionally the same as the example.
All the initializations and configurations seemed to work fine (every function returned ESP_OK).
But when I tried to connect to a open WiFi Network at first a connected-event got triggered but shortly after(1-2s) a disconnected-event got triggered and any reconnect attempt failed (another disconnected-event). And at no point the event "IP_EVENT_STA_GOT_IP" got triggered. So i guess I never really was connected to the chosen AP.
For testing I used an WiFi AP with no password. Later on I would use another Network with proper security.
But also when i created a Hotspot with my phone I wasn't able to connect to this one either (WPA2).
Nothing seemd to work.
So I started to check my code for obvious mistakes and google for any similar errors.
Sadly I didn't find any mistakes or posts with similar problems on any forum.
To verify this error I tried to implement WiFi on a different ESP32 in an empty project and not connected to any PCB (only USB to my Laptop) using the same example.
And it worked... so far so good.
But when I tried to modifiy the code it started to have the same faulty behaviour again.
As soon as I add any substantial code before the Initialization I can't connect to any AP. None of the functions used to initialize the WiFi return an error. Everything seems to run fine but I can't connect to any AP...
Even when I only add a delay of 1ms before calling the initialization-function the code stops working...
At the end of this post you'll find my test-code. I commented the delay out...
I can't figure out how to solve this behaviour. Or what the reason would be...
Can anyone reproduce this error?
Is it a problem with the Arduino Core?
Does anyone have an idea what the problem could be?
I would be very grateful for any suggestions for solutions or topics to research.
If you need any additional information please let me know.
greets,
Paul
My Code (second testing without other project code):
[Codebox]
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include <HardwareSerial.h>
#include "lwip/err.h"
#include "lwip/sys.h"
#define WIFI_DEFAULT_SSID "EXAMPLE"
#define WIFI_DEFAULT_PASSWORD ""
#define WIFI_DEFAULT_RETRY_NUMBER 5
#if CONFIG_ESP_WPA3_SAE_PWE_HUNT_AND_PECK
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HUNT_AND_PECK
#define EXAMPLE_H2E_IDENTIFIER ""
#elif CONFIG_ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HASH_TO_ELEMENT
#define EXAMPLE_H2E_IDENTIFIER CONFIG_ESP_WIFI_PW_ID
#elif CONFIG_ESP_WPA3_SAE_PWE_BOTH
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_BOTH
#define EXAMPLE_H2E_IDENTIFIER CONFIG_ESP_WIFI_PW_ID
#endif
#if CONFIG_ESP_WIFI_AUTH_OPEN
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
#elif CONFIG_ESP_WIFI_AUTH_WEP
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
#elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "wifi station";
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 < WIFI_DEFAULT_RETRY_NUMBER) {
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);
}
}
void wifi_init_sta(void)
{
// 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);
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_wifi_set_ps(WIFI_PS_NONE);
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));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t wifi_config;
memcpy(wifi_config.sta.ssid, WIFI_DEFAULT_SSID, sizeof(WIFI_DEFAULT_SSID));
memcpy(wifi_config.sta.password, WIFI_DEFAULT_PASSWORD, sizeof(WIFI_DEFAULT_PASSWORD));
wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
wifi_config.sta.sae_pwe_h2e = WPA3_SAE_PWE_HUNT_AND_PECK;
wifi_config.sta.sae_pwe_h2e = WPA3_SAE_PWE_UNSPECIFIED;
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,pdFALSE,pdFALSE,portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
WIFI_DEFAULT_SSID, WIFI_DEFAULT_PASSWORD);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
WIFI_DEFAULT_SSID, WIFI_DEFAULT_PASSWORD);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}
void setup() {
//When I uncomment this the code stops working...
// vTaskDelay(pdMS_TO_TICKS(1));
wifi_init_sta();
}
void loop() {
// put your main code here, to run repeatedly:
vTaskDelay(pdMS_TO_TICKS(10));
}
[/Codebox]
WiFi not connecting when delay before initialization
-
- Posts: 3
- Joined: Thu Apr 13, 2023 3:26 pm
-
- Posts: 826
- Joined: Mon Jul 22, 2019 3:20 pm
Re: WiFi not connecting when delay before initialization
Test to see if it works on Wokwi.
The SSID there is Wokwi-GUEST, no password
The SSID there is Wokwi-GUEST, no password
Who is online
Users browsing this forum: No registered users and 67 guests