Hint: Use WiFi.status() with WL_CONNECTED constant [OK]
Common Mistakes:
Using non-existent methods like WiFi.connected()
Confusing status check syntax
Forgetting to compare with WL_CONNECTED
3. What will the following code print if the ESP8266 successfully connects to WiFi?
WiFi.begin("MySSID", "MyPass");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());
medium
A. The IP address assigned to the ESP8266
B. The SSID of the WiFi network
C. The WiFi password
D. Nothing, it will cause an error
Solution
Step 1: Analyze the connection loop
The code waits until WiFi.status() returns WL_CONNECTED, meaning the device is connected to WiFi.
Step 2: Understand the output command
WiFi.localIP() returns the IP address assigned to the device by the router, which is printed to Serial.
Final Answer:
The IP address assigned to the ESP8266 -> Option A
Quick Check:
WiFi.localIP() prints device IP [OK]
Hint: localIP() shows device IP after connection [OK]
Common Mistakes:
Thinking it prints SSID or password
Assuming it prints connection status
Believing it causes an error
4. Identify the error in this ESP32 WiFi connection code:
WiFi.begin(ssid, password);
while (WiFi.status() = WL_CONNECTED) {
delay(1000);
}
Serial.println("Connected!");
medium
A. Missing WiFi.begin() call
B. Incorrect delay value
C. Using single '=' instead of '==' in the while condition
D. No Serial.begin() before printing
Solution
Step 1: Check the while loop condition
The condition uses a single '=' which is an assignment, not a comparison. It should be '==' to compare values.
Step 2: Confirm other parts
WiFi.begin() is called, delay is valid, and Serial.begin() is not shown but not the main error here.
Final Answer:
Using single '=' instead of '==' in the while condition -> Option C
Quick Check:
Use '==' for comparison in conditions [OK]
Hint: Use '==' to compare, '=' assigns value [OK]
Common Mistakes:
Confusing '=' with '==' in conditions
Ignoring Serial.begin() setup
Assuming delay value causes error
5. You want your ESP32 to connect to WiFi and print its IP address only after a successful connection, but also stop trying after 10 seconds if it fails. Which code snippet correctly implements this?
hard
A. WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (millis() > 10000) break;
}
Serial.println(WiFi.localIP());
B. WiFi.begin(ssid, password);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) {
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
} else {
Serial.println("Failed to connect");
}
C. WiFi.begin(ssid, password);
for (int i = 0; i < 20; i++) {
if (WiFi.status() == WL_CONNECTED) break;
delay(500);
}
Serial.println(WiFi.localIP());
D. WiFi.begin(ssid, password);
while (WiFi.status() == WL_CONNECTED) {
delay(10000);
}
Serial.println(WiFi.localIP());
Solution
Step 1: Understand the timeout logic
WiFi.begin(ssid, password);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) {
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
} else {
Serial.println("Failed to connect");
} uses millis() to track elapsed time and stops trying after 10 seconds (10000 ms) if not connected.
Step 2: Check connection and print accordingly
It prints the IP only if connected; otherwise, it prints a failure message. This matches the requirement exactly.
Final Answer:
The code with 10-second timeout using millis() and conditional IP printing -> Option B
Quick Check:
Use millis() for timeout and check status before printing [OK]
Hint: Use millis() to limit connection wait time [OK]
Common Mistakes:
Not stopping connection attempts after timeout
Printing IP without checking connection
Using wrong while condition logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.