Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taking #RRGGBB hex string and using it to create the JSON block with decimal values #19

Closed
linker3000 opened this issue May 28, 2017 · 2 comments

Comments

@linker3000
Copy link

linker3000 commented May 28, 2017

One for the notes/wiki??

I was using a color wheel icon/function in an Android-based MQTT app to test my RGB LED build with esp-mqtt-rgb-led and Node-red. I created this node function to turn the received hex MQTT payload (#RRGGBB) from the app into an input string for this code on an ESP8266. Might be useful for others...

// Convert #RRGGBB hex payload into JSON formatted string for
// corbanmailloux/esp-mqtt-rgb-led code
// https://github.com/corbanmailloux/esp-mqtt-rgb-led

if ((msg.payload.toString().length != 7) &&
    (msg.payload.toString().substring(0.1) != "#"))
  {return null;}

msg.payload = '{"state": "ON","brightness": 255,"color": {' + 
  '"r": ' + parseInt(msg.payload.toString().substring(1,3),16) + ',' + 
  '"g": ' + parseInt(msg.payload.toString().substring(3,5),16) + ',' +
  '"b": ' + parseInt(msg.payload.toString().substring(5),16) + 
  '},"transition": 2' +
  '}';

return msg;
@corbanmailloux
Copy link
Owner

@linker3000 Thank you for this addition. I've added a wiki page here and credited you.

@linker3000
Copy link
Author

Thanks! In further testing I modified the code slightly:

// Convert #RRGGBB hex payload into JSON formatted string for
// corbanmailloux/esp-mqtt-rgb-led code
// https://github.com/corbanmailloux/esp-mqtt-rgb-led

if ((msg.payload.toString().length != 7) ||
    (msg.payload.toString().substring(0,1) != "#"))
  {return null;}

msg.payload = '{"state": "ON","brightness": 255,"color": {' + 
  '"r": ' + parseInt(msg.payload.toString().substring(1,3),16) + ',' + 
  '"g": ' + parseInt(msg.payload.toString().substring(3,5),16) + ',' +
  '"b": ' + parseInt(msg.payload.toString().substring(5),16) + 
  '}' +
  '}';

return msg;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants