vintlabs / fauxmoESP

Add voice control of your ESP32 and ESP8266 devices using Amazon Alexa
MIT License
391 stars 71 forks source link

How do numeric values work? #153

Closed sebkouba closed 3 years ago

sebkouba commented 3 years ago

First off, I love this library. It has made it really easy to get started with my esp8266 and alexa which is a lot of fun.

I'm still poking around, especially in onSetState. It seems that setting a value is possible between 1 and 100 which returns values between 2 and 255. When I say "turn device to 50" the value I expected was 50 but what I get is 127. I'm guessing it's related to how colours work. In the readme it seems that the expected behaviour is getting returned the number you set. Am I missing something?

I get what I thought was the intended behaviour using this:

fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {       
  Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);

  int val = 1 + ( int(value) * 100 / 255 );

  Serial.println("value: " + String(val));
});

The only exception is 100, which returns 101 :| Maybe use math.h and round?

Cheers

Jorgen-VikingGod commented 3 years ago

Hi @sebkouba what you are setting is value in percent (0 to 100); on code site you have 8 Bit so it is 0 to 255. Which means setting to 50 (%) means arrive of value 127.

If you want that value back, the map method in arduino framework is easy to use for that purpose. https://www.arduino.cc/reference/en/language/functions/math/map/

fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {       
  Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);

  int val = map(value, 0, 255, 0, 100);

  Serial.println("value: " + String(val));
});
sebkouba commented 3 years ago

Thanks Jorgen! That's really helpful. Good explanation and elegant solution :)