[esp32] wifi is unable connect and display connecting connecting ..... in serial monitor

I am powering esp32 extranally because the usb is not enble to provide enough power for wifi application.after When I upload the code and open serial monitor in keeps repeating "connecting..." Again and again.and does not connect and show ip I Haven't hooked up pwm.


#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebSrv.h>

const char* ssid = "esp32";
const char* password = "Yassword";

const int led_pin = 14;
String slider_value = "0";

const int frequency = 5000;
const int led_channel = 0;
const int resolution = 8;

const char* input_parameter = "value";

AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ESP32 Brightness Control Web Server</title>
  <style>
    html {font-family: Times New Roman; display: inline-block; text-align: center;}
    h2 {font-size: 2.3rem;}
    p {font-size: 1.9rem;}
    body {max-width: 400px; margin:0px auto; padding-bottom: 25px;}
    .slider { -webkit-appearance: none; margin: 14px; width: 360px; height: 25px; background: #38c0ff  ;
      outline: none; -webkit-transition: .2s; transition: opacity .2s;}
    .slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 35px; height: 35px; background:#01070a; cursor: pointer;}
    .slider::-moz-range-thumb { width: 35px; height: 35px; background: #01070a; cursor: pointer; } 
  </style>
</head>
<body>
  <h2>ESP32 Brightness Control Web Server</h2>
  <p><span id="textslider_value">%SLIDERVALUE%</span></p>
  <p><input type="range" onchange="updateSliderPWM(this)" id="pwmSlider" min="0" max="255" value="%SLIDERVALUE%" step="1" class="slider"></p>
<script>
function updateSliderPWM(element) {
  var slider_value = document.getElementById("pwmSlider").value;
  document.getElementById("textslider_value").innerHTML = slider_value;
  console.log(slider_value);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value="+slider_value, true);
  xhr.send();
}
</script>
</body>
</html>
)rawliteral";

String processor(const String& var){
  if (var == "SLIDERVALUE"){
    return slider_value;
  }
  return String();
}

void setup(){
  Serial.begin(115200);

  ledcSetup(led_channel, frequency, resolution);
  ledcAttachPin(led_pin, led_channel);
  ledcWrite(led_channel, slider_value.toInt());

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }

  Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html",index_html, processor);
  });

  server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String message;
    if (request->hasParam(input_parameter)) {
      message = request->getParam(input_parameter)->value();
      slider_value = message;
      ledcWrite(led_channel, slider_value.toInt());
    }
    else {
      message = "No message sent";
    }
    Serial.println(message);
    request->send(200, "text/plain", "OK");
  });
  
  server.begin();
}
  
void loop() {
  
}


That is the first time I heard that. Of course the USB has enough power, I and thousands of others do it all the time. After you do that, read ALL of the pinned post re how to get the most from the forum.

When I tried to usb it give a message "brownout detector was triggered" i suppose which means it has not enough power. Yeah not every usb has this problem. Code which doesn't has wifi it works no problem.

There are a few relevant status values other than WL_CONNECTED. To track them in more detail

  auto lastStatus = WiFi.status();
  auto lastChange = millis();
  auto status = WiFi.begin(SECRET_SSID, SECRET_PASS);

  for (int i = 0; lastStatus != WL_CONNECTED; i++) {
    if (status != lastStatus) {
      auto now = millis();
      Serial.printf("WiFi.status() %d ==> %d after %dms\n", lastStatus, status, now - lastChange);
      lastStatus = status;
      lastChange = now;
    } else {
      Serial.print(i % 10 ? "." : "\n.");
      delay(100);
    }
    status = WiFi.status();
  }

A successful connection looks like

WiFi.status() 254 ==> 6 after 81ms
..WiFi.status() 6 ==> 0 after 200ms
......
..........
...WiFi.status() 0 ==> 3 after 1900ms

status() returns an enum

typedef enum {
  WL_NO_SHIELD = 255,  // for compatibility with WiFi Shield library
  WL_STOPPED = 254,
  WL_IDLE_STATUS = 0,
  WL_NO_SSID_AVAIL = 1,
  WL_SCAN_COMPLETED = 2,
  WL_CONNECTED = 3,
  WL_CONNECT_FAILED = 4,
  WL_CONNECTION_LOST = 5,
  WL_DISCONNECTED = 6
} wl_status_t;

Under the Tools menu, about in the middle, you should also set Core Debug Level to at least Warning (could go all the way to Verbose). If you see

[  4032][W][STA.cpp:135] _onStaArduinoEvent(): Reason: 15 - 4WAY_HANDSHAKE_TIMEOUT

that could mean the password is wrong.

The WiFi also has auto-reconnect that defaults on, making it more likely to be failing repeatedly rather than just "stuck".

what else do you have connected to the ESP32?