RPI Keyboard input convert to numbers

Hi,

I am trying to take data from a USB barcode scanner to a MYSql database. The issue I have is the data from the RPI Keyboard node needs converting back to the numbers/letter & also omitting the Key Down or Key Up. i have used the join node to separate each bit with white space.

if I scan a barcode 5000157062673 I get the following

"6 6 11 11 11 11 11 11 2 2 6 6 8 8 11 11 7 7 3 3 7 7 8 8 4 4 28 28"

How would I write a JS function to cover this back to 5000157062673.

Any help would be much appreciated.

Thanks

Sy

That is most odd!

anyhow, assuming only numbers are expected, this will work...

demo flow...

[{"id":"4f165741.e54618","type":"inject","z":"553814a2.1248ec","name":"6 6 11 11 11 11 11 11 2 2 6 6 8 8 11 11 7 7 3 3 7 7 8 8 4 4 28 28","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"6 6 11 11 11 11 11 11 2 2 6 6 8 8 11 11 7 7 3 3 7 7 8 8 4 4 28 28","payloadType":"str","x":730,"y":1500,"wires":[["c05ce092.17ab4"]]},{"id":"c05ce092.17ab4","type":"function","z":"553814a2.1248ec","name":"","func":"let data = (msg.payload + \"\");\nlet parts = data.split(\" \");\nlet asciichars = [];\n\nfor (let index = 0; index < parts.length; index += 2) {\n    let charCode = parseInt(parts[index]);\n    let char = lookup(charCode);\n    if (char == null) {\n        break; \n    } else {\n        asciichars.push(char);\n    }\n}\n\nmsg.payload = asciichars.join(\"\");\n\nreturn msg;\n\n\nfunction lookup(val) {\n    var table = {\n        11: \"0\",\n        2: \"1\",\n        3: \"2\",\n        4: \"3\",\n        5: \"4\",\n        6: \"5\",\n        7: \"6\",\n        8: \"7\",\n        9: \"8\"\n    }\n    return table[val];\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1080,"y":1500,"wires":[["c4115492.418e38"]]},{"id":"c4115492.418e38","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1250,"y":1500,"wires":[]}]

or

let input = msg.payload.split(" ")
msg.payload="";
for(let i=0; i < input.length-2; i+=2){
    msg.payload += String(input[i]-1).substr(-1);
}
//msg.payload = Number(msg.payload); // uncomment if you require a number
return msg;

:love_you_gesture: absolutely brilliant, both methods work a treat.

thank you so much

Sy

Could this be converted to letters & punctuation aswell or just numbers?

Thanks

Sy

I would think just numbers, but can not be sure as I not sure how your input would be with letters and punctuation. Are the letter/punc represented by number?

The solution I provided was written in such a way that you can add to the lookup. So yes you can add support for letters, numbers & punctuation but since I have no idea what numbers they will arrive into your flow as, you will have to figure it out. Once you know what a number means, just add it to the lookup in the function.

Thanks Steve-Mc. & E1cid

I have tried to append the lookup but no joy with the output. See screen shot of debug window & appended alphabet in lowercase. Surely there is some easier way to bring in data from the keyboard to numbers & letters? if not then here is the number map for the letters.

Lowercase a-z - I think 30 & 28 is something else begging and after !

"30 30 48 48 46 46 32 32 18 18 33 33 34 34 35 35 23 23 36 36 37 37 38 38 50 50 49 49 24 24 25 25 16 16 19 19 31 31 20 20 22 22 47 47 17 17 45 45 21 21 44 44 28 28"

Uppercase

"42 30 30 48 48 46 46 32 32 18 18 33 33 34 34 35 35 23 23 36 36 37 37 38 38 50 50 24 24 25 25 16 16 19 19 31 31 20 20 22 22 47 47 17 17 45 45 21 21 44 42 44 28 28"

symbols. !@£$%^&*()-=\

"42 2 2 3 3 5 5 6 6 7 7 8 8 9 9 10 10 11 42 11 12 12 13 13 43 43 28 28"

Thanks

Sy

You know, looking at the raw values, they look very much like hexadecimal equivalent ASCII characters. I suspect the flow you have is corrupting things.

Can you capture debug messages direct from this keyboard node? (The input directly from the scanner)

Thats how the data comes in from a USB scanner.

Can you show the complete msg?

Hi E1cid, Sorry for the late reply,

Here is the full message Debug, I have expanded a few drop downs.

See scree shot also.

object

topic: "pi/key"

payload: 11

action: "up"

_msgid: "b02ef24c.7a7f7"

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 10, action: "down", _msgid: "95c6cf8.159593" }

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

10

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

2

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 10, action: "up", _msgid: "c00ab7a6.922e18" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 2, action: "down", _msgid: "c32fd892.310068" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 2, action: "up", _msgid: "8f82ff64.3b191" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 8, action: "down", _msgid: "27b8d18.824952e" }

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

2

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

8

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

8

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

2

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

2

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

2

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 8, action: "up", _msgid: "d8ed9ea8.dcd45" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 2, action: "down", _msgid: "bb6a11f7.6414c" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 2, action: "up", _msgid: "b9accfb4.9d353" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 2, action: "down", _msgid: "83148b04.219388" }

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

2

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

10

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 2, action: "up", _msgid: "8069002f.c8bde" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 10, action: "down", _msgid: "1b931aa9.63ec55" }

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

10

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

4

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 10, action: "up", _msgid: "428dfad5.0f0074" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 4, action: "down", _msgid: "cb28e46c.5e01f8" }

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

4

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

5

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 4, action: "up", _msgid: "8663c72c.730728" }

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

{ topic: "pi/key", payload: 5, action: "down", _msgid: "8c482406.ad43b8" }

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

5

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

8

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

object

topic: "pi/key"

payload: 5

action: "up"

_msgid: "a95e34be.50a328"

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

object

topic: "pi/key"

payload: 8

action: "down"

_msgid: "d8e881d5.959f4"

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

8

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

28

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

object

topic: "pi/key"

payload: 8

action: "up"

_msgid: "1f996dae.a92d72"

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

object

topic: "pi/key"

payload: 28

action: "down"

_msgid: "6dfa0bd6.cdccf4"

6/6/2021, 9:47:25 AMnode: a3ac43c2.91892pi/key : msg.payload : number

28

6/6/2021, 9:47:25 AMnode: 36cafb24.21bde4pi/key : msg : Object

object

topic: "pi/key"

payload: 28

action: "up"

_msgid: "a90e0c22.bef03"

Thanks

Sy

So you are getting one msg for a ‘keypress down’ condition and a copy of that number for a ‘key press up’ condition. (It is in msg.action)

You should filter out one of those two conditions I.e. put a switch node in to only pass the ‘up’ or ‘down’ condition.

The ‘28’ at the end must be an end of code value
It looks like you should subtract 1 from each number and if the result is 10 change it to a 0 and the concatenation the numbers together to get the result.

Hi here is a function to decode the values, this could be simplified if you used the messge action var to filter out the up key releases except value 42 which is the shift key, as it is needed to move from lower to uppercase
try it

[{"id":"7c921235.e58804","type":"function","z":"c74669a0.6a34f8","name":"","func":"var cases = [\"  1234567890-=  qwertyuiop[]  asdfghjkl;'#  zxcvbnm,./\",\n            '  !\"£$%^&*()_+  QWERTYUIOP{}  ASDFGHJKL:@~  ZXCVBNM<>?'],\nkey =\"\",\nupper = 0;\nmsg.payload.split(\" \").forEach((code, index)=>{\n    if(code == 28) return {payload:key};\n    else if(code == 42) upper = upper === 0 ? 1 : 0;\n    else if((upper+index)%2===0) key += cases[upper].substr(code, 1);\n})\nreturn {payload:key};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":4840,"wires":[["1d0c244.c372b5c"]]},{"id":"cd23ea3d.85eff","type":"inject","z":"c74669a0.6a34f8","name":"upper","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"42 30 30 48 48 46 46 32 32 18 18 33 33 34 34 35 35 23 23 36 36 37 37 38 38 49 49 50 50 24 24 25 25 16 16 19 19 31 31 20 20 22 22 47 47 17 17 45 45 21 21 44 42 44 28 28","payloadType":"str","x":130,"y":4860,"wires":[["7c921235.e58804","1d0c244.c372b5c"]]},{"id":"1d0c244.c372b5c","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":4840,"wires":[]},{"id":"a4b7f549.3c5bd","type":"inject","z":"c74669a0.6a34f8","name":"lower","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30 30 48 48 46 46 32 32 18 18 33 33 34 34 35 35 23 23 36 36 37 37 38 38 50 50 49 49 24 24 25 25 16 16 19 19 31 31 20 20 22 22 47 47 17 17 45 45 21 21 44 44 28 28","payloadType":"str","x":120,"y":4780,"wires":[["7c921235.e58804","1d0c244.c372b5c"]]},{"id":"3f4c378b.bb53e","type":"inject","z":"c74669a0.6a34f8","name":"symbols","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"42 2 2 3 3 5 5 6 6 7 7 8 8 9 9 10 10 11 42 11 12 12 13 13 43 43 28 28","payloadType":"str","x":130,"y":4920,"wires":[["7c921235.e58804","1d0c244.c372b5c"]]}]

I think 28 is the enter key.
You may need to adjust lookup strings as this is set for UK local keyboard.

here is code for others to see without importing flow

var cases = ["  1234567890-=  qwertyuiop[]  asdfghjkl;'#  zxcvbnm,./",
             '  !"£$%^&*()_+  QWERTYUIOP{}  ASDFGHJKL:@~  ZXCVBNM<>?'],
key ="",
upper = 0;
msg.payload.split(" ").forEach((code, index)=>{
    if(code == 28) return {payload:key};
    else if(code == 42) upper = upper === 0 ? 1 : 0;
    else if((upper+index)%2===0) key += cases[upper].substr(code, 1);
})
return {payload:key};

This may be a fuller list of characters to include ` ¬ \ | and spacebar

cases = [" `1234567890-=  qwertyuiop[]  asdfghjkl;'# \\zxcvbnm,./     ",
         ' ¬!"£$%^&*()_+  QWERTYUIOP{}  ASDFGHJKL:@~ |ZXCVBNM<>?     '],
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.