One ESP32 connects to my network. NO OTHERS WILL

I have ESP32's that I got from amazon here: https://amzn.to/3QRS0Sn

I started with one 3 pack.

Working on a project for a while. Not important to the issue, but it controls a hacked electronic dog door, reads motion sensors inside and outside, an rfid tag to let the dog in and out and based on the pattern of what sensors trigger in what order - it knows if the dog actually went in or out or if the door just opened. With the ESP32, wifi access and twilo, it texts me status of the dog going in and out. And also has a little web server so I can log in to see status, open the door remotely, etc.

For weeks it was connecting to my network, getting the time, texting my phone, etc. No problem.

I'm not great with electronics and more of a software guy and I accidentally blew a couple of the ESPs. Dumb stuff. I test on the fly, plug in jumper cables, etc. One time I think pins touched each other. But anyway I fried 2 of them. But the 3rd one only has a bad RX2/TX2 because.. well.. I plugged them into the RFID 5v serial accidentally. It happens. But otherwise that one can still be programmed and run code.

Ok.. so I ordered a couple more 3 packs. And these just will not connect to wifi. I run the same code on them and they won't connect. But if I run the code on that 3rd one from the first batch - it connects fine right away. Same code.

I've been fighting with it all day. I reduced my code to just a simple Wifi connect. Actually because I was trying to figure out why it's happening, this code scans for networks, prints out when it sees my network name, then connects to a network with that name. The scanning is because the status I was getting after a fail was 1 which is WL_NO_SSID_AVAIL. So I did a scan to see if the SSIDs are available. Here's the test code:

#include "WiFi.h"

// Replace with your network credentials
const char* ssid = "Kermit";
const char* password = "password";


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

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  Serial.println("scan start");

  // WiFi.scanNetworks will return the number of networks found
  int n = WiFi.scanNetworks();

  Serial.println("scan done");
  if (n == 0) {
      Serial.println("no networks found");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) {
      if(WiFi.SSID(i) == "Kermit")
      {
        Serial.println("Found Kermit");
      }
    }
  }
  Serial.println("Setup done");
}

void loop() {
  if(WiFi.status() != WL_CONNECTED) {
    initWiFi();
  }
}

void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

And here is what is output when I run it on that 3rd ESP32 from the first batch:

11 networks found
Found Kermit
Found Kermit
Found Kermit
Setup done
Connecting to WiFi ...192.168.124.201

It connects right away no problem - every time.

But if I use one of the newer ESP's:

19 networks found
Found Kermit
Found Kermit
Found Kermit
Setup done
Connecting to WiFi ............................

Notice that a different number of networks is found.. 11 on the old ESP vs 19 on the newer one. But actually every time I run it there's a different number of networks. So that might not mean anything.

And also that it finds my network 3 times because it is a mesh network and I have 3 nodes.

I tried 6 or 7 of the new boards and none of them will connect to my network. But if I load the sketch on the older one it connects fine. And this is with any of my test apps or even my full sketch that controls the dog door.

I tried connecting to my iPhone hot spot and that works fine on all of them.

So it would seem that it's something to do with my network but if so why does one ESP connect fine?

could you be building with an older veriosn of the ESP32 libraries which are not compatible with the new ESP32 modules
what version of the IDE are you using?
if you load an Access Point into one of the new ESP32 will another running a station find and connect to it?
how far apart are the nodes?

Latest IDE, mac, latest libraries for ESP 32.

The ESP modules are the same version. Running the sample sketch that prints out the board version results the same on both. All markings on the boards are the same. The only distinction between the ”old” ESP and the “new” ESP boards is that one connects to my network and the others don’t. In fact as I said, if I try to connect to my iPhone hot spot they’ll all do that. But this isn’t a solution of course as I can’t leave my phone at home all the time.

As far as how far the nodes are apart. The nodes I mentioned are my home wife mesh. Not esp nodes. And I guess I don’t know which of the nodes it’s connecting to. I assume the first one that shows up in the scan.

does you network have a security check on MAC addresses which it will accept?
do you have to register the MAC addresses of the new ESP32s before they can connect?
if there a limit to the number of MAC addresses it can accept and you have reached that limit?
can you connect to the network manager and see if there is log which is indicating problems when the new ESP32s attempt to connect?

My WiFi mesh is an Amplifi. I don’t see any way to look at a log and there’s no security or mac filtering going on.

I was wondering if maybe something changed in my network and the devices that attached before that change are getting through but the new ones aren’t. Like a Mac filtering but that’s not it. Or like auto connecting to 2.5g. It’s weird that it attaches to the iPhone hot spot. Which to me would indicate that it’s the routers fault somehow.

I’m going to turn on the iot network and see if it’ll attach to that.

It would be nice if your router had a way to show connected devices and log failed attempts, but a quick scan of the manual doesn't show much.

Try running this code to see if you can narrow down the connection issue:

#include <WiFi.h>

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

String get_wifi_status(int status){
    switch(status){
        case WL_IDLE_STATUS:
        return "WL_IDLE_STATUS";
        case WL_SCAN_COMPLETED:
        return "WL_SCAN_COMPLETED";
        case WL_NO_SSID_AVAIL:
        return "WL_NO_SSID_AVAIL";
        case WL_CONNECT_FAILED:
        return "WL_CONNECT_FAILED";
        case WL_CONNECTION_LOST:
        return "WL_CONNECTION_LOST";
        case WL_CONNECTED:
        return "WL_CONNECTED";
        case WL_DISCONNECTED:
        return "WL_DISCONNECTED";
    }
}

void setup(){
    Serial.begin(115200);
    delay(1000);
    int status = WL_IDLE_STATUS;
    Serial.println("\nConnecting");
    Serial.println(get_wifi_status(status));
    WiFi.begin(ssid, password);
    while(status != WL_CONNECTED){
        delay(500);
        status = WiFi.status();
        Serial.println(get_wifi_status(status));
    }

    Serial.println("\nConnected to the WiFi network");
    Serial.print("Local ESP32 IP: ");
    Serial.println(WiFi.localIP());
}

void loop(){}

When I run that it says WL_DISCONNECTED

Just as a test I changed the name of the SSID to "kermit" instead of "Kermit" and then it says WL_NO_SSID_AVAIL, which makes sense because my networks has the K capitalized.

It seems like one time on these newer boards I got it to connect and I did it by changing the WiFi.mode and then changing it back again. But I tried all of those combinations, retrying after a second, doing a disconnect or not, having a .begin in the reconnect loop, etc.

My router does show connected devices.

When I run your code on the ESP that does connect it prints:

Connecting
WL_IDLE_STATUS
WL_DISCONNECTED
WL_CONNECTED
Connected to the WiFi network
Local ESP32 IP: 192.168.124.201

So I added a few things to the code to try to track it better:

const char* MyHostName = "ESP32-DogDoor";

void setup(){
    Serial.begin(9600);
    delay(1000);
    int status = WL_IDLE_STATUS;
    Serial.println("\nConnecting");
    Serial.println(get_wifi_status(status));

    WiFi.setHostname(MyHostName);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while(status != WL_CONNECTED){
        delay(500);
        status = WiFi.status();
        Serial.println(get_wifi_status(status));
    }

    Serial.println("\nConnected to the WiFi network");
    Serial.print("Local ESP32 IP: ");
    Serial.println(WiFi.localIP());
    Serial.print("\nDefault ESP32 MAC Address: ");
    Serial.println(WiFi.macAddress());
    Serial.print("ESP32 HostName: ");
    Serial.println(WiFi.getHostname());
}

So now when I run it on the one that will connect I get:

Connecting
WL_IDLE_STATUS
WL_DISCONNECTED
WL_CONNECTED
Connected to the WiFi network
Local ESP32 IP: 192.168.124.201
Default ESP32 MAC Address: 48:E7:29:96:6F:94
ESP32 HostName: ESP32-DogDoor

And now I can see that it's connected to my kitchen node at 2.4 Ghz

Run the code on one of my newer ones that won't connect. Printed "WL_DISCONNECTED".

I added a WiFi.reconnect() in the while loop and instead it prints:

Connecting
WL_IDLE_STATUS
WL_DISCONNECTED
WL_DISCONNECTED
WL_DISCONNECTED
WL_CONNECT_FAILED
WL_CONNECT_FAILED
WL_DISCONNECTED
WL_CONNECT_FAILED
WL_DISCONNECTED
WL_DISCONNECTED
WL_CONNECT_FAILED

So we know it's trying but it's not making a good connection or it's getting rejected. BTW my RSSI for all the nodes of Kermit are in a decent range. I'm going to keep trying some stuff here and see if I can figure out why it's connecting the one ESP and not the other ones.

By the way this is what my router shows when the one ESP is connected...

Ok more craziness... I found another ESP32 module. This is the one that previously I was able to eventually get to connect to my network. I don't remember what I did to get it to work but I think it was related to the WiFi.mode, but it could have just been dumb luck.

Here is the print log for this one. It takes a min to connect but eventually does:

Connecting
WL_IDLE_STATUS
WL_DISCONNECTED
WL_DISCONNECTED
WL_DISCONNECTED
WL_IDLE_STATUS
WL_DISCONNECTED
WL_IDLE_STATUS
WL_DISCONNECTED
WL_DISCONNECTED
WL_CONNECTED
Connected to the WiFi network
Local ESP32 IP: 0.0.0.0
Default ESP32 MAC Address: 48:E7:29:AF:AE:68
ESP32 HostName: ESP32-DogDoor

And it said that the IP address was 0 but when I check the server it does have a real IP address. Probably tried to print it before it was served one.

The crappy thing is that I can just use this one ESP now to do my project. But that doesn't solve my problem. If no other ESPs are going to easily connect I'll just be putting off the problem until the next project.

Sometimes on my full code, I see this print out:

E (30768) wifi:sta is connecting, return error

and I'm not sure why because I'm not printing it out myself.

I also want to leave this resource. Other than the annoying ads and slow page loading t's the best webpage I've found for info on ESP32 wifi connection:

And then this other page of his also has some useful tips for troubleshooting:

:man_shrugging:

Heck if I know. This code is working on the ESPs that were not connecting before. It's from the 2nd link in my previous post..

#include <WiFi.h>
 
const char* ssid = "Kermit";
const char* password = "password";
 
void ConnectedToAP_Handler(WiFiEvent_t wifi_event, WiFiEventInfo_t wifi_info) {
  Serial.println("Connected To The WiFi Network");
}
 
void GotIP_Handler(WiFiEvent_t wifi_event, WiFiEventInfo_t wifi_info) {
  Serial.print("Local ESP32 IP: ");
  Serial.println(WiFi.localIP());
}
 
void setup() {
  Serial.begin(9600);
  delay(500);
  
  WiFi.mode(WIFI_STA);
  WiFi.onEvent(ConnectedToAP_Handler, ARDUINO_EVENT_WIFI_STA_CONNECTED);
  WiFi.onEvent(GotIP_Handler, ARDUINO_EVENT_WIFI_STA_GOT_IP);
  WiFi.begin(ssid, password);
  Serial.println("\nConnecting to WiFi Network ..");
}
 
void loop() {
  // Do Nothing
}

It's connecting now with this code. Strangely it's not printing the "connecting to wifi" message but maybe the serial isn't actually getting set up quick enough. I added a delay to give it time but it's still not printing that out.

I'll probably modify my code to use something like this and see if it works more reliably.

Here is the code the is working best for me now.

EDIT - It's not working perfectly on all my ESP's so I still have some issue. But at least it's eventually connecting now on most of them. I'm marking this as the solution but if anyone has any ideas on why it's still only kinda working - let me know.

And the good thing about this is there are no endless while loops. It will attempt to reconnect every 10 seconds but it doesn't delay whatever is happening in the main loop...

#include <WiFi.h>
 
// Replace with your own network credentials
const char* ssid = "Kermit";
const char* password = "password";
 
bool firstConnected = false;
unsigned long nextReconnectAttemptTime;

void ConnectedToAP_Handler(WiFiEvent_t wifi_event, WiFiEventInfo_t wifi_info) {
  Serial.println("Connected To The WiFi Network");
}
 
void GotIP_Handler(WiFiEvent_t wifi_event, WiFiEventInfo_t wifi_info) {
  Serial.print("Local ESP32 IP: ");
  Serial.println(WiFi.localIP());
}
 
void setup() {
  Serial.begin(9600);
  delay(1000);

  WiFi.mode(WIFI_STA);
  WiFi.onEvent(ConnectedToAP_Handler, ARDUINO_EVENT_WIFI_STA_CONNECTED);
  WiFi.onEvent(GotIP_Handler, ARDUINO_EVENT_WIFI_STA_GOT_IP);
  WiFi.begin(ssid, password);
  Serial.println("\nConnecting to WiFi Network ..");

  nextReconnectAttemptTime = millis() + 10000; // try again in 10 seconds
}
 
void loop() {
  handleWiFiConnection();
  // do your other stuff
}

void handleWiFiConnection() {
  if (WiFi.status() != WL_CONNECTED){
    if (firstConnected == true) {
      // we were connected but aren't now
      firstConnected = false;
      Serial.println("Wifi Disconnected");
      nextReconnectAttemptTime = millis() + 10000; // try again in 10 seconds
    }
    else if (millis() > nextReconnectAttemptTime) {
      // after X seconds attempt a reconnect
      Serial.println("Trying to reconnect");
      WiFi.begin(ssid, password);
      nextReconnectAttemptTime = millis() + 10000; // try again in 10 seconds
    }
  }
  else if (firstConnected == false) {
    firstConnected = true;
    // do some newly connected stuff like
    Serial.println("Wifi Connected");
  }
}

Here is my theory: The way I was doing it before, which is sort of the way that every website suggests you do it, was inundating the library with connection attempts and not giving it time to handle the handshakes. For some reason some of the ESP32's were able to connect right away and thus it wasn't a problem. But others were needing more time to connect and the retries were making it fail. At least that's my theory. Who knows.

But the way this one works is that it sets up two onEvent interrupts that will run when those events happen. One is when it connects and the other is when it gets an IP address. The setup gets all of that set up and sets a bool so that certain things can happen only once when it connects. For me this is sending a text message via twilo that it connected. Maybe it's turning on an LED that shows internet connected status. The other var is so that we know when to attempt a reconnect, which is every 10 seconds. If the network is rebooted (which happens at my house almost daily) it will disconnect and then retry and when it gets the connection again it will re run those first connection items.

Meanwhile the rest of the project can run in loop and the wifi stuff can sort of work in the background. You can use if (WiFi.status() == WL_CONNECTED) or if (firstConnected == true) elsewhere in the code to only do connected stuff if you're connected.

Feel free to use or modify this code as I took the important parts from the link a few posts up.

1 Like

There's got to be some additional voodoo magic going on. I had this code not connect at all again. And then work again a bit after that. Now a version of it is in my main project and working pretty well although sometimes it takes a couple of 10 sec cycles before it actually connects.

Try to print WiFi.RSSI() every second when it's connected. Then start 1m away from your AP and increase the distance. Most liely you'll see that the "good" ESPs start with high RSSI values, while the bad wones start with low values and loos connection after some meters. I have two ESPs that worked fine and the suddenly did not connect any more - did not find the exact reason, but I suspect ether thermal/mecanical stress or ESD issues as this happend when the units were solders to a PCB and did not pass the test afterwards.

I still had all kinds of trouble connecting. To where even the one that sort of worked stopped working. All the while the "bad" ESP board would connect instantly.

I fixed it now though. Now all the ESPs connect right away.

The fix was an option on the router called "Router Steering". It says that directs devices to the router instead of the mesh points. So this fixed it for me.

My network is a mesh with a main router and two mesh points. The ESPs were often connecting to the main point before as well, as it is the closest and has the best signal.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.