Go Down

Topic: Help regarding esp8266 & Alexa - additional code (Read 294 times) previous topic - next topic

Obsessionfatale

Hi Guys,

I am new to Arduino and programming. I have currently got a esp8266 working with Alexa but I need the output turned on by Alexa to be a timed output rather than a constant.

I have read through the millis sticky and have been experimenting but cannot seem to get the desired effect.

I have even tried to use a simple project with a button and led to try and simulate what I want in a simpler project, I can get the led to follow the button but can't get it to stay on for a set time.

Any help is much appreciated

Here is the default code;

 /**********************************************************************************
  * Code for controlling multiple devices connected to one NodeMCU using Amazon Echo
  *
  * Written by Sid for Sid's E Classroom
  *
  * https://www.youtube.com/c/SidsEClassroom
  *********************************************************************************/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

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

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define Kitchen D1
#define Bedroom D2
#define Living D3
// -----------------------------------------------------------------------------
// 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, "Kitchen Lights") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(Kitchen, HIGH);
    } else {
      digitalWrite(Kitchen, LOW);
    }
  }
  if ( (strcmp(device_name, "Bedroom Lights") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(Bedroom, HIGH);
    } else {
      digitalWrite(Bedroom, LOW);
    }
  }
  if ( (strcmp(device_name, "Living Room Lights") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(Living, HIGH);
    } else {
      digitalWrite(Living, LOW);
    }
  }
}

void setup() {
    //Initialize pins to Low on device start
    pinMode(Kitchen, OUTPUT);
    digitalWrite(Kitchen, LOW);
    pinMode(Bedroom, OUTPUT);
    digitalWrite(Bedroom, LOW);
    pinMode(Living, OUTPUT);
    digitalWrite(Living, 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 <devicename> on' or 'off'");

    // Wifi
    wifiSetup();

    // Device Names for Simulated Wemo switches
    fauxmo.addDevice("Living Room Lights");
    fauxmo.addDevice("Kitchen Lights");
    fauxmo.addDevice("Bedroom Lights");
    fauxmo.onMessage(callback);
}

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

blh64

Hello,

If I understand you, you want Alexa to turn on a light and then it will stay on for a period of time and then turn off?  If so, you need to start the timer in your callback() but finish it in your loop() something like this:

Code: [Select]

/**********************************************************************************
  * Code for controlling multiple devices connected to one NodeMCU using Amazon Echo
  *
  * Written by Sid for Sid's E Classroom
  *
  * https://www.youtube.com/c/SidsEClassroom
  *********************************************************************************/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

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

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define Kitchen D1
#define Bedroom D2
#define Living D3

volatile bool KitchenOn = false;  // needs to be volatile since used in ISR
volatile bool BedroomOn = false;
volatile bool LivingOn = false;

const unsigned long  KitchenOnTime = 10;    // seconds
const unsigned long BedroomOnTime = 15;     // seconds
const unsigned long LivingOnTime = 20;

volatile unsigned long KitchenStartTime;
volatile unsigned long BedroomStartTime;
volatile unsigned long LivingStartTime;

// -----------------------------------------------------------------------------
// 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, "Kitchen Lights") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(Kitchen, HIGH);
      KitchenOn = true;
      KitchenStartTime = millis();
    } else {
      digitalWrite(Kitchen, LOW);
      KitchenOn = false;
    }
  }
  if ( (strcmp(device_name, "Bedroom Lights") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(Bedroom, HIGH);
      BedroomOn = true;
      BedroomStartTime = millis();
    } else {
      digitalWrite(Bedroom, LOW);
      BedroomOn = false;
    }
  }
  if ( (strcmp(device_name, "Living Room Lights") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(Living, HIGH);
      LivingOn = true;
      LivingStartTime = millis();
    } else {
      digitalWrite(Living, LOW);
      LivingOn = false;
    }
  }
}

void setup() {
    //Initialize pins to Low on device start
    pinMode(Kitchen, OUTPUT);
    digitalWrite(Kitchen, LOW);
    pinMode(Bedroom, OUTPUT);
    digitalWrite(Bedroom, LOW);
    pinMode(Living, OUTPUT);
    digitalWrite(Living, 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 <devicename> on' or 'off'");

    // Wifi
    wifiSetup();

    // Device Names for Simulated Wemo switches
    fauxmo.addDevice("Living Room Lights");
    fauxmo.addDevice("Kitchen Lights");
    fauxmo.addDevice("Bedroom Lights");
    fauxmo.onMessage(callback);
}

void loop() {
  unsigned long duration;
 
  fauxmo.handle();
  if ( KitchenOn == true ) {
    duration = (millis() - KitchenStartTime) / 1000;  // convert to seconds
    if ( duration >= KitchenOnTime ) {
      digitalWrite(Kitchen, LOW);
      KitchenOn = false;
    }
  }
 
  if ( BedroomOn == true ) {
    duration = (millis() - BedroomStartTime) / 1000;  // convert to seconds
    if ( duration >= BedroomOnTime ) {
      digitalWrite(Bedroom, LOW);
      BedroomOn = false;
    }
  }
  if ( LivingOn == true ) {
    duration = (millis() - LivingStartTime) / 1000;  // convert to seconds
    if ( duration >= LivingOnTime ) {
      digitalWrite(Living, LOW);
      LivingOn = false;
    }
  }

}

Obsessionfatale

Hi,

Thank you so much that seems to work. I was reluctant to post on the forum as I like to try and work things out myself but I don't think I would have ever have worked that out.

Only practice and time will tell.

The reason behind my momentary action is that I'm hoping to have a TV lower from the ceiling and only want the motor to run for a short period.

Again thank you for your help and keep up the good work.

 ;D

PaulS

Quote
The reason behind my momentary action is that I'm hoping to have a TV lower from the ceiling and only want the motor to run for a short period.
If you were smart, you'd have the motor run for as long as it takes to trigger a limit switch. Timing alone will NOT tell you whether the TV reached the intended position, or not.
The art of getting good answers lies in asking good questions.

Obsessionfatale

I will be implementing limit switches but the motor will require the voltage to be reversed to raise the TV, the voltage will remain a second or two after it has hit the limit switch.

This way the motor will be ready for the reversed voltage, if it wasn't a 'timed' voltage as soon as the TV comes off the limit switch it would have both voltages and short.


PaulS

Quote
if it wasn't a 'timed' voltage as soon as the TV comes off the limit switch it would have both voltages and short.
That is nonsense.
The art of getting good answers lies in asking good questions.

Obsessionfatale

Thanks for the positive response, I'm not using the limit switches through the esp. They are hard wired and not connected to the controller. I would much prefer the switches to be failsafe and completely break the voltage.

I am open to advise, but your input offers no value.

Go Up