Connecting the ESP8266 to your local Wi-Fi network
In this recipe, we will show you how to connect your ESP8266 board to your local Wi-Fi network. Before you can send or receive data to and from the Internet, your ESP8266 board has to be connected to a Wi-Fi network that has Internet connectivity. This is done in the setup part of your program since it is supposed to be done once, when the ESP8266 is powered on.
Getting ready
Ensure your ESP8266 board is connected to your computer via the USB cable. No other components will be used in this exercise, since there are no inputs or outputs that are required.
How to do it…
We will include the ESP8266
library in the program and set the Wi-Fi network ssid
and password
so that the ESP8266
connects to the network when it gets powered on. We will print out a confirmation and the IP address of the ESP8266
when the connection is successful.
This is the sketch for this exercise:
// Libraries #include <ESP8266WiFi.h> // WiFi network const char* ssid = "your-ssid"; const char* password = "your-password"; void setup() { // Start serial Serial.begin(115200); delay(10); // Connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { }
Refer the following steps:
- Copy this sketch and paste it in your Arduino IDE.
- Change the SSID in the code from
your-ssid
to the name of your Wi-Fi network, and the password fromyour-password
to the password of your Wi-Fi network. - Make sure you have selected the correct board from the Tools|Board menu, which in this case is Adafruit HUZZAH ESP8266, and the correct serial port from the Tools|Port menu.
- Upload the code to your ESP8266 board.
- Open the serial monitor (and make sure that the serial speed is set at 115200) so that you can check to see whether the ESP8266 board has connected to the Wi-Fi network.
The results are as shown in the following screenshot. The ESP8266 prints its IP address once the connection has been successfully established:
How it works…
The program sets up the Wi-Fi SSID and password using the WiFi.begin()
function of the ESP8266Wifi
library, and then executes the WiFi.status()
function to try to connect to the Wi-Fi network. The program checks whether the WiFi.status()
function returns a true
value to indicate that the ESP8266 board has successfully connected to the Wi-Fi network. If the connection was not successful, the sketch tries to connect again using the WiFi.status()
function and repeats that until the WiFi.status()
function returns a true
value to indicate that the ESP8266 has successfully connected to the Wi-Fi network.
When the ESP8266 connects to the Wi-Fi network, the sketch displays a message, WiFi connected
, and the IP address of the ESP8266 on the serial monitor.
There's more…
You can enter the wrong password and SSID and see what output you get in the serial monitor.
See also
Now that you have successfully connected your ESP8266 board to your local Wi-Fi network, you can proceed to the next recipe, where we will be connecting the ESP8266 board to the Internet.