Connect ESP8266 to Wi-Fi Router
Upload these code to your Arduino WeMos D1 ESP8266 W-Fi board.
#include <ESP8266WiFi.h>
//SSID of your network
char ssid[] = "myRouter"; //SSID of your Wi-Fi router
char pass[] = "myPassWord"; //Password of your Wi-Fi router
void setup()
{
Serial.begin(115200);
delay(10);
// Connect to Wi-Fi network
Serial.println();
Serial.println();
Serial.print("Connecting to...");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Wi-Fi connected successfully");
}
void loop () {}
Using ESP8266 to connect to Wi-Fi need to use the function of:
WiFi.begin(ssid, pass); // connect to target Wi-Fi
SSID is the name of the Wi-Fi you want to connect to.
while (WiFi.status() != WL_CONNECTED)
WiFi.status can be check current connection condition, it will return like the status provided below:
WL_CONNECTED: assigned when connected to a WiFi network;
WL_NO_SHIELD: assigned when no WiFi shield is present;
WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED);
WL_NO_SSID_AVAIL: assigned when no SSID are available;
WL_SCAN_COMPLETED: assigned when the scan networks is completed;
WL_CONNECT_FAILED: assigned when the connection fails for all the attempts;
WL_CONNECTION_LOST: assigned when the connection is lost;
WL_DISCONNECTED: assigned when disconnected from a network;
If we get a message: “Wi-Fi connected successfully” means that we can link our ESP8266 to the internet!
Comments
Post a Comment