Muse Automator: send strings to TCP device example
Table of Contents
Questions:
How do you send strings to a device using a TCP connection in Muse Automator?
Answer:
The follow example will show how to send string to an IP Device using both Change Nodes and Function Nodes which connect to a TCP Request Node.
The TCP Server in this example is mainly to provide some feedback for the example strings that are sent but it does show the basic setup of the TCP IN and TCP REPLY Nodes as well.
Import the snippet below into Muse Automator to see the example.
[
{
"id": "c83664d50953a4ab",
"type": "tab",
"label": "Send Strings to IP Devices",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ee1c19d0938067f8",
"type": "group",
"z": "c83664d50953a4ab",
"name": "TCP Server to get responses from example strings",
"style": {
"label": true
},
"nodes": [
"29c698b7d1f250af",
"b337c2dab3887e5c",
"6fdbf4a4bf36e923",
"bd4ff2b880a0f1c8",
"444e26e60cdbdd54",
"5faa309f653ce0ec",
"4f641fdcca8bfa0b",
"3ebe96b2f42147c9",
"fb1250a5ec7dbffd",
"8014b99fbe2ab92d"
],
"x": 34,
"y": 659,
"w": 1412,
"h": 182
},
{
"id": "00e93950fb3ab863",
"type": "group",
"z": "c83664d50953a4ab",
"name": "Example of sending strings over a TCP connecting using the TCP Request Node",
"style": {
"label": true
},
"nodes": [
"6f8b782623bab3ae",
"59f00fd2d46bfb5c",
"e8f2098a83fd528e",
"1e4b88bdb162d98e",
"4dbd00b33cbee359",
"7d4ad474753e8473",
"69beadef8123bee8",
"2511ec1a9f8e890c",
"a34f2d53e104564d"
],
"x": 28,
"y": 53,
"w": 1438,
"h": 534
},
{
"id": "69beadef8123bee8",
"type": "group",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "Using Change Node to build strings",
"style": {
"label": true
},
"nodes": [
"b112f9e455d6367b",
"19bba7669ead84f0",
"84466faea4687be5",
"f2818c80a2b92cab",
"133c68a0f0bfc536",
"bcc736893c69e62e"
],
"x": 54,
"y": 79,
"w": 352,
"h": 162
},
{
"id": "2511ec1a9f8e890c",
"type": "group",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "Using Function Node to build strings",
"style": {
"label": true
},
"nodes": [
"63d02b8f6f4e8208",
"b6e80b68b6849ac9",
"d7a988a81ce42830",
"96f83f55ef8cd596",
"66253b33a24e0866",
"41f0f68b2b723304"
],
"x": 54,
"y": 279,
"w": 372,
"h": 162
},
{
"id": "a34f2d53e104564d",
"type": "group",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "Keep Alive",
"style": {
"label": true
},
"nodes": [
"885d4d2a9de046da",
"44ad884a969cc37a"
],
"x": 54,
"y": 479,
"w": 372,
"h": 82
},
{
"id": "29c698b7d1f250af",
"type": "tcp in",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "TCP SERVER",
"server": "server",
"host": "",
"port": "1234",
"datamode": "stream",
"datatype": "buffer",
"newline": "",
"topic": "",
"trim": false,
"base64": false,
"tls": "",
"x": 120,
"y": 760,
"wires": [
[
"fb1250a5ec7dbffd"
]
]
},
{
"id": "b337c2dab3887e5c",
"type": "function",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "toLowerCase",
"func": "msg.payload = msg.payload.toLowerCase()\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 490,
"y": 760,
"wires": [
[
"444e26e60cdbdd54"
]
]
},
{
"id": "6fdbf4a4bf36e923",
"type": "template",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "Reply with TCP received",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "TCP Server just received the following message: {{payload}}\n",
"output": "str",
"x": 900,
"y": 800,
"wires": [
[
"8014b99fbe2ab92d"
]
]
},
{
"id": "bd4ff2b880a0f1c8",
"type": "tcp out",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "TCP Reply",
"host": "",
"port": "",
"beserver": "reply",
"base64": false,
"end": false,
"tls": "",
"x": 1360,
"y": 760,
"wires": []
},
{
"id": "444e26e60cdbdd54",
"type": "switch",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "cont",
"v": "hello",
"vt": "str"
},
{
"t": "cont",
"v": "how about",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 650,
"y": 760,
"wires": [
[
"5faa309f653ce0ec"
],
[
"4f641fdcca8bfa0b"
],
[
"6fdbf4a4bf36e923"
]
]
},
{
"id": "5faa309f653ce0ec",
"type": "template",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "Reply with \"Shall we play\"",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "Shall we play a game?\n",
"output": "str",
"x": 910,
"y": 720,
"wires": [
[
"8014b99fbe2ab92d"
]
]
},
{
"id": "4f641fdcca8bfa0b",
"type": "template",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "Reply with WOULDN'T YOU PREFER",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "WOULDN'T YOU PREFER A NICE GAME OF CHESS?\n",
"output": "str",
"x": 930,
"y": 760,
"wires": [
[
"8014b99fbe2ab92d"
]
]
},
{
"id": "b112f9e455d6367b",
"type": "inject",
"z": "c83664d50953a4ab",
"g": "69beadef8123bee8",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 120,
"wires": [
[
"19bba7669ead84f0"
]
]
},
{
"id": "19bba7669ead84f0",
"type": "change",
"z": "c83664d50953a4ab",
"g": "69beadef8123bee8",
"name": "Hello",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "Hello",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 310,
"y": 120,
"wires": [
[
"6f8b782623bab3ae"
]
]
},
{
"id": "6f8b782623bab3ae",
"type": "tcp request",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "TCP Request to localhost:1234",
"server": "localhost",
"port": "1234",
"out": "sit",
"ret": "buffer",
"splitc": " ",
"newline": "",
"trim": false,
"tls": "",
"x": 640,
"y": 240,
"wires": [
[
"e8f2098a83fd528e"
]
]
},
{
"id": "59f00fd2d46bfb5c",
"type": "debug",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "Shall We Debug",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1300,
"y": 200,
"wires": []
},
{
"id": "3ebe96b2f42147c9",
"type": "debug",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "TCP Server Received",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 510,
"y": 700,
"wires": []
},
{
"id": "fb1250a5ec7dbffd",
"type": "function",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "bufferToString",
"func": "msg.payload = msg.payload.toString();\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 310,
"y": 760,
"wires": [
[
"b337c2dab3887e5c",
"3ebe96b2f42147c9"
]
]
},
{
"id": "e8f2098a83fd528e",
"type": "function",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "bufferToString",
"func": "msg.payload = msg.payload.toString();\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 870,
"y": 240,
"wires": [
[
"1e4b88bdb162d98e"
]
]
},
{
"id": "8014b99fbe2ab92d",
"type": "function",
"z": "c83664d50953a4ab",
"g": "ee1c19d0938067f8",
"name": "Add CR",
"func": "msg.payload = Buffer.from(msg.payload+\"\\r\");\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1190,
"y": 760,
"wires": [
[
"bd4ff2b880a0f1c8"
]
]
},
{
"id": "f2818c80a2b92cab",
"type": "inject",
"z": "c83664d50953a4ab",
"g": "69beadef8123bee8",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 160,
"wires": [
[
"84466faea4687be5"
]
]
},
{
"id": "84466faea4687be5",
"type": "change",
"z": "c83664d50953a4ab",
"g": "69beadef8123bee8",
"name": "How About",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "How about Global Thermonuclear War?",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 320,
"y": 160,
"wires": [
[
"6f8b782623bab3ae"
]
]
},
{
"id": "133c68a0f0bfc536",
"type": "inject",
"z": "c83664d50953a4ab",
"g": "69beadef8123bee8",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 200,
"wires": [
[
"bcc736893c69e62e"
]
]
},
{
"id": "bcc736893c69e62e",
"type": "change",
"z": "c83664d50953a4ab",
"g": "69beadef8123bee8",
"name": "Any Other",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "Any Other Message",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 320,
"y": 200,
"wires": [
[
"6f8b782623bab3ae"
]
]
},
{
"id": "b6e80b68b6849ac9",
"type": "function",
"z": "c83664d50953a4ab",
"g": "2511ec1a9f8e890c",
"name": "Hello",
"func": "msg.payload = Buffer.from(\"Hello\"+\"\\r\"+\"\\n\");\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 330,
"y": 320,
"wires": [
[
"6f8b782623bab3ae"
]
]
},
{
"id": "96f83f55ef8cd596",
"type": "function",
"z": "c83664d50953a4ab",
"g": "2511ec1a9f8e890c",
"name": "How About",
"func": "msg.payload = Buffer.from(\"How about Global Thermonuclear War?\"+\"\\r\"+\"\\n\");\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 340,
"y": 360,
"wires": [
[
"6f8b782623bab3ae"
]
]
},
{
"id": "41f0f68b2b723304",
"type": "function",
"z": "c83664d50953a4ab",
"g": "2511ec1a9f8e890c",
"name": "Any Other",
"func": "msg.payload = Buffer.from(\"Any Other Message\"+\"\\r\"+\"\\n\");\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 340,
"y": 400,
"wires": [
[
"6f8b782623bab3ae"
]
]
},
{
"id": "63d02b8f6f4e8208",
"type": "inject",
"z": "c83664d50953a4ab",
"g": "2511ec1a9f8e890c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 320,
"wires": [
[
"b6e80b68b6849ac9"
]
]
},
{
"id": "d7a988a81ce42830",
"type": "inject",
"z": "c83664d50953a4ab",
"g": "2511ec1a9f8e890c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 360,
"wires": [
[
"96f83f55ef8cd596"
]
]
},
{
"id": "66253b33a24e0866",
"type": "inject",
"z": "c83664d50953a4ab",
"g": "2511ec1a9f8e890c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 400,
"wires": [
[
"41f0f68b2b723304"
]
]
},
{
"id": "1e4b88bdb162d98e",
"type": "switch",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "cont",
"v": "Shall we play a game?",
"vt": "str"
},
{
"t": "cont",
"v": "WOULDN'T YOU PREFER A NICE GAME OF CHESS?",
"vt": "str"
},
{
"t": "cont",
"v": "TCP Server just received the following message:",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 1030,
"y": 240,
"wires": [
[
"59f00fd2d46bfb5c"
],
[
"4dbd00b33cbee359"
],
[
"7d4ad474753e8473"
]
]
},
{
"id": "4dbd00b33cbee359",
"type": "debug",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "WOULDN'T YOU DEBUG",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1320,
"y": 240,
"wires": []
},
{
"id": "7d4ad474753e8473",
"type": "debug",
"z": "c83664d50953a4ab",
"g": "00e93950fb3ab863",
"name": "Everything Else Debug",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1320,
"y": 280,
"wires": []
},
{
"id": "885d4d2a9de046da",
"type": "inject",
"z": "c83664d50953a4ab",
"g": "a34f2d53e104564d",
"name": "Keep Alive",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "30",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 170,
"y": 520,
"wires": [
[
"44ad884a969cc37a"
]
]
},
{
"id": "44ad884a969cc37a",
"type": "change",
"z": "c83664d50953a4ab",
"g": "a34f2d53e104564d",
"name": "Keep Alive",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "Keep Alive",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 340,
"y": 520,
"wires": [
[
"6f8b782623bab3ae"
]
]
}
]
Table of Contents