Connecting NodeMCU ESP8266 to Wi-Fi in Station Mode
Introduction
In this lesson, we'll learn how to connect a NodeMCU ESP8266 to a Wi-Fi access point, such as a Wi-Fi router. This process involves configuring the ESP8266 to connect to a specified Wi-Fi network using the SSID and password.
Connecting NodeMCU to a Wi-Fi Access Point
The following example demonstrates how to configure the ESP8266 to connect to a Wi-Fi access point. The network can be password-protected or open, and the ESP8266 can also connect to hidden networks.
Connecting to a Password-Protected Network
To connect to a network with a password, use:
WiFi.begin(ssid, password);
Connecting to an Open Network
To connect to an open network, use:
WiFi.begin(ssid);
Example: Connecting to a Wi-Fi Network
Replace `"Wi-Fi-name"` and `"Wi-Fi-password"` with the name and password of the Wi-Fi network you want to connect to. Upload this sketch to the NodeMCU module and open the serial monitor to see the connection status.
Code
#include <ESP8266WiFi.h>
// Enter SSID and Password of your Wi-Fi Router
const char* ssid = "Wi-Fi-name";
const char* password = "Wi-Fi-password";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Setting Wi-Fi Mode...");
WiFi.mode(WIFI_STA); // Set Wi-Fi mode to station
WiFi.begin(ssid, password); // Begin connection
Serial.print("Connecting to ");
Serial.println(ssid);
// Wait for connection
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); // Display IP address assigned by Wi-Fi router
}
void loop() {
Serial.printf("Signal Strength in dB = %d\n", WiFi.RSSI());
delay(3000);
}
Running the Example
1. **Upload the Sketch**: Upload the code to your NodeMCU.
2. **Open Serial Monitor**: Open the serial monitor to view the connection status.
3. **Connection Status**: The serial monitor will show the progress of connecting to the Wi-Fi network and display the IP address once connected.
4. **Signal Strength**: The loop function will print the Wi-Fi signal strength every 3 seconds.
Connecting to Wi-Fi-name
......
Connected, IP address: 192.168.1.100
Signal Strength in dB = -50
Signal Strength in dB = -50
...
Using Serial Debug
The ESP8266 Wi-Fi library includes a useful debug function that helps troubleshoot connection issues. The following example demonstrates how to enable serial debugging.
Code with Serial Debugging
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true); // Enable Serial Debug of ESP
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); // Display IP address assigned by Wi-Fi router
}
void loop() {
Serial.printf("Signal Strength in dB = %d\n", WiFi.RSSI());
delay(3000);
}
1. **Upload the Sketch**: Upload the code to your NodeMCU.
2. **Open Serial Monitor**: Open the serial monitor to view the debug output.
3. **Debug Information**: The serial monitor will show detailed information about the connection process and signal strength.
*WM: [1] Connecting to saved WiFi...
*WM: [2] Connection result: WL_CONNECTED
Connected, IP address: 192.168.1.100
Signal Strength in dB = -50
Signal Strength in dB = -50
Conclusion
Connecting the NodeMCU ESP8266 to a Wi-Fi network is straightforward with the provided examples. Whether connecting to a password-protected network, an open network, or using serial debugging, these steps help ensure a successful connection. Experiment with different configurations and utilize the debug function for troubleshooting to explore the full potential of the ESP8266 module.
No comments