Why does Adafruit IO code for Arduino wait for Serial monitor?

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
ecodad
 
Posts: 18
Joined: Wed Jun 05, 2013 2:18 pm

Why does Adafruit IO code for Arduino wait for Serial monitor?

Post by ecodad »

I am trying to make something similar to the adafruitio 22 environmental monitor that will eventually be solar powered. The problem I am running into is that the code doesn't run until I open the serial monitor because of the line While (!Serial)

Code: Select all

void setup() {
  // start the serial connection
  Serial.begin(9600);

  // wait for serial monitor to open
  while (!Serial);

  Serial.println("Adafruit IO Environmental Logger");
If this line is in the code, the Feather ESP32-S3 connects to the wifi network, connects to Adafruit.io and adds data to the feed only after I open the Serial Monitor. If I comment out the line, it never works. And if I connect it to an external supply via USB-C it never connects to the wifi, which means it doesn't work.

It also does not work if I comment out every Serial.print line throughout the code.

Any thoughts?

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Why does Adafruit IO code for Arduino wait for Serial monitor?

Post by dastels »

It should work if you comment out all references to Serial.

You can use conditional compilation of avoid having to comment/uncomment when you want serial output or not. Something like:

Code: Select all

#define DEBUG         // Comment this line when DEBUG mode is not needed

void setup()
{
#ifdef DEBUG
  Serial.begin(115200);
  while (!Serial);
#endif
}

void loop() 
{
#ifdef DEBUG
  Serial.print("Generate Signal ");
#endif 

  for (int j = 0; j <= 200; j++) {
    digitalWrite(13, HIGH);
    delayMicroseconds(100); 
    digitalWrite(13, LOW);
    delayMicroseconds(200 - 100);
  }
}
Dave

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: Why does Adafruit IO code for Arduino wait for Serial monitor?

Post by westfw »

This behavior is inherited from other "native USB" Arduino implementations. Unfortunately.

I believe you can replace the "while (!Serial);" with a second or two worth of delay, as a compromise.

User avatar
ecodad
 
Posts: 18
Joined: Wed Jun 05, 2013 2:18 pm

Re: Why does Adafruit IO code for Arduino wait for Serial monitor?

Post by ecodad »

westfw wrote: Fri Oct 28, 2022 2:57 am I believe you can replace the "while (!Serial);" with a second or two worth of delay, as a compromise.
Thank you! That worked :-)

Now I'm curious why having the delay enables it to work.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Why does Adafruit IO code for Arduino wait for Serial monitor?

Post by dastels »

I assume it gives the connection time to establish, sync, handshake, whatever it has to do.

Dave

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: Why does Adafruit IO code for Arduino wait for Serial monitor?

Post by westfw »

it gives the connection time to establish, sync, handshake, whatever it has to do.
Yes. When the "Serial" connection works directly over USB, there is a relatively complex startup sequence where the computer goes "hey, you're a new device! Tell me about yourself", and the Arduino responds "well, I'm an Adafruit Feather xxx and I implement a Serial port" and some other stuff. depending on how busy the computer is, this can take a couple of seconds. Before that, the Serial port doesn't exist on the Arduino side.

The usual "while (!Serial);" loop waits for all of this to complete, so that data sent by the sketch doesn't just disappear. I don't know for sure that I agree with this behavior - after all if you have a real serial port that isn't connected to a computer, the data disappears. But that's the way the first people who implemented native USB ports did things, and everyone else has followed along.
(which is probably better than different boards behaving differently.)

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”