Consider this Arduino code for ESP8266 that tries to connect to a WiFi network. What will be printed on the Serial Monitor?
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() {}Think about what happens if the password is wrong and how many times the code tries to connect.
The code tries to connect 5 times, printing a dot each second. Since the password is wrong, it never connects, so after 5 dots it prints 'Failed to connect'.
ESP32 supports multiple WiFi modes. Which mode should you use if you want your ESP32 to connect to your home WiFi router?
Think about the mode where the device acts as a client connecting to an access point.
WIFI_STA mode sets the ESP32 as a station (client) that connects to an existing WiFi network (access point).
Look at this code snippet. It never prints 'Connected!'. What is the bug?
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "password");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting...");
}
Serial.println("Connected!");
}
void loop() {}ESP32 needs to be in the right mode before connecting.
Without setting WiFi.mode(WIFI_STA), the ESP32 may not connect properly because it defaults to AP mode or no mode.
Choose the correct code snippet to start an access point named "MyESP" with password "12345678" on ESP8266.
Check the official ESP8266 WiFi library function names.
The correct function to start an access point on ESP8266 is WiFi.softAP().
Consider this ESP32 code that sets up both station and access point modes. How many IP addresses will the ESP32 have after setup?
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() {}Think about IP addresses for both station and access point interfaces.
In WIFI_AP_STA mode, ESP32 acts as both access point and station, so it gets one IP for each interface, totaling 2 IP addresses.