How to: Use Serial with Node-RED
Guide on connecting to a serial device, configuring a serial port in node-RED, and an introduction to node-RED library flows
This article will describe how to read and write serial data from Tulip Edge devices using Node-RED. It will cover how to connect to the different serial ports on each type of Edge device, and two Tulip Node-RED library flows that use serial communication.
What you will need is:
An Edge IO or Edge MC registered to your Tulip accoun
A device that will communicate over serial to your Edge device
1. Hardware Setup: Connect to serial device
First, make sure that you physically connect your serial peripheral to the Edge device.
Optional: If you would like to first test your Node-RED flow by communicating with a host device (ie your laptop, another Tulip Edge device, etc.), you can do that by using RS-232 with a null modem, or USB-UART cables with crossed-over TX/RX lines (do not connect power).
Examples of programs you can use on the host device to communicate with the Edge device are:
- screen (MacOS): A command-line utility for serial communication.
- PuTTY (Windows/Linux): A free terminal emulator that can act as a serial console.
- Node-RED: Create a complementary Node-RED flow in an instance of Node-RED that’s running on the host device.
2. Node-RED Setup: Configure serial port
We will be using the nodes in the
node-red-contrib-serialport package for serial communications in Node-RED. If on a Tulip Edge device OS44+, this package is preinstalled. Otherwise, you can add the nodes to the palette using the Node-RED editor as described here.
The serial nodes will appear under the
To read serial inputs, drag and drop the
serial in node. To write serial outputs, drag and drop the
serial out node. On either node, to configure the serial port, click the edit button for the “Serial Port” property:
You’ll then fill in the following properties:
- Serial Port: The devpath of the serial port (ex: /dev/ttyS0). This depends which Edge device and which physical serial port you are connecting to (see articles for Edge IO or Edge MC). Available paths can be viewed by clicking the search button as shown below.
Note: There is currently no support for hot-plugging USB devices in Node-RED. This means that the USB device must be plugged in when the device boots for Node-RED to have access to it.
- Settings: The baud rate and other settings should match the baud rate of the serial device you are communicating with.
- Input: Choose when the node will output a message. Options include waiting for a split character (such as a line break, comma, or space), waiting for every
ncharacters if the output is fixed-length, or you can set a timeout to get an output every
- Output: Add character the output message (for example, add back the line break).
You can see that you are connected to a valid serial port if, after deploying, the node status is “connected”:
3. Node-RED Library Flows: Communicate with Tulip
Tulip provides several library flows to read serial inputs and write serial outputs on Tulip Edge devices using Node-RED. See Importing Tulip Node-RED Flows for information on how to import a Tulip library flow.
To write a serial output to a Tulip Edge device from a Tulip app, you can use the
serial_http_endpoint flow and set up an HTTP connector on your Tulip instance running on the Edge device connector host. You’ll need to add a connector function to this connector that to sends POST requests to the
/serial HTTP endpoint, and call that connector function from the app. Learn more about writing values to Node-RED using the Connector Host in Communicate with Machines using Edge MC's Connector Host & Node-RED .
To read serial input on a Tulip Edge device and send to Tulip, you can use either the Tables API (to write the data to a table) or Machine API (to write the data to a machine attribute). The library flow
serial_to_tulip_table is an example of how to write newline-delimited serial inputs to a tulip table. You’ll need to configure the serial node, and the two Tulip tables nodes, with the correct settings for your use case. Learn more about how to use the Tulip Tables API node in Using Node-RED with the Tulip API