Hello everyone,
I’m completely new to this, so please be kind I have been stuck on this problem for 2 days now and I can’t find anything helpful about this online.
I am trying to control each pin of an Arduino individually via Openhab. To do so, I have set up my Arduino to recieve MQTT messages. The first 3 digits of the Message are used as an ID, that is supposed to tell the Arduino, which pin I am trying to access. The rest of the message is the command that I am sending. So the Message looks somewhat like this:
00250
Meaning:
002: I am controlling the pin with ID 002
50: Set dimmer value to 50 (on the pin corresponding to the ID 002)
Now here is where i ran into a problem:
This is the corresponding line in my Items file:
Dimmer Test_Dimmer1 “Dimmer” (All) {mqtt=“>[localbroker:/openHAB/Test_Switch1:command:*:default]”}
this will of course only send values from 0-100. The problem here is, that I don’t know which pin I am trying to control, since there is no ID in the sent data.
I have also tried to transform the value with a Javascript. So I added a transformation in my Items file:
Dimmer Test_Dimmer1 “Dimmer”(All) {mqtt=“>[localbroker:/openHAB/Test_Switch1:command:*:JS(getValue.js)]”}
The Javascript file getValue.js looked as follows:
(function(i){ //the variable i is the value openhab sends (I think) so for a dimmer that would be 0-100
var
message=“002”+i; //002 is the ID of the Pin I am controlling - so I’m adding this to the beginning
return message;
})(input)
This sort of worked. I got the desired Message (002xxx) out of Openhab sent to the Arduino. However I was not able to pass any parameters (speaking the ID) to the Javascript from Openhab. So I’d have to hardcode the ID into the script for every pin I am trying to access. (So I would end up with a bunch of *.js-Files for each ID)
That just doesn’t seem like the right solution. There has got to be an easier way to do this! This has been driving me insane for 6 hours now.
Is there a way to add the ID to all the MQTT-Messages sent to a certain device?
Thank You so much for Your help!!!