Aircoookie / Espalexa

Alexa voice control for ESP8266/ESP32 (including brightness and color!)
MIT License
548 stars 137 forks source link

Idea Motion Sensor #110

Open luccas222 opened 4 years ago

luccas222 commented 4 years ago

I'm very new to programming, and I don't think I will be able to do this, but I'm thinking that if you modify your emulator to not only be a HUE light but to appear as a HUE Motion Sensor... can Alexa trigger routines by sensor state? I know that Phillips have an open source api for sensors, but I don't understand how to make your code to implement it and be recognized by alexa as a motion-sensor. Maybe you already tried this.

BTW I managed to mix your code with some other to get the status of the sensor-pin, and also to turn on/off a light manually with a touch sensor. But even when alexa knows the status and shows me that status in the app, I can't trigger a routine with the created device.

Sorry for my bad english, I'm from argentina. I will paste my code for you here:

So basically... 1- Can you tweak/update your library to manage Motion Sensors? 2- Once Motion Sensor is recognized as is... can Alexa trigger routines with it?


ifdef ARDUINO_ARCH_ESP32

include

else

include

define TRIAC_PIN 0 // define triac pin 0

define TOUCH_SENSOR_PIN 2 // define pin sensor 2

endif

include

// prototypes boolean connectWifi();

//callback functions void PirSensorChanged(uint8_t brightness);

// Change this!! const char* ssid = "*****"; const char* password = "*****";

boolean wifiConnected = false;

// -------------------------- VARIABLES SWITCH FOR SENSOR ---------------

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 const char* PIRSensor = "PIRSensor";

unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 50;

// ----------------------------------------------------------------------------------------------

Espalexa espalexa;

EspalexaDevice* PirDevice; //this is optional

void setup() {

Serial.begin(115200); pinMode(TRIAC_PIN, OUTPUT); // define el triac pin como un output pinMode(TOUCH_SENSOR_PIN, INPUT); //define el TOUCH_SENSOR_PIN como input // Initialise wifi connection wifiConnected = connectWifi();

if(wifiConnected){

// Define your devices here. 
//simplest definition, default state third parameter = off
//third parameter is beginning state (255 or 0)

PirDevice = new EspalexaDevice("PIRSensor", PirSensorChanged, 0); //you can also create the Device objects yourself like here
espalexa.addDevice(PirDevice); //and then add them
//PirDevice->setValue(255); //this allows you to e.g. update their state value at any time!

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

// ----------------- START PIR SENSOR -----------------

static unsigned long last = millis(); if (millis() - last > 5000) { last = millis(); }

int reading = digitalRead(TOUCH_SENSOR_PIN); // lee el valor del pin TOUCH_SENSOR_PIN

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;

// ----------------- START PIR SENSOR TEST -----------------

        Serial.println("Switch pressed");
        PirDevice->setValue(PirDevice->getValue() > 0 ? 0 : 255); // Value toggle
        PirDevice->doCallback();                  // and set device to value

// ----------------- END PIR SENSOR TEST -----------------

  // 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;

}

//our callback functions void PirSensorChanged(uint8_t brightness) { Serial.print("Device 1 changed to ");

//do what you need to do here

//EXAMPLE

if (brightness) { Serial.print("ON, brightness "); Serial.println(brightness); digitalWrite(TRIAC_PIN, HIGH); PirDevice->setValue(255); } else { Serial.println("OFF"); digitalWrite(TRIAC_PIN, LOW); PirDevice->setValue(0); } }

// 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; }

luccas222 commented 4 years ago

I found yesterday a Sinric Pro, and it works like a charm, but it works like a HUE hub.

mharmon12 commented 4 years ago

@luccas222 can you share a link to what you've found?

I've added code to my light switch device that sends an update to Alexa. What I need to do is make it a "state" device. That is to say a device that reflects open or closed for instance. BUT the skill would also need to have like a "triggered" state also. Does that make sense?

luccas222 commented 4 years ago

Hi Mike, I managed to mix some code and make the app know if the device is on/off, but still that doesn't make alexa to trigger a routine. Right now and "for the moment", I will use the service of SinricPro, you can have 5 devices with the free account. The URL is https://sinric.pro/.

I really don't know how they managed to make it work...

mharmon12 commented 4 years ago

I used sinric from an instructable and it didn't have ability to know what state it was currently in OR update Alexa if the state changed at the device as a Smart switch would.

This Espalexa gets me to 95% of where I want to be with my apps. Once I hack the ONOFF functionality then I'll be 98% because then I just need a sensor BUT I could spoof that with a switch.


From: luccas222 notifications@github.com Sent: Wednesday, April 1, 2020 14:27 To: Aircoookie/Espalexa Espalexa@noreply.github.com Cc: Mike Harmon mharmon12@msn.com; Comment comment@noreply.github.com Subject: Re: [Aircoookie/Espalexa] Idea Motion Sensor (#110)

Hi Mike, I managed to mix some code and make the app know if the device is on/off, but still that doesn't make alexa to trigger a routine. Right now and "for the moment", I will use the service of SinricPro, you can have 5 devices with the free account. The URL is https://sinric.pro/https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsinric.pro%2F&data=02%7C01%7C%7C8daddf49c12c4f4905b808d7d67b1e09%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637213696575872155&sdata=FOBB2btqrt%2BG91YKvTkhKa%2F7t%2FQ%2BnMwlpq9W0lglRCw%3D&reserved=0.

I really don't know how they managed to make it work...

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FAircoookie%2FEspalexa%2Fissues%2F110%23issuecomment-607472823&data=02%7C01%7C%7C8daddf49c12c4f4905b808d7d67b1e09%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637213696575882156&sdata=ucUoMGf9EEiOf%2F6AHlyjbxzvDX6xs78hq2s4cQn0uEw%3D&reserved=0, or unsubscribehttps://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAHMOEYHXCKZZ445O2E6O7KTRKOPTRANCNFSM4LRPRP7A&data=02%7C01%7C%7C8daddf49c12c4f4905b808d7d67b1e09%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637213696575892116&sdata=YvuNohLSwqSQS3CcOrKRztYZudHCdLfffe7YtCrPseo%3D&reserved=0.

mharmon12 commented 4 years ago

@luccas222 What do you mean by, ..."doesn't make Alexa to trigger a routine??

luccas222 commented 4 years ago

Hey Mike, sorry for my bad english... I will try to explain better. YES "Sinric" is not able to trigger Alexa routines, but "Sinric Pro" is.

What I mean is that you are "not able" to create an alexa routine based on the sensor state. Not even to make alexa to say something. I was searching for a way of making alexa to manage sensors and trigger routines, acting as an alarm.

EspAlexa and FauxmoESP are great! but I they lack of "something" and I really don't know what!! to make the devices to work with alexa routines.

If your read more in this repository I think the author answered to other similar issue about what he thinks the problem is.

I just want to help the way I can, but to me, not being a developer, the only way to help is to send good vibes and my findings.

I'm from argentina and live in mexico, if you want, you can contact me directly and we can chat, my email is luccas222@hotmail.com