📖 Scenario: You are working on a small IoT project using ESP8266 and ESP32 microcontrollers. Both devices need to connect to a WiFi network to send sensor data to a server. You will write simple code snippets to set up WiFi connection details and check connection status.
🎯 Goal: Build a step-by-step program that sets up WiFi credentials, configures connection parameters, attempts to connect to WiFi, and finally prints the connection status.
📋 What You'll Learn
Create variables for WiFi SSID and password
Add a timeout variable for connection attempts
Write code to attempt WiFi connection using the variables
Print the final WiFi connection status
💡 Why This Matters
🌍 Real World
ESP8266 and ESP32 devices are popular for IoT projects that need WiFi connectivity to send data or receive commands.
💼 Career
Understanding how to set up and verify WiFi connections on these devices is essential for IoT developers and DevOps engineers working with embedded systems.
Progress0 / 4 steps
1
Set WiFi Credentials
Create two variables called wifi_ssid and wifi_password with the exact values "HomeNetwork" and "SecurePass123" respectively.
IOT Protocols
Hint
Use simple string variables to store the WiFi name and password.
2
Add Connection Timeout
Add a variable called connection_timeout and set it to 10 to represent seconds to wait for WiFi connection.
IOT Protocols
Hint
This variable will help control how long the device tries to connect before giving up.
3
Attempt WiFi Connection
Write code to simulate connecting to WiFi by creating a variable connected and set it to True if wifi_ssid equals "HomeNetwork" and wifi_password equals "SecurePass123". Otherwise, set connected to False.
IOT Protocols
Hint
Use an if-else statement to check credentials and set the connection status.
4
Print Connection Status
Write a print statement that outputs "WiFi connected successfully" if connected is True, otherwise print "WiFi connection failed".
IOT Protocols
Hint
Use an if statement to print the correct message based on the connection status.
Practice
(1/5)
1. What is the main purpose of the WiFi.begin() function on ESP8266 and ESP32 devices?
easy
A. To start connecting the device to a WiFi network
B. To disconnect the device from the WiFi network
C. To check the current WiFi connection status
D. To get the device's IP address
Solution
Step 1: Understand the function purpose
WiFi.begin() is used to initiate the connection process to a WiFi network by providing the network credentials.
Hint: Use WiFi.status() to check connection status [OK]
Common Mistakes:
Using WiFi.begin() to check connection
Assuming WiFi.connected() exists
Comparing IP address directly to zero
3. Given this code snippet on an ESP8266 device:
WiFi.begin("MySSID", "MyPass");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());
What will be printed if the device successfully connects to the WiFi?
medium
A. WL_CONNECTED
B. The SSID of the WiFi network
C. The IP address assigned to the device
D. An error message
Solution
Step 1: Understand the code flow
The code waits until WiFi.status() returns WL_CONNECTED, meaning connection is successful.
Step 2: Identify the output of WiFi.localIP()
This function returns the device's IP address on the network, which is printed by Serial.println().
Final Answer:
The IP address assigned to the device -> Option C
Quick Check:
WiFi.localIP() prints IP address [OK]
Hint: WiFi.localIP() shows device IP after connection [OK]
Common Mistakes:
Expecting SSID instead of IP
Thinking WiFi.status() prints status automatically
Confusing connection status with output
4. You wrote this code to connect ESP32 to WiFi:
WiFi.begin("HomeNetwork", "password123");
if (WiFi.status() = WL_CONNECTED) {
Serial.println("Connected!");
}
Why does this code fail to print "Connected!" even when WiFi is connected?
medium
A. Because Serial.println() cannot print strings
B. Because the assignment operator '=' is used instead of '==' in the if condition
C. Because WiFi.status() returns a string, not a constant
D. Because WiFi.begin() must be called inside the if statement
Solution
Step 1: Identify the error in the if condition
The code uses '=' which assigns value instead of '==' which compares values. This causes a logic error.
Step 2: Understand the impact on execution
Because of assignment, the condition does not properly check connection status, so "Connected!" is never printed.
Final Answer:
Because the assignment operator '=' is used instead of '==' in the if condition -> Option B
Quick Check:
Use '==' to compare in if conditions [OK]
Hint: Use '==' for comparison, not '=' [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Calling WiFi.begin() inside if statement
Misunderstanding Serial.println() capabilities
5. You want your ESP32 to connect to WiFi and print its IP address only after a successful connection, but also retry connection up to 5 times with 1 second delay each. Which code snippet correctly implements this?
hard
A. int retries = 0;
WiFi.begin("SSID", "pass");
while (WiFi.status() != WL_CONNECTED && retries < 5) {
delay(1000);
retries++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
}
B. WiFi.begin("SSID", "pass");
for (int i = 0; i < 5; i++) {
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
break;
}
delay(1000);
}
C. WiFi.begin("SSID", "pass");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.println(WiFi.localIP());
D. WiFi.begin("SSID", "pass");
int retries = 0;
while (retries < 5) {
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
break;
}
retries++;
delay(1000);
}
Solution
Step 1: Analyze retry logic with condition and counter
int retries = 0;
WiFi.begin("SSID", "pass");
while (WiFi.status() != WL_CONNECTED && retries < 5) {
delay(1000);
retries++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
} uses a while loop that checks connection status and retries count, delaying 1 second each time, stopping after 5 tries or success.
Step 2: Confirm printing IP only after successful connection
int retries = 0;
WiFi.begin("SSID", "pass");
while (WiFi.status() != WL_CONNECTED && retries < 5) {
delay(1000);
retries++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
} prints IP only if WiFi.status() == WL_CONNECTED after retry loop, meeting requirements.
Final Answer:
Option A correctly implements retry with delay and prints IP after connection -> Option A
Quick Check:
Retry loop with condition and counter works best [OK]
Hint: Use while loop with counter for retries [OK]
Common Mistakes:
Not limiting retries causing infinite loops
Printing IP before confirming connection
Using for loop without checking connection inside
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.