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
Advertisements
This entry was posted in ESP8266 and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s