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
-
Sid's E Classroom - YouTube
*********************************************************************************/
#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 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();
}