Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- /*
- * Project: Wi-Fi PC Remote Power Control & Status Monitor
- * Hardware Components:
- * 1. Controller: NodeMCU ESP8266 V3
- * 2. Isolation: 2x PC817 DIP Optocouplers
- * 3. Resistors: 2x 220 Ohm (1/4W)
- * 4. Power Supply: Micro-USB (5V)
- *
- * Schematic & Wiring Diagram:
- *
- * [ NodeMCU ] [ PC817 #1 - Power Control ] [ Motherboard F_PANEL ]
- * Pin D1 (GPIO5) ---> [220Ω] ---> Pin 1 (Anode •)
- * GND --------------------------> Pin 2 (Cathode)
- * Pin 4 (Collector) ----------------------> Pin 7 (POWER SW +)
- * Pin 3 (Emitter) ------------------------> Pin 8 (POWER SW -)
- *
- * [ NodeMCU ] [ PC817 #2 - Power LED Sense ] [ Motherboard F_PANEL ]
- * Pin D2 (GPIO4) <--------------- Pin 4 (Collector)
- * GND --------------------------> Pin 3 (Emitter)
- * Pin 2 (Cathode) <----------------------- Pin 3 (PLED -)
- * Pin 1 (Anode •) <------- [220Ω] -------- Pin 1 (PLED +)
- *
- * Hardware Notes:
- * - PC817 Pin 1 is identified by the small indented circle/dot on top of the DIP package.
- * - Pin D2 (GPIO4) uses ESP8266 internal pull-up logic (INPUT_PULLUP).
- * - Target local URL: http://streampc.local
- */
- // --- Network Configuration ---
- const char* ssid = "YOUR_WIFI_SSID";
- const char* password = "YOUR_WIFI_PASSWORD";
- // --- Hostname Configuration ---
- const char* hostName = "streampc"; // Resolves to http://streampc.local
- // --- Pin Definitions ---
- const int PIN_POWER_BTN = 5; // D1 (GPIO5) -> PC817 #1 (Power Control Output)
- const int PIN_POWER_SENS = 4; // D2 (GPIO4) -> PC817 #2 (Power LED Sense Input)
- ESP8266WebServer server(80);
- // Helper function to pulse power pin
- void pressPowerButton(int durationMs) {
- digitalWrite(PIN_POWER_BTN, HIGH);
- delay(durationMs);
- digitalWrite(PIN_POWER_BTN, LOW);
- }
- // Check power state (Returns true if PC is ON)
- bool isPcOn() {
- return digitalRead(PIN_POWER_SENS) == LOW;
- }
- // Handler for HTML Interface
- void handleRoot() {
- String html = "<html><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1'><style>body{text-align:center;font-family:Arial,sans-serif;background:#f4f4f4;padding:20px;}h1{display:flex;align-items:center;justify-content:center;margin-bottom:30px;}.indicator{display:inline-block;width:22px;height:22px;border-radius:50%;margin-left:15px;background:#2ecc71;box-shadow: 0 0 10px #2ecc71;transition: 0.3s;}.btn{width:100%;max-width:300px;height:80px;font-size:20px;font-weight:bold;color:white;border:none;border-radius:15px;margin:15px 0;cursor:pointer;display:block;margin-left:auto;margin-right:auto;transition:0.2s;}.btn:active{transform:scale(0.98);}.on{background:#2ecc71;box-shadow:0 5px #27ae60;} .off{background:#e74c3c;box-shadow:0 5px #c0392b;}</style><script>setInterval(function(){ fetch('/status').then(r => r.text()).then(status => { var ind = document.querySelector('.indicator'); if(status==='ON'){ind.style.background='#2ecc71'; ind.style.boxShadow='0 0 10px #2ecc71';} else{ind.style.background='#e74c3c'; ind.style.boxShadow='0 0 10px #e74c3c';} });}, 2000);function safeToggle() { fetch('/status').then(r => r.text()).then(status => { if (status === 'ON') { if (confirm('PC is already ON. Power action?')) location.href='/power'; } else { location.href='/power'; } });}</script></head><body><h1>PC Remote <span class='indicator'></span></h1><button class='btn on' onclick='safeToggle()'>POWER ON/OFF</button><button class='btn off' onclick=\"if(confirm('FORCE SHUTDOWN (8S)?')){location.href='/force';}\">FORCE SHUTDOWN</button><p>http://streampc.local</p></body></html>";
- server.send(200, "text/html", html);
- }
- // Endpoint returning plain text status ("ON" or "OFF")
- void handleStatus() {
- server.send(200, "text/plain", isPcOn() ? "ON" : "OFF");
- }
- // Endpoint for standard power tap (500ms)
- void handlePower() {
- pressPowerButton(500);
- server.sendHeader("Location", "/");
- server.send(303);
- }
- // Endpoint for hard shutdown (8000ms / 8s)
- void handleForce() {
- pressPowerButton(8000);
- server.sendHeader("Location", "/");
- server.send(303);
- }
- void setup() {
- Serial.begin(115200);
- // Configure output pin
- pinMode(PIN_POWER_BTN, OUTPUT);
- digitalWrite(PIN_POWER_BTN, LOW);
- // Configure input pin with internal pull-up
- pinMode(PIN_POWER_SENS, INPUT_PULLUP);
- // Static IP Configuration
- IPAddress local_IP(192, 168, 0, 166);
- IPAddress gateway(192, 168, 0, 1);
- IPAddress subnet(255, 255, 255, 0);
- IPAddress primaryDNS(192, 168, 0, 1);
- if (!WiFi.config(local_IP, gateway, subnet, primaryDNS)) {
- Serial.println("STA Failed to configure static IP!");
- }
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.println("\nConnecting to WiFi...");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nWiFi Connected!");
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- // Start mDNS responder for http://streampc.local
- if (MDNS.begin(hostName)) {
- Serial.println("mDNS responder started: http://streampc.local");
- } else {
- Serial.println("Error setting up MDNS responder!");
- }
- // Web server routes
- server.on("/", handleRoot);
- server.on("/status", handleStatus);
- server.on("/power", handlePower);
- server.on("/force", handleForce);
- server.begin();
- Serial.println("HTTP Server started.");
- }
- void loop() {
- MDNS.update();
- server.handleClient();
- }
Advertisement