Trigger a message payload based on input from parallel port

Hi, I am completely new to Node-RED but I love it already.
I have an arduino board connected to my raspberry pi, I need to send a one character output from the pi to the Arduino based on another input that comes from a tape dispenser.

Input ----------------- Raspberry Pi --------[read] --- if [variable}-----payload ------ Arduino.

Now I have different inputs coming from my tape dispenser and they are all in the form of @number. I want to trigger a different payload for each possible input.

The total of inputs are 13
The outputs for the arduino are a 1 character payload [1/2/3/4/5/6/7/8/9/a/b/c/s]

I don't know if a single function can handle it or if I need different functions.

Thank You

Several ways you can do this.

Probably the most "readable"/native way would be to use a function node with a switch statement...

switch (msg.payload){
  case: 1
  msg.payload = "A";
  break;
  case: 2
  msg.payload = "B";
  break;
}
return msg;

There are clever ways (use a lookup object) and even contrib nodes e.g node-red-contrib-map

1 Like

Iteresting, so case must be the exact input or can I grab only part of it?

I mean, for each case the tape machine gives me:

tr

ch

@123

123 here is only an example. But do I need to specify the tr and ch with the spaces?

Case is an exact match.
So you could strip out the white space first and use "trch@123" as the case.

Search the internet for JavaScript remove whitespace for clues

1 Like

Thank you so much for the quick help, I will go and do it immediately.

So far I had no luck.

tried with

switch (msg.payload){
case msg.payload=="@1T @1XR @1Y198":
msg.output = "1";
break;
switch (msg.payload){
case msg.payload=="@1T @1XR @1Y163":
msg.output = "2";
break;

But it does not work. all the message gets through unfiltered no matter what I do.
I also tried with

var topic=msg.topic;
if (topic=="@1Y198"){
return [null,1]

}
var topic=msg.topic;
if (topic=="@1T"){
return [null]

}
var topic=msg.topic;
if (topic=="@1XR"){
return [null]

}

Even in this case the message goes all the way through unfiltered.
No Idea how to solve this.

Put a debug node before and after the switch node and see what it being output - i would guess it is probably not a fully formed message depending on how you got it into Node Red in the first place i.e. what is the message flow - how do you interface to the Rpi ports to get the data ? Maybe past your whole flow ?

Craig

Also you have the syntax of the switch statement completely wrong, go back and look at the example @Steve-Mcl posted and you will see that yours is not the same.

Hi, I tried with that syntax but the editor was complaining about the "@" that was unexpected.
The debug node tells me that the output (after the function) is @1T @1XR @1Y198

Tried different syntax for the node


switch (msg.payload){
    case: "@1T @1XR @1Y10"
    msg.output = "2";
    break;
    case msg.payload=="@1T @1XR @1Y10":
    msg.output = "3";
    break;
        case msg.payload=="@1T ":
    msg.output = "4";
    break;
        case msg.payload=="@1XR ":
    msg.output = "5";
    break;
}
return msg;

But when I use case: case: "@1T @1XR @1Y10"
the error is Expected ':' and instead saw @1T @1XR @1Y10"

Spot the 2 deliberate errors in that line ^

1 Like

Actually, every case line is incorrect.

case: statement;
Not
case statement:

1 Like

Hi, I don't know what is going on.

Can you try to put

switch (msg.payload) {
   case: @1T @1XR @1Y10
msg.output = "1";
break;
}
return msg;

inside a function?

Node red tells me:
Unexpected '@'.
Expected an identifier and instead saw ':'

if I quote everything like this:
case: "@1T @1XR @1Y10"
i get
Expected an identifier and instead saw ':'.
Expected ':' and instead saw '@1T @1XR @1Y10'.
Missing ':' on a case clause.

Now you have lost the quotes, that is the one that you did have right the first time
case: "@1T @1XR @1Y10"
Strictly there should be a ; on the end but a ; on the end of a line is often optional.

I see, problem is that even with the quotes and the ';' at the end of the line I have different errors

Copy and paste what you have now

switch (msg.payload) {
   case: "@1T @1XR @1Y10";
msg.output = "1";
break;
   case: "@1T @1XR @1Y236";
msg.output = "2";
break;
}
return msg;

Sorry, I have got it wrong myself now, it should be

switch (msg.payload) {
   case "@1T @1XR @1Y10":
msg.output = "1";
break;
   case "@1T @1XR @1Y236":
msg.output = "2";
break;
}
return msg;
1 Like

Let me give it a try :smiley:

Unfortunately all the message goes through the function unfiltered.
I really have no clues why.

I did try to change
return msg;
to
return msg.output but no luck :smiley: