Home / References / ESP32 Library / WiFi API / Station
Description
The config
method in the ESP32 WiFi library is used to configure the network settings for the WiFi interface. This method allows the user to set a static IP, gateway, and subnet mask, as well as DNS servers, enhancing control over the device’s network configuration.
Syntax and Usage
WiFi.config(ip, gateway, subnet, dns1, dns2)
The config
method can be called with arguments to set a static IP address, gateway, subnet mask, and optional DNS servers.
Arguments
The following arguments can be passed to the config
method:
- ip: (IPAddress) The static IP address to assign to the device.
- gateway: (IPAddress) The gateway address used for the network.
- subnet: (IPAddress) The subnet mask for the network.
- dns1: (Optional, IPAddress) The primary DNS server address.
- dns2: (Optional, IPAddress) The secondary DNS server address.
Return Value
The config
method returns a boolean value:
- true: Indicates that the configuration was applied successfully.
- false: Indicates that the configuration failed.
Example Codes
Here are examples demonstrating the usage of the config
method:
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: This code demonstrates how to configure
* and connect an ESP32 to a WiFi network using ESP32 WiFI Library's
* config method. It defines the static IP configuration including
* the local IP, gateway, subnet mask, and optional DNS servers.
* The program attempts to apply the static IP configuration
* and then connects to a specified WiFi network using its SSID
* and password. Once connected, it prints the assigned IP
* address to confirm the successful connection.
*
* License: MIT
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their nnovative ideas to life.
*/
#include <WiFi.h> // Include the WiFi library for ESP32
void setup() {
// Initialize serial communication for debug output
Serial.begin(115200);
Serial.println(); // Print a blank line for better readability
Serial.println("WiFi Static IP Configuration Example");
// Define the static IP address and network configuration
IPAddress local_IP(192, 168, 1, 186); // Static IP address for the ESP32
IPAddress gateway(192, 168, 1, 1); // Gateway for the network
IPAddress subnet(255, 255, 255, 0); // Subnet mask
IPAddress primaryDNS(8, 8, 8, 8); // Primary DNS server (optional)
IPAddress secondaryDNS(8, 8, 4, 4); // Secondary DNS server (optional)
// Attempt to configure the WiFi settings with the static IP
Serial.println("Applying static IP configuration...");
if (WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("Static IP configuration applied successfully.");
} else {
Serial.println("Static IP configuration failed. Using default settings.");
}
// Connect to the WiFi network with SSID and password
const char* ssid = "YourSSID"; // Replace with your WiFi SSID
const char* password = "YourPassword"; // Replace with your WiFi password
Serial.print("Connecting to WiFi network: ");
Serial.println(ssid);
WiFi.begin(ssid, password); // Start the connection process
// Wait until the WiFi is connected
while (WiFi.status() != WL_CONNECTED) {
delay(1000); // Wait 1 second
Serial.println("Attempting to connect to WiFi...");
}
// Once connected, print the connection details
Serial.println("Connected to WiFi!");
Serial.print("Assigned IP address: ");
Serial.println(WiFi.localIP()); // Print the assigned IP address
}
void loop() {
// Placeholder for user code
// In this example, the loop does nothing
}
How to Use:
- Replace
"YourSSID"
and"YourPassword"
with your WiFi network’s credentials. - Upload the code to your ESP32 using the Arduino IDE.
- Open the Serial Monitor (set baud rate to 115200) to observe the output.
Additional Notes:
- Ensure your WiFi network supports static IP configuration.
- Verify the IP address range is compatible with your network.
- Some routers might require specific network configuration settings.
AvantMaker Tutorial Suggestion
If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).
ESP32 Library Index
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFi Library
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!
Related WiFi Library Methods