How to add a serial device to use in node-red

  • Hey there,

    I successfully installed node-red and have it working. I want to build a flow the connects to my ESP gateway and relays all the information to a serial port without any logic. To my thought it should look like this:
    But as you can see, the serial port does not connect. Do I have to add the serial device manually before using it in node-red?
    And btw, how can I copy this one line of code with the complete flow?
    update: found the way to export the whole flow. Here the flow for the upper line:

    [{"id":"6172181f.bb6c88","type":"tcp in","z":"4e8861e9.29fed8","name":"","server":"client","host":"","port":"5003","datamode":"stream","datatype":"buffer","newline":"","topic":"","base64":false,"x":259.5,"y":148,"wires":[["1c4e836e.cda87d"]]},{"id":"1c4e836e.cda87d","type":"serial out","z":"4e8861e9.29fed8","name":"","serial":"8c0eff27.9accc","x":581.5,"y":147,"wires":[]},{"id":"8c0eff27.9accc","type":"serial-port","z":"4e8861e9.29fed8","serialport":"/dev/ttyMySensors-nodered","serialbaud":"57600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false}]

  • Hardware Contributor

    Does the use, you run node-red have permission to use the serial port?

    Maybe you can find a hint in you nodered-Logfile?

  • well that might be... it is not existing upto now. I thought node-red would create a device like socat is doing (the only programm connecting to serial devices I used so far). But seems not to be that way. How do I create a new serial device to then connect node-red to?

  • Hardware Contributor

    A serial device is usually automatically created by the OS when it detects serial hardware. On most linux systems they can be found in /dev/serial/....

    It seems, you want to create a serial device without hardware?

    You can use a pseudo-teletype on linux like /dev/ptyp5

    What is the exact use case for your need of a serial device?

  • I use a controller not capable of using ethernet gateways. By having a serial device connected to an ethernet stream via node-red would circumvent this limitation for me and also in future make it possible to use multiple controller at once (connecting additional controllers also to node-red and filter the messages).
    I tried socat some time ago, which is stated to have exactly this functionality: connecting a device to a tcp stream. But it came out that this tunnel is not stable and disconnects from time to time, causing a disconnect of my controller from MyS network.

  • Admin


    You have to throw socat into the equation. It can mimic a serial port with two endpoints. One end you connect to in node-red, while the other you connect to from the controller.

    I use the following line to start socat:

    socat PTY,link=/dev/ttyS81,mode=666,group=dialout,raw PTY,link=/dev/ttyUSB21,mode=666,group=dialout,raw &

    this sends the command to the background (&). now you should be able to connect to /dev/ttyS81 in node-red, and use /dev/ttyUSB21 in your controller

    (they are named like this, because some controllers wants their serial ports to be ttyUSBxx)

    Now that you have thrown node-red into the mix, you can do all kinds of cool things, like change message content on the fly, between your controller, and the GW. (f.ex. I use node-red to invert lock state to domoticz, as it's the opposite of what is documented in mysensors). Or you can use node-red to inject test messages into the stream to your controller

    0_1471882338312_Screenshot from 2016-08-22 18-11-40.png

  • @tbowmo @FotoFieber thank you both for your help. Hope a socat with 2 serial devices is more stable then a network based socat tunnel. That gave me disconnects after max 1-2 days. Now I will automate socat, node-red and all the other stuff, but it's too late to do that today 😪

  • ok things are running ok, but not perfect. I put all things into init.d scripts and works. But when not transfering any data over long periods (>1 day) connection seems to crash. I don't see new messages in my ttyUSB interface and in the debug node. When changing something (position of a node) and redeploying it works again. I will try with continous messages being sent and report. Does the ESP8266 somehow goes to sleep when nithing is happening and node-red loses connection then?

  • I just spent an hour trying to figure this out and I'm not much wiser! I have a serial GW on a RPI-2. Domoticz is connected to ttyUSB0.

    sudo socat PTY,link=/dev/ttySOCAT1,mode=666,group=dialout,raw PTY,link=/dev/ttyUSB0,mode=666,group=dialout,raw &

    so I have the socat link but I want to be able to run mycontroller just to test OTA on my anarduino that I flashed dualoptiboot on.

    I thought I might be able to allow mycontroller to inject OTA messages onto the mysensors network via ttySOCAT1.

    What have I misunderstood?

  • Hardware Contributor

    I have written a simple serial/mqtt gateway for node.js to solve the problem of docker with serial devices. I then use MQTT Input and Output nodes in node-red. (This solution requires an mqtt-broker.)

    var PORT = "/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0";
    var PUB_TOPIC = "mysensors/1/out";
    var SUB_TOPIC = "mysensors/1/in";
    var mqtt    = require('mqtt');
    var client  = mqtt.connect('mqtt://localhost');
    client.on('connect', function () {
    var serialport = require("serialport");
    var SerialPort = serialport;
    var serialPort = new SerialPort(PORT, {
      baudrate: 115200,
      parser: serialport.parsers.readline("\n")
    serialPort.on("open", function () {
      serialPort.on('data', function(data) {
    client.on('message', function (topic, message) {
      // message is Buffer 

  • I have added

    socat PTY,link=/dev/ttyS81,mode=666,group=dialout,raw PTY,link=/dev/ttyUSB026,mode=666,group=dialout,raw &

    to /etc/rc.local

    pi@Host-I:~ $ ls -l /dev/ttyU*
    lrwxrwxrwx 1 root root 10 Mar 13 16:27 /dev/ttyUSB020 -> /dev/pts/1
    lrwxrwxrwx 1 root root 10 Mar 13 16:27 /dev/ttyUSB026 -> /dev/pts/3

    On /dev/ttyUSB020 I have my NRF24 module connected and Domoticz and use it with no issues. I want to bring Nod-Red into the equation so I created /dev/ttyUSB026

    for some reason I cant connect Node-Red to /dev/ttyUSB020
    Node-Red keeps saying
    Error: Permission denied, cannot open /dev/ttyUSB020

  • Admin

    @hiddenuser I know this is really late but I just ran into this problem and thought I'd post the solution in case any other people run into it. Thanks go to @mfalkvidd for providing the solution. You need to add this line:

    sudo chmod 666 /dev/ttyUSB020

    to /etc/rc.local

    Here is a link on how to do that (and for more info):

  • @petewill Thanks Pete!!! .. I shall give it a try.

  • Admin

    @petewill @hiddenuser

    The correct method is to add the user running node-red to the correct group on the system.. Probably the dialout group.

    ls -l /dev/ttyUSB*

    And note the group / user that is allowed to use the port. (as mentioned earlier, the group is probably dialout)

    then issue the following command:
    sudo usermod -a -G dialout <userName that node-red runs under>

    then restart your rpi/computer/(whatever you are running on :))

    Now you should have access to the ttyUSB20.

    (I do not have access to a linux box right now, so I can not make screenshots / shell dumps of actual displayed data.. Can do this later today, when I'm at my private machine)

  • Admin

    @tbowmo Thanks! I just had a chance to go through this. I ran the command and looks like root has access?

    pi@rpi:~ $ lrwxrwxrwx 1 root root 10 Nov  8 21:07 /dev/ttyUSB020 -> /dev/pts/1

    Also it appears that my user that I think is running node-red (still called pi) already has access to the dialout group?

    pi@rpi:~ $ awk -F':' '/dialout/{print $4}' /etc/group

  • Admin


    when you run socat, you specify access rights with the mode option, which is set to 666.. So USB20 should already be read/writeable by all..

    The original problem by @hiddenuser was the "real" serialport for the arduino, which should help with the adding the user to dialout group.

    Normally you have to add your own user to the dialout group, as it is not added per default..

  • Admin

    @tbowmo Ah, ok. That makes sense. My USB20 was created by the MySensors Raspberry Pi Gateway so that's where the disconnect is here. Sorry for clogging up this thread...

  • I thought that it should be quite easy to use Node-red but I struggle with it and especially the serial port.

    I have created a passive node:

    // Enable debug prints
    #define MY_DEBUG
    // Enable passive mode
    #define MY_PASSIVE_NODE
    // Passive mode requires static node ID
    #define MY_NODE_ID 0
    // Enable and select radio type attached
    // #define MY_RADIO_NRF24
    //#define MY_RF24_PA_LEVEL RF24_PA_HIGH
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    #include <MySensors.h>
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_BINARY 2
    // Initialize general message
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgBinary(CHILD_ID_BINARY, V_STATUS);
    int counter = 0;
    float temperature = 0;
    float humidity = 0;
    int binary = 0;
    void setup()
    void presentation()
      // Send the sketch version information to the gateway and controller
      sendSketchInfo("Passive node", "1.0");
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_BINARY, S_BINARY);
    void loop()
      // generate some random data
      counter ++;
      if (counter == 20 ){
        temperature = 0;
        humidity = 0;
        counter = 0;
        temperature = 25.0 + random(0,30)/10.0;
        humidity = 55.0 + random(-20,20);
      if (binary == 0){
        binary = 1;
      }else {
        binary = 0;

    To generate some data and I get the data in to Node-Red and can send the data to Influx/Grafana but the output to a socat serial port is strange. I have tested and I got the best result when I use a baudrate of 9600.

    When I look to the output data in a terminal a lot of messages are missing or has the format of:


    Have the most simple flow at the moment:

    ![](image url)0_1510606542661_Screenshot from 2017-11-13 21-54-42.png

    with this properties 0_1510606605539_Screenshot from 2017-11-13 21-56-13.png

    sudo socat PTY,link=/dev/ttyS81,mode=666,group=dialout,raw PTY,link=/dev/ttyUSB026,mode=666,group=dialouraw

    How should I configure the serial output?

  • Admin

    looks like your socat line is wrong..

    It should be

    sudo socat PTY,link=/dev/ttyS81,mode=666,group=dialout,raw PTY,link=/dev/ttyUSB026,mode=666,group=dialout,raw

    Also, try and connect node-red to ttyS81 instead, as it's the "source". (probably doesn't matter much, but it makes more sense to me 🙂 )

  • Hi,

    I had the same problem: I have a serial GW on the PI that outputs data on /tty/USB20, and is used by Domoticz.

    I want to test MQTT via node-red, so I need to read this serial port from node-red.

    I tried:

    sudo socat PTY,link=/dev/ttyS81,mode=666,group=dialout,raw PTY,link=/dev/ttyUSB20,mode=666,group=dialout,raw

    But nothing appears on ttyS81: I tried with

    sudo screen /dev/ttyS81 115200

    and of course on node-red, I set up a node that reads from ttyS81 and outputs to debug: the serial port is opened successfully, but nothing appears on debug.

    If I use

    sudo screen /dev/ttyUSB20 115200

    I can see some data coming

    I'm no expert with socat (I learned its existence via this forum post), so any help is appreciated.


  • Admin


    is /dev/ttyUSB20 your gateway device?

    What I have done, is the following:

    /dev/tty<gateway> <- nodered is connected to this one directly

    then I have socat create a dummy set of serial ports
    /dev/ttyS81 -> /dev/ttyUSB21

    Nodered connects to /dev/ttyS81 and domoticz connects to /dev/ttyUSB21

    Now nodered is in the middle, between the gateway and your domoticz instance. You then need to create a flow in nodered, that moves data from your gateway, to domoticz, and vice versa. Start with a simple flow, that just connects input serial node, to an output serial node, and then build up on that.

    Haven't got access to my nodered installation at the moment, so can't make any screenshots..

  • I tried (/dev/ttyUSB20 being the port used and created by the MySensors Gateway):

    sudo socat PTY,link=/dev/ttyUSB21,mode=666,group=dialout,raw PTY,link=/dev/ttyUSB20,mode=666,group=dialout,raw

    (I found that Domoticz needs the port to be ttyUSB*).

    I connected Domoticz to a MySensors Gateway USB on port /dev/ttyUSB21
    And Node-RED to /dev/ttyUSB20

    But nothing worked: Domoticz did not see new data arrive and Node-RED didnt receive anything. I tried rebooting (and redo the socat dance), nothing happened.

    I suppose my non-understanding of socat is the main cause of this failure.

    So, I took the plunge and moved to a MySensors MQTT gateway. I had to reflash my sensors with a fixed unit_id (not sure if it's still a requirement), and voila.

    Now I can play with Node-RED and try other controllers in parallel of Domoticz.

    Thanks for the help.

  • here is my solution;

    add a new udev rules file for permissions:

    sudoedit /etc/udev/rules.d/50-usb-permissions.rules

    paste the following in our new rules file:
    (it gives all permission to any USB* and ACM* devices)


    that's it. this will give you all the permissions to all usb devices on every boot. also you can reload udev instead of rebooting.

    udevadm control --reload-rules

    *** please note that this gives permissions to all USB* and ACM* devices.

    if you're concerned about security you can add the following so that only the mentioned device will be accessable:

    ATTRS{idVendor}=="XXXX", ATTRS{idProduct}=="YYYY"

    for example, in my case (output from dmesg):

    [ 2654.037338] usb 1-1: New USB device found, idVendor=1a86, idProduct=7523
    [ 2654.037344] usb 1-1: New USB device strings: Mfr=0, Product=2, SerialNumber=0
    [ 2654.037347] usb 1-1: Product: USB2.0-Serial
    [ 2654.038531] usb 1-1: ch341-uart converter now attached to ttyUSB0

    so, for me it's:

    ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523"

Log in to reply

Suggested Topics

  • 9
  • 2
  • 5
  • 9
  • 2
  • 6