Aircoookie / Espalexa

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

espalexa.addDevice("name" + void with parameters #43

Open kovi44 opened 5 years ago

kovi44 commented 5 years ago

Dear friends,

I would kindly ask for help. The espalexa is a great module. Thanks for that. Already implement several times. But nowadays I got this issue Void setup -> register alexa devices using int i = 6 espalexa.addDevice("switch one", switch(i));

void switch(int switch_nr, uint8_t value); switch_nr I need to know which relay to use, and value to get 0-255 to understand if "On" or "Off" should be applied.

I'm unable to create a void for each relay 1-24 and the call it espalexa.addDevice("switch one", switch_one()); espalexa.addDevice("switch two", switch_two());.....

Small note: not each of relay1-24 is enabled for alexa usage so for each I check if yes or no Is there a way how to do it.

Thanks for your help Lukas

kovi44 commented 5 years ago

Hi,

Can someone help me. Maybe it's a noob question, but I'm totally lost. I would like to register alexa device using espalexa.addDevice with device name and function (void) with parameter. If there is no way to do so, it;s also an answer for me. Thanks Lukas

Aircoookie commented 5 years ago

Hi Lukas,

I don't really know what issue you are encountering. espalexa.addDevice("switchOne", firstDeviceChanged); makes a new device which will call the firstDeviceChanged() callback function. You can add all the relais you want Alexa to be able to control as separate devices. Depending on the function called you know which device it was, and can call a switch() function for all relais from there, if thats's helpful for you. You can get the ON/OFF by treating the brightness parameter as a bool, so 0 means off and any other value means on. Here is a small example:

void device0Changed(uint8_t brightness);
void device1Changed(uint8_t brightness);

void setup() {
//... see basic example
espalexa.addDevice("Switch 1", device0Changed);
espalexa.addDevice("Switch 2", device1Changed);
//... see basic example
}

//... loop function

void device0Changed(uint8_t brightness)
{
  switch(0, brightness);
}

void device1Changed(uint8_t brightness)
{
  switch(1, brightness);
}

void switch(int switch_nr, bool onoff)
{
  //... switch the relais
}

Let me know if you need any further help using Espalexa :)

kovi44 commented 5 years ago

Thanks for the response, let me explain my issue in detail. I got json config, where I stored the data for all 24 relays, which I want to manage using Amazon Echo. In config there're stored information like if the relay is enabled for Alexa, what's the name of the device and many other In setup void I got function to go through the config and if relay is enabled for alexa then use command espalexa.addDevice(name, relay); name is a String and relay is void. Now what's my issue. I don't want to handle it using like: if (relay1_alexa == enabled) {espalexa.addDevice(name, relay1);} if (relay2_alexa == enabled) {espalexa.addDevice(name, relay2);} ......

Instead of it I would like to use something like: for (int i = 1; i< maxSize; i++) { if (data.containsKey(String(i))) { String alex = data[String(i)]["alexa"]; if (alex.length() > 2) { Serial.print("Registering Alexa device name:");Serial.println(alex); espalexa.addDevice(alex, relay(1)); //where relay is a void and 1 is parameter to define which relay

Is it possible to call a void with parameter to identify relay?

Do I explain it correctly? If something not understandable please let me know. Is there a way how to do it? Thanks Lukas

Aircoookie commented 5 years ago

Ok, thank you, now I understand what you are trying to achieve. First of all, please put #define ESPALEXA_MAXDEVICES 24 in the sketch before #include <Espalexa.h>. Otherwise only 10 devices will be added max.

The second parameter of the addDevice() is a callback function (Espalexa will call the function if the device changed. It is of the type void*). You should be able to do something like this, but I didn't test it myself. Unfortunately, one callback function per relay is still required, so apart from looking better than if (relay1_alexa == enabled) {espalexa.addDevice(name, relay1);} if (relay2_alexa == enabled) {espalexa.addDevice(name, relay2);} there is no benefit.

void relay0Changed(uint8_t brightness);
void relay1Changed(uint8_t brightness);
[...]
void** callbacks = {relay0Changed, relay1Changed,[...]};

void setup {
//...
for (int i = 1; i<= maxSize; i++) {
  if (data.containsKey(String(i))) {
    String alex = data[String(i)]["alexa"];
    if (alex.length() > 2) {
      Serial.print("Registering Alexa device name:");Serial.println(alex);
      espalexa.addDevice(alex, callbacks[i-1]);
}}}
//...
}

void relay0Changed(uint8_t brightness) { switch(0, brightness); }
void relay1Changed(uint8_t brightness) { switch(1, brightness); }
[...]
kovi44 commented 5 years ago

Hi, thanks, it's a great idea to handle it this way. I already tried to implement however I was not successful. Let me share: `void SentAlexa_1(uint8_t brightness); void SentAlexa_2(uint8_t brightness); ..... ... void** callbacks = {SentAlexa_1, SentAlexa_2, .... ,...};

for (int i = 1; i< maxSize; i++) { if (data.containsKey(String(i))) { String alex = data[String(i)]["alexa"]; if (alex.length() > 2) { Serial.print("Registering Alexa device name:");Serial.println(alex); espalexa.addDevice(alex, callbacks[i-1]); ..... ... ..`

But I got this kind of error and due to my limited C++ knowledge, I'm unable to solve it f433_controller_latest_funkcny_2:74:272: error: scalar object 'callbacks' requires one element in initializer void** callbacks = {SentAlexa_1, SentAlexa_2,....,....}; ^ exit status 1 scalar object 'callbacks' requires one element in initializer

May I asked for help? I really appreciate your help Ciao Lukas