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

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

  • 5 min

We continue with programming 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 post we saw how to connect to an existing WiFi network, acting in ‘station’ (STA) mode. Now we will use access point mode (AP), meaning we will make the ESP8266 generate its own WiFi network for other devices to connect to.

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

We will refer to the ESP8266, but the same code is compatible with the ESP32, adjusting the library names. At the end you have the code for both ESP8266 and ESP32.

Create a WiFi Network

Using AP mode to create a WiFi network is just as easy as using 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 was created successfully and false otherwise.

WiFi.softAP(ssid, passphrase = NULL, channel = 1, ssid_hidden = 0, max_connection = 4)
Copied!
  • ssid, name of the WiFi network we will generate (up to 32 chars)
  • password, optional, WiFi password (min 8, max 63 chars)
  • channel, optional, WiFi channel to use (1 to 13)
  • hidden, optional, if true the SSID will not be broadcast
  • max_connection, optional, maximum number of connections (max 8)

Therefore, 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() { }
Copied!

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

On the other hand, the WiFi.mode(…) function 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 STA mode, the network credentials are stored in the ESP8266’s memory, which persists even if we reprogram the ESP8266. Therefore, if we want to disable AP mode we must do so explicitly by calling the WiFi.mode(…) function. Otherwise, the ESP8266 will generate its own WiFi even after being reprogrammed, even if we haven’t used the WiFi.SoftAP(…) function in the new program.

Summarized Example

If we do as in the previous post 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() 
{ 
}
Copied!

An additional file called ‘ESP8266_Utils.hpp’ containing 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());
}
Copied!

A ‘config.h’ file with our WiFi data

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

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

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

Download the Code

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

github-full

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

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