WiFi lets your ESP8266 or ESP32 connect to the internet or local networks. This helps your device send or receive data wirelessly.
0
0
WiFi with ESP8266/ESP32 in Arduino
Introduction
You want to control home devices from your phone using WiFi.
You want to send sensor data to a website or cloud service.
You want your device to download updates or information from the internet.
You want to create a small web server on your ESP to show data or control things.
You want to connect multiple ESP devices together over WiFi.
Syntax
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.
Examples
Connects ESP32 to a WiFi network named "HomeWiFi" with password "12345678".
Arduino
#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() {}
Connects ESP8266 to a WiFi network named "CafeWiFi" with password "coffee123".
Arduino
#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() {}
Sample Program
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.
Arduino
#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 }
OutputSuccess
Important Notes
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.
Summary
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().