WiFi with ESP8266/ESP32 in Arduino - Time & Space Complexity
When using WiFi on ESP32, it's important to understand how the time to connect and send data changes as the program runs.
We want to know how the program's running time grows when it tries to connect or send data over WiFi.
Analyze the time complexity of the following code snippet.
#include <WiFi.h>
const char* ssid = "MyNetwork";
const char* password = "MyPassword";
void setup() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void loop() {
// main code
}
This code tries to connect the ESP board to a WiFi network by repeatedly checking the connection status until it succeeds.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that checks if WiFi is connected.
- How many times: It repeats until the connection is established, which depends on network conditions.
Here, the "input" is the time or attempts needed to connect to WiFi, which can vary.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (attempts) | 10 checks of WiFi status |
| 100 (attempts) | 100 checks of WiFi status |
| 1000 (attempts) | 1000 checks of WiFi status |
Pattern observation: The number of operations grows directly with the number of connection attempts.
Time Complexity: O(n)
This means the time to connect grows linearly with the number of attempts made to check the connection.
[X] Wrong: "The connection will always happen instantly, so the loop runs only once."
[OK] Correct: WiFi connection depends on many factors like signal strength and network response, so the loop may run many times before connecting.
Understanding how loops behave when waiting for hardware or network responses is a useful skill. It shows you can think about how long your code might take in real situations.
"What if we added a timeout to stop trying after a fixed number of attempts? How would the time complexity change?"
Practice
WiFi.begin(ssid, password) in ESP8266/ESP32 programming?Solution
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.Step 2: Compare with other options
Disconnecting, checking signal strength, or getting IP address are done by other functions, notWiFi.begin.Final Answer:
To start connecting the device to a WiFi network -> Option AQuick Check:
WiFi.begin starts connection [OK]
- Confusing begin with disconnect
- Thinking it returns IP address
- Assuming it checks signal strength
Solution
Step 1: Recall the correct method to check WiFi connection
The standard way is to useWiFi.status()and compare it toWL_CONNECTEDto confirm connection.Step 2: Verify other options
Methods likeWiFi.connected(),WiFi.isConnected(), orWiFi.checkConnection()do not exist in the ESP8266 WiFi library.Final Answer:
if (WiFi.status() == WL_CONNECTED) -> Option DQuick Check:
WiFi.status() == WL_CONNECTED checks connection [OK]
- Using non-existent methods like WiFi.connected()
- Confusing status check syntax
- Forgetting to compare with WL_CONNECTED
WiFi.begin("MySSID", "MyPass");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());Solution
Step 1: Analyze the connection loop
The code waits untilWiFi.status()returnsWL_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 AQuick Check:
WiFi.localIP() prints device IP [OK]
- Thinking it prints SSID or password
- Assuming it prints connection status
- Believing it causes an error
WiFi.begin(ssid, password);
while (WiFi.status() = WL_CONNECTED) {
delay(1000);
}
Serial.println("Connected!");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 CQuick Check:
Use '==' for comparison in conditions [OK]
- Confusing '=' with '==' in conditions
- Ignoring Serial.begin() setup
- Assuming delay value causes error
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"); } usesmillis()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 BQuick Check:
Use millis() for timeout and check status before printing [OK]
- Not stopping connection attempts after timeout
- Printing IP without checking connection
- Using wrong while condition logic
Ready to prove it?
Pass the test and this pattern moves unknown
