0
0
Arduinoprogramming~10 mins

WiFi with ESP8266/ESP32 in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WiFi with ESP8266/ESP32
Start
Include WiFi library
Set WiFi SSID and Password
Begin WiFi connection
Check connection status
Connected
Use WiFi connection
End
This flow shows how the ESP connects to WiFi: include library, set credentials, start connection, check status, retry if needed, then use connection.
Execution Sample
Arduino
#include <WiFi.h>
const char* ssid = "MyWiFi";
const char* password = "pass1234";
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
}
void loop() {}
This code starts WiFi connection on ESP32/ESP8266 using given network name and password.
Execution Table
StepActionWiFi StatusOutput
1Include WiFi libraryDisconnectedNo output
2Set SSID and PasswordDisconnectedNo output
3Call WiFi.begin(ssid, password)ConnectingNo output
4Check WiFi.status()ConnectingNo output
5Wait and retryConnectingNo output
6Check WiFi.status()ConnectedConnected to MyWiFi
7Use WiFi connectionConnectedReady for network tasks
8Loop continuesConnectedNo output
💡 WiFi.status() returns WL_CONNECTED, connection established successfully
Variable Tracker
VariableStartAfter Step 3After Step 6Final
WiFi.status()DisconnectedConnectingConnectedConnected
ssid"MyWiFi""MyWiFi""MyWiFi""MyWiFi"
password"pass1234""pass1234""pass1234""pass1234"
Key Moments - 2 Insights
Why does WiFi.status() return CONNECTING before it returns CONNECTED?
Because the ESP is trying to connect to the network; it takes some time to establish the connection as shown in steps 4 and 5 of the execution table.
What happens if the SSID or password is wrong?
The WiFi.status() will never return CONNECTED, it will stay in CONNECTING or eventually fail, so the code should handle retries or errors (not shown in this simple example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is WiFi.status() at step 5?
AConnected
BDisconnected
CConnecting
DFailed
💡 Hint
Check the 'WiFi Status' column at step 5 in the execution table.
At which step does the ESP successfully connect to WiFi?
AStep 3
BStep 6
CStep 4
DStep 8
💡 Hint
Look for the step where WiFi.status() changes to Connected in the execution table.
If the password is incorrect, how would the variable tracker change?
AWiFi.status() would stay Connecting and never become Connected
BWiFi.status() would stay Disconnected
CWiFi.status() would become Connected immediately
Dssid variable would change
💡 Hint
Refer to the variable tracker and key moments about connection status when credentials are wrong.
Concept Snapshot
WiFi with ESP8266/ESP32:
- Include WiFi library
- Set SSID and password
- Call WiFi.begin(ssid, password)
- Check WiFi.status() until WL_CONNECTED
- Use connection for network tasks
- Handle retries if connection fails
Full Transcript
This visual execution shows how an ESP8266 or ESP32 connects to WiFi. First, the WiFi library is included. Then, the network name (SSID) and password are set. The program calls WiFi.begin() to start connecting. The WiFi.status() is checked repeatedly: it starts as Disconnected, then Connecting, and finally Connected when successful. If the credentials are wrong, the device stays Connecting or fails. Once connected, the ESP can use the network. Variables like WiFi.status() change over time, reflecting connection progress. This step-by-step helps beginners see how WiFi connection works on ESP devices.