ESP8266: Setting an access point

The objective of this post is to explain how to configure the ESP8266 to work as an access point. Additionally, we will also set a webserver to work on top of it.


Introduction

The objective of this post is to explain how to configure the ESP8266 to work as an access point. Additionally, we will also set a webserver to work on top of it. Most of the coding is based on the example provided in the ESP8266 Arduino IDE libraries, which I encourage you to try.

With this method, another device can connect to the ESP8266 and exchange data with it, without the need for a external WiFi network.


The code

First of all, we will need to include ESP8266WiFi.h library, which will provide all the functionality needed to set the access point, in the WiFi extern variable. You can check the implementation of the functionalities here.

Additionally, we will also include the ESP8266WebServer.h library, in order to be able to set our webserver. This will be a very simple demonstration but you can read more about setting a HTTP webserver on the ESP8266 in this previous post.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

Since we are going to set an access point, we need to specify its SSID (network name) and password. We are going to do it in two global variables, so we can easily change the values.

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

Next, we declare a global object variable from the ESP8266WebServer class, so we will be able to access it in our functions. This class will provide the methods needed to set the HTTP server.

As argument for the constructor of this class, we will pass the port where the server will be listening to. Since 80 is the default port for HTTP, we will use this value, so we will not need to specify it in the URL when accessing to our ESP8266 server using a browser.

ESP8266WebServer server(80);

Next, we start our setup function by opening a serial connection, to output some messages to the Arduino IDE serial monitor.

After that, we call the softAP method on the WiFi extern variable, passing as input both the SSID and password variables defined early.

Serial.begin(115200);
WiFi.softAP(ssid, password);

Since we need to know the IP of the server in order to contact it, we will now get it using the softAPIP method on the WiFi object. Just as an example, we will also get the server MAC address with the softAPmacAddress method.

Serial.println();
Serial.print("Server IP address: ");
Serial.println(WiFi.softAPIP());
Serial.print("Server MAC address: ");
Serial.println(WiFi.softAPmacAddress());

Finally, we are going to configure the HTTP server. So, we call the on method on our previously declared server global object and specify as first argument a URL and as second argument a handling function that will be executed when a HTTP request is made to that URL. We will define the handling function later.

Finally, to start our server, we call the begin method on the server object, still in the setup function.

server.on("/", handleRoot);
server.begin();

To handle the actual incoming of HTTP requests, we need to call the handleClient method on the server object, on the main loop function.

void loop() {
   server.handleClient();
}

 To finish the code, we will specify a handling function, which will just return a simple “Hello World” message, as response to a HTTP request. To do so, we call the send method on our server object, passing as input the HTTP response code, the content type of the response and the actual response.

Important: WordPress inserts a line break automatically after and before the HTML tags on the code. Please remove them and make sure all the arguments of the send method are on the same line, to avoid an error while compiling.

void handleRoot() {
    server.send(200, "text/html", "
<h1>Hello from ESP8266 AP!</h1>
");
}

The full source code can be seen bellow.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

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

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/html", "
<h1>Hello from ESP8266 AP!</h1>
");
}

void setup() {

  Serial.begin(115200);

  WiFi.softAP(ssid, password);

  Serial.println();
  Serial.print("Server IP address: ");
  Serial.println(WiFi.softAPIP());
  Serial.print("Server MAC address: ");
  Serial.println(WiFi.softAPmacAddress());

  server.on("/", handleRoot);
  server.begin();

  Serial.println("Server listening");
}

void loop() {
  server.handleClient();
}

 

Testing the code

To test the code, just upload it to the ESP8266 and open the Arduino IDE serial monitor. You should get an output similar to figure 1, where both the IP and the MAC address of the server are printed.

ESP8266 setting access point

Figure 1 – Output of the program on the Arduino IDE serial console.

After that, if you check the available networks on your computer or smartphone, you should see the “MyESP8266AP” network, as shown in figure 2 (Windows 8 menu). Connect to it using the password we defined in the code.

ESP8266 access point

Figure 2 – ESP8266 Access Point as an available network.

Finally, open a web browser and connect to the IP that was printed on the serial console. To do so, type the following on the search bar (I’m using the IP of my device, if yours is different please change it):

http://192.168.4.1

You should get an output similar to figure 3, which shows the message we defined on the code.

ESP8266 access point webserver response

Figure 3 – Hello World response from the ESP8266 server.


Related Posts

 

Technical details

  • ESP8266 libraries: v2.3.0

23 thoughts on “ESP8266: Setting an access point”

  1. Nice tutorial.
    I see the ESP8266_AP in the network list that appears in my smartphone but when trying to connect to the specific IP with the browser I get the “the site can’t be reached xxx.xxx.xxx.xxx refused to connect” notice. Can you give me some advice to overcome this problem?

    1. Hi! Thanks for the feedback 🙂

      That is a weird problem, never ran into that. Did your smartphone successfully connected to the soft AP network before you trying to make the request?

      It may take a while or it may have failed and rolled back to your previous WiFi network without giving any warning.

      Are you able to test it from a laptop, just to confirm?

      Also, did you include the http:// before the IP? The browser should handle that by default, but just to be sure 🙂

      Best regards,
      Nuno Santos

  2. Nice tutorial.
    I see the ESP8266_AP in the network list that appears in my smartphone but when trying to connect to the specific IP with the browser I get the “the site can’t be reached xxx.xxx.xxx.xxx refused to connect” notice. Can you give me some advice to overcome this problem?

    1. Hi! Thanks for the feedback 🙂
      That is a weird problem, never ran into that. Did your smartphone successfully connected to the soft AP network before you trying to make the request?
      It may take a while or it may have failed and rolled back to your previous WiFi network without giving any warning.
      Are you able to test it from a laptop, just to confirm?
      Also, did you include the http:// before the IP? The browser should handle that by default, but just to be sure 🙂
      Best regards,
      Nuno Santos

    1. Hi!

      That function should be called inside the route handling function.

      So, the answer will be returned to the client that sent the HTTP request to that route of the server.

      Basically the library you mention is agnostic (or at least should be) about if the ESP8266 is connected to a WiFi network hosted by a router or if it is operating as an Access point and other stations are connected to it.

      So, you use that library to setup a HTTP server on your ESP8266. That library “doesn’t know” which clients are connected to the ESP soft ap network.

      I don’t know of any specific function to send some kind of message to all the clients connected in soft AP mode and most likely there is none.

      The best approach is to first decide which protocol you want to use to establish the communication. Will you use raw sockets or will you use something like HTTP (which is a higher layer protocol) to establish the connection between your devices?

      Then, after choosing the protocol, you need to decide a good architecture. In your case, it seems like you are trying to broadcast a message to your “Clients”.

      Most likely, they will need to be running a webserver themselves so you can send a message to all of them.

      There’s plenty of ways to approach the problem and pretty much depends on your final objective. Hope this helps getting you in the right track 🙂

      Best regards,
      Nuno Santos

    1. Hi!
      That function should be called inside the route handling function.
      So, the answer will be returned to the client that sent the HTTP request to that route of the server.
      Basically the library you mention is agnostic (or at least should be) about if the ESP8266 is connected to a WiFi network hosted by a router or if it is operating as an Access point and other stations are connected to it.
      So, you use that library to setup a HTTP server on your ESP8266. That library “doesn’t know” which clients are connected to the ESP soft ap network.
      I don’t know of any specific function to send some kind of message to all the clients connected in soft AP mode and most likely there is none.
      The best approach is to first decide which protocol you want to use to establish the communication. Will you use raw sockets or will you use something like HTTP (which is a higher layer protocol) to establish the connection between your devices?
      Then, after choosing the protocol, you need to decide a good architecture. In your case, it seems like you are trying to broadcast a message to your “Clients”.
      Most likely, they will need to be running a webserver themselves so you can send a message to all of them.
      There’s plenty of ways to approach the problem and pretty much depends on your final objective. Hope this helps getting you in the right track 🙂
      Best regards,
      Nuno Santos

    1. Hi!

      I’ve never tried it in the ESP8266, so I cannot confirm. My suggestion is that you ask around the GitHub page of the Arduino core for the ESP8266.

      Nonetheless, the ESP32 can work in both modes 🙂

      Just as a note, what you refer to as client point is technically called station. In short, if the ESP is connected to a router’s WiFi network, then it is operating as station 🙂

      Best regards,
      Nuno Santos

    1. Hi!
      I’ve never tried it in the ESP8266, so I cannot confirm. My suggestion is that you ask around the GitHub page of the Arduino core for the ESP8266.
      Nonetheless, the ESP32 can work in both modes 🙂
      Just as a note, what you refer to as client point is technically called station. In short, if the ESP is connected to a router’s WiFi network, then it is operating as station 🙂
      Best regards,
      Nuno Santos

  3. Thanks for your answer and yours posts so well explained.
    I have solved the problem by making the AP as client and connecting to the other points configured as servers sequentially opening and closing the connection.
    client.connect (URL, port)
    client.stop()
    …..

    About the previous post: “Can an esp can be both access point and client point???”
    I have tested it and it works perfectly, defining two different server objects.

    WiFi.mode(WIFI_AP_STA);

    //AP
    IPAddress myIP =WiFi.softAP(myssid, mypassword);
    serverAP.on(“/”, handleRoot);
    serverAP.begin();

    //Station
    WiFi.begin(ssid, password);
    }

    Best regards,

    1. Hi! You’re welcome, and thanks for the feedback 🙂

      Awesome, I’m glad you have solved it.

      I’m also glad to know that the ESP8266 works in both modes. This is very useful for many applications, such as WiFi configuration portals.

      Although I’ve tested in the ESP32, never had the chance to check it on the ESP8266, so thanks for sharing the information :).

      Best regards,
      Nuno Santos

  4. Thanks for your answer and yours posts so well explained.
    I have solved the problem by making the AP as client and connecting to the other points configured as servers sequentially opening and closing the connection.
    client.connect (URL, port)
    client.stop()
    …..
    About the previous post: “Can an esp can be both access point and client point???”
    I have tested it and it works perfectly, defining two different server objects.
    WiFi.mode(WIFI_AP_STA);
    //AP
    IPAddress myIP =WiFi.softAP(myssid, mypassword);
    serverAP.on(“/”, handleRoot);
    serverAP.begin();
    //Station
    WiFi.begin(ssid, password);
    }
    Best regards,

    1. Hi! You’re welcome, and thanks for the feedback 🙂
      Awesome, I’m glad you have solved it.
      I’m also glad to know that the ESP8266 works in both modes. This is very useful for many applications, such as WiFi configuration portals.
      Although I’ve tested in the ESP32, never had the chance to check it on the ESP8266, so thanks for sharing the information :).
      Best regards,
      Nuno Santos

Leave a Reply