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


  • 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): https://www.raspberrypi.org/documentation/linux/usage/rc-local.md



  • @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.

    try
    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
    pi
    

  • Admin

    @petewill

    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
    #define MY_GATEWAY_SERIAL
    
    // Enable passive mode
    #define MY_PASSIVE_NODE
    #define MY_REPEATER_FEATURE
    
    // 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;
      }
      else
      {
        temperature = 25.0 + random(0,30)/10.0;
        humidity = 55.0 + random(-20,20);
      }
    /*
      if (binary == 0){
        send(msgBinary.set(binary,2));
        binary = 1;
      }else {
        send(msgBinary.set(binary,2));
        binary = 0;
    
      }
    */
      send(msgTemp.set(temperature,2));
      send(msgHum.set(humidity,2));
      wait(1000);
    }
    

    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:

    0;1;1;0;0;26.00^J^M0;0;1;0;1;67.00^J^M0;1;1;0;0;27.40^J^M0
    

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


Log in to reply
 

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