Esp32 Ap From Web Server
Esp32 Ap From Web Server
The ESP32 can act as a Wi-Fi station, as an access point, or both. In this tutorial we’ll show you how to set the ESP32 as
an access point using Arduino IDE.
In most projects with the ESP32, we connect the ESP32 to a wireless router (see our ESP32 web server tutorial). This way
we can access the ESP32 through the local network.
In this situation the router acts as an access point and the ESP32 is set as a station. In this scenario, you need to be
connected to your router (local network) to control the ESP32.
But if you set the ESP32 as an access point (hotspot), you can be connected to the ESP32 using any device with Wi-Fi
capabilities without the need to connect to your router.
In simple words, when you set the ESP32 as an access point you create its own Wi-Fi network and nearby Wi-Fi devices
(stations) can connect to it (like your smartphone or your computer).
Here we’ll show you how to set the ESP32 as an access point in your web server projects. This way, you don’t need to be
connected to a router to control your ESP32. Because the ESP32 doesn’t connect further to a wired network (like your
router), it is called soft-AP (soft Access Point).
There’s an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming
language. Follow one of the following tutorials to prepare your Arduino IDE:
In this example, we’ll modify an ESP32 Web Server from a previous tutorial to add access point capabilities. What we’ll
show you here can be used with any ESP32 web server example.
Upload the sketch provided below to set the ESP32 as an access point.
/*********
Rui Santos
*********/
#include <WiFi.h>
WiFiServer server(80);
String header;
void setup() {
Serial.begin(115200);
pinMode(output27, OUTPUT);
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.println(IP);
server.begin();
void loop(){
String currentLine = ""; // make a String to hold incoming data from the client
header += c;
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
client.println("<!DOCTYPE html><html>");
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
if (output26State=="off") {
} else {
} else {
client.println("</body></html>");
client.println();
break;
currentLine = "";
} else if (c != '\r') { // if you got anything else but a carriage return character,
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
You need to define a SSID name and a password to access the ESP32. In this example we’re setting the ESP32 SSID name
to ESP32-Access-Point, but you can modify the name to whatever you want. The password is 123456789, but you can
also modify it.
// You can customize the SSID name and change the password
There’s a section in the setup() to set the ESP32 as an access point using the softAP() method:
WiFi.softAP(ssid, password);
There are also other optional parameters you can pass to the softAP() method. Here’s all the parameters:
.softAP(const char* ssid, const char* password, int channel, int ssid_hidden, int max_connection)
Next, we need to get the access point IP address using the softAPIP() method and print it in the Serial Monitor.
IPAddress IP = WiFi.softAPIP();
Serial.println(IP);
These are the snippets of code you need to include in your web server sketches to set the ESP32 as an access point. To
learn how the full web server code works, take a look at the ESP32 Web Server tutorial.
Parts Required
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the
best price!
Schematic
Start by building the circuit. Connect two LEDs to the ESP32 as shown in the following schematic diagram – one LED
connected to GPIO 26, and the other to GPIO 27.
Note: We’re using the ESP32 DEVKIT DOIT board with 36 pins. Before assembling the circuit, make sure you check the
pinout for the board you’re using.
ESP32 IP Address
Upload the code to your ESP32 (make sure you have the right board and COM port selected). Open the Serial Monitor at
a baud rate of 115200. Press the ESP32 “Enable” button.
The IP address you need to access the ESP32 point will be printed. In this case, it is 192.168.4.1.
Having the ESP32 running the new sketch, in your smartphone open your Wi-Fi settings and tap the ESP32-Access-
Point network:
Enter the password you’ve defined earlier in the code.
Open your web browser and type the IP address 192.168.4.1. The web server page should load:
To connect to the access point on your computer, go to the Network and Internet Settings and select the “ESP32-Access-
Point“.
And it’s done! Now, to access the ESP32 web server page, you just need to type the ESP32 IP address on your browser.