Serial Port Nodes over NATS for Node-RED
@rosepetal/node-red-contrib-serialport provides 3 nodes for Node-RED (serial in, serial out and the configuration node they share) that mirror the stock Node-RED serial nodes over NATS: a separate engine, normally a Docker container, opens the physical serial port, and Node-RED only publishes the writes and subscribes to the reads.
Installation
cd ~/.node-red
npm install rosepetal-ai/node-red-contrib-serialportRestart Node-RED afterwards. The package is not published on npmjs.com: the command installs it from its GitHub repository, which is private, so npm needs git credentials with access to it.
The nodes do not open the serial port themselves. They need a NATS server and the serial engine: see Serial engine.
Install script
On a host that runs both Node-RED and the engine, install.sh at the root of a clone of the repository does the whole setup. It needs docker, npm and node, and a running Docker daemon.
./install.sh- It pulls the engine image of the package version, or builds it from
engine/when the pull fails, and tags itrosepetal-serial-engine:followed by the version. - It runs
npm installof the clone in the Node-RED user directory:NODE_RED_DIR, elseNODE_RED_HOME, else~/.node-red. - Unless the
rosepetal-serial-enginecontainer is already running, it starts it if it exists, or launches it from that image with--network host --privilegedand theNATS_URL,SUBJECT_ROOTandSTATUS_HEARTBEAT_MSof its own environment (defaultsnats://localhost:4222,rosepetal.serialand5000).
The container it launches has no restart policy. Unless ROSEPETAL_ENGINE_AUTOSTART=false, Node-RED starts it again when Node-RED starts: see Docker engine.
Requirements
- Node.js >= 18
- A NATS server that both Node-RED and the engine can reach (
nats://localhost:4222by default) - The serial engine on the host that has the serial device: the container built from the
engine/folder of the repository, or another service that implements the NATS channels - Docker, to build and run the engine as a container
Nodes
rosepetal-serial-in and rosepetal-serial-out appear under the network category in the editor. rosepetal-serial-config is a configuration node, created from the Serial Port field of the other two.
| Node | Purpose |
|---|---|
| rosepetal-serial-config | Configuration node: the port and its line settings, how the input is split into messages, the character added to the output, the NATS server and the subject root |
| rosepetal-serial-in | Emit a message for each piece of data read from the serial port, split by the rules of the configuration node |
| rosepetal-serial-out | Write msg.payload to the serial port; msg.baudrate asks for a baud rate change |
Reference
| Page | Contents |
|---|---|
| NATS channels | The four subjects that the nodes and the engine share, with the JSON payload of every message |
Quick start
- Start a NATS server that Node-RED and the engine can reach.
- Build and run the engine on the host that has the serial device: see Serial engine.
- In Node-RED, add a
rosepetal-serial-innode and create arosepetal-serial-confignode from its Serial Port field. If the NATS server is not atnats://localhost:4222, first set its URL under Advanced settings: the port list is asked through it. Choose the port from the list (the engine reports the ports of its host) and set the baud rate and the other line settings. - Wire the node to a debug node and deploy. The status under the node becomes
serial connectedwhen the engine has opened the port, and each line received arrives as one message: by default the input is split on\nand delivered as a string. - To write to the port, add a
rosepetal-serial-outnode that uses the same configuration node and send it a message with the data inmsg.payload.
How it works
Node-RED and the engine only talk through NATS. The subjects share a root, rosepetal.serial by default:
| Subject | Direction | Carries |
|---|---|---|
rosepetal.serial.out | Node-RED to engine | The data to write, from rosepetal-serial-out |
rosepetal.serial.in | Engine to Node-RED | The data read from the port, for rosepetal-serial-in |
rosepetal.serial.control | Both | From Node-RED, the port settings and the baud rate changes; from the engine, the state of the port and its heartbeat |
rosepetal.serial.discovery | Request and reply | The serial ports of the engine's host, for the port list of the editor |
- The configuration node sends its settings to the engine, which opens the port with them and reports its state. The engine repeats the state as a heartbeat, every 5 seconds by default.
- The status under each node shows the NATS connection first and then the state of the port:
serial connectedwhile the engine has it open,serial heartbeat lostwhen the engine has been silent for 15 seconds. - Every write, frame and control message names the port it belongs to, so one engine serves all the ports that the configuration nodes ask for.
- The messages are JSON envelopes with the data in base64, so any NATS client can follow the traffic next to Node-RED, for example with the NATS command line client:
nats sub 'rosepetal.serial.>'The payload of every message is described in NATS channels.
Serial engine
The engine is a Node.js service in the engine/ folder of the repository. It opens the serial devices with the serialport library and implements the NATS channels. Build its image from a clone of the repository:
cd engine
docker build -t rosepetal-serial-engine .Run it on the host that has the serial device:
docker run --rm \
--name rosepetal-serial-engine \
--network host \
--privileged \
-e NATS_URL=nats://localhost:4222 \
-e SUBJECT_ROOT=rosepetal.serial \
-e STATUS_HEARTBEAT_MS=5000 \
rosepetal-serial-engineThe container needs access to the serial device (--privileged is the easiest way) and a NATS server it can reach (--network host lets it use one on localhost). The name matters: rosepetal-serial-engine is the container that the configuration node looks for before it tries to start an engine by itself.
| Variable | Default | Purpose |
|---|---|---|
NATS_URL | nats://localhost:4222 | NATS server |
SUBJECT_ROOT | rosepetal.serial | Root of the subjects. It must match the Subject root of the configuration node; several engines can share a NATS server if each has its own root |
STATUS_HEARTBEAT_MS | 5000 | Interval of the status heartbeat of every port. Keep it well below 15000: the nodes show serial heartbeat lost after 15 seconds without a status |
SERIAL_RECONNECT_MS | 15000 | Wait before the engine opens a port again after an error or after the port closes. It doubles at every failed attempt |
SERIAL_RECONNECT_MAX_MS | 60000 | Longest wait between two attempts |
SERIAL_RECONNECT_JITTER_MS | 500 | Upper limit of the random time added to every wait |
The engine opens a port when it receives the settings of a configuration node for it, and opens it again when the port, the baud rate, the data bits, the parity or the stop bits change. Writes that arrive while the port is not open are queued (up to 1000; beyond that the oldest are dropped) and sent when it opens. A configure message or a write for a port that is not open makes the engine try to open it at once, without waiting for the next attempt.
By default the configuration node also tries to start the engine container from Node-RED, through the Docker command line of the Node-RED host. Docker engine describes what it runs and the environment variables that change or disable it: set ROSEPETAL_ENGINE_IMAGE=rosepetal-serial-engine in the environment of Node-RED to make it use the image built above, or ROSEPETAL_ENGINE_AUTOSTART=false so that Node-RED does not start a container by itself.