Language: EN

como-generar-una-red-wifi-con-el-esp8266-modo-ap

How to create a WiFi network with the ESP8266 or ESP32 (AP mode)

We continue with the programming of the ESP8266 and ESP32. This time we will see how to make the ESP8266 act as an access point (AP) generating its own WiFi.

In the previous entry, we saw how to connect to an existing WiFi network, acting in ‘station’ (STA) mode. Now we will use the access point (AP) mode, that is, we will make the ESP8266 generate its own WiFi network to which the rest of the devices connect.

In a way, the ESP8266 will act “like the router in your house”, except that the router in your house shares a connection (ADSL, fiber…) with the connected devices. However, our ESP8266 will not share “anything”, it is only an AP for devices. This mode is called “SoftAP”.

We will refer to the ESP8266, but the same code is compatible for the ESP32, adjusting the name of the libraries. At the end you have the code for both the ESP8266 and the ESP32.

Create a WiFi network

Using the AP mode to create a WiFi network is just as simple as using the STA mode to connect to an existing WiFI, thanks to the same ESP8266WiFi library.

In the case of the ESP32, the library is simply called ‘WiFi’.

To generate the WiFi network, we use the function WiFi.softAP(…), which returns true if the WiFi network has been created successfully and false otherwise.

WiFi.softAP(ssid, passphrase = NULL, channel = 1, ssid_hidden = 0, max_connection = 4)

Where:

  • ssid, the name of the WiFi network we are going to generate (up to 32 characters)
  • password, optional, WiFi password (min 8 and max 63 characters)
  • channel, optional, the WiFi channel to use (from 1 to 13)
  • hidden, optional, if true the SSID will not be shown
  • max_connection, optional, maximum number of connections (max 8)

So an example to create a WiFi network with the ESP8266 in AP mode would be the following

#include <ESP8266WiFi.h>        // Include the Wi-Fi library

// Configuration of the generated WiFi
const char *ssid = "ssid";
const char *password = "password";

void setup() {
  Serial.begin(115200);
  delay(10);
  
  WiFi.mode(WIFI_AP);
  while(!WiFi.softAP(ssid, password))
  {
  Serial.println(".");
    delay(100);
  }
  
  Serial.print("Started AP ");
  Serial.println(ssid);
  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP());
  
}

void loop() { }

By default, 4 devices can be connected to the network generated by the ESP8266, although the maximum number of stations that can be connected simultaneously can be changed from 0 to 8. Once the maximum number has been reached, any other station that wants to connect will be forced to wait until a connected station disconnects.

For its part, the function WiFi.mode(…) sets the operating mode of the ESP8266, which can be:

WIFI_OFFOff
WIFI_STAStation
WIFI_APAccess point
WIFI_AP_STAStation+Access Point

Just like in the STA case, the network credentials are stored in the memory of the ESP8266, which is maintained even if we reprogram the ESP8266. So, if we want to deactivate the AP mode we must do it explicitly by calling the WiFi.mode(…) function. Otherwise, the ESP8266 will generate its own WiFi even after reprogramming it, even if we have not used the WiFi.SoftAP(…) function in the new program.

Summarized example

If we do as in the previous entry, and divide our code to make it simpler and reusable,

#include <ESP8266WiFi.h>
 
#include "config.h"  // Replace with your network data
#include "ESP8266_Utils.hpp"
 
void setup() 
{
  Serial.begin(115200);
  
  ConnectWiFi_AP();
}
 
void loop() 
{ 
}

An additional file that we will call ‘ESP8266_Utils.hpp’ that contains the connection functions.

void ConnectWiFi_AP()
{ 
   Serial.println("");
   WiFi.mode(WIFI_AP);
   while(!WiFi.softAP(ssid, password))
   {
     Serial.println(".");
     delay(100);
   }

   Serial.println("");
   Serial.print("Started AP:\t");
   Serial.println(ssid);
   Serial.print("IP address:\t");
   Serial.println(WiFi.softAPIP());
}

A ‘config.h’ file with the data of our WiFi

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

Other ESP8266 AP functions

Here is a summary of some of the additional functions available in ESP8266 AP. The names are self-descriptive of their function but, if you have any doubts, you can consult the ESP8266 documentation.

// configure the generated WiFi
bool WiFi.softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);

// disconnect the AP
bool WiFi.softAPdisconnect(bool wifioff = false);
    
// returns the IP address of the ESP8266
IPAddress WiFi.softAPIP()

// returns the MAC address of the ESP8266
String WiFi.softAPmacAddress(void);
uint8_t* WiFi.softAPmacAddress(uint8_t* mac);

Simple, right? Connecting an ESP8266 to an existing WiFi network and generating its own WiFi network is really easy thanks to the ESP8266 WiFi library.

In the next entries we will start working with the ESP8266 to act as both a client and a server, something we can do both in STA mode and in AP mode. Until next time!

Download the code

All the code in this post is available for download on Github.

github-full

Version for the ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples

Version for the ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples