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. Hi 🙂 thanks for the tutorial! just wondering how i can include a mroe complex html file in tis code (still in access point mode) such as with data and a chart.
    Thanks x

  2. Do anyone know if it is possible to fix the IP address of an AccessPoint ?
    I want the ESP8266 to connect onto network 192.168.0.1
    But I want the same ESP8266 be the 192.168.100.1 as AccessPoint.
    Is there anyone who knows how ?
    I would guess on
    WiFi.softAPconfig(local_IP, gateway, subnet);

    to mimic the WiFi server configuration
    WiFi.config(local_IP, gateway, subnet);

    …… but !?!?!?!?

    If you know the proper way …..

    Thanks in advance.

      1. Hello all!

        yes, you can have fix IP for access point and still get access to internet through it. ESP8266 deals good with it all

        Here the code:

        —- On the ESP8266 acting as access point ———————-
        #include

        //Household wifi definitions
        const char* ssid = “MyWifiNetwork”; // Household wifi ssid
        const char* pswd = “1234567890A”; // Househole wifi pwsd
        IPAddress local_IP(192, 168, 0, 106); //If you need fix IP
        IPAddress gateway(192, 168, 0, 1);
        IPAddress subnet(255, 255, 255, 0);
        IPAddress premDNS(8, 8, 8, 8);
        IPAddress deuxDNS(8, 8, 4, 4);

        Access point definitions
        const char* APssid = “myAccessPointNet”;
        const char* APpwsd = “pswdForAccesPt”;
        IPAddress APlocalIP(192,168,7,19); //If you need fixed IP for your access point
        IPAddress APgateway(192,168,7,1);
        IPAddress APsousRes(255,255,255,0);

        WiFiServer serveurESP(80);
        WiFiServer serveurWifi(portPostes);

        void setup() {
        // Connexion au Wi-Fi local
        WiFi.mode(WIFI_AP_STA); //Mode mixte
        WiFi.softAPConfig(APlocalIP, APgateway, APsousRes);
        WiFi.softAP(APssid, APpwsd);
        Serial.print (“Point d`accès a cette adresse : “);
        Serial.println(WiFi.softAPIP());

        ////En tant que serveur, connecté au réseau local
        // WiFi.config(local_IP, gateway, subnet);
        WiFi.begin(ssid, pswd);
        Serial.print (“Connexion Wifi “);
        while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(“.”);
        }
        Serial.println(“\nConnexion WiFi réussie”);

        // Démarrage du serveur HTTP
        serveurESP.begin();
        Serial.println(“Seveur web démarré”);
        serveurWifi.begin();
        Serial.println(“Serveur infos wifi démarré”);

        // Afficher l’adresse IP dans la console arduino
        Serial.print (“Veuillez consulter notre site à cette adresse: “);
        Serial.print (WiFi.localIP());
        Serial.println(“/”);
        Serial.print (“Point d`accès à cette adresse : “);
        Serial.println(WiFi.softAPIP());
        Serial.print (“Adresse MAC du point d`accés : : “);
        Serial.println(WiFi.softAPmacAddress());

        }

        —————— Then you define your web server and your loop routine ———————-

        =========== On the AccesPoint client ====================
        #include
        const char* ssid = “myAccessPointNet”;
        const char* pwsd = “pswdForAccesPt”;

        void setup() {
        Serial.begin(115200);
        WiFi.begin(ssid, pswd);

        SerialPrintln(“IP : ” + WiFi.localIP());
        SerialPrintln(“Gate: ” + WiFi.gatewayIP());
        }

        =====================================

        That’s it!

        All you need on top of that is already given in the tutorial.

        Have fun!

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading