Logging in setup() not working

I am wondering why I am seeing the following behaviour using SerialLogHandler with a P1 device, deviceOS 3.3.0. I want to explicitly start my cloud connection in loop(), and hence use Semi-automatic mode. However, I am doing quite a bit of initialization in setup(), and would like to log the progress/results. However, I am not seeing any Log messages coming through in setup(), only once loop() starts. Any ideas ? Simplified example code is below:

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
SerialLogHandler logHandler;
void setup() {
    Log.info("Setup entered"); // this does not work
    pinMode(D7,OUTPUT);
    Log.info("Setup complete"); // this does not work
}
void loop() {
    digitalWrite(D7,TRUE);
    Log.info("LED on");  // this works
    delay(2000);
    digitalWrite(D7,FALSE);
    Log.info("LED off");  // this works
    delay(2000);
}

It can take several seconds for USB serial to connect, sometimes up to 8 seconds on Windows and typically setup will already have completed by then. If I’m trying to debug stuff in setup I usually add this at the top of setup():

// Wait for a USB serial connection for up to 15 seconds
waitFor(Serial.isConnected, 15000);
delay(1000);

Or use UART debugging (Serial1LogHandler) which logs immediately because the USB to UART converter connection stays up when the Particle device resets so logging can begin immediately.

1 Like