using nodemcu,relay to use for computer on/off button with alexa

as topic stated, i am using nodemcu esp8266 with a relay to put in the computer as on/off button and use alexa to control it. so far i can turn on and off state with relay. problem i am having is i want relay to go back to off state after 1 second (i just needed relay to toggle on/off and stay off ). here is my code. any help would be appreciated.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "XXXXX"//change your Wifi name
#define WIFI_PASS "XXXXX"//Change your Wifi Password
#define SERIAL_BAUDRATE 115200

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define Computer D1
#define Optional D2

// -----------------------------------------------------------------------------
// Wifi Setup
// -----------------------------------------------------------------------------

void wifiSetup() {

// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);

// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);

// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();

// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// -----------------------------------------------------------------------------
// Device Callback
// -----------------------------------------------------------------------------
void callback(uint8_t device_id, const char * device_name, bool state) {
Serial.print("Device "); Serial.print(device_name);
Serial.print(" state: ");
if (state) {
Serial.println("ON");
} else {
Serial.println("OFF");
}
//Switching action on detection of device name
if ( (strcmp(device_name, "Computer") == 0) ) {
// adjust the relay immediately!
if (state){
digitalWrite(Computer, LOW);
} else {
digitalWrite(Computer, HIGH);
}
}
if ( (strcmp(device_name, "Optional") == 0) ) {
// adjust the relay immediately!
if (state) {
digitalWrite(Optional, HIGH);
} else {
digitalWrite(Optional, LOW);
}
}
}

void setup() {
//Initialize pins to Low on device start
pinMode(Computer, OUTPUT);
digitalWrite(Computer, HIGH);
pinMode(Optional, OUTPUT);
digitalWrite(Optional, LOW);

// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println("FauxMo demo sketch");
Serial.println("After connection, ask Alexa/Echo to 'turn on' or 'off'");

// Wifi
wifiSetup();

// Device Names for Simulated Wemo switches
fauxmo.addDevice("Computer");
fauxmo.addDevice("D2");
fauxmo.onMessage(callback);
}

void loop() {
fauxmo.handle();
}

Sounds like you are intending the relay to turn off the AC power to a computer. Is this correct?

OR are you replacing the off/on switch on the computer?

For many years, the computer off/on switched merely "tell" the computer to shut down. A lot happens in the computer after you push the switch and when the computer shuts itself off. You don't to just kill the power like unplugging the computer.

Paul

I am replacing on/off switch.

naynaing:
I am replacing on/off switch.

And that is a momentary switch, right?

Paul

Yes, it is.

I probably doing this all wrong. i can't find a way to make relay to turn on for 1 second then turn off. i changed the code with delay like below , still doesn't work. i must have done something wrong. Can someone help?

void callback(uint8_t device_id, const char * device_name, bool state) {
Serial.print("Device "); Serial.print(device_name);
Serial.print(" state: ");
if (state) {
Serial.println("ON");
} else {
Serial.println("OFF");
}
//Switching action on detection of device name
if ( (strcmp(device_name, "Computer") == 0) ) {
// adjust the relay immediately!
if (state){
digitalWrite(Computer, LOW);
delay(1000);
digitalWrite(Computer, HIGH);
} else {
digitalWrite(Computer, HIGH);
delay(1000);
digitalWrite(Computer, LOW);
}

I must have posted on wrong section. How do i close this and reopen , ask for help, in programming section?