@zuleen27 Have you solved your problem?
If you try your code with version 3.0.4 of the ESP32 Arduino core released just a few days ago, it may very well work. Until that version, the RF switch was not enabled.
I have tried your initWifi()
function and it connects within 6 seconds every time to an access point 15 metres away with two walls in between. That’s with the on-board ceramic antenna. A Wi-Fi connection cannot be established in a reasonable amount of time if the RF switch is disabled which was the default in EP32 v3.0.2 and v3.0.3.
By the way, with a much shorter delay in the while loop, a connection can be made within half a second.
Using an external antenna does not reduce the connection time very much, but it can improve the signal strength. Details here Connection Time vs Transmit Power if you are interested.