ESP32 Wifi wont connect without the serial being open

I am using the code below to test the wifi connection. I use Visual Studio Code, the code runs and connects only if the serial monitor in VSC is open. If you close it and press reset on the ESP32 the code does not connect. If you open the serial monitor again and press reset it then connects. As you can see from the code it does not use Serial at all. Likewise if you plug the ESP into a wall psu it does not connect. The problem is not the psu. When you plug a USB device into a computer a discussion is held between the computer and device. This problem must be something to do with a lack of discussion however it needs someone who understands what goes on. There are quite a number of similar reports but none (that I can find) offer a solution that works. Is there anyone out there who understands what is going on.

/*
 *
 */

#include <WiFi.h>

#define LED_RED 32
#define LED_GRN 33
#define RED_ON    0  
#define GREEN_ON  1
#define BOTH_ON   2
#define BOTH_OFF  3

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

void SetLeds(uint8_t red, uint8_t green, uint8_t val);
void FlashLed(bool State, uint8_t period, uint8_t Flashes);


void setup()
{
    pinMode(LED_RED, OUTPUT);
    pinMode(LED_GRN, OUTPUT);

    FlashLed(true, 150, 2);

    delay(1000);

    // We start by connecting to a WiFi network
    WiFi.persistent(false);
    WiFi.hostname("WiFi_Test");
    WiFi.mode(WIFI_MODE_STA);
    WiFi.enableSTA(true);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        FlashLed(false, 150, 2);
    }

    FlashLed(WiFi.status() == WL_CONNECTED, 150, 5);
}


void loop() {
}

void FlashLed(bool State, uint8_t period, uint8_t Flashes) {

  for (byte i = 0; i < Flashes; i++) {
    if (State) {
      SetLeds(LED_RED, LED_GRN, GREEN_ON);        
    } else {
      SetLeds(LED_RED, LED_GRN, RED_ON);        
    }
    
    delay(period);
    SetLeds(LED_RED, LED_GRN, BOTH_OFF);        
    delay(period);
  }
}

void SetLeds(uint8_t red, uint8_t green, uint8_t val) {
  switch (val) {
    case RED_ON:    digitalWrite(red, 0);
                    digitalWrite(green, 1);
                    break;
    case GREEN_ON:  digitalWrite(red, 1);
                    digitalWrite(green, 0);
                    break;
    case BOTH_ON:   digitalWrite(red, 0);
                    digitalWrite(green, 0);
                    break;
    case BOTH_OFF:  digitalWrite(red, 1);
                    digitalWrite(green, 1);
                    break;
  }
}

the following works for me on different USB ports on my laptop or the USB charger for my phone using a TTGO T-Koala

there are distinct LED states indicating the progress thru the code

/*
*
*/

#include <WiFi.h>

#define LED_RED 32
#define LED_GRN 33
#define RED_ON    0
#define GREEN_ON  1
#define BOTH_ON   2
#define BOTH_OFF  3

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

void SetLeds (uint8_t red, uint8_t green, uint8_t val);
void FlashLed (bool State, uint8_t period, uint8_t Flashes);

enum { Off = HIGH, On = LOW };

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

    pinMode (LED_RED, OUTPUT);
    pinMode (LED_GRN, OUTPUT);

    digitalWrite (LED_RED, On);
    digitalWrite (LED_GRN, On);

    Serial.println ("start");
    delay (1000);

    // We start by connecting to a WiFi network
    WiFi.persistent (false);
    WiFi.hostname ("WiFi_Test");
    WiFi.mode (WIFI_MODE_STA);
    WiFi.enableSTA (true);
    WiFi.begin (ssid, password);

    digitalWrite (LED_GRN, Off);
    Serial.println ("begin");
    delay (1000);

    while (WiFi.status() != WL_CONNECTED) {
        digitalWrite (LED_GRN, ! digitalRead (LED_GRN)); 
        digitalWrite (LED_RED, ! digitalRead (LED_RED)); 

        Serial.println ("! WL_CONNECTED");
        delay (500);
    }
    digitalWrite (LED_RED, Off);
}

void loop ()
{
    digitalWrite (LED_GRN, ! digitalRead (LED_GRN)); 
    Serial.println ("loop");
    delay (250);
}

Thanks for the comments. Quite a few things tried but very inconsistent results, mostly not working and then randomly working for no good reason. It had the feeling of a hardware problem which is what it turned out to be. The version of ESP32-WROOM32 (Nodemcu) that I have appears not have a pullup resistor fitted to the enable input (pin 3). I pulled the enable pin (3) up to +3.3V with a 10K resistor and lo and behold it all works ok and importantly, consistently. I have wasted so many hours on this, I should have thought laterally a bit sooner.

which is pin3?

see ESP32 Pinout Reference which describes caveats of various pins.

Here is the pinout from the Espressif ESP-WROOM-32 Datasheet. On the Nodemcu ESP32-WROOM32 module the enable pin is the top pin in the left hand side with the USB at the bottom.

i'm confused, your soldered a wire directly to pin 3 (EN) pad on the board, not a pin on periphery of the board that could be plugged into a solderless breadboard?

As in my last reply I am using a Nodemcu ESP32-WROOM32 module which has 15 pins on the left and right sides of the module. From a top view with the module USB facing downwards the enable pin (EN) appears as the top pin on the left hand side. I hope this clarifies things. I put the ESP32-WRROM-32 pin down so that those not using a Nodemcu module could work out where it appears on their module.

image

sounds like it shouldn't work even when connected to USB, that the EN pins needs to be pulled up. puzzled

Hmm, well the data sheet doesn't have any fine detail, the image shows all the info there is. Although it says it is active high it doesn't say whether it has an internal pullup or its high impedance and just floating around. If its high impedance it could have 50/60Hz hum on it which could cause who knows what. The board could/should have a pullup fitted. For me connecting the pullup fixed the problem immediately. What I did find was that you need to connect the pullup and then press reset on the board.

image

the pullup would be on the board, not internal to the chip and possibly connected to a reset button

here are the circuits on the EN pin of an LILYGO T-Koala and a LOLIN

rstEn

lolin

I would have expected that the pullup would be on the module, I guess that it is possible that my module has a faulty track/missing resistor. There are however plenty of problems out there similar to those that I experienced. I need to order another module as the only one I have is now in service so I will check the new one when it comes.

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