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!
Where To Buy? | ||||
---|---|---|---|---|
No. | Components | Distributor | Link To Buy | |
1 | ESP8266 | Amazon | Buy Now |
The ESP8266 WiFi module can operate in 2 different modes:
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:
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:
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:
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.
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.
Let's take one for each parameter:
This function sets some parameters referring to IP addresses. It has the format: WiFi.softAPConfig(local_ip, gateway, subnet)
Where the parameters represent:
With the code, you will configure a simple access point visible to your cell phone or computer.
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.