Built-in project · ESP32 DevKit V1

ESP32 access point web server

The ESP32 makes its own WiFi network, MyESP: press Run, open the Web panel, which joins it as a phone, and its ON and OFF buttons switch the LED on GPIO 26. The whole build, an ESP32 DevKit and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $9.15. On Mokxi they're free.

ESP32 access point web server · ESP32 DevKit V1live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • IoT
  • Lights
  • Choosing and reading a resistor
  • How an LED lights and why it needs a resistor
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: ESP32 DevKit V1
  • Parts: 3

How it works

The ESP32 makes its own WiFi network, MyESP: press Run, open the Web panel, which joins it as a phone, and its ON and OFF buttons switch the LED on GPIO 26.

ESP32 DevKit V1 runs the sketch below. The circuit around it: Resistor, 220 Ω, LED, green. pinMode sets ledPin as an output. digitalWrite drives ledPin LOW. delay waits 1000 milliseconds. Serial.begin opens the serial port at 115200 baud.

Parts list

3 parts, plus the jumper wires. They retail for about $9.15; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω pin 2ESP32 DevKit V1 pin D26
LED, green pin AResistor, 220 Ω pin 1
LED, green pin CGround

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ESP32 DevKit V1
/*
 * ESP32 access point web server: the board makes its own WiFi network,
 * "MyESP", and serves a page on it with ON and OFF buttons for the LED on
 * GPIO 26 and how many phones have joined.
 *
 * Press Run, then open the Web panel. It is the phone: it joins MyESP about
 * a second and a half after the access point comes up, and the address bar
 * moves to http://192.168.4.1/. Press Go. Leave and Join in the panel take
 * the phone off the network and back, and the serial monitor shows the
 * events the ESP32 core raises.
 *
 * The network is SIMULATED: no radio, no 802.11, and the Web panel is the one
 * phone that can join. On a real ESP32 your phone joins MyESP with the
 * password below and opens the same address.
 */
#include <WiFi.h>
#include <WebServer.h>

const char *ssid = "MyESP";
const char *password = "12345678";  // 8 to 63 characters, or "" for an open network

const int ledPin = 26;
bool ledOn = false;

WebServer server(80);

void onWiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
  switch (event) {
    case ARDUINO_EVENT_WIFI_AP_START:
      Serial.println("Access point started");
      break;
    case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
      Serial.printf("A phone joined: " MACSTR "\n", MAC2STR(info.wifi_ap_staconnected.mac));
      break;
    case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
      Serial.print("It got the address ");
      Serial.println(IPAddress(info.wifi_ap_staipassigned.ip.addr));
      break;
    case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
      Serial.println("A phone left");
      break;
    default:
      break;
  }
}

void handleRoot() {
  String page = "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  page += "<style>html{font-family:Helvetica;text-align:center}";
  page += ".button{background:#4CAF50;border:none;color:white;padding:16px 40px;font-size:30px;margin:2px}";
  page += ".off{background:#555555}</style></head><body>";
  page += "<h1>ESP32 Access Point</h1>";
  page += "<p>Network: " + WiFi.softAPSSID() + "</p>";
  page += "<p>Phones joined: " + String(WiFi.softAPgetStationNum()) + "</p>";
  page += "<p>LED on GPIO 26 is " + String(ledOn ? "on" : "off") + "</p>";
  if (ledOn) {
    page += "<p><a href=\"/off\"><button class=\"button off\">OFF</button></a></p>";
  } else {
    page += "<p><a href=\"/on\"><button class=\"button\">ON</button></a></p>";
  }
  page += "</body></html>";
  server.send(200, "text/html", page);
}

void setLed(bool on) {
  ledOn = on;
  digitalWrite(ledPin, on ? HIGH : LOW);
  Serial.println(on ? "LED on" : "LED off");
  server.sendHeader("Location", "/");
  server.send(303);
}

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  WiFi.onEvent(onWiFiEvent);
  WiFi.mode(WIFI_AP);
  if (!WiFi.softAP(ssid, password)) {
    Serial.println("The access point did not start");
    while (true) delay(1000);
  }
  Serial.print("Access point ");
  Serial.print(ssid);
  Serial.print(" at http://");
  Serial.println(WiFi.softAPIP());

  server.on("/", handleRoot);
  server.on("/on", []() { setLed(true); });
  server.on("/off", []() { setLed(false); });
  server.begin();
}

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

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  WiFi.onEvent(onWiFiEvent);
  WiFi.mode(WIFI_AP);
  if (!WiFi.softAP(ssid, password)) {
    Serial.println("The access point did not start");
    while (true) delay(1000);
  }
  Serial.print("Access point ");
  Serial.print(ssid);
  Serial.print(" at http://");
  Serial.println(WiFi.softAPIP());

  server.on("/", handleRoot);
  server.on("/on", []() { setLed(true); });
  server.on("/off", []() { setLed(false); });
  server.begin();
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

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

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
  • A pin does not work as an input or output, or the board will not boot or upload. ESP32 DevKit V1
    GPIO 34, 35, 36 and 39 are input only with no pull-ups. GPIO 6 to 11 are used by flash, so avoid them. GPIO 0, 2 and 12 are strapping pins and can block boot if held wrong.
  • A 5 V sensor or signal damages the board or reads wrong. ESP32 DevKit V1
    ESP32 pins are 3.3 V and not 5 V tolerant. Use a level shifter or a divider for 5 V outputs. Power 3.3 V sensors from the 3V3 pin.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.