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

Hi,

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"
  https://create.arduino.cc/cloud/things/2cc3e0a7-3e9d-4679-aeb8-88d2c20d428b

  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:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // 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();

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

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


}

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


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:
Serial.begin(9600);

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.