fbpixel
Tags: ,

The esp8266 microcontroller can be configured as an access point (AP) and generate its own wifi network with ssid and password. This method is useful when you do not have access to a wifi network or if you want to work on a network specific to the microcontroller.

Material

  • Computer
  • NodeMCU ESP8266
  • USB A Male/Micro B Male Cable

Principle

The ESP8266 NodeMCU has a Wi-Fi chip that can generate its own network in case a Wi-Fi network is not available. This configuration is called the AP (Access Point) mode

Code

To configure the ESP8266 NodeMCU as an access point, we will use the SoftAp class included in the ESP8266WiFi.h library. To configure a Wifi access point you just have to give it a name

WiFi.softAP(ssid)

Other parameters, such as the password or the number of connection limits can be defined.

  • ssid network identifier (max. 31 characters)
  • password network password (max. 63 characters) (Optional).
  • Network channel between 1 and 13. Default value 1 (Optional).
  • hidden hides the SSID if true (Optional).
  • max_connection number of simultaneous connections allowed from 0 to 8. Default value 4. (Optional).
#include <ESP8266WiFi.h>

const char *ssid = "AC-ESP8266";
const char *password = "987654321";

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(ssid,password) ? "Ready" : "Failed!");
  //WiFi.softAP(ssid);
  //WiFi.softAP(ssid, password, channel, hidden, max_connection)
  
  Serial.print("Soft-AP IP address = ");
  Serial.println(WiFi.softAPIP());
}

void loop() {
  Serial.print("[Server Connected] ");
  Serial.println(WiFi.softAPIP());

  delay(500);
}

Note: If the assigned password is shorter than 8 characters, the SSID will be ignored. If you want to change the SSID, make sure the password is longer than 8 characters or there is no password.

If you do not call the softApConfig function to set the IP addresses, the network will use default addresses.

Result

Once the access point is configured and the code is uploaded to the card, we can see in the available networks a new network appearing. We check that it has the SSID defined in the code.

esp8266-apmode-result Configure an ESP8266 as a Wi-Fi Access Point
esp8266-apmode-monitor-result Configure an ESP8266 as a Wi-Fi Access Point

Once the NodeMCU ESP8266 is configured as an access point, the network, thus generated, can be used like any other Wifi network. Other devices can connect and communicate via this network.

Sources