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:
    0_1471719502320_node-red.PNG
    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":"192.168.38.19","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

    @Anduril

    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 :sleepy:



  • 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 () {
      client.subscribe(SUB_TOPIC);
    })
    
    var serialport = require("serialport");
    var SerialPort = serialport;
    
    
    var serialPort = new SerialPort(PORT, {
      baudrate: 115200,
      parser: serialport.parsers.readline("\n")
    });
    
    serialPort.on("open", function () {
      console.log('open');
      serialPort.on('data', function(data) {
        console.log(data);
        client.publish(PUB_TOPIC,data);
      });
    });
    
    client.on('message', function (topic, message) {
      // message is Buffer 
      console.log(message.toString());
      serialPort.write(message.toString());
    })
    


  • 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


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.