ESP32 can't connect to the wifi unless serial is open


I have an issue with my ESP32 project. If a serial monitor is open with the appropriate baud rate the esp32 can connect to the wifi and than it can connect to the IoT Cloud. If no serial monitor is open or the open monitor has wrongly set the baud rate the esp can not connect to the wifi.

Pleas find my code below.

  Sketch generated by the Arduino IoT Cloud Thing "Untitled"

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudDimmedLight g_LED;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.

#include "thingProperties.h"

#define LED_OUT_PIN 2 //A12 or IO2

void setup() {
  // Initialize serial and wait for port to open:
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found

  // Defined in thingProperties.h

  // Connect to Arduino IoT Cloud

     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

  ledcSetup(1, 12000, 8); // 12 kHz PWM, 8-bit resolution

void loop() {
  // Your code here


void onGLEDChange() {
  // Do something
  if ( g_LED.getSwitch())
    ledcWrite(1, g_LED.getBrightness());

Thank you for your time and answers!

Sorry, I do not have an ESP32, but you could try to remove the following line:

// Initialize serial and wait for port to open:

I suspect Serial.begin will wait for a serial connection via the USB connection (as the description says).

I was not aware of Serial.begin waits until the connection is established. Commenting out the line solved the problem.
Thank you!

1 Like

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