Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Controllers
  3. Node-RED
  4. How to add a serial device to use in node-red

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

Scheduled Pinned Locked Moved Node-RED
23 Posts 9 Posters 27.7k Views 9 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AndurilA Offline
    AndurilA Offline
    Anduril
    wrote on last edited by
    #5

    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.

    1 Reply Last reply
    0
    • tbowmoT Offline
      tbowmoT Offline
      tbowmo
      Admin
      wrote on last edited by
      #6

      @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

      1 Reply Last reply
      0
      • AndurilA Offline
        AndurilA Offline
        Anduril
        wrote on last edited by
        #7

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

        1 Reply Last reply
        0
        • AndurilA Offline
          AndurilA Offline
          Anduril
          wrote on last edited by
          #8

          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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shabba
            wrote on last edited by
            #9

            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?

            1 Reply Last reply
            0
            • FotoFieberF Offline
              FotoFieberF Offline
              FotoFieber
              Hardware Contributor
              wrote on last edited by FotoFieber
              #10

              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());
              })
              
              1 Reply Last reply
              0
              • H Offline
                H Offline
                hiddenuser
                wrote on last edited by hiddenuser
                #11

                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

                petewillP 1 Reply Last reply
                0
                • H hiddenuser

                  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

                  petewillP Offline
                  petewillP Offline
                  petewill
                  Admin
                  wrote on last edited by
                  #12

                  @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

                  My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                  H 1 Reply Last reply
                  1
                  • petewillP petewill

                    @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

                    H Offline
                    H Offline
                    hiddenuser
                    wrote on last edited by
                    #13

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

                    1 Reply Last reply
                    0
                    • tbowmoT Offline
                      tbowmoT Offline
                      tbowmo
                      Admin
                      wrote on last edited by
                      #14

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

                      petewillP 1 Reply Last reply
                      0
                      • tbowmoT tbowmo

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

                        petewillP Offline
                        petewillP Offline
                        petewill
                        Admin
                        wrote on last edited by
                        #15

                        @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
                        

                        My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                        1 Reply Last reply
                        0
                        • tbowmoT Offline
                          tbowmoT Offline
                          tbowmo
                          Admin
                          wrote on last edited by
                          #16

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

                          petewillP 1 Reply Last reply
                          0
                          • tbowmoT tbowmo

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

                            petewillP Offline
                            petewillP Offline
                            petewill
                            Admin
                            wrote on last edited by
                            #17

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

                            My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              smilvert
                              wrote on last edited by smilvert
                              #18

                              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?

                              1 Reply Last reply
                              0
                              • tbowmoT Offline
                                tbowmoT Offline
                                tbowmo
                                Admin
                                wrote on last edited by
                                #19

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

                                1 Reply Last reply
                                0
                                • sburlotS Offline
                                  sburlotS Offline
                                  sburlot
                                  wrote on last edited by
                                  #20

                                  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.

                                  Thanks,
                                  Stephan

                                  1 Reply Last reply
                                  0
                                  • tbowmoT Offline
                                    tbowmoT Offline
                                    tbowmo
                                    Admin
                                    wrote on last edited by
                                    #21

                                    @sburlot

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

                                    1 Reply Last reply
                                    0
                                    • sburlotS Offline
                                      sburlotS Offline
                                      sburlot
                                      wrote on last edited by
                                      #22

                                      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.

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        gn0st1c
                                        wrote on last edited by
                                        #23

                                        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)

                                        KERNEL=="ttyUSB[0-9]*",MODE="0666"
                                        KERNEL=="ttyACM[0-9]*",MODE="0666"
                                        

                                        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"
                                        
                                        1 Reply Last reply
                                        1
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        20

                                        Online

                                        11.7k

                                        Users

                                        11.2k

                                        Topics

                                        113.1k

                                        Posts


                                        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • MySensors
                                        • OpenHardware.io
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular