#include WiFi.h stops sketch running

I'm unsure where to post this. I'm using an ESP32-C3 Zero with the IDE v 2,3,4
I've created a trivial sketch to show the problem, here it is

//#include <WiFi.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial) delay(100);  // wait for native usb
  Serial.println("setup");
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("loop");
  delay(1000);
}

It compiles, loads and runs as you would expect, printing 'setup' and then 'loop' forever.
The problem is, if you uncomment the #include <WiFi.h>, it compiles and loads but produces no serial output whatsoever!
I'm completely at a loss as to what's going on.

I'm not very familiar with the esp32 but there might be an error in your code. You had:

I think the proper syntax for that would be:

while (!Serial) {
  delay(100);
}

So that might somehow be effecting your output.

1 Like

There is no difference between those two while loops. One statement does note require curly braces....

Thanks for the suggestion but as pointed out my syntax is correct. If it were not, it would not compile. BUT, as this seems so odd to me, I did try adding the curly braces and they made no difference.

I had a look at WiFi.h but debugging it is not easy.

I was a bit concerned that perhaps the sketch was in fact running and it was just the Serial that was broken so I added code to flash the internal LED. The sketch is NOT running at all when WiFi.h is included. Even though I'm not calling any WiFi code at all!

Aha!
I've now found that I can make the sketch run by adding the line
WiFi.mode(WIFI_STA);
before the
Serial.begin(115200);

I speculate that you must somehow initialise something in WiFi before Serial will work. That seems wrong to me.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.