Http endpoint from modbus


How to create endpoint from this.

[{"id":"1cb4ba37.d45666","type":"http in","z":"a6893b09.aee2d8","name":"","url":"/setpoint","method":"get","upload":false,"swaggerDoc":"","x":90,"y":660,"wires":[["82cd602c.e8fbb"]]},{"id":"cab61139.cca8b","type":"http response","z":"a6893b09.aee2d8","name":"","statusCode":"","headers":{},"x":1167,"y":661,"wires":[]},{"id":"908f714b.063278","type":"template","z":"a6893b09.aee2d8","name":"","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{{{payload}}}","output":"json","x":1037,"y":661,"wires":[["cab61139.cca8b"]]},{"id":"2b13900a.68e1a8","type":"json","z":"a6893b09.aee2d8","name":"","property":"payload","action":"","pretty":false,"x":907,"y":661,"wires":[["908f714b.063278","d487f215.7234f8"]]},{"id":"7bebe530.f78774","type":"modbus-flex-getter","z":"a6893b09.aee2d8","name":"","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"server":"2a6356bf.4c0d8a","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":700,"y":660,"wires":[[],["2b13900a.68e1a8"]]},{"id":"82cd602c.e8fbb","type":"function","z":"a6893b09.aee2d8","name":"17100-17157","func":"var fc=3;\nvar address=17100;\nvar quantity=57;\n//var slave_ip=msg.payload.slave_ip;\n//msg.slave_ip=\"192.168.1.31\";\n\n\nmsg.payload={\n    value: msg.payload, \n    'fc': fc,\n    'unitid' : 255,\n    'address': address , \n    'quantity': quantity };\n    \n    msg.metadata = {\n\t\t\t//user defined\n\t\t\t'deviceType' : 'Radio Model X',\n\t\t\t'deviceId' : 'x001',\n\t\t\t'units' : 'Celcius',\n\t\t\t}\n\t\t\t\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":660,"wires":[["7bebe530.f78774"]]},{"id":"f7c337a1.07c8e8","type":"inject","z":"a6893b09.aee2d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":720,"wires":[["82cd602c.e8fbb"]]},{"id":"d487f215.7234f8","type":"debug","z":"a6893b09.aee2d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1160,"y":740,"wires":[]},{"id":"2a6356bf.4c0d8a","type":"modbus-client","name":"PLC150","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"10.0.0.150","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

what do you want to achieve with that?

Simple API.
I want send values from modbus to http as json.
From another service read this point.

just give msg.payload of modbus response to http response node. You can get the modbus response in to new function node and save it in to msg.payload.

I trying that but without success

[{"id":"ea2b1f34.82a73","type":"function","z":"af493244.24b478","name":"","func":"msg.payload = msg.payload.data;\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":760,"y":1300,"wires":[["c7426cbe.265c5"]]},{"id":"ca4f6a35.9dfb","type":"function","z":"af493244.24b478","name":"17100-17157","func":"var fc=3;\nvar address=17100;\nvar quantity=57;\n//var slave_ip=msg.payload.slave_ip;\n//msg.slave_ip=\"192.168.1.31\";\n\n\nmsg.payload={\n    value: msg.payload, \n    'fc': fc,\n    'unitid' : 255,\n    'address': address , \n    'quantity': quantity };\n    \n    msg.metadata = {\n\t\t\t//user defined\n\t\t\t'deviceType' : 'Radio Model X',\n\t\t\t'deviceId' : 'x001',\n\t\t\t'units' : 'Celcius',\n\t\t\t}\n\t\t\t\n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":1300,"wires":[["3d1d0353.917a74"]]},{"id":"58c4d225.c9eb04","type":"inject","z":"af493244.24b478","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":1400,"wires":[["ca4f6a35.9dfb"]]},{"id":"a4c5cebc.7e0ae","type":"debug","z":"af493244.24b478","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":1340,"wires":[]},{"id":"3d1d0353.917a74","type":"modbus-flex-getter","z":"af493244.24b478","name":"","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"server":"2a6356bf.4c0d8a","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":560,"y":1300,"wires":[[],["ea2b1f34.82a73","a4c5cebc.7e0ae"]]},{"id":"2c9a4c3e.9107b4","type":"http in","z":"af493244.24b478","name":"","url":"/mod1","method":"get","upload":false,"swaggerDoc":"","x":130,"y":1300,"wires":[["ca4f6a35.9dfb"]]},{"id":"c7426cbe.265c5","type":"template","z":"af493244.24b478","name":"","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<h1>This is the payload: {{payload}} !</h1>\n<p>to test</p>","output":"str","x":940,"y":1300,"wires":[["1659abd0.b26284","45c8a71d.2a262"]]},{"id":"1659abd0.b26284","type":"http response","z":"af493244.24b478","name":"","statusCode":"","headers":{},"x":1100,"y":1300,"wires":[]},{"id":"45c8a71d.2a262","type":"debug","z":"af493244.24b478","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":1340,"wires":[]},{"id":"2a6356bf.4c0d8a","type":"modbus-client","name":"PLC150","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"10.0.0.150","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

Hi Ketpa,

If you add a 'complete msg' Debug node after your Http in node, you will notice that additional information regarding req and res are present. These MUST arrive to your Http out node in order to complete the http request to your endpoint.

image

Now the issue is that the Modbus Flex Getter node doesnt preserve the original msg properties (req and res is lost and the chain to http out broken)

To be fair to the authors of the modbus nodes they do provide a property in order to pass on data from the original msg and that is the value property.

If we try to save the whole msg including the needed req and res to value in order to pass them on to http out .. unfortunately we get an error.

image

image

One workaround is to set and get Flow Context in functions before and after Modbus Flex
(you can read more about Context here)

Test Flow :

[{"id":"ca4f6a35.9dfb","type":"function","z":"af25975e21b17ff4","name":"17100-17157","func":"var fc = 3;\nvar address = 17100;\nvar quantity = 57;\n//var slave_ip=msg.payload.slave_ip;\n//msg.slave_ip=\"192.168.1.31\";\n\nmsg.topic = \"modbus\"\n\n// store original http request in flow context \nflow.set(\"http\", msg)\n\nmsg.metadata = {\n    //user defined\n    'deviceType': 'Radio Model X',\n    'deviceId': 'x001',\n    'units': 'Celcius',\n}\n\nmsg.payload = {\n    // 'value': msg,\n    'fc': fc,\n    'unitid': 1,\n    'address': address,\n    'quantity': quantity\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":200,"wires":[["3d1d0353.917a74"]]},{"id":"3d1d0353.917a74","type":"modbus-flex-getter","z":"af25975e21b17ff4","name":"","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"server":"2a6356bf.4c0d8a","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":600,"y":200,"wires":[["ec2527c2d09a7c4b","e57bb6f40b3532ff"],[]]},{"id":"2c9a4c3e.9107b4","type":"http in","z":"af25975e21b17ff4","name":"","url":"/mod1","method":"get","upload":false,"swaggerDoc":"","x":190,"y":200,"wires":[["ca4f6a35.9dfb","60a6dd0af9879f0b"]]},{"id":"c7426cbe.265c5","type":"template","z":"af25975e21b17ff4","name":"","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<h1>This is the payload:!</h1>\n<pre> {{payload}} </pre>\n<p>to test</p>","output":"str","x":1000,"y":200,"wires":[["1659abd0.b26284","45c8a71d.2a262"]]},{"id":"1659abd0.b26284","type":"http response","z":"af25975e21b17ff4","name":"","statusCode":"","headers":{},"x":1160,"y":200,"wires":[]},{"id":"45c8a71d.2a262","type":"debug","z":"af25975e21b17ff4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":280,"wires":[]},{"id":"60a6dd0af9879f0b","type":"debug","z":"af25975e21b17ff4","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":310,"y":120,"wires":[]},{"id":"ec2527c2d09a7c4b","type":"debug","z":"af25975e21b17ff4","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":120,"wires":[]},{"id":"5e15431f262e1df5","type":"debug","z":"af25975e21b17ff4","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":120,"wires":[]},{"id":"e57bb6f40b3532ff","type":"function","z":"af25975e21b17ff4","name":"","func":"// retrieve original http request from flow context \nlet newMsg = flow.get('http')\nnewMsg.payload = msg.payload\n\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":200,"wires":[["5e15431f262e1df5","c7426cbe.265c5"]]},{"id":"2a6356bf.4c0d8a","type":"modbus-client","name":"PLC150","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"192.168.0.3","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

ps. change the modbus configuration back to your ip

1 Like

Actually forget about the above post because there is an option in Modbus Flex Getter to actually pass the needed req and res properties to http in by enabling Keep Msg Properties

image

new Test Flow :

[{"id":"ca4f6a35.9dfb","type":"function","z":"af25975e21b17ff4","name":"17100-17157","func":"var fc = 3;\nvar address = 17100;\nvar quantity = 57;\n//var slave_ip=msg.payload.slave_ip;\n//msg.slave_ip=\"192.168.1.31\";\n\n\nmsg.metadata = {\n    //user defined\n    'deviceType': 'Radio Model X',\n    'deviceId': 'x001',\n    'units': 'Celcius',\n}\n\nmsg.payload = {\n    // 'value': \"test\",\n    'fc': fc,\n    'unitid': 1,\n    'address': address,\n    'quantity': quantity\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":200,"wires":[["3d1d0353.917a74"]]},{"id":"3d1d0353.917a74","type":"modbus-flex-getter","z":"af25975e21b17ff4","name":"","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"server":"2a6356bf.4c0d8a","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":true,"x":600,"y":200,"wires":[["ec2527c2d09a7c4b","c7426cbe.265c5"],[]]},{"id":"2c9a4c3e.9107b4","type":"http in","z":"af25975e21b17ff4","name":"","url":"/mod1","method":"get","upload":false,"swaggerDoc":"","x":190,"y":200,"wires":[["ca4f6a35.9dfb","60a6dd0af9879f0b"]]},{"id":"c7426cbe.265c5","type":"template","z":"af25975e21b17ff4","name":"","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<h1>This is the payload:!</h1>\n<pre> {{payload}} </pre>\n<p>to test</p>","output":"str","x":860,"y":180,"wires":[["1659abd0.b26284","45c8a71d.2a262"]]},{"id":"1659abd0.b26284","type":"http response","z":"af25975e21b17ff4","name":"","statusCode":"","headers":{},"x":1030,"y":140,"wires":[]},{"id":"45c8a71d.2a262","type":"debug","z":"af25975e21b17ff4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":220,"wires":[]},{"id":"60a6dd0af9879f0b","type":"debug","z":"af25975e21b17ff4","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":310,"y":120,"wires":[]},{"id":"ec2527c2d09a7c4b","type":"debug","z":"af25975e21b17ff4","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":120,"wires":[]},{"id":"2a6356bf.4c0d8a","type":"modbus-client","name":"PLC150","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"tcpHost":"192.168.0.3","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]
1 Like

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