How to use ESP8266 Wifi Modes: Station and Access Point

ESP8266 works in two modes: Station (STA) and Access Point (AP).  In short, AP mode allows it to create its own network and have other devices (your phone) connect to it and STA mode allows the ESP8266 to connect to a Wi-Fi network (one created by your wireless router). So, an important feature about the ESP8266 is that it can function as a client or as an access point or even both.

1

Station Mode

Station (STA) mode is used to get ESP module connected to a Wi-Fi network established by an access point.

Station class has several features to facilitate management of Wi-Fi connection. In case the connection is lost, ESP8266 will automatically reconnect to the last used access point, once it is again available. The same happens on module reboot. This is possible since ESP is saving credentials to last used access point in flash (non-volatile) memory. Using the saved data ESP will also reconnect if sketch has been changed but code does not alter the Wi-Fi mode or credentials.

Switching the module to Station mode is done with begin function. Typical parameters passed to begin include SSID and password, so module can connect to specific Access Point.

WiFi.begin(ssid, password)

By default, ESP will attempt to reconnect to Wi-Fi network whenever it is disconnected. There is no need to handle this by separate code. A good way to simulate disconnection would be to reset the access point. ESP will report disconnection, and then try to reconnect automatically.

begin

There are several versions of begin function. One was presented just above: WiFi.begin(ssid, password). Overloads provide flexibility in number or type of accepted parameters. The simplest overload of begin is as follows:

WiFi.begin()

Calling it will instruct module to switch to the station mode and connect to the last used access point based on configuration saved in flash memory.

Below is the syntax of another overload of begin with the all possible parameters:

WiFi.begin(ssid, password, channel, bssid, connect)

Meaning of parameters is as follows:

ssid – a character string containing the SSID of Access Point we would like to connect to, may have up to 32 characters

password to the access point, a character string that should be minimum 8 characters long and not longer than 64 characters

channel of AP, if we like to operate using specific channel, otherwise this parameter may be omitted

bssid – mac address of AP, this parameter is also optional

connect – a boolean parameter that if set to false, will instruct module just to save the other parameters without actually establishing connection to the access point

config

Disable DHCP client (Dynamic Host Configuration Protocol) and set the IP configuration of station interface to user-defined arbitrary values. The interface will be a static IP configuration instead of values provided by DHCP.

WiFi.config(local_ip, gateway, subnet, dns1, dns2)

The function will return true if configuration change is applied successfully. If configuration can not be applied, because e.g. module is not in station or station + soft access point mode, then false will be returned.

The following IP configuration may be provided:

local_ip – enter here IP address you would like to assign the ESP station’s interface

gateway – should contain IP address of gateway (a router) to access external networks

subnet – this is a mask that defines the range of IP addresses of the local network

dns1, dns2 – optional parameters that define IP addresses of Domain Name Servers (DNS) that maintain a directory of domain names (like e.g. www.google.co.uk) and translate them for us to IP addresses

Example code

#include <ESP8266WiFi.h>


const char* ssid = "********";

const char* password = "********";


IPAddress staticIP(192,168,1,22);

IPAddress gateway(192,168,1,9);

IPAddress subnet(255,255,255,0);


void setup(void)

{

Serial.begin(115200);

Serial.println();

Serial.printf("Connecting to %s\n", ssid);

WiFi.begin(ssid, password);

WiFi.config(staticIP, gateway, subnet);

while (WiFi.status() != WL_CONNECTED)

{

delay(500);

Serial.print(".");

}

Serial.println();

Serial.print("Connected, IP address: ");

Serial.println(WiFi.localIP());

}
void loop() {}

Soft Access Point

An access point (AP) is a device that provides access to Wi-Fi network to other devices (stations) and connects them further to a wired network. ESP8266 can provide similar functionality except it does not have interface to a wired network. Such mode of operation is called soft access point (soft-AP). The maximum number of stations connected to the soft-AP is five.

Setting up a soft access point to for a Wi-Fi network.

The simplest version of this function requires only one parameter and is used to set up an open Wi-Fi network.

WiFi.softAP(ssid)

To set up password protected network, or to configure additional network parameters, use the following overload:

WiFi.softAP(ssid, password, channel, hidden)

The first parameter of this function is required, remaining three are optional.

Meaning of all parameters is as follows: –

ssid – character string containing network SSID (max. 63 characters)

password – optional character string with a password. For WPA2-PSK network it should be at least 8 character long. If not specified, the access point will be open for anybody to connect.

channel – optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1.

hidden – optional parameter, if set to true will hide SSID

The function will return true or false depending on result of setting the soft-AP.

softAPConfig

Configure the soft access point’s network interface.

softAPConfig (local_ip, gateway, subnet)

All parameters are the type of IPAddress and defined as follows:

local_ip – IP address of the soft access point

gateway – gateway IP address

subnet – subnet mask

The function will return true or false depending on the result of changing the configuration.

Example code:

#include <ESP8266WiFi.h>

IPAddress local_IP(192,168,4,22);

IPAddress gateway(192,168,4,9);

IPAddress subnet(255,255,255,0);


void setup()

{

Serial.begin(115200);

Serial.println();

Serial.print("Setting soft-AP configuration ... ");

Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");

Serial.print("Setting soft-AP ... ");

Serial.println(WiFi.softAP("ESPsoftAP_01") ? "Ready" : "Failed!");

Serial.print("Soft-AP IP address = ");

Serial.println(WiFi.softAPIP());

}

void loop() {}

sarah ali

sarah ali

Sarah is a passionate writer and blogger. As an early adopter, she enjoys trying out new social media and Internet tools along with WordPress plugins and Web apps.
sarah ali

Leave a Reply

Your email address will not be published.