Serial.print in void setup

Hi!
My first post ever :o
So far I was always able to find posts of people struggling with the same issues like me and others who helped them. This time I cannot find any entries on the following problem (and please give me a link if I'm wrong and it has been discussed already):

When I run this code on my mkr wifi 1010:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "test_thing"
  https://create.arduino.cc/cloud/things/XXX

  Arduino IoT Cloud Properties description

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

  bool led;

  Properties 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"


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();
  
  pinMode(LED_BUILTIN, OUTPUT);
  
  Serial.println("this is the last line of void setup");
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  
  Serial.println("this is inside void loop");
  
  delay(3000);
  
}


void onLedChange() {
      digitalWrite(LED_BUILTIN, led);
    Serial.print("The light is ");
    if (led) {
        Serial.println("ON");
    } else {
        Serial.println("OFF");
    }
}

which is basically the LED example with some extra debugging messages, I get this in the serial monitor:

[ 10717 ] Connected to "Kaffeenetz"
this is inside void loop
this is inside void loop
this is inside void loop

[ 19718 ] Connecting to Arduino IoT Cloud...
this is inside void loop
Compile time: 1553299200

[ 27047 ] Connected to Arduino IoT Cloud
this is inside void loop
this is inside void loop
this is inside void loop
this is inside void loop
this is inside void loop
this is inside void loop
The light is ON
this is inside void loop
The light is OFF
this is inside void loop
this is inside void loop
this is inside void loop
this is inside void loop

And i wonder why there is no entry from the void setup function?

Uploading this modified blink sketch with Arduino IDE:

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Blink
*/

// the setup function runs once when you press reset or power the board
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); 
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.println("this is the last line of void setup");
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second

  Serial.println("this is inside void loop");
}

I do get a message out of the setup function:

this is the last line of void setup
this is inside void loop
this is inside void loop
this is inside void loop
this is inside void loop
this is inside void loop

Please help me understand what I am missing.. The void setup function must be passed, since the serial port is available and the pin mode is set (right?) But why is there no message in the serial monitor when uploading it from arduino create/iot cloud?

I noticed this behavior when I tried to insert https://www.arduino.cc/en/Tutorial/WiFiRTC into my "thing" sketch. The connection to the ntp server is established inside void setup - and it wasn't.

Thanks

Moritz

Yeah, I've seen this as well. There are other debug messages that do not display from setup namely the ArduinoCloud.printDebugInfo() which should show the Thing ID and Device ID.

If I just compile and upload the code from the Create Editor the messages from setup and other related debug messages do not appear in the Monitor window.

However, I found if I unplug my device and plug it back in, the debugMessage and Serial.print messages from setup including ArduinoCloud.printDebugInfo() show in the Monitor output.

It is most likely an issue of time.

When working inside Create there can sometimes be a delay between when the board connects to the Serial port (via the Create Agent) and the socket connection being established across the browser window (Monitor) and the Agent itself.

Try extend that delay in setup to 5000 or 7000 milliseconds and let me know if it helps :slight_smile:
(Implementing some form of timeout would also work)

Moritz, could you try to reset your board leaving the Monitor tab open?
It should print everything.
If you can confirm I can investigate if there's some loss of messages after the upload :slight_smile:

I found if I add the long delay after the ArduinoCloud.begin, the Serial.print and debugMessage output appear in the Monitor window after code load. However, any print messages prior to this do not appear even if the delay is before the ArduinoCloud.begin. Actually, if the delay is placed before ArduinoCloud.begin, none of the setup messages appear in the Monitor window.

Ex:

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  delay(7000);

It may be more than just a delay to the monitor, it looks like setup lines aren't being executed.

I have the following code for a temp sensor:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Temp"
  https://create.arduino.cc/cloud/things/3b3b213f-68cc-4015-9776-b14cbb952ea4 

  Arduino IoT Cloud Properties description

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

  float Temp;

  Properties 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"
// Adafruit MCP9808 Library - Version: Latest 
#include <Adafruit_MCP9808.h>
#include <Wire.h>

// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

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

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

}

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

        while(!tempsensor.begin(0x18)){
    //stay here, try again
    delay(250);
  }
  Serial.println("Sensor initialized");

  tempsensor.setResolution(0); // sets the resolution mode of reading, the modes are defined in the table bellow:

  delay(2000);
  tempsensor.wake();   // wake up, ready to read!
  Temp = tempsensor.readTempF();
  tempsensor.shutdown(); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere, stops temperature sampling
  Serial.println(Temp);  
}

This has the initialization and temp print inside loop() and both print before the connection according to the monitor output:

77.90
[ 14828 ] Connected to "Network1"
[ 14828 ] Connecting to Arduino IoT Cloud...
Sensor initialized
77.90
[ 17289 ] Bogus NTP time from API, fallback to UDP method
[ 19791 ] Connected to Arduino IoT Cloud
Sensor initialized
77.90
Sensor initialized
77.90
Sensor initialized
77.90

The sensor doesn't initialize if the initialization is setup():

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Temp"
  https://create.arduino.cc/cloud/things/3b3b213f-68cc-4015-9776-b14cbb952ea4 

  Arduino IoT Cloud Properties description

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

  float Temp;

  Properties 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"
// Adafruit MCP9808 Library - Version: Latest 
#include <Adafruit_MCP9808.h>
#include <Wire.h>

// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

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

    while(!tempsensor.begin(0x18)){
    //stay here, try again
    delay(250);
  }
  Serial.println("Sensor initialized");

  // 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(3);
  ArduinoCloud.printDebugInfo();

}

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

  tempsensor.setResolution(0); // sets the resolution mode of reading, the modes are defined in the table bellow:

  delay(2000);
  tempsensor.wake();   // wake up, ready to read!
  Temp = tempsensor.readTempF();
  tempsensor.shutdown(); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere, stops temperature sampling
  Serial.println(Temp);  
}
 31.89
[ 13825 ] Connected to "Network1"
[ 13825 ] Connecting to Arduino IoT Cloud...
31.89
[ 16667 ] Bogus NTP time from API, fallback to UDP method
[ 19548 ] Connected to Arduino IoT Cloud
31.89
31.89
31.89

Something in ArduinoCloud.Update() maybe??

Here's better output but the sensor is de-initialized somehow:

[ 8142 ] ***** Arduino IoT Cloud - configuration info *****
[ 8142 ] Device ID: xxxxxxx
[ 8143 ] Thing ID: xxxxxxx
[ 8143 ] MQTT Broker: mqtts-sa.iot.arduino.cc:8883
Sensor initialized
[ 8910 ] WiFi.status(): 0
[ 8910 ] Current WiFi Firmware: 1.2.1
[ 8911 ] Connecting to "Network1"
78.80
[ 17546 ] Connected to "Network1"
[ 17546 ] Connecting to Arduino IoT Cloud...
78.80
[ 20207 ] Bogus NTP time from API, fallback to UDP method
[ 22738 ] Connected to Arduino IoT Cloud
31.89
31.89
31.89
31.89
/* 
  Sketch generated by the Arduino IoT Cloud Thing "Temp"
  https://create.arduino.cc/cloud/things/3b3b213f-68cc-4015-9776-b14cbb952ea4 

  Arduino IoT Cloud Properties description

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

  float Temp;

  Properties 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"
// Adafruit MCP9808 Library - Version: Latest 
#include <Adafruit_MCP9808.h>
#include <Wire.h>

// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while(!Serial){
    delay(250);
  };

  // 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(3);
  ArduinoCloud.printDebugInfo();
  
    while(!tempsensor.begin(0x18)){
    //stay here, try again
    delay(250);
  }
  Serial.println("Sensor initialized");

}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  tempsensor.setResolution(0); // sets the resolution mode of reading, the modes are defined in the table bellow:

  delay(2000);
  tempsensor.wake();   // wake up, ready to read!
  Temp = tempsensor.readTempF();
  tempsensor.shutdown(); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere, stops temperature sampling
  Serial.println(Temp);  
}

Try putting the initialization after the 'ArduinoCloud.begin(ArduinoIoTPreferredConnection);' statement with a delay as I posted previously just after 'ArduinoCloud.begin(ArduinoIoTPreferredConnection);'.

I am doing this and my devices initialize without issue.

Thank you very much for your help.

My code works great now with a delay(7000); after ArduinoCloud.begin(ArduinoIoTPreferredConnection); in setup just as ubidefeo and jomoengineer suggested. It shows the debug messages and executes the code.

I don't see a positive effect when resetting the board with the serial monitor open without a delay as decribed above.

jld13, does your sensor initialize when giving ArduinoCloud.begin some extra time?