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();
}