ESP8266 can' t initialize wifi agent

Hello , I am trying to begin my wifi client in my setup () function.

void setup() {
  //delay(5000);
  // Setting up Serial communication
  Serial.begin(115200);
 
  
  WiFi.begin("name", "pass");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}

and everything works fine. BUT if i add a delay() before the wifi.begin nothing works. It keeps trying to connect to wifi like forever. Any ideas?

thank you :slight_smile:

Any ideas?

Yeah. Don't use delay().

Post ALL of your code. The problem may not be where YOU think it is.

"Dont use delay()" is not an option. I actually have some code instead of delay() where my android app is connecting to the arduino via bluetooth module.

the code is:

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <SoftwareSerial.h>


#define BUTTONPIN D3

#include <string.h>
#include <stdio.h>

SoftwareSerial mySerial(D1,D2);


String Data = "";
String password1 = "";
String ssid1 = "";

int button = 0;   // 0 isrealeashed, 1 is pressed

char inChar;

int READINGSTATUS = -1;

char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = ":";
char *token;


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

const char* host = "***.com";
const int httpsPort = 3245;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";

void parseInput() {
  int i=0;
  int pos = 0;
  if(Data[0] == '<' && Data[Data.length()] == '>' ) {
ζ}
  }
  for(i=0; i<Data.length(); i++) {
      str[pos] = Data[i];
      pos++;
    
  }
  
  token = strtok(str, s);
  password1 = token;
  
  while( token != NULL ) {
    Serial.println(token );
  
    token = strtok(NULL, s);
    ssid1 = token;
    break;
  }
  Serial.println("");
  Serial.print("SSID: ");
  Serial.println(ssid1);
  Serial.print("PASSWORD: ");
  Serial.println(password1);
  Serial.println("Serial input was read sucesfully.");   
}

void setup() {
  // Setting up Serial communication
  Serial.begin(115200);
  mySerial.begin(9600);

   // Setting up pins
  pinMode(BUTTONPIN, INPUT);
  
  // Blinking red -waiting to connect to a bluetooth device.
  // ...
  
  // Waiting to reveice data from bluetooth.
  Serial.println(" Android device message:");
  while(1) {
    delay(100);
    if (mySerial.available() > 0) {
      char myc = mySerial.read();
      Data.concat(myc);
      Serial.print(myc);
      Serial.print(" ");
      if (myc == '>') {
        break;
      }
    }
  }

  parseInput();
  mySerial.write("Everything is ok");
  
  //Serial.print("Android said: ");
  //Serial.println(Data);
  Serial.println("Connecting with: ");
  Serial.println(password1);
  Serial.println(ssid1);
  
  char id[ssid1.length()+1];
  ssid1.toCharArray(id, ssid1.length()+1);
  //password.toCharArray(password1, );
  // Connecting to WIFI hotspot 
  Serial.print("SSID: ");
  Serial.println(id);
  
  // Connecting to WIFI hotspot 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
/*
  if (client.verify(fingerprint, host)) {
    Serial.println("certificate matches");
  } else {
    Serial.println("certificate doesn't match");
  }
*/
  String url = "/";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
    int buttonState = digitalRead(BUTTONPIN);
    if( buttonState == 1) {
      Serial.println("Button is releashed!");
    }else {
      Serial.println("Button is pressed!");
    }
    
    if (mySerial.available() > 0) {
      //Serial.println("---> Incoming byte!");
      char myc = mySerial.read();
      Serial.print(myc);
      Serial.print(" ");
    }
  
    delay(50);
}

The while (1) loop in setup function is "the same" as if I had a delay(5000). If I comment that loop everything is ok.