In previous articles, we connected the ESP8266 to a pre-existing WIFI network. It is the commonly used method in projects, especially when there is interest in internet access.
For these cases, the ESP8266 operates as a “station” on the network. But we can find scenarios where there is no WIFI network to connect. Can we still use the ESP8266 in these cases? Yes, we can!
ESP8266 Operational Modes
The ESP8266 WiFi module can operate in 2 different modes:
- STA (the module operates as a station and is available to connect to an Access Point).
- AP (the module creates a network with customizable SSID and Password. We will discuss how each mode works, its limitations, and how to use
We use the STA mode to connect the ESP8266 to a pre-existing Wi-Fi network. This connection is made from an Access Point, which will be responsible for managing information traffic.
For configuration and use on the Arduino platform, we use the ESP8266WiFi.h library. Simple to use and extremely powerful, this library offers us all the tools to configure the WiFi module, without overloading us with flags and registers.
For our configuration, there are two more relevant functions, begin() and config().
The begin() function needs some parameters necessarily and others optionally. This is because this function is of type overload, which provides more flexibility when calling the function. For a better example, let's look at the begin() function in its full form and its minimal form:
- Complete form: begin(ssid, password, channel, bssid, connect)
- Minimum Form: begin(ssid, password)
Same function, two ways to call it. And both works. This is because it was built with more than one declaration format in the library.
Let's take a look at the parameters it accepts:
- SSID: The name of the network we want to connect to. Required field, and can contain up to 32
- password: password for the chosen Required field, must be between 8 and 64 characters.
- channel: Defines the bandwidth to be This parameter is optional and can be useful in areas with many different networks. Choosing a good channel can minimize interference and increase network range. If omitted, it will be selected automatically.
- bssid: One more optional parameter. If set to true, the function will return the MAC of the AP it was connected
- Connect: A Boolean parameter which, if set to false, will save the parameters defined in the function, but will not connect to the
This information will be saved in a reserved area of FLASH and in case of loss of connection, the attempt to reconnect will occur automatically.
Another important point is that, by default, the station is configured as a DHCP (Dynamic Host Configuration Protocol) client. This means that when connecting, the ESP8266 will ask the Access Point for an IP address. If the AP has DHCP enabled, we will receive a random IP within the network range configured there.
The config() function is not mandatory for a connection such as a station. But you will need it if you want to connect to the network with a fixed IP address. The function has the following format:
- config(local_ip, gateway, subnet, dns1, dns2) Where the parameters represent:
- local_ip: IP address we want to assign to the
- gateway: Access Point IP address.
- Subnet: IP mask of the network where we will
- dns1 and dn2: optional fields for IP address of DNS servers (Domain Name Server).
When we call the config() function, the DHCP mode is automatically disabled. Then the station will force the use of the address we choose. This method is useful when connecting over a network that does not have a DHCP server, or when having a fixed address is an essential project requirement.
You need to be careful when choosing the IP address and the subnet, as if it's incompatible with the network configuration, we will connect, but we won't be able to interact with anything.
In the image, we have a code for configuration and connection as a station.
Access Point Mode (AP)
In AP mode, the ESP8266 creates its WiFi network, allowing stations to connect to it. The figure below should help you better understand how it works. The ESP8266 configured as AP, replaces the role of the router in the network (with some limitations, but the principle is the same).
Strictly speaking, the name of this mode is Soft Access Point, because the functionality as an AP does not use any hardware resources equivalent to that of a common AP. It's like a Virtual AP. This does not impact health, but it does severely impact performance.
The main limitation is the number of connections it can manage. Although the manufacturer suggests up to 8 stations connected, you will have serious problems if you go beyond 5. If your application has a heavy data flow, I recommend that you limit it to 4 connections.
Another limitation is that the created network is not connected to the internet. So keep in mind that this is a model for applications that work well on local networks and for a few devices.
An example application for this format is an access control system. Approach with your cell phone, connect to the ESP8266 network, and be authorized to open a door.
Setting up this mode is very similar to that of a station. We have an overload function for begin and another one for configuration.
It would be the equivalent of our station mode begin() function.
- softAP(ssid): to create an open network, without a password.
- softAP(ssid, password, channel, hidden, max_connection): to create a protected network.
Let's take one for each parameter:
- SSID: The name of our network, can contain a maximum of 63 This is the only mandatory field in the role and cannot be empty.
- password: This field contains the password that the station needs to enter to connect. If not informed, the network will be open and can be accessed without any security. If you include one, it must contain a minimum of 8 characters, following the WPA2-PSK network security standard.
- Channel: As we discussed for the station, this field defines the wifi operation It must receive a numeric value from 1 to 13. If not informed, it will receive 1 as the default value.
- Hidden: If set to true, the SSID will be invisible and cannot be detected by identifiers (in your mobile's WiFi network list, for example. The network can still be connected if the station writes
- Max_connection: Defines the maximum number of stations allowed. Receives values from 0 to 8, with 4 as the default.
This function sets some parameters referring to IP addresses. It has the format: WiFi.softAPConfig(local_ip, gateway, subnet)
Where the parameters represent:
- Local_ip: IP address of the access point
- Gateway: IP address of the gateway (this is what stations will use as a switch)
- Subnet: Defines the IP range to be
With the code, you will configure a simple access point visible to your cell phone or computer.
STA + AP Mode
As the name suggests, the esp8266 will operate both as a station (being able to connect to a network) and as an Access Point (allowing stations to connect to it) at the same time.
The purpose behind this method is to use esp8266 in mesh network configurations. The idea is interesting, but if the performance is not already excellent operating as AP, imagine as AP and STA.
The documentation for this format is very scarce and, in a way, abandoned by the manufacturer itself. Espressif, when launching the successor of ESP8266, ESP32, included a specific library for MESH.