readme.md
Markdown · 81 lines
# ESP32 + E18-D80NK — Captive Portal Proximity Tester
<table>
<tr>
<td><img src="https://i.imgur.com/jvFQVSO.png" alt="Example usage" width="350"></td>
<td><img src="https://i.imgur.com/sPwzVmj.png" alt="Example usage" width="350"></td>
</tr>
</table>
This project turns an **ESP32** into a **Wi-Fi captive portal** that shows the **live state of an E18-D80NK** infrared reflective proximity sensor (NPN, 5–80 cm).
After connecting to the ESP32's Wi-Fi network, any URL redirects to the portal page, where you can see whether the sensor currently detects an object — a quick way to confirm the sensor simply works.
## Features
- Captive portal (AP + DNS redirect)
- Live detected / clear status with colored indicator (auto-refresh)
- Detection counter + "how long in current state" timer
- Simple settings in the portal (label + refresh interval)
- Works **without an external Wi-Fi network** (ESP32 runs as an Access Point)
## Hardware
- ESP32 development board
- E18-D80NK IR reflective proximity sensor (3 wires)
- (Optional) 10 kΩ resistor — only if you don't use the internal pull-up
No level shifter is needed. The sensor's output is **open-collector NPN**: it only pulls the line to GND when an object is detected, so a 3.3 V pull-up on the ESP32 side is enough. The 5 V supply never reaches the GPIO.
## Wiring (MOST IMPORTANT)
The E18-D80NK has 3 wires:
| Wire | Function | Connect to ESP32 |
|---------|----------|------------------|
| Brown | VCC | **5V / VIN** (sensor needs ~5 V) |
| Blue | GND | **GND** (common ground) |
| Black | OUT | **GPIO4** |
### Schematic
```
ESP32 E18-D80NK
+--------------+ +-----------+
| 5V |------------------| VCC (brown)
| GND |------------------| GND (blue)
| GPIO4 |------------------| OUT (black)
| |
| (internal pull-up to 3V3 enabled in code via INPUT_PULLUP)
+--------------+
Optional external pull-up if not using INPUT_PULLUP:
GPIO4 ----[ 10k ]---- 3V3
```
**Logic (default NPN):**
- Object detected → OUT pulled **LOW**
- No object → OUT floats **HIGH** (held high by the pull-up)
### Pin choice warning
Use a GPIO that has an internal pull-up. **GPIO 34–39 are input-only and have NO pull-up** — don't use them here (or add an external 10 kΩ pull-up to 3V3). Good choices: GPIO 4, 5, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33.
## How to use
1. Flash the sketch to the ESP32.
2. Connect your phone/PC to the Wi-Fi network: **esp32-e18-d80nk**
3. Open any website (or go directly to **http://192.168.4.1**)
4. Wave your hand / an object in front of the sensor:
- **OBJECT** (green) = sensor detects something
- **clear** (red) = nothing detected
5. The E18-D80NK also has a small **potentiometer** on the back — turn it to adjust detection distance (5–80 cm).
## Customization
- Change the sensor OUT pin in code:
- `static const uint8_t SENSOR_PIN = 4;`
- If your module's logic is inverted (detected = HIGH), set:
- `static const bool DETECTED_IS_LOW = false;`
- Adjust refresh interval in the web form (100–10000 ms).
## Notes / Troubleshooting
- **Always shows "OBJECT":** wire is likely floating — confirm `INPUT_PULLUP` (default in code) or add the external 10 kΩ pull-up, and check the OUT wire is really on GPIO4.
- **Never detects:** confirm the sensor is powered from **5 V** (not 3.3 V — most E18-D80NK need 5 V), share a **common GND**, and adjust the potentiometer on the sensor.
- **Random flicker:** shorten the sensing distance with the potentiometer; IR reflective sensors can be fooled by very dark, matte, or angled surfaces.
- **State seems stuck:** the ESP32's onboard LED / Serial monitor at 115200 baud can help — the boot log prints the AP IP and the sensor pin.