ESP32 Not Conneting to Wifi

Hi,
I read many threads about ESP32 not connecting, and got some code snippets to troubleshoot but I am no where closer to finding out why it's so much harder to make ESP32 work! I have few 8266 boards and they connect to wifi first try, and stay connected.

I tried the NTP exampe, mDNS example, and STA enable... nothing worked...

if I leave the ESP32 for a day or two trying over and over again, I may get lucky and come back and see it connected. Here is what I see:

17:35:00.930 -> Restarting
17:35:00.964 -> ets Jul 29 2019 12:21:46
17:35:00.964 -> 
17:35:00.964 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
17:35:00.964 -> configsip: 0, SPIWP:0xee
17:35:00.964 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
17:35:00.964 -> mode:DIO, clock div:1
17:35:00.964 -> load:0x3fff0030,len:1184
17:35:00.964 -> load:0x40078000,len:13220
17:35:01.007 -> ho 0 tail 12 room 4
17:35:01.007 -> load:0x40080400,len:3028
17:35:01.007 -> entry 0x400805e4
17:35:02.917 -> Connecting...
17:35:03.010 -> 1,2,3,4,5,6,7,8,10
17:32:00.433 -> E (35617) wifi:sta is connecting, return error
17:32:04.453 -> 9
17:32:04.453 -> E (39624) wifi:sta is connecting, return error
17:32:08.469 -> 10
17:32:08.469 -> E (43632) wifi:sta is connecting, return error
17:32:12.467 -> 
17:32:12.467 -> Restarting

After trying everything, wondering if it's just a bad board.

Any help is greatly appreciated.

NOTE: This is what I have:

Wemos D1 R32 ESP32 WiFi Bluetooth Compatible CH340 Development Board Micro USB Interface for Arduino R3 Type-B USB DC 5-12V - AliExpress

Thanks,

Hi @shadisoft ,
Please paste your code here which you´re using to connect the DUNO to WiFi.
This is very important, the more we know, the more simpler is it for us to take a view into your problem, now we are looking at a closed door if you know what I mean. :wink:

Hi,

Here is my code :slight_smile:

#include "arduino_secrets.h"

#include <WiFi.h>
#include <esp_wifi.h>

//Time in seconds
#define CONNECTION_TIMEOUT 5
#define DEEP_SLEEP_DURATION 10

#include "thingProperties.h"

void setup() {
  // Initialize serial and wait for port to open:

  Serial.begin(115200);
  
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  //Wifi
  connectToWiFi();
 
  Serial.println("\nConnected to the WiFi network");
  get_network_info();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 

  delay(1000);
   
  
}

const char* wl_status_to_string(wl_status_t status) {
  switch (status) {
    case WL_NO_SHIELD: return "WL_NO_SHIELD";
    case WL_IDLE_STATUS: return "WL_IDLE_STATUS";
    case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL";
    case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";
    case WL_CONNECTED: return "WL_CONNECTED";
    case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";
    case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST";
    case WL_DISCONNECTED: return "WL_DISCONNECTED";
  }
}

void connectToWiFi()
{
  int TryCount = 0;
  Serial.println( "Connecting..." );
  while ( WiFi.status() != WL_CONNECTED )
  {
    TryCount++;
    WiFi.disconnect();
  
    WiFi.mode(WIFI_STA);
    WiFi.enableSTA(true);

    WiFi.begin(SSID, PASS);
    Serial.print( TryCount );Serial.print( " " );
    //Serial.print( "." );
    delay(4000);
    if ( TryCount == 10 )
    {
      Serial.println( "" );Serial.println( "Restarting" );
      ESP.restart();
    }
  }
  WiFi.onEvent(WiFiEvent);
}

void get_network_info(){
    if(WiFi.status() == WL_CONNECTED) {
        Serial.print("[*] Network information for ");
        Serial.println(SSID);

        Serial.println("[+] BSSID : " + WiFi.BSSIDstr());
        Serial.print("[+] Gateway IP : ");
        Serial.println(WiFi.gatewayIP());
        Serial.print("[+] Subnet Mask : ");
        Serial.println(WiFi.subnetMask());
        Serial.println((String)"[+] RSSI : " + WiFi.RSSI() + " dB");
        Serial.print("[+] ESP32 IP : ");
        Serial.println(WiFi.localIP());
    }
}

void WiFiEvent(WiFiEvent_t event)
{
   Serial.println( "[WiFi-event] event: " + event );
  switch (event) {
        case SYSTEM_EVENT_WIFI_READY:
          Serial.println("WiFi interface ready");
          break;
        case SYSTEM_EVENT_SCAN_DONE:
          Serial.println("Completed scan for access points");
          break;
        case SYSTEM_EVENT_STA_START:
          Serial.println("WiFi client started");
          break;
        case SYSTEM_EVENT_STA_STOP:
          Serial.println("WiFi clients stopped");
          break;
    case SYSTEM_EVENT_STA_CONNECTED:
      Serial.println("Connected to access point");
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("Disconnected from WiFi access point");
      break;
        case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
          Serial.println("Authentication mode of access point has changed");
          break;
        case SYSTEM_EVENT_STA_GOT_IP:
          Serial.println("Obtained IP address: " + WiFi.localIP() );
          break;
        case SYSTEM_EVENT_STA_LOST_IP:
          Serial.println("Lost IP address and IP address is reset to 0");
          //      vTaskDelay( 5000 );
          //      ESP.restart();
          break;
        case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
          Serial.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
          break;
        case SYSTEM_EVENT_STA_WPS_ER_FAILED:
          Serial.println("WiFi Protected Setup (WPS): failed in enrollee mode");
          //      ESP.restart();
          break;
        case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
          Serial.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
          break;
        case SYSTEM_EVENT_STA_WPS_ER_PIN:
          Serial.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
          break;
        case SYSTEM_EVENT_AP_START:
          Serial.println("WiFi access point started");
          break;
        case SYSTEM_EVENT_AP_STOP:
          Serial.println("WiFi access point  stopped");
          //      WiFi.mode( WIFI_OFF);
          //      esp_sleep_enable_timer_wakeup( 1000000 * 2 ); // 1 second times how many seconds wanted
          //      esp_deep_sleep_start();
          break;
        case SYSTEM_EVENT_AP_STACONNECTED:
          Serial.println("Client connected");
          break;
    case SYSTEM_EVENT_AP_STADISCONNECTED:
      Serial.println("WiFi client disconnected");
          break;
        case SYSTEM_EVENT_AP_STAIPASSIGNED:
          Serial.println("Assigned IP address to client");
          break;
        case SYSTEM_EVENT_AP_PROBEREQRECVED:
          Serial.println("Received probe request");
          break;
        case SYSTEM_EVENT_GOT_IP6:
          Serial.println("IPv6 is preferred");
          break;
        case SYSTEM_EVENT_ETH_GOT_IP:
          Serial.println("Obtained IP address");
          break;
    default: break;
  }
}

For firsties, I would place your ssid , password in the main file and increase the timeout to 10.
Make sure you have the correct ssid and password. I know you checked it though. You do
have a board with a built-in antenna, right ?

The connection timeout is that in seconds or milliseconds ? Maybe it should be 5000.

@shadisoft Try following code and replace the xxx with your internet ssid and password:

// Try this sketch before removing the // from your file
// #include "arduino_secrets.h" 
// #include "thingProperties.h"
#include <WiFi.h>
#include <esp_wifi.h>

//Time in seconds
#define CONNECTION_TIMEOUT 5
#define DEEP_SLEEP_DURATION 10

// Paste your SSID and PASSWORD from your internet provider or settings
const char *SSID = "xxxxxxxxxxxx";
const char *PASS = "xxxxxxxxxxxxx";

void setup() {
  // Initialize serial and wait for port to open:

  Serial.begin(115200);

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  // initProperties();

  //Wifi
  connectToWiFi();

  Serial.println("\nConnected to the WiFi network");
  get_network_info();

  // Connect to Arduino IoT Cloud
  // ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */

  // setDebugMessageLevel(2);
  // ArduinoCloud.printDebugInfo();
}

void loop() {
  // ArduinoCloud.update();
  // Your code here

  delay(1000);
}

const char *wl_status_to_string(wl_status_t status) {
  switch (status) {
    case WL_NO_SHIELD: return "WL_NO_SHIELD";
    case WL_IDLE_STATUS: return "WL_IDLE_STATUS";
    case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL";
    case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";
    case WL_CONNECTED: return "WL_CONNECTED";
    case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";
    case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST";
    case WL_DISCONNECTED: return "WL_DISCONNECTED";
  }
}

void connectToWiFi() {
  int TryCount = 0;
  Serial.println("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {
    TryCount++;
    WiFi.disconnect();

    WiFi.mode(WIFI_STA);
    WiFi.enableSTA(true);

    WiFi.begin(SSID, PASS);
    Serial.print(TryCount);
    Serial.print(" ");
    //Serial.print( "." );
    delay(4000);
    if (TryCount == 100) {
      Serial.println("");
      Serial.println("Restarting");
      ESP.restart();
    }
  }
  WiFi.onEvent(WiFiEvent);
}

void get_network_info() {
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("[*] Network information for ");
    Serial.println(SSID);

    Serial.println("[+] BSSID : " + WiFi.BSSIDstr());
    Serial.print("[+] Gateway IP : ");
    Serial.println(WiFi.gatewayIP());
    Serial.print("[+] Subnet Mask : ");
    Serial.println(WiFi.subnetMask());
    Serial.println((String) "[+] RSSI : " + WiFi.RSSI() + " dB");
    Serial.print("[+] ESP32 IP : ");
    Serial.println(WiFi.localIP());
  }
}

void WiFiEvent(WiFiEvent_t event) {
  Serial.println("[WiFi-event] event: " + event);
  switch (event) {
    case SYSTEM_EVENT_WIFI_READY:
      Serial.println("WiFi interface ready");
      break;
    case SYSTEM_EVENT_SCAN_DONE:
      Serial.println("Completed scan for access points");
      break;
    case SYSTEM_EVENT_STA_START:
      Serial.println("WiFi client started");
      break;
    case SYSTEM_EVENT_STA_STOP:
      Serial.println("WiFi clients stopped");
      break;
    case SYSTEM_EVENT_STA_CONNECTED:
      Serial.println("Connected to access point");
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("Disconnected from WiFi access point");
      break;
    case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
      Serial.println("Authentication mode of access point has changed");
      break;
    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.println("Obtained IP address: " + WiFi.localIP());
      break;
    case SYSTEM_EVENT_STA_LOST_IP:
      Serial.println("Lost IP address and IP address is reset to 0");
      //      vTaskDelay( 5000 );
      //      ESP.restart();
      break;
    case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
      Serial.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
      break;
    case SYSTEM_EVENT_STA_WPS_ER_FAILED:
      Serial.println("WiFi Protected Setup (WPS): failed in enrollee mode");
      //      ESP.restart();
      break;
    case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
      Serial.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
      break;
    case SYSTEM_EVENT_STA_WPS_ER_PIN:
      Serial.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
      break;
    case SYSTEM_EVENT_AP_START:
      Serial.println("WiFi access point started");
      break;
    case SYSTEM_EVENT_AP_STOP:
      Serial.println("WiFi access point  stopped");
      //      WiFi.mode( WIFI_OFF);
      //      esp_sleep_enable_timer_wakeup( 1000000 * 2 ); // 1 second times how many seconds wanted
      //      esp_deep_sleep_start();
      break;
    case SYSTEM_EVENT_AP_STACONNECTED:
      Serial.println("Client connected");
      break;
    case SYSTEM_EVENT_AP_STADISCONNECTED:
      Serial.println("WiFi client disconnected");
      break;
    case SYSTEM_EVENT_AP_STAIPASSIGNED:
      Serial.println("Assigned IP address to client");
      break;
    case SYSTEM_EVENT_AP_PROBEREQRECVED:
      Serial.println("Received probe request");
      break;
    case SYSTEM_EVENT_GOT_IP6:
      Serial.println("IPv6 is preferred");
      break;
    case SYSTEM_EVENT_ETH_GOT_IP:
      Serial.println("Obtained IP address");
      break;
    default: break;
  }
}

For me it worked and I get something like this in the serial monitor:

Connecting...
1 
Connected to the WiFi network
[*] Network information for xxxxxxx-dd1cd
[+] BSSID : XX:XX:33:7D:X1:X1
[+] Gateway IP :192.168.0.1
[+] Subnet Mask : 255.255.255.0
[+] RSSI : -57 dB
[+] ESP32 IP : 192.168.0.14

Thanks for the code.

I got:

But I then decided to try another SSID (I have a couple) and the other SSID worked... I know ssid/pass is correct as my router shows the device as being connected on the bad SSID.

So I'll use the new SSID and see if it stick or intermittent. At least I would know it's not defective.

Thanks,

I think it's defective, cause even when I connected and I got an IP. The ping for it was very very poor. it also failed to connect to cloud. I tried both SSIDs. even when they connect it's very poor connection.

not much else other than maybe defective unit. I guess I should try another one.

Thanks for all the help.

1 Like

You´re welcome. And I´m happy to see you found the problem.

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