We begin with the posts dedicated to programming the ESP8266 and ESP32. As it could not be otherwise, we start with the simplest case, how to connect the ESP8266 or ESP32 to an existing WiFi network.
In previous posts we have seen how to program the ESP8266 with the Arduino environment, and the ESP8266 programming guide, emphasizing the differences between programming an Arduino and an ESP8266.
We have also seen two of the most popular development boards based on the ESP8266, the NodeMCU and the Wemos D1 Mini. There are many more to see, but for now it’s more than enough to get into the nitty-gritty of programming.
So (finally!) we can start playing with this fun device, connecting our ESP8266 to a WiFi. In this case, the ESP8266 will act in station mode (STA, Station), that is, as a device that connects to an access point (AP, Access Point). In the next post we will see how to make the ESP8266 itself generate its own WiFi acting as an AP.
We will refer to the ESP8266, but the same code is compatible for the ESP32, adjusting the library names. At the end you have the code for both the ESP8266 and the ESP32.
Connect to WiFi Network
Connecting to a WiFi network with the ESP8266 is very easy thanks to the functions of the ESP8266WiFi library, which is part of the ESP8266 definition we downloaded with the board manager when configuring the Arduino IDE.
In the case of the ESP32, the library is simply called ‘WiFi’.
To connect we simply use the method ‘Wifi.begin(…)’, which has the following definition,
WiFi.begin(ssid, password = null, channel = 0, bssid = null, connect = true)
- ssid, name of the WiFi network we want to connect to (max 32 char)
- password, optional, password (minimum 8 char and maximum 64)
- channel, optional, the WiFi channel to use
- bssid, optional, MAC address of the access point
- connect, indicates if we want to connect immediately (if false, it only saves the parameters)
An example code to connect to an existing WiFi network with the ESP8266 would look like this.
#include <ESP8266WiFi.h>
// Replace with your network data
const char* ssid = "ssid";
const char* password = "password";
void setup()
{
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to:\t");
Serial.println(ssid);
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(200);
Serial.print('.');
}
// Show success message and assigned IP address
Serial.println();
Serial.print("Connected to:\t");
Serial.println(WiFi.SSID());
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
}
void loop()
{
}
The WiFi.begin(…) function of the ESP8266 saves the indicated credentials in non-volatile flash memory (which persists even if we load another program). In case of a connection failure, the ESP8266 will automatically connect to the last access point when available. Furthermore, once configured once, we can reconnect by calling the overload without parameters.
WiFi.begin()
The WiFi.begin(…) functions return the status of the ESP8266 to determine when we have established the connection with the network. We can also obtain it at another point in our program with the WiFi.status() function. This ESP8266 status variable can be:
| WL_CONNECTED | Connection established |
|---|---|
| WL_NO_SSID_AVAIL | SSID not found |
| WL_CONNECT_FAILED | Incorrect password |
| WL_IDLE_STATUS | Wi-Fi is changing between states |
| WL_DISCONNECTED | The ESP8266 is not configured in STA mode |
The WiFi.mode(WIFI_STA) function is optional in this example. It serves to leave only the STA mode and deactivate the AP in case we had configured it previously since, as we said, the ESP8266 saves the WiFi configuration even if we reprogram it. The mode options are:
| WIFI_OFF | Off |
|---|---|
| WIFI_STA | Station |
| WIFI_AP | Access point |
| WIFI_AP_STA | Station+Access Point |
We will see the last two in the next post when we see how to generate our own WiFi with the ESP8266.
Summarized Example
The connection code may seem a bit long, but if we clean and organize the code, the code is reduced to a few lines to connect to a WiFi network.
#include <ESP8266WiFi.h>
#include "config.h" // Replace with your network data
#include "ESP8266_Utils.hpp"
void setup()
{
Serial.begin(115200);
ConnectWiFi_STA();
}
void loop()
{
}
An additional file we’ll call ‘ESP8266_Utils.hpp’ containing the usual functions.
void ConnectWiFi_STA()
{
Serial.println("");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print('.');
}
Serial.println("");
Serial.print("Started STA:\t");
Serial.println(ssid);
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
}
A configuration file called ‘config.h’ containing the SSID and password.
const char* ssid = "ssid";
const char* password = "password";
Organizing our projects in this way is a good habit, allowing for cleaner, more maintainable, and reusable code.
Connect to Multiple WiFi Network
There is another way to connect to a WiFi network, or rather, to the one with the best signal among available WiFi networks. For this we will use the ‘ESP8266WiFiMulti’ library.
In the configuration we will use wifiMulti.addAP(…) to add several WiFi networks providing their respective SSIDs and passwords. On startup, the ESP8266 will connect to the network with the best reception.
This mode is less common than the previous one, since normally we won’t have several WiFi networks available (it’s more common to have several APs with the same SSID and password). But it is interesting in some mesh network projects, sniffers, etc.
However, managing multiple WiFi also means additional workload for the ESP8266, although not too large. So use this function only when it is really necessary and don’t get used to using it “just because”.
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
void setup()
{
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.println("Connecting");
while (wifiMulti.run() != WL_CONNECTED)
{
delay(250);
Serial.print('.');
}
// Show success message, connected WiFI, and assigned IP address
Serial.println();
Serial.print("Connected to:\t");
Serial.println(WiFi.SSID());
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
}
void loop()
{
}
That easy! Isn’t it impressive what this little device can do? In the next post we will see something even more impressive, how to configure the ESP8266 to act as an AP, that is, generate its own WiFi for other devices to connect to. See you soon!
Download the Code
All the code from this post is available for download on Github.
Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples
Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples

