Hola, estoy usando este código para controlar mis dispositivos con Alexa, pero no consigo modificar correctamente el código para usarlo como si fuera un pulsador, es decir, que al dar la orden de encender el relé se active solo unos segundos.
un saludo.
/*
* anonimo
* 02-Ago-2019
*/
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#define RF_RECEIVER 13
#define TRIAC_PIN 12
#define RELAY_PIN_2 14
#else
#include <ESP8266WiFi.h>
#define TRIAC_PIN 0
#define boton 2
#endif
#include "fauxmoESP.h"
int estado = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50;
#define SERIAL_BAUDRATE 115200
#define WIFI_SSID "...."
#define WIFI_PASS ".........."
#define LAMP_1 "led" // Nombre del dispositivo en Alexa
fauxmoESP fauxmo;
// Wi-Fi Connection
void wifiSetup() {
// Setear mod
WiFi.mode(WIFI_STA);
// Conectar
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());
}
void setup() {
// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
// Wi-Fi connection
wifiSetup();
// LED
pinMode(TRIAC_PIN, OUTPUT);
//digitalWrite(TRIAC_PIN, HIGH);
//mySwitch.enableReceive(RF_RECEIVER); // Receiver on interrupt 0 => that is pin #2
// By default, fauxmoESP creates it's own webserver on the defined port
// The TCP port must be 80 for gen3 devices (default is 1901)
// This has to be done before the call to enable()
fauxmo.createServer(true); // not needed, this is the default value
fauxmo.setPort(80); // This is required for gen3 devices
// You have to call enable(true) once you have a WiFi connection
// You can enable or disable the library at any moment
// Disabling it will prevent the devices from being discovered and switched
fauxmo.enable(true);
// You can use different ways to invoke alexa to modify the devices state:
// "Alexa, turn lamp two on"
// Add virtual devices
fauxmo.addDevice(LAMP_1);
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
// Callback when a command from Alexa is received.
// You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
// State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
// Just remember not to delay too much here, this is a callback, exit as soon as possible.
// If you have to do something more involved here set a flag and process it in your main loop.
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if ( (strcmp(device_name, LAMP_1) == 0) ) {
// this just sets a variable that the main loop() does something about
Serial.println("RELAY 1 switched by Alexa");
//digitalWrite(TRIAC_PIN, !digitalRead(TRIAC_PIN));
if (state) {
digitalWrite(TRIAC_PIN, HIGH);
estado = 1;
} else {
digitalWrite(TRIAC_PIN, LOW);
estado = 0;
}
}
});
}
void loop() {
// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets
fauxmo.handle();
static unsigned long last = millis();
if (millis() - last > 5000) {
last = millis();
Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
}
int reading = digitalRead(boton);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is LOW
if (buttonState == LOW) {
estado = !estado;
// Encender la luz:
digitalWrite(TRIAC_PIN, estado);
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}