Bird
Raised Fist0
Arduinoprogramming~20 mins

WiFi with ESP8266/ESP32 in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
WiFi Master with ESP8266/ESP32
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this ESP8266 WiFi connection code?

Consider this Arduino code for ESP8266 that tries to connect to a WiFi network. What will be printed on the Serial Monitor?

Arduino
void setup() {
  Serial.begin(115200);
  WiFi.begin("MySSID", "WrongPassword");
  int attempts = 0;
  while (WiFi.status() != WL_CONNECTED && attempts < 5) {
    delay(1000);
    Serial.print(".");
    attempts++;
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected!");
  } else {
    Serial.println("Failed to connect");
  }
}

void loop() {}
A.....Failed to connect
B.....Connected!
CConnected!
DFailed to connect
Attempts:
2 left
💡 Hint

Think about what happens if the password is wrong and how many times the code tries to connect.

🧠 Conceptual
intermediate
1:30remaining
Which WiFi mode allows ESP32 to connect to another WiFi network?

ESP32 supports multiple WiFi modes. Which mode should you use if you want your ESP32 to connect to your home WiFi router?

AWiFi.mode(WIFI_STA);
BWiFi.mode(WIFI_AP);
CWiFi.mode(WIFI_AP_STA);
DWiFi.mode(WIFI_OFF);
Attempts:
2 left
💡 Hint

Think about the mode where the device acts as a client connecting to an access point.

🔧 Debug
advanced
2:30remaining
Why does this ESP32 code fail to connect to WiFi?

Look at this code snippet. It never prints 'Connected!'. What is the bug?

Arduino
void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "password");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting...");
  }
  Serial.println("Connected!");
}

void loop() {}
AThe delay is too short to connect
BWiFi.begin() is missing WiFi.mode(WIFI_STA); before it
CSerial.begin() baud rate is incorrect
DWiFi.status() returns WL_DISCONNECTED instead of WL_CONNECTED
Attempts:
2 left
💡 Hint

ESP32 needs to be in the right mode before connecting.

📝 Syntax
advanced
1:30remaining
Which option correctly initializes WiFi on ESP8266 to create an access point?

Choose the correct code snippet to start an access point named "MyESP" with password "12345678" on ESP8266.

AWiFi.createAP("MyESP", "12345678");
BWiFi.beginAP("MyESP", "12345678");
CWiFi.startAP("MyESP", "12345678");
DWiFi.softAP("MyESP", "12345678");
Attempts:
2 left
💡 Hint

Check the official ESP8266 WiFi library function names.

🚀 Application
expert
3:00remaining
How many IP addresses will be assigned after this ESP32 code runs?

Consider this ESP32 code that sets up both station and access point modes. How many IP addresses will the ESP32 have after setup?

Arduino
void setup() {
  WiFi.mode(WIFI_AP_STA);
  WiFi.softAP("ESP32_AP", "password");
  WiFi.begin("HomeSSID", "homepass");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {}
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Think about IP addresses for both station and access point interfaces.

Practice

(1/5)
1. What is the main purpose of WiFi.begin(ssid, password) in ESP8266/ESP32 programming?
easy
A. To start connecting the device to a WiFi network
B. To disconnect from the current WiFi network
C. To check the current WiFi signal strength
D. To get the IP address of the device

Solution

  1. Step 1: Understand the function purpose

    WiFi.begin(ssid, password) is used to initiate connection to a WiFi network using the given network name (SSID) and password.
  2. Step 2: Compare with other options

    Disconnecting, checking signal strength, or getting IP address are done by other functions, not WiFi.begin.
  3. Final Answer:

    To start connecting the device to a WiFi network -> Option A
  4. Quick Check:

    WiFi.begin starts connection [OK]
Hint: Remember: begin means start connection [OK]
Common Mistakes:
  • Confusing begin with disconnect
  • Thinking it returns IP address
  • Assuming it checks signal strength
2. Which of the following is the correct syntax to check if the ESP8266 is connected to WiFi?
easy
A. if (WiFi.connected())
B. if (WiFi.checkConnection())
C. if (WiFi.isConnected())
D. if (WiFi.status() == WL_CONNECTED)

Solution

  1. Step 1: Recall the correct method to check WiFi connection

    The standard way is to use WiFi.status() and compare it to WL_CONNECTED to confirm connection.
  2. Step 2: Verify other options

    Methods like WiFi.connected(), WiFi.isConnected(), or WiFi.checkConnection() do not exist in the ESP8266 WiFi library.
  3. Final Answer:

    if (WiFi.status() == WL_CONNECTED) -> Option D
  4. Quick Check:

    WiFi.status() == WL_CONNECTED checks connection [OK]
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

  1. Step 1: Analyze the connection loop

    The code waits until WiFi.status() returns WL_CONNECTED, meaning the device is connected to WiFi.
  2. Step 2: Understand the output command

    WiFi.localIP() returns the IP address assigned to the device by the router, which is printed to Serial.
  3. Final Answer:

    The IP address assigned to the ESP8266 -> Option A
  4. 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

  1. 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.
  2. Step 2: Confirm other parts

    WiFi.begin() is called, delay is valid, and Serial.begin() is not shown but not the main error here.
  3. Final Answer:

    Using single '=' instead of '==' in the while condition -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The code with 10-second timeout using millis() and conditional IP printing -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard