Control your home appliances from anywhere in the world.
A self-hosted, firewall-bypassing IoT smart plug system with a sleek, app-like web interface.
SmartNode is a complete IoT solution for remotely controlling two electrical outlets. Unlike commercial smart plugs that rely on third-party clouds (like Tuya or eWeLink), this project is entirely self-hosted.
By using a 1-second HTTPS polling architecture, the ESP8266 fetches its state directly from your PHP server. This means no port-forwarding, no dynamic DNS, and no complex router configurations are required. If the device has internet access, it works.
- 📱 Modern Web App UI: A responsive, Tailwind-powered interface with a glass-morphism design, PIN protection, and real-time AJAX polling.
- ⏳ Automated Timers: Set a countdown (15m, 30m, 60m, 90m) to automatically power off a plug.
- 📅 Daily Scheduling: Define hour-based schedules (e.g.,
06:00 to 20:00) for fully autonomous operation. - 🔍 "Find Me" Feature: Clicking the Find button in the UI triggers a physical blinking LED on the specific plug, helping you identify it in a dark room.
- 👨💻 Live Terminal: A slide-up terminal panel in the web UI lets you monitor the raw sync data in real-time.
- 🛡️ Fail-Safe Logic: If the device loses WiFi, it enters an offline-safe mode and auto-reconnects.
- Microcontroller: WeMos D1 (ESP8266 WiFi Core)
- Relays: 2x 5V/3.3V Relay Modules (Safely mapped to avoid boot-clicking)
- Status LEDs:
- Boot/Search Mode LED
- WiFi Connected LED
- Server Sync LED
- Indicator LEDs: 2x LEDs for identifying Plug 1 and Plug 2.
graph TD
subgraph "Your House (No Port-Forwarding Needed)"
ESP[WeMos D1 ESP8266]
Relay1[Plug 1 Relay]
Relay2[Plug 2 Relay]
ESP -->|Controls| Relay1
ESP -->|Controls| Relay2
end
subgraph "Web Server"
PHP[index.php / API]
TXT[(status.txt)]
CRON[cron.php daemon]
PHP <--> TXT
CRON -->|Checks timers & offline status| TXT
end
subgraph "You (Anywhere)"
UI[Web Interface / Phone]
end
ESP -->|1. HTTPS POST / Heartbeat (Every 1s)| PHP
PHP -->|2. Returns JSON state| ESP
UI <-->|3. AJAX Polling & Commands| PHP
style ESP fill:#00979d,stroke:#333,color:#fff
style PHP fill:#777bb4,stroke:#333,color:#fff
style CRON fill:#4eaa25,stroke:#333,color:#fff
- Upload
index.php,cron.php, and create an emptystatus.txton your web server. - Crucial Permission Fix: The PHP script needs to read and write to the text file. Run the following via SSH (or change permissions via cPanel/FTP to
666or777):chmod 666 status.txt # OR give ownership to the web server chown www-data:www-data status.txt - Open
index.phpand modify the default PIN if desired:$password_protect = "1670"; // Change this
- Open
cron.phpand ensure$file_pathpoints to the absolute path of yourstatus.txton the server.
To process timers and schedules accurately, cron.php must run frequently. Standard cron only runs every minute. To run it every 15 seconds, add the following 4 lines to your server's crontab (crontab -e):
* * * * * php /absolute/path/to/your/cron.php >/dev/null 2>&1
* * * * * sleep 15; php /absolute/path/to/your/cron.php >/dev/null 2>&1
* * * * * sleep 30; php /absolute/path/to/your/cron.php >/dev/null 2>&1
* * * * * sleep 45; php /absolute/path/to/your/cron.php >/dev/null 2>&1- Install the ESP8266 board package in the Arduino IDE.
- Install the ArduinoJson library via the Library Manager.
- Open
smartnode.inoand update the configuration section:const char* ssid = "YOUR_WIFI_NAME"; const char* password = "YOUR_WIFI_PASSWORD"; const char* serverUrl = "https://YOURDOMAIN.COM/index.php";
- Connect your WeMos D1 via USB and upload the code. (Note: Ensure Pin D8 / GPIO0 is NOT grounded during boot, or the upload will fail).
- Access the App: Open the URL where you hosted
index.phpon your phone or PC. - Log In: Enter your 4-digit PIN (Default:
1670). - Control:
- Click the main power button to toggle the relay.
- Click Timer to select an auto-shutoff time.
- Click Sched to set daily active hours.
- Click Find to make the physical device blink (useful if you forget which plug is which).
- Monitoring: Click the "System Log" at the bottom to expand the live terminal and view raw data streams and sync health.
- High Voltage Warning: You are dealing with Relays that control mains AC voltage (110V/220V). Ensure proper isolation, use electrical tape, and house the WeMos D1 in a safe, non-conductive 3D-printed or plastic enclosure.
- Insecure HTTPS Check: The ESP8266 code uses
client.setInsecure();. This allows it to connect to your HTTPS domain without verifying the SSL fingerprint, saving memory and preventing the device from breaking when your SSL certificate auto-renews.
Distributed under the MIT License. Feel free to modify and adapt for your own home automation setups!
Connecting the physical world to the web. 🌐