How does this program use serial port module without defining it?

I am loading existing example in Arduino which runs as expected. What I don't understand is it sends serial data that I see on PC using Serial.Begin(9600) and Serial.println(); statements but Serial module is not defined anywhere? The same is true for WiFi module but at list there is an included for that #include <ESP8266WiFi.h>

My question is how this example is using these classes which are never defined?

Here is the complete example code.

#include <ESP8266WiFi.h>

const char ssid[] = "MyNetworkName";
const char pass[] = "password";

void setup() {
 Serial.begin(9600);
 while (!Serial) {
   ; // Wait for serial port to connect.
 }
 delay(1000);
 Serial.println();
 Serial.println("IoT DEVICE STARTED");
  
 // Connect to Wi-Fi network
 Serial.printf("\nConnecting to SSID %s ", ssid);

 // Start the Wi-Fi connection and wait until it connects
 WiFi.begin(ssid, pass);
 
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.println(status);
   Serial.print(".");
   
 }

 // Print the IP address
 Serial.print("\nDevice is on the network at ");
 Serial.print(WiFi.localIP());

}

void loop() {
 // Nothing to do here yet
}

A global instance of the HardwareSerial (or equivalent depending on the board architecture) class is declared in the core library.
Here is an example from the Arduino AVR Boards core of the Uno, etc., but it's something similar for every board:

This is a relatively common approach in Arduino libraries where only a single instance is likely to be needed by the user. You can see another example of it in the Keyboard library:

skippy1:
The same is true for WiFi module but at list there is an included for that #include <ESP8266WiFi.h>

During sketch preprocessing, an #include directive for Arduino.h is added to the .ino files of a sketch if an #include directive is not already present:
https://arduino.github.io/arduino-cli/latest/sketch-build-process/#pre-processing

pert:
A global instance of the HardwareSerial (or equivalent depending on the board architecture) class is declared in the core library.
Here is an example from the Arduino AVR Boards core of the Uno, etc., but it's something similar for every board:
ArduinoCore-avr/cores/arduino/HardwareSerial.h at 1.8.3 · arduino/ArduinoCore-avr · GitHub
ArduinoCore-avr/cores/arduino/HardwareSerial0.cpp at 1.8.3 · arduino/ArduinoCore-avr · GitHub
This is a relatively common approach in Arduino libraries where only a single instance is likely to be needed by the user. You can see another example of it in the Keyboard library:
Keyboard/src/Keyboard.h at 1.0.2 · arduino-libraries/Keyboard · GitHub
During sketch preprocessing, an #include directive for Arduino.h is added to the .ino files of a sketch if an #include directive is not already present:
Redirecting
ArduinoCore-avr/cores/arduino/Arduino.h at 1.8.3 · arduino/ArduinoCore-avr · GitHub

great explanation
congrats!

pert:
A global instance of the HardwareSerial (or equivalent depending on the board architecture) class is declared in the core library.
Here is an example from the Arduino AVR Boards core of the Uno, etc., but it's something similar for every board:
ArduinoCore-avr/cores/arduino/HardwareSerial.h at 1.8.3 · arduino/ArduinoCore-avr · GitHub
ArduinoCore-avr/cores/arduino/HardwareSerial0.cpp at 1.8.3 · arduino/ArduinoCore-avr · GitHub
This is a relatively common approach in Arduino libraries where only a single instance is likely to be needed by the user. You can see another example of it in the Keyboard library:
Keyboard/src/Keyboard.h at 1.0.2 · arduino-libraries/Keyboard · GitHub
During sketch preprocessing, an #include directive for Arduino.h is added to the .ino files of a sketch if an #include directive is not already present:
Redirecting
ArduinoCore-avr/cores/arduino/Arduino.h at 1.8.3 · arduino/ArduinoCore-avr · GitHub

Thank you!

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per

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