Help! Fail to use WiFiManager

Since I am doing a project that I want to use a physical button to connect the NodeMCU when it is connecting the internet. So when I press the physical button, it will call the ifttt trigger to do sending mail etc.

But now I facing a problem. When avoiding the hard code of ssid and wifi password for connect the internet, it dose not successful.

When I am try to do the hard code of ssid and the password for the whole action, it runs as well (using the below function connectToWifi()). But when I am trying to use wifiManager, It seems that not work.

At the below have some capture screen.

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
 
//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/kentaylor/WiFiManager
#include <IFTTTWebhook.h>

// Onboard LED I/O pin on NodeMCU board
const int PIN_LED = 2; // D4 on NodeMCU and WeMos. Controls the onboard LED.
int wakePin = 16;

//const char* ssid="";
//const char* password="";

const char* resource = "/trigger/ xxxxx";
const char* iftttserver = "maker.ifttt.com";

 
void setup() {
  // put your setup code here, to run once:
  // initialize the LED digital pin as an output.
  pinMode(PIN_LED, OUTPUT);
 
  Serial.begin(115200);

  while (!Serial) {
  }
  Serial.println(" ");// print an empty line before and after Button Press
  Serial.println("Button Pressed");
  Serial.println(" ");

  //connectToWifi();
  
  Serial.println("\n Starting");
  unsigned long startedAt = millis();
 
  WiFi.printDiag(Serial); //Remove this line if you do not want to see WiFi password printed
  Serial.println("Opening configuration portal");
  digitalWrite(PIN_LED, LOW); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
  //Local intialization. Once its business is done, there is no need to keep it around
 
  WiFiManager wifiManager;  
  //sets timeout in seconds until configuration portal gets turned off.
  //If not specified device will remain in configuration mode until
  //switched off via webserver.
  if (WiFi.SSID()!="") wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
 
  //it starts an access point 
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.startConfigPortal("ESP8266","password"))  //Delete these two parameters if you do not want a WiFi password on your configuration access point
  {
     Serial.println("Not connected to WiFi but continuing anyway.");
  } 
  else 
  {
     //if you get here you have connected to the WiFi
     Serial.println("connected...yeey :)");
  }
      digitalWrite(PIN_LED, HIGH); // Turn led off as we are not in configuration mode.
      // For some unknown reason webserver can only be started once per boot up 
      // so webserver can not be used again in the sketch.
 
  Serial.print("After waiting ");
  int connRes = WiFi.waitForConnectResult();
  float waited = (millis()- startedAt);
  Serial.print(waited/1000);
  Serial.print(" secs in setup() connection result is ");
  Serial.println(connRes);
  if (WiFi.status()!=WL_CONNECTED)
  {
      Serial.println("failed to connect, finishing setup anyway");
  } 
  else
  {
    Serial.print("local ip: ");
    Serial.println(WiFi.localIP());
  }


  makeIFTTTRequest();

  ESP.deepSleep(wakePin);
  
}

 /*
void connectToWifi() {
  Serial.print("Connecting to: NAME "); //uncomment next line to show SSID name
  //Serial.print(ssid);
  WiFi.begin(ssid, password);
  Serial.println(" ");
  Serial.print("Attempting to connect: ");

  //try to connect for 10 seconds
  int i = 10;
  while (WiFi.status() != WL_CONNECTED && i >= 0) {
    delay(1000);
    Serial.print(i);
    Serial.print(", ");
    i--;
  }
  Serial.println(" ");

  //print connection result
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("Connected.");
    Serial.println(" ");
    Serial.print("NodeMCU ip address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed - check your credentials or connection");
  }  
}

*/


void makeIFTTTRequest() {
  Serial.print("Connecting to ");
  Serial.print(iftttserver);

  WiFiClient client;
  int retries = 5;
  while (!!!client.connect(iftttserver, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if (!!!client.connected()) {
    Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("Request resource: ");
  Serial.println(resource);
  client.print(String("GET ") + resource +
               " HTTP/1.1\r\n" +
               "Host: " + iftttserver + "\r\n" +
               "Connection: close\r\n\r\n");

  int timeout = 5 * 10; // 5 seconds
  while (!!!client.available() && (timeout-- > 0)) {
    delay(100);
  }
  if (!!!client.available()) {
    Serial.println("No response, going back to sleep");
  }
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("\nclosing connection");
  client.stop();
}

void loop() {
 delay(10000);
  // put your main code here, to run repeatedly:
 
}

For Serial Monitor output, please copy text and paste into your post between code tags.

PaulRB:

For Serial Monitor output, please copy text and paste into your post between code tags.

Serial Monitor output

18:12:58.361 -> *WM: Request redirected to captive portal
18:12:58.361 -> *WM: Handle root
18:13:02.076 -> *WM: Scan done
18:13:02.076 -> *WM: TP-LINK_26AB
18:13:02.076 -> *WM: -55
18:13:02.076 -> *WM: ASUS
18:13:02.076 -> *WM: -75
18:13:02.076 -> *WM: conniechin1606-2.4G
18:13:02.076 -> *WM: -80
18:13:02.076 -> *WM: natalie
18:13:02.076 -> *WM: -81
18:13:02.076 -> *WM: GAB-AP
18:13:02.076 -> *WM: -86
18:13:02.076 -> *WM: McDonald's Free Wifi
18:13:02.076 -> *WM: -86
18:13:02.076 -> *WM: i_not_tell_you
18:13:02.076 -> *WM: -87
18:13:02.076 -> *WM: Tang
18:13:02.076 -> *WM: -87
18:13:02.076 -> *WM: Ricky
18:13:02.076 -> *WM: -87
18:13:02.111 -> *WM: Andystation-2.4G
18:13:02.111 -> *WM: -88
18:13:02.111 -> *WM: TP-LINK_073D
18:13:02.111 -> *WM: -90
18:13:02.111 -> *WM: Sent config page
18:13:02.111 -> *WM: Request redirected to captive portal
18:13:04.288 -> *WM: Scan done
18:13:04.288 -> *WM: TP-LINK_26AB
18:13:04.288 -> *WM: -56
18:13:04.288 -> *WM: One&two
18:13:04.288 -> *WM: -77
18:13:04.288 -> *WM: conniechin1606-2.4G
18:13:04.288 -> *WM: -79
18:13:04.288 -> *WM: McDonald's Free Wifi
18:13:04.288 -> *WM: -84
18:13:04.288 -> *WM: Ricky
18:13:04.288 -> *WM: -86
18:13:04.322 -> *WM: Chiu 2.4G
18:13:04.322 -> *WM: -87
18:13:04.322 -> *WM: TP-LINK_073D
18:13:04.322 -> *WM: -87
18:13:04.322 -> *WM: Yeung Home
18:13:04.322 -> *WM: -89
18:13:04.356 -> *WM: Sent config page
18:13:04.356 -> *WM: Request redirected to captive portal
18:13:04.356 -> *WM: Handle root
18:13:04.390 -> *WM: Handle root
18:13:04.425 -> *WM: Request redirected to captive portal
18:13:04.425 -> *WM: Handle root
18:13:14.167 -> *WM: WiFi save
18:13:14.167 -> *WM: Sent wifi save page
18:13:16.176 -> *WM: Connecting to new AP
18:13:16.176 -> *WM: Connecting as wifi client...
18:13:16.176 -> *WM: Connection result:
18:13:16.176 -> *WM: 0
18:13:16.176 -> *WM: Failed to connect.
18:13:16.208 -> *WM: Request redirected to captive portal
18:13:16.208 -> *WM: Request redirected to captive portal
18:13:16.241 -> *WM: Handle root
18:13:16.241 -> *WM: Handle root

I asked for code tags. I know you know how to use code tags.

Please don't use Quote button unless you want to ask about a specific thing or answer a specific question, and then quote only the relavent part. Don't quote entire replies. Just use Reply, not Quote.

Is that the end of the serial output? I don't see any text printed from your sketch, only text printed by the WiFi manager, so I guess WiFi manager is still running and has not returned control to your sketch. What do you see on phone screen at the end?