#include #include #include // Project: Remote PC Power Control via ESP8266 // Hardware components: // 1. Controller: NodeMCU ESP8266 V3 (CH340) // 2. Relay: 1-Channel 3.3V Low-Level Trigger Relay // 3. Power: USB Type-C (5V) // 4. Connection: // - Relay VCC -> NodeMCU VIN (5V) or 3V // - Relay GND -> NodeMCU GND // - Relay IN -> NodeMCU D1 (GPIO5) // - Relay COM & NO -> PC Motherboard Power SW pins // // Features: // - Web-server for remote control via VPN // - mDNS support (access via modified name like http://myname.local) // - Force Shutdown support (8s long press via /force) // - Physical FLASH button on board triggers the relay // - English UI // // Demo: https://social.vivaldi.net/@sneaky4oe/115810896025815481 // Your network config const char* ssid = "WiFI_NAME_REGISTRY_DEPENDANT"; // Change this const char* password = "WiFI_PASSWORD"; // Change this const char* hostName = "myname"; // Change this to access in browser with myname.local const int relayPin = 5; // D1 (GPIO5) const int buttonPin = 0; // FLASH button (GPIO0) const int ledPin = 2; // Onboard LED (D4) // Durations in milliseconds const int standardPress = 500; const int forcePress = 8000; // Force kill duration (8 sec) ESP8266WebServer server(80); void pressButton(int durationMs) { Serial.print("Action: pressing button for "); Serial.print(durationMs); Serial.println("ms"); digitalWrite(ledPin, LOW); digitalWrite(relayPin, LOW); delay(durationMs); digitalWrite(relayPin, HIGH); digitalWrite(ledPin, HIGH); } void handleRoot() { String html = ""; html += ""; html += "

PC Remote Control

"; html += ""; html += ""; html += "

Network name: http://" + String(hostName) + ".local

"; server.send(200, "text/html", html); } void handlePower() { server.send(200, "text/html", "

OK! Standard press sent.

"); pressButton(standardPress); } void handleForce() { server.send(200, "text/html", "

OK! Force shutdown sent.

"); pressButton(forcePress); } void setup() { Serial.begin(115200); pinMode(relayPin, OUTPUT); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); digitalWrite(relayPin, HIGH); digitalWrite(ledPin, HIGH); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi Connected!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); if (MDNS.begin(hostName)) { Serial.println("mDNS responder started: http://" + String(hostName) + ".local"); } server.on("/", handleRoot); server.on("/power", handlePower); server.on("/force", handleForce); server.begin(); MDNS.addService("http", "tcp", 80); } void loop() { MDNS.update(); server.handleClient(); if (digitalRead(buttonPin) == LOW) { delay(50); if (digitalRead(buttonPin) == LOW) { pressButton(standardPress); while(digitalRead(buttonPin) == LOW) delay(10); } } }