Aircoookie / Espalexa

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

Device does not respond #164

Open FriendlyFlash opened 3 years ago

FriendlyFlash commented 3 years ago

Hello community :)

First of all I'm relatively new to programming so please be gentle if there is a simple solution to my problem I dont see now. As a huge fan of ESPAlexa I didn't have any issues to setting it up for my needs (controlling a motor as dimmabale device). The Problem is, that the motor turns very slowly (for higher torgue), and while it's turning Alexa says: "Device not responding". After reaching the desired position it stops and Alexa shows the connection is now active again. Is there any way to turn this off or let alexa know the answer can be delayed? I actually searched the internet before posting it here and it seems to be a general Philips Hue problem. So maybe it cant be fixed. But if any of you guys got an idea please let me know.

Thank you for your attention

barneyz commented 3 years ago

The problem is espalexa.loop(); is not called for a longer time (>500ms or so).

Your callback function should return immediately, so you set only a flag in the callback() and you implement a non blocking function in your loop() until this function comes to an end and can reset the flag. Non blocking means the function monitors the motor turn without waiting for the end.

Here some (pseudo)code:

char flag = 0;

callback()
{
   flag = 1;
   // start motor turn here
}

loop()
{
  espalexa.loop();

  if (flag)
  {
     if ("test for motor reaches final position")
     {
            // "stop motor turn";
            flag = 0;
     }
  }
}
FriendlyFlash commented 3 years ago

Thank you very much for your reply!

Actually this is an Idea I already had and testing via serial monitor prooved the flag is set correctly. I also tried sending a message after callback is closed to check how long the callback function is active and ist's closed immediately after setting the flag. So far everything works as expected, but the issue ist still the same! Maybe the reason is somewhere in the espalexa.loop(), but this is just a suggestion

EDIT: I also tried just to delay the void loop (10 secs) without content in the callback function and this also leads to the same issue. Maybe I could use a variable to do the first "half" of the movement, then stop for espalexa.loop(), and then move the motor the "second" half, but I think this solution is very unattractive. If anyone knows a better solution please let me know :) I've also read, that puttin the device into a group via the Alexa app might fix it, and indeed this does, but for this "solution" I need a second device in group (with one device it's same issue), and setting a second device as dummy on the same ESP isn't an option because then two devices don't reply.

Jorgen-VikingGod commented 3 years ago

As @barneyz wrote, it is important that you make your code non-blocking. You can also make your motor-loop not by using a for loop, instead you can increment the current step. In your callback, you set the flag to start and reset the last step variable. Inside the motor-loop (which is called in the loop() method) you just increment each time and make small moves, until your end position is reached.

If you are using ESP32 it is more easier, just create a new task on the other core.