does printing to serial lock up program progress if not connected to serial?

You can see my code here: BBQThermostat/BBQ_Thermostat.ino at master · djotaku/BBQThermostat · GitHub as of commit Added an mqtt topic for the smoker temp and one for the SSID · djotaku/BBQThermostat@da23769 · GitHub if it helps later if I update the file. I think the relevant code is:

void setup() {
  // Serial init code. This is for debugging and should be commented out when it's running at the smoker if debugging is not taking place
  Serial.begin(9600);
  while (!Serial);

  // make sure THERM shield will initialize
  if (!THERM.begin()) {
    Serial.println("Failed to initialize MKR THERM shield!");
    while (1);
  }

  //attempt to connect to Wifi Network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data: - this is also for debugging and should be commented out when running at the smoker if not debugging
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();

  //mqtt
  client.setServer(server, 1883);
  client.setCallback(callback);
}

void loop() {
  // debugging - wifi check
  printCurrentNet();
  // debugging - therm check
  Serial.print("Reference temperature ");
  Serial.print(THERM.readReferenceTemperature());
  Serial.println(" °C");
  Serial.print("Temperature ");
  Serial.print(THERM.readTemperature());
  Serial.println(" °C");
  Serial.println();
  //mqtt
  int smokertempnum = THERM.readTemperature();
  char cstr[100];
  itoa(smokertempnum,cstr,10);
  if (!client.connected()) {
    reconnect();
  }
  client.publish("smoker/temp",cstr);
  client.publish("smoker/WiFi/SSID",WiFi.SSID());
  
  delay(10000);

}

Right now if I connect the board to power, I don't get anything published to MQTT. As soon as I connect a serial monitor (from the Arduino IDE), it APPEARS to me to print code as if it were at the beginning of the program. In other words, it prints that it's connected to the Wifi, the Wifi data, and then that it's connecting to the MQTT. Then I start getting stuff published to MQTT.

So that leads me to believe that if I'm printing to the serial and in my code, but I'm not connected to the serial console it will not go through the code. Is there a way to easily change this so that when I'm debugging and connected to the console I can see the data, but when I'm not connected to the console it works? I mean, it's not the end of the world if I have to comment out the serial lines, but I was wondering if there were a more elegant solution.

Thanks!

Serial doesn't block just because nothing is listening. The Arduino has no idea if anyone received the transmission or not. It's just like my wife, it will happily keep talking to you whether you are listening or not.

Opening the serial connection resets the board. That's why it looks like the beginning of the program when you open the serial connection.

Since you're using a MKR Therm shield, I'm guessing you're also using a MKR board. On the MKR boards, and all other boards that have native USB functionality, this line:

djotaku:

  while (!Serial);

Causes your program to wait in an infinite loop until a serial connection is opened (e.g., opening Serial Monitor). That can be useful because, unlike the non-native USB boards like the Uno, Mega, Nano, etc., on the native USB boards opening the serial connection does not reset the board. So any serial output from a native serial board between the time the program starts and you get Serial Monitor open is lost.

However, that code is certainly not helpful when you want to run your code without a serial connection open.

Oh, thanks! After I posted the help I was thinking through my code and thought that might do it, but I've been away from the computer since then to test. Glad I'm starting to understand hardware programming and glad it turned out to be what I thought it was.