Wifi not connecting

I want to setup a Wifi connection with a NodeMCU to control WS2812 LEDs but the program does not connect to my Wifi - the code compiles fine with no errors.

Code used:

#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>
#include <Wire.h>
#include <EEPROM.h>

char ssid[32] = "MY_SSID";
char password[32] = "MY_Password";

void setupWiFi() {
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  unsigned long start = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - start < 15000) {
    delay(500); Serial.print(".");
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nConnected: " + WiFi.localIP().toString());
  } else {
    Serial.println("\nWiFi failed.");
  }
}

Can figure out what the problem is.

Thanks

Presuming you have put your correct SSID and Password on the lines below, you may be trying to connect to a 5.0GHz net. If so, try a 2.4GHz.

You haven't told what happens. You keep getting the dots until the 15s are completed?

Add WiFi Mode Explicitly and Print Detailed Connection Status.

void setupWiFi() {
  WiFi.mode(WIFI_STA);  // Ensure in Station Mode
  WiFi.begin(ssid, password);

  Serial.print("Connecting to WiFi");
  unsigned long start = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - start < 15000) {
    delay(500); Serial.print(".");
    Serial.print(" Status: "); Serial.println(WiFi.status());
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nConnected to WiFi.");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nWiFi connection failed.");
  }
}

Thank for the answers. Yes, I'm getting the dots and afterwards a "WiFi failed." The SSID and and the passwort are correct and I specifically set the network tpo 2.4 GHz only.

I added the line ahsrabrifat suggested but still end up with "WiFi failed."

In case it matters encryption is WPA2

I added

Serial.print("Status: ");
Serial.println(WiFi.status());

and get as return WL_NO_SSID_AVAIL = 1


I went back to a simple code

#include <ESP8266WiFi.h>

const char* ssid = "Guest";      // Change this
const char* password = "xxxxxx";     // And this

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

  WiFi.mode(WIFI_STA);
  //wifi_set_phy_mode(PHY_MODE_11G);
  WiFi.begin(ssid, password);

  Serial.print("Connecting");
  unsigned long start = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - start < 15000) {
    delay(500);
    Serial.print(".");
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nConnected!");
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nFailed to connect.");
    Serial.print("Status: ");
    Serial.println(WiFi.status());
  }
}

void loop() {}

and it connects fine.

Changing my code to 

const char* ssid = "Guest";      // Change this
const char* password = "xxxxxx";     // And this

does not work and givers an error


error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
  228 |       strncpy(ssid, server.arg("ssid").c_str(), sizeof(ssid));

I was waiting for someone else with more programming skills to join in...

We don't have your entire sketch, just the snippet of post #1, so it's not clear for me if the error comes from another line in your code or from a function inside one of the libraries. But the compiler rages against a conversion between a constant pointer and a pointer. So my next step would be to change

const char* ssid = "Guest";      // Change this
const char* password = "xxxxxx";     // And this

to

char* ssid = "Guest";      // Change this
char* password = "xxxxxx";     // And this

and see what happens...

That gives

WiFi connection failed.

18:08:32.844 -> Status: 0

I attached the entire code

WordClock_ESP8266_OTA_UI1.ino (7.2 KB)

there's a way to scan for networks. might help to know that your board is seeing yours

// https://openlabpro.com/guide/scanning-of-wifi-on-esp32-controller/
static void
_wifiScan (void)
{
    printf ("%s:\n", __func__);

    // WiFi.scanNetworks will return the number of networks found
    WiFi.mode (WIFI_OFF);
    int nNet = WiFi.scanNetworks ();
    if (nNet == 0) {
        printf ("  %s: no networks found\n", __func__);
    }
    else {
        printf ("  %s: %2d networks found\n", __func__, nNet);
        if (0 > nNet)
            nNet = -nNet;
        for (int i = 0; i < nNet; ++i) {
            printf (" %s: %2d - %s (%d) %s\n", __func__, i,
                WiFi.SSID (i).c_str (),
                WiFi.RSSI (i),
                WiFi.encryptionType (i) == WIFI_AUTH_OPEN ? "open" : "locked");
        }
    }
}

Tried the below code to scan the networks and see nothing comming back in the serial monitor.

#include <ESP8266WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect(); // Disconnect from any previous connection
  delay(100);        // Small delay to settle

  Serial.println("Scanning for networks...");

  int numberOfNetworks = WiFi.scanNetworks();
  if (numberOfNetworks == 0) {
    Serial.println("No networks found.");
  } else {
    Serial.println();
    Serial.print(numberOfNetworks);
    Serial.println(" network(s) found:");
    Serial.println("-----------------------");
    for (int i = 0; i < numberOfNetworks; ++i) {
      Serial.print("Network name: ");
      Serial.println(WiFi.SSID(i));
      Serial.print("Signal strength (RSSI): ");
      Serial.println(WiFi.RSSI(i));
      Serial.println("-----------------------");
    }
  }
}

void loop() {}

Even the simplest program shows no ouitput in the console, getting mad

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("Hello from NodeMCU!");
}

void loop() {}

is the serial monitor set to the same speed as the code (i.e. 115200)?

Did you double check your SSID and Password?

sounds like a print problem

I have the same error when antenna is disconnected, maybe your transmitter is not good.

What router or access point do you have?

Sorry, I was busy and had no time to answer.

Nevertheless, I first did a reset for the controller and played around with the code that was not working:

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect(); // Disconnect from any previous connection
  delay(100);        // Small delay to settle

  Serial.println("Scanning for networks...");

  int numberOfNetworks = WiFi.scanNetworks();
  if (numberOfNetworks == 0) {
    Serial.println("No networks found.");
  } else {
    Serial.println();
    Serial.print(numberOfNetworks);
    Serial.println(" network(s) found:");
    Serial.println("-----------------------");
    for (int i = 0; i < numberOfNetworks; ++i) {
      Serial.print("Network name: ");
      Serial.println(WiFi.SSID(i));
      Serial.print("Signal strength (RSSI): ");
      Serial.println(WiFi.RSSI(i));
      Serial.println("-----------------------");
    }
  }
}

void loop() {}

The issue was the

delay(100);        // Small delay to settle

After I set it to

delay(2000);   

I got first the response

WL_DISCONNECTED     = 7

and afterwards

WL_CONNECTED     = 1

With regards to the the problem in the first posting I figured there is a part in the code that passes the ssid and passwort, took that out and the Wifi connects fine.

Thanks for any help :+1: