Station Mode

In **Station mode (STA)**, the ESP32 connects to an existing Wi-Fi network, functioning as a **client**. It’s similar to how your phone or PC connects to a home or office wireless network. # Basic Code ```cpp #include // Replace with your network credentials const char* ssid = "your_SSID"; // Your Wi-Fi SSID const char* password = "your_PASSWORD"; // Your Wi-Fi password void setup() { // Start serial communication Serial.begin(115200); Serial.println("Connecting to Wi-Fi..."); // Connect to Wi-Fi WiFi.begin(ssid, password); // Wait for the connection to establish while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting..."); } // Once connected, print the IP address Serial.println("Connected to Wi-Fi!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); } void loop() { // Nothing to do here, the ESP32 is connected to Wi-Fi } ``` ## Const vs Define ```cpp const char* ssid = "your_SSID"; // Your Wi-Fi SSID const char* password = "your_PASSWORD"; // Your Wi-Fi password ``` You might notice that we use const char* instead of # define for the wireless SSID and password, such as: ```cpp #define SSID your_SSID #define PASSWORD your_PASSWORD ``` In C and C++, const char* is a constant C-style string. It is a pointer to a constant character. The * means this is a pointer, so it holds the memory address of the first character in a sequence of characters. Using const char* instead of # define comes down to several factors, including type safety, debugging, and scoping. Here’s a breakdown of why const char* is often preferred over # define for defining string constants: **1. Type Safety** - const char* has a defined type (const char*), while # define is just a preprocessor macro with no type checking. - This means the compiler can catch errors when using const char*, such as passing it to a function expecting a different type. In contrast, # define simply substitutes the text and can lead to subtle errors. **2. Debugging** - const char* appears in the debugger as a variable with a name and type, so you can inspect its value at runtime. - define is replaced by the preprocessor before compilation, so the name is not visible in the debugger, making it harder to trace issues **3. Scoping** - A const char* respects C++ scoping rules. It can be limited to a specific block, file, or namespace. - A define is global and does not respect scoping. Once defined, it can affect code in other parts of the program, potentially leading to conflicts. It is highly recommend to use const instead of define for above reasons. Using define only for simple, constant, type-independent values such as ```cpp #define PI 3.14159 ``` ## Connecting to WIFI ```cpp WiFi.begin(ssid, password); ``` Initiates a connection to a Wi-Fi network using the specified SSID (network name) and password. The WiFi.begin() function sends a request to the Wi-Fi module to start connecting to the specified network. This function does **not block the code execution** while attempting to connect, so the connection process continues in the background. ```cpp WiFi.status() ``` Returns the current connection status of the Wi-Fi module. WL_CONNECTED is an enumerator of the wl_status_t type, representing a value equal to 3. Below are the other possible statuses: ```cpp typedef enum { WL_NO_SHIELD = 255, WL_STOPPED = 254, WL_IDLE_STATUS = 0, WL_NO_SSID_AVAIL = 1, WL_SCAN_COMPLETED = 2, WL_CONNECTED = 3, WL_CONNECT_FAILED = 4, WL_CONNECTION_LOST = 5, WL_DISCONNECTED = 6 } wl_status_t; ``` ```cpp while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting..."); } ``` This is a **blocking loop** that continuously checks if the Wi-Fi module has successfully connected to the network. If the Wi-Fi status is not connected, the code pauses for 1 second before rechecking. It will remain in this loop until the Wi-Fi connection is established. ```cpp Serial.println("Connected to Wi-Fi!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); ``` After successfully connecting to a Wi-Fi network, the wireless router assigns an IP address to the ESP32 using its DHCP service. You can use WiFi.localIP() to retrieve the dynamically assigned IP address. To test the communication, you can use the ping command. ![[Pasted image 20250125224034.png]]

RELATED ARTICLES