WiFi lets your ESP8266 or ESP32 connect to the internet or local networks. This helps your device send or receive data wirelessly.
WiFi with ESP8266/ESP32 in Arduino
#include <WiFi.h> // For ESP32 // or #include <ESP8266WiFi.h> // For ESP8266 const char* ssid = "yourNetworkName"; const char* password = "yourPassword"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to WiFi"); } void loop() { // Your code here }
Use #include <WiFi.h> for ESP32 and #include <ESP8266WiFi.h> for ESP8266.
WiFi.begin(ssid, password) starts the connection to your WiFi network.
#include <WiFi.h> const char* ssid = "HomeWiFi"; const char* password = "12345678"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to HomeWiFi"); } void loop() {}
#include <ESP8266WiFi.h> const char* ssid = "CafeWiFi"; const char* password = "coffee123"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to CafeWiFi"); } void loop() {}
This program connects the ESP32 to a WiFi network called "TestNetwork" with password "testpass". It prints dots while connecting, then shows a success message and the device's IP address.
#include <WiFi.h> const char* ssid = "TestNetwork"; const char* password = "testpass"; void setup() { Serial.begin(115200); Serial.println("Connecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // Nothing here }
Make sure your WiFi network name and password are correct.
The IP address shown is assigned by your router and may be different each time.
Use Serial.begin(115200) to see messages in the Serial Monitor at 115200 baud rate.
WiFi allows ESP8266/ESP32 to connect to wireless networks.
Use WiFi.begin(ssid, password) to start connection.
Check connection status with WiFi.status() and get IP with WiFi.localIP().
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
