arduino-esp32
arduino-esp32 copied to clipboard
ESP32 unable to connect to WiFi AP (Raspberry PI with RTL8812AU WiFi module) but connects to "classic" WLAN Router
Board
DOIT ESP32 DEVKIT V1
Device Description
DOIT ESP32 DEVKIT V1
Hardware Configuration
No
Version
v2.0.14
IDE Name
Arduino IDE 2.2.1
Operating System
Windows 11
Flash frequency
40Mhz
PSRAM enabled
yes
Upload speed
921600
Description
Hello, I am trying a very basic WiFi connection example with Arduino IDE (Examples -> WiFi -> WiFiClientConnect). I only changed "ssid+password", rest of the Example code is unchanged. I am trying to connect to an AP that is a Raspberry Pi 4, with Raspbian "Bullseye" and a USB WiFi dongle "TP-Link Archer T2U PLUS [RTL8821AU]" configured as AP. The ESP32 is not able to connect to the "Raspberry Pi" AP. For the sketch output please see "Debug Message" section.
The strange thing is:
- My Windows 11 PC can successfully connect to the Raspberry Pi AP
- The same ESP32 with the same sketch (just changed ssid+password) is able to connect to my "classic" WLAN Router
This leads me to believe there is some handshaking with the Raspberry Pi AP that ESP32 is not able to handle correctly.
I have tried Arduino ESP32 v2.0.14 and also 3.0.0 Alpha 3, both have the same problem connecting to the Raspberry Pi AP.
I am open to providing any necessary information to help diagnose and solve this problem. Thank you!
Sketch
/* Wi-Fi STA Connect and Disconnect Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <WiFi.h>
const char* ssid = "************";
const char* password = "************";
int btnGPIO = 0;
int btnState = false;
void setup()
{
Serial.begin(115200);
delay(10);
// Set GPIO0 Boot button as input
pinMode(btnGPIO, INPUT);
// We start by connecting to a WiFi network
// To debug, please enable Core Debug Level to Verbose
Serial.println();
Serial.print("[WiFi] Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Auto reconnect is set true as default
// To set auto connect off, use the following function
// WiFi.setAutoReconnect(false);
// Will try for about 10 seconds (20x 500ms)
int tryDelay = 500;
int numberOfTries = 20;
// Wait for the WiFi event
while (true) {
switch(WiFi.status()) {
case WL_NO_SSID_AVAIL:
Serial.println("[WiFi] SSID not found");
break;
case WL_CONNECT_FAILED:
Serial.print("[WiFi] Failed - WiFi not connected! Reason: ");
return;
break;
case WL_CONNECTION_LOST:
Serial.println("[WiFi] Connection was lost");
break;
case WL_SCAN_COMPLETED:
Serial.println("[WiFi] Scan is completed");
break;
case WL_DISCONNECTED:
Serial.println("[WiFi] WiFi is disconnected");
break;
case WL_CONNECTED:
Serial.println("[WiFi] WiFi is connected!");
Serial.print("[WiFi] IP address: ");
Serial.println(WiFi.localIP());
return;
break;
default:
Serial.print("[WiFi] WiFi Status: ");
Serial.println(WiFi.status());
break;
}
delay(tryDelay);
if(numberOfTries <= 0){
Serial.print("[WiFi] Failed to connect to WiFi!");
// Use disconnect function to force stop trying to connect
WiFi.disconnect();
return;
} else {
numberOfTries--;
}
}
}
void loop()
{
// Read the button state
btnState = digitalRead(btnGPIO);
if (btnState == LOW) {
// Disconnect from WiFi
Serial.println("[WiFi] Disconnecting from WiFi!");
// This function will disconnect and turn off the WiFi (NVS WiFi data is kept)
if(WiFi.disconnect(true, false)){
Serial.println("[WiFi] Disconnected from WiFi!");
}
delay(1000);
}
}
Debug Message
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13260
load:0x40080400,len:3028
entry 0x400805e4
[ 31][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
[WiFi] Connecting to ***********
[ 78][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 0 - WIFI_READY
[ 145][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 2 - STA_START
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] Failed to connect to WiFi![ 10654][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 10654][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 10659][D][WiFiGeneric.cpp:1081] _eventCallback(): WiFi Reconnect Running
Other Steps to Reproduce
I have tried Arduino ESP32 v2.0.14 and also 3.0.0 Alpha 3, both have the same problem connecting to the Raspberry Pi AP.
I have checked existing issues, online documentation and the Troubleshooting Guide
- [X] I confirm I have checked existing issues, online documentation and Troubleshooting guide.
is it possible that you have set it as 5GHz AP and not 2.4GHz?
Unfortunately I can confirm that the AP network is set to 2.4Ghz: This is the output of "iwconfig" from raspberry pi command line (where the AP is running):
wlan1 IEEE 802.11bg ESSID:"********" Nickname:"<WIFI@REALTEK>"
Mode:Master Frequency:2.437 GHz Access Point: 6C:5A:B0:D0:4A:87
Bit Rate:54 Mb/s Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Power Management:off
Link Quality:0 Signal level:0 Noise level:0
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
And also confirmation from Windows 11 WiFi properties for that network:
Hi, so I just wanted to see if a ESP8266 can connect to the AP. It's a WEMOS D1 mini. I know it's most probably a different base-system regarding the libraries interfacing the ESP but I thought I'll just give it a try. I used the following release:
https://github.com/esp8266/Arduino
I run this simple code:
#include <ESP8266WiFi.h> // Include the Wi-Fi library
const char* ssid = "*******"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "******"; // The password of the Wi-Fi network
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to WiFi AP ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}
void loop() { }
And it connected successfully to the AP and got an IP address. This is the output on the serial monitor:
Connection established!
IP address: 10.0.1.23
I did also the following test:
- Started the ESP32 WiFiClientConnect sketch.
- The ESP32 could not connect.
- Reboot of the Raspberry Pi and after Raspbian boot-up the ESP32 plotted the following:
...
...
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] Failed to connect to WiFi![ 10657][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 10657][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 10662][D][WiFiGeneric.cpp:1081] _eventCallback(): WiFi Reconnect Running
<<<<< RASPBERRY PI REBOOT HERE >>>>>
[ 60765][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 60765][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 201 - NO_AP_FOUND
[ 60768][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
[ 63190][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 63191][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 201 - NO_AP_FOUND
[ 63194][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
[ 65616][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 65616][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 201 - NO_AP_FOUND
[ 65619][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
[ 68041][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 68042][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 201 - NO_AP_FOUND
[ 68045][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
[ 69102][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[ 76643][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[ 76643][D][WiFiGeneric.cpp:1102] _eventCallback(): STA IP: 10.0.1.22, MASK: 255.255.255.0, GW: 10.0.1.1
If I subsequently push the "EN"-button on the ESP32-DevKit board, the ESP32 again is NOT able to connect to the Raspberry Pi AP with the same Serial Monitor log as in the first post.
I did multiple time the "Raspberry Pi AP" reboot-test and the ESP32 behavior is reproducible. Each time after a "Raspberry Pi AP" reboot, the ESP32 automatically connects once to the AP. On every subsequent EN-button push it can't connect to the AP.
A similar behavior is observable if I first start the ESP32 and subsequently power the Raspberry Pi AP. Once the SSID is visible, ESP32 connects successfully once. If I push the EN-button, the ESP32 no more connects to the AP
With that sketch, if you hold the boot (prg) button down after it fails, you should see it say that it is disconnecting from wifi. That should make it clear any saved lease. I would also restart the hostapd/dnsmasq and clear the lease file (maybe /var/lib/misc/dnsmasq.leases). Post the verbose log from the first connection there if it fails.
After a failed connection, if I press the PROG-button, the following output is printed on the serial monitor:
[WiFi] Disconnecting from WiFi!
[ 39608][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 39608][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 39613][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
[ 39620][E][WiFiGeneric.cpp:1272] mode(): Could not set mode! 12308
[ 39626][E][WiFiSTA.cpp:299] begin(): STA enable failed!
[ 39631][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 3 - STA_STOP
[WiFi] Disconnected from WiFi!
If I then press the EN-button, the ESP32 reboots but fails to connect to the AP.
After a successful connection, if I press the PROG-button, the following output is printed on the serial monitor:
[WiFi] Disconnecting from WiFi!
[ 81512][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 81512][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 8 - ASSOC_LEAVE
[ 81515][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 3 - STA_STOP
[WiFi] Disconnected from WiFi!
If I then press the EN-button, one successful connection to the AP is possible:
[WiFi] Connecting to ********
[ 71][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 0 - WIFI_READY
[ 139][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 2 - STA_START
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[WiFi] WiFi is disconnected
[ 1212][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[ 1337][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[ 1337][D][WiFiGeneric.cpp:1102] _eventCallback(): STA IP: 10.0.1.22, MASK: 255.255.255.0, GW: 10.0.1.1
[WiFi] WiFi is connected!
[WiFi] IP address: 10.0.1.22
After that I have two options:
- Press PROG-button again (WiFi disconnects successfully) and subsequently the EN button and the ESP32 connects successfully
- Just press the EN button and the ESP32 reboots but fails to connect to the AP
If i restart hostapd via "sudo systemctl restart hostapd" then the ESP32 is able to connect once to the WiFi AP:
[ 19067][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 19067][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 19071][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
[ 21493][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 21493][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 201 - NO_AP_FOUND
[ 21497][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
[ 22547][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[ 26086][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[ 26086][D][WiFiGeneric.cpp:1102] _eventCallback(): STA IP: 10.0.1.22, MASK: 255.255.255.0, GW: 10.0.1.1
Further connections to the AP are not possible.
Sometimes the ESP32 crashes completely. This is not always reproducible behavior:
- Successful connection to AP
- press EN-button -> reboot
- press PROG-button -> crash
[WiFi] Disconnecting from WiFi!
[ 12953][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 12953][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 12958][D][WiFiGeneric.cpp:1085] _eventCallback(): WiFi AutoReconnect Running
E (12948) wifi:wifi deinit internal: wifi not stop
E (12951) wifi_init: Failed to deinit Wi-Fi driver (0x3003)
[ 12976][D][WiFiGeneric.cpp:1039] _eventCallback(): Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x40120255 PS : 0x00060a30 A0 : 0x80122cec A1 : 0x3ffc9c50
A2 : 0x3ffcbf20 A3 : 0x3ffc9ce0 A4 : 0x00000000 A5 : 0x00000000
A6 : 0x3ffcbf98 A7 : 0x00000000 A8 : 0x00000000 A9 : 0x3ffc6acc
A10 : 0x3ffbfc50 A11 : 0x00000015 A12 : 0x00000084 A13 : 0x3ffcbffd
A14 : 0x3ffcbfb0 A15 : 0x00000008 SAR : 0x00000010 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000050 LBEG : 0x400898c0 LEND : 0x400898cb LCOUNT : 0x00000000
Backtrace: 0x40120252:0x3ffc9c50 0x40122ce9:0x3ffc9cd0 0x4014d1b9:0x3ffc9dc0 0x4014d261:0x3ffc9e10 0x4014f075:0x3ffc9e30 0x4014bec0:0x3ffc9e50
ELF file SHA256: 36df6213ed626d05
if restarting hostapd is allowing the ESP to connect, then I guess it's something on the PI side that is preventing it. It's trying... but is failing to assoc with the AP. Any chance you can get some logs off of the Pi? Can you also share your AP configuration (if any)?
I can provide whatever is needed. I can share the hostapd.conf if that is helpful or any other file, please just mention what you need for your analysis. I can get logs of the Pi, just say which one you need.
Please bear in mind that the ESP8266 with a simple sketch can connect multiple times without problems
it's best to provide everything necessary to replicate the issue and all logs that are in any way connected to the problem
Since I am no expert regarding WiFi on ESP32 and on Raspberry Pi I am not really sure which logs would be useful for the analysis. If you could mention which logs (on Raspberry Pi) are necessary I'm more than happy to provide them.
Does it consistently work if you put WiFi.disconnect(false,true);
before the begin?
This code doesn't change the connection behaviour of the sketch at all. The previous observed problems all remain.
WiFi.disconnect(false,true);
WiFi.begin(ssid, password);
That means that only one connection is possible after a successful disconnection (by pressing the PROG-button). Further reconnections (by pressing the EN-button and reboot the ESP32) fail.
I have enabled verbose logging of hostapd and dnsmasq on the raspberry pi. hostapd doesn't print anything, not on successful connection and not on unsuccessful connection. It just says that it successfully started the AP. dnsmasq only prints something on successful connection:
Jan 26 18:09:37 dnsmasq-dhcp[1204]: DHCP packet received on wlan1 which has no address
Jan 26 18:09:38 dnsmasq-dhcp[1204]: DHCP packet received on wlan1 which has no address
Jan 26 18:09:39 dnsmasq-dhcp[1204]: DHCPDISCOVER(wlan1) 24:6f:28:17:3f:44
Jan 26 18:09:39 dnsmasq-dhcp[1204]: DHCPOFFER(wlan1) 10.0.1.22 24:6f:28:17:3f:44
Jan 26 18:09:39 dnsmasq-dhcp[1204]: DHCPREQUEST(wlan1) 10.0.1.22 24:6f:28:17:3f:44
Jan 26 18:09:39 dnsmasq-dhcp[1204]: DHCPACK(wlan1) 10.0.1.22 24:6f:28:17:3f:44 esp32-173F44
Hello, can I provide some more information that is useful for further debugging?
Turn on all the logging in /etc/config/dhcp
option logfacility '/tmp/log/dnsmasq.log'
option logdhcp '1'
option logqueries '1'
option quietdhcp '1'
Post the log of when it fails (matching the 203/201 errors as above). Is the esp32 powered by the onboard ldo? Do you have anything else drawing power from that? Do you have any other devices connected?
I couldn't find /etc/config/dhcp in Raspbian but I have added the following 2 options to /etc/dnsmasq.conf
log-queries
log-dhcp
Unfortunately, dnsmasq doesn't log anything when a connection attempt by ESP32 fails. It only logs around 40 lines in the log-file when the connection is successful.
The ESP32 is powered via USB. No peripherals connected to the ESP32-DevKit Board, it's just the Board and nothing else.
I also tried with a static ip:
IPAddress ip(10, 0, 1, 52);
IPAddress gw(10, 0, 1, 1);
IPAddress sub(255, 255, 255, 0);
WiFi.config(ip, gw, sub);
WiFi.begin(ssid, password);
And there are the same problems as with dynamic IP. First attempt is successful:
[ 25097][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[ 25099][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[ 25101][D][WiFiGeneric.cpp:1102] _eventCallback(): STA IP: 10.0.1.52, MASK: 255.255.255.0, GW: 10.0.1.1
Futher attempts are all without success:
[WiFi] Failed to connect to WiFi![ 10629][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[ 10629][W][WiFiGeneric.cpp:1061] _eventCallback(): Reason: 203 - ASSOC_FAIL
[ 10634][D][WiFiGeneric.cpp:1081] _eventCallback(): WiFi Reconnect Running
you do not have an IP problem, so checking DHCP is pointless. It is failing to connect to the AP, so you need to check the wifi logs. I can not tell which logs you need, because i do not have such setup and you did not provide any pointers on how to configure one.
Hi, I followed this guide: https://raspberrypi-guide.github.io/networking/create-wireless-access-point
Up to the chapter "Enable routing and IP masquerading", which I did not do. Please note this guide only works for Raspbian up to (including) "Bullseye". Debian "Bookworm" switched to network-manager and therefore the guide is no longer valid.
I did all the steps again to make sure the guide works and I noticed the following: If I use the built-in raspberry-pi wifi-adapter, then ESP32 connects successfully even multiple times. The connection problem can be observed only when I use the WiFi dongle "TP-Link Archer T2U PLUS [RTL8821AU]" as an AP with the aircrack-ng driver: https://github.com/aircrack-ng/rtl8812au
I'm more than happy to provide any log necessary to debug this problem.
I faced with the same problem. There is no difference whether I used built-in Raspberry WiFi module or external - ESP32 didn`t connected to the Raspberry access point. The solution that I found is to use Ubuntu not Raspbian.
I'd like to give a short update: I tried with a WiFi dongle with rtl8812bu chipset and the problem is not present. The problem evidently has to do something with the rtl8812au driver and with the interaction between ESP32 and the rtl8812au AP.
If there is interest I can provide informations to further analyze the problem or otherwise we can also close the Ticket.