Alexa with relays and auto off

Okay so i will put this out there
I have no programming skills at all so i am looking for some help
i have a code that will turn on 5 lights using relays with voice command over alexa
what i would like to happen is to have it shut off after a set time limit
can someone show me how this is done
i have it working in the house and i am able to turn on and off with voice command just cant find out how to make it go auto off after 30 secs
i have attached the code i am working with below
Please any help would be nice
thanks in advance

example
Alexas “turn light 1 on”
would like to go off after 30 sec
Alexas "turn light 2 on
would like it to go off after 30 sec

/**********************************************************************************
*  TITLE: Alexa control 5 channel Relay Module using NodeMCU or ESP32
*  Click on the following links to learn more. 
*  YouTube Video: https://youtu.be/tIFEtHVLexw
*  Related Blog : https://easyelectronicsproject.com/esp32-projects/
*  by Subhajit (Tech StudyCell)
*  
*  Download the libraries:
*  https://github.com/Aircoookie/Espalexa
*  
*  Preferences--> Aditional boards Manager URLs : 
*  https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
*  
*  Download Board:
*  For ESP8266 NodeMCU (2.5.1): https://github.com/esp8266/Arduino
*  For ESP32                  : https://github.com/espressif/arduino-esp32
**********************************************************************************/


#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
// define the GPIO connected with Relays
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 13 //D7
#define RelayPin4 14 //D5
#define RelayPin5 12 //D6


// prototypes
boolean connectWifi();


//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);
void fifthLightChanged(uint8_t brightness);


// WiFi Credentials
const char* ssid = "Farmhouse2019";
const char* password = "Tisa1960";


// device names
String Device_1_Name = "light 1";
String Device_2_Name = "light 2";
String Device_3_Name = "light 3";
String Device_4_Name = "light 4";
String Device_5_Name = "light 5";


boolean wifiConnected = false;


Espalexa espalexa;


void setup()
{
 Serial.begin(115200);


 pinMode(RelayPin1, OUTPUT);
 pinMode(RelayPin2, OUTPUT);
 pinMode(RelayPin3, OUTPUT);
 pinMode(RelayPin4, OUTPUT);
 pinMode(RelayPin5, OUTPUT);


 // Initialise wifi connection
 wifiConnected = connectWifi();


 if (wifiConnected)
 {
   // Define your devices here.
   espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
   espalexa.addDevice(Device_2_Name, secondLightChanged);
   espalexa.addDevice(Device_3_Name, thirdLightChanged);
   espalexa.addDevice(Device_4_Name, fourthLightChanged);
   espalexa.addDevice(Device_5_Name, fifthLightChanged);


   espalexa.begin();
 }
 else
 {
   while (1)
   {
     Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
     delay(2500);
   }
 }
}


void loop()
{
 espalexa.loop();
 delay(1);
}


//our callback functions
void firstLightChanged(uint8_t brightness)
{
 //Control the device
 if (brightness == 255)
   {
     digitalWrite(RelayPin1, LOW);
     delay(5000);
     Serial.println("Device1 ON");
     }
   
 else
 {
   digitalWrite(RelayPin1, HIGH);
   delay(5000);
   Serial.println("Device1 OFF");
 }
}


void secondLightChanged(uint8_t brightness)
{
 //Control the device 
 if (brightness == 255)
   {
     digitalWrite(RelayPin2, LOW);
     Serial.println("Device2 ON");
   }
 else
 {
   digitalWrite(RelayPin2, HIGH);
   Serial.println("Device2 OFF");
 }
}


void thirdLightChanged(uint8_t brightness)
{
 //Control the device  
 if (brightness == 255)
   {
     digitalWrite(RelayPin3, LOW);
     Serial.println("Device3 ON");
   }
 else
 {
   digitalWrite(RelayPin3, HIGH);
   Serial.println("Device3 OFF");
 }
}


void fourthLightChanged(uint8_t brightness)
{
 //Control the device 
 if (brightness == 255)
   {
     digitalWrite(RelayPin4, LOW);
     Serial.println("Device4 ON");
   }
 else
 {
   digitalWrite(RelayPin4, HIGH);
   Serial.println("Device4 OFF");
 }
}


void fifthLightChanged(uint8_t brightness)
{
 //Control the device 
 if (brightness == 255)
   {
     digitalWrite(RelayPin5, HIGH);
     Serial.println("Device5 ON");
   }
 else
 {
   digitalWrite(RelayPin5, LOW);
   Serial.println("Device5 OFF");
 }
}


// connect to wifi – returns true if successful or false if not
boolean connectWifi()
{
 boolean state = true;
 int i = 0;


 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);
 Serial.println("");
 Serial.println("Connecting to WiFi");


 // Wait for connection
 Serial.print("Connecting...");
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
   if (i > 20) {
     state = false; break;
   }
   i++;
 }
 Serial.println("");
 if (state) {
   Serial.print("Connected to ");
   Serial.println(ssid);
   Serial.print("IP address: ");
   Serial.println(WiFi.localIP());
 }
 else {
   Serial.println("Connection failed.");
 }
 return state;
}

lights.ino (4.81 KB)

You need to set a flag in the callback function that the light is on and then let loop() code deal with turning it off. as an example for light 1 set a global variable…

uint32_t device1Off = 0;

then in the callback for light 1 set the variable to the millis() time the light should go off…

void firstLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness == 255)
  {
    digitalWrite(RelayPin1, LOW);
    //delay(5000);
    Serial.println("Device1 ON");
    device1Off = millis() + 30000UL; // current time + 30 seconds
  }
  
  else
  {
    digitalWrite(RelayPin1, HIGH);
    //delay(5000);
    device1Off = 0;
    Serial.println("Device1 OFF");
  }
}

In loop check if the light is on and then if it needs turning off…

 if(device1Off != 0)
  {
    if(device1Off < millis())
    {
      digitalWrite(RelayPin1, HIGH);
      device1Off = 0;
      Serial.println("Device1 OFF");
    }
  }

If you are concerned about changing the code, can't you use the Alexa App to turn the lights off after a given time?

RIVA
thanks for the help
it does what i want
AGAIN BIG THANKS