Flow to Request Data from a Serial Device

Estimated reading time: 1 minute

This example shows how to request data from a serial device and output the requested data to the debug console in Flows.

If you scroll to the bottom of the steps, you can copy the JSON for the nodes described in the steps and import them into Flows.

Steps for how to request and output data from a serial device:

  1. Create an inject node, a function node, and a serial out node and connect them in that order.
  2. Double-click on the inject node and change the payload to string. Write the string that will request the data that you want from the device. In this example the string is _F _for the temperature in Fahrenheit. Click Done.

  3. Double-click on the function node and paste the following code into the node:

    msg.payload = msg.payload+"\r"; // Adds a carriage return to the string so the device can read it.
    return msg;
    

    Click Done.

  4. Double-click on the serial out node and select the serial port of your device. Click Done.
  5. Add a serial in node and a debug node connected together in a separate section of your flow.
  6. Double-click on the serial in node and select the serial port for your device. Click Done.
  7. Ensure that your flow looks like the picture below and click Save.

  8. Click the tab to the left of the inject node to have the Flows console display the data that you are requesting from the device.

JSON Nodes

Import the JSON below to create the nodes as described in the steps above. See Export and Import Flows for more information on importing JSON into a flow.

[{"id":"9a20ff26.7d872","type":"serial in","z":"2fab4826.31c128","name":"","serial":"3c5c7301.41f03c","x":160,"y":60,"wires":[["b24d0af9.9b6728"]]},{"id":"b24d0af9.9b6728","type":"debug","z":"2fab4826.31c128","name":"","active":true,"console":"false","complete":"false","x":320,"y":60,"wires":[]},{"id":"fb71f851.b01738","type":"function","z":"2fab4826.31c128","name":"","func":"msg.payload = msg.payload+\"\\r\";\nreturn msg;","outputs":1,"noerr":0,"x":245,"y":180,"wires":[["b785d425.84a818"]]},{"id":"50efdde5.3c1b04","type":"inject","z":"2fab4826.31c128","name":"","topic":"","payload":"ENQ","payloadType":"str","repeat":"","crontab":"","once":false,"x":140,"y":180,"wires":[["fb71f851.b01738"]]},{"id":"b785d425.84a818","type":"serial out","z":"2fab4826.31c128","name":"","serial":"3c5c7301.41f03c","x":360,"y":180,"wires":[]},{"id":"3c5c7301.41f03c","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false}]