readme.md
Markdown · 119 lines
# Wemos D1 Mini — LED Blink Captive Portal
### Control the on-board LED over WiFi with a captive portal
This project turns a **Wemos D1 Mini (ESP8266)** into a small WiFi-controlled blinking module.
When powered, the board creates an **open WiFi Access Point** and a **captive portal**.
As soon as a device connects to the AP, a control page appears that lets you **start/stop blinking** the on-board LED and **adjust the blink speed** with a slider.
The blink runs **non-blocking** (`millis()`-based), so the web interface and captive portal stay responsive at all times.
---
## ⭐ Features
- **Open WiFi Access Point** (no password)
- SSID: `Wemos-LED-Blink`
- **Captive portal**
- Any URL request (e.g. `google.com`) is redirected to the control page
- Detection endpoints for Android / iOS / macOS / Windows / Linux
- **Web-based LED control**
- **Start** / **Stop** buttons
- **Speed slider** to set the blink interval (50–2000 ms)
- Live status display (blinking on/off + current interval)
- No external network or internet required — fully self-contained
---
## 📦 Hardware requirements
- **Wemos D1 Mini** (clone or original) — ESP8266 based
- Uses the **on-board LED** only — no external wiring needed
### On-board LED note (important)
On the Wemos D1 Mini the on-board blue LED is connected to **GPIO2 (D4)** and is **active LOW**:
| Write | LED |
|-------|-----|
| `LOW` | ON |
| `HIGH` | OFF |
The sketch handles this inverted logic for you via the `writeLed()` helper. The pin is defined as:
```cpp
#define LED_PIN LED_BUILTIN // = GPIO2 / D4 on the Wemos D1 Mini
```
If your particular clone uses a different pin for its on-board LED, change `LED_PIN` accordingly.
---
## 🛠️ Software requirements
- **Arduino IDE** (or PlatformIO)
- **ESP8266 board package** installed
- In Arduino IDE: *File → Preferences → Additional Boards Manager URLs* →
`https://arduino.esp8266.com/stable/package_esp8266com_index.json`
- Then *Tools → Board → Boards Manager* → install **esp8266**
- Board selection: **Tools → Board → LOLIN(WEMOS) D1 R2 & mini**
No extra libraries are required — `ESP8266WiFi`, `ESP8266WebServer`, and `DNSServer` ship with the ESP8266 core.
---
## ⚡ Flashing
**Option A — flashesp.com (easiest)**
Flash the prebuilt firmware directly from your browser at **[flashesp.com](https://flashesp.com)** — no toolchain needed.
**Option B — Arduino IDE**
1. Place `WemosD1_LED_Blink.ino` inside a folder named `WemosD1_LED_Blink/`.
2. Open it in the Arduino IDE.
3. Select the board (**LOLIN(WEMOS) D1 R2 & mini**) and the correct COM port.
4. Click **Upload**.
---
## ▶️ Usage
1. Power the Wemos D1 Mini (USB or 5V).
2. On your phone/laptop, connect to the WiFi network **`Wemos-LED-Blink`** (open, no password).
3. The **captive portal** should open the control page automatically.
- If it doesn't, open a browser and go to **`http://192.168.4.1`**.
4. Use **Start** / **Stop** to control blinking and drag the **slider** to change the speed.
- Lower interval = faster blinking.
---
## ⚙️ Configuration
All settings are constants at the top of the sketch:
| Setting | Default | Description |
|---------|---------|-------------|
| `ap_ssid` | `Wemos-LED-Blink` | Access Point name |
| `LED_PIN` | `LED_BUILTIN` (GPIO2) | On-board LED pin |
| `blinkInterval` | `500` | Default toggle interval (ms) |
| `MIN_INTERVAL` | `50` | Fastest interval allowed by the slider (ms) |
| `MAX_INTERVAL` | `2000` | Slowest interval allowed by the slider (ms) |
| `apIP` | `192.168.4.1` | Portal / gateway IP |
---
## 🔍 How it works
- The ESP8266 starts in **AP mode** and runs a wildcard **DNS server**, so every domain resolves to the device (`192.168.4.1`). This is what triggers the captive portal popup on most operating systems.
- The **web server** serves the control page on `/` and handles state changes on `/set`:
- `/set?blink=on` / `/set?blink=off` — start/stop blinking
- `/set?interval=<ms>` — set the blink interval (clamped to the allowed range)
- `/set` responds with a small JSON status (`{"blinking":true,"interval":500}`) that the page uses to update the UI live.
- The **`loop()`** toggles the LED based on `millis()` timing — never `delay()` — so HTTP and DNS requests are always handled promptly.
---
## 🧰 Troubleshooting
- **LED logic looks reversed** — that's expected; GPIO2 is active LOW. The sketch already inverts it.
- **Captive portal doesn't pop up** — some built-in captive-portal mini-browsers are limited. Open `http://192.168.4.1` in a full browser instead.
- **GPIO2 / boot** — GPIO2 is a boot-strapping pin and must be HIGH at boot. The on-board LED is wired so that it does not interfere with normal startup; just don't repurpose this pin for something that pulls it LOW during reset.