PersistentUSBDevices

From Domoticz
Jump to navigation Jump to search

If you have multiple USB devices connected to a Linux system, sometimes after a reboot the device order is changed (/dev/ttyUSB0 becomes /dev/ttyUSB1, or the other way around). The consequence of this is that Domoticz will have the incorrect devices selected in its setup.

Use /dev/serial/by-id devices.

** Don't use /dev/ttyUSBx ** but /dev/serial/by-id !

In Domoticz you should also be able to choose the serial port by ID. These by-id devices do not change even if you put the USB stick in another port.

We do this by issuing the following command to find out the attached USB devices:

ls /dev/serial/by-id

You should see one or multiple serial devices, for example it's: usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0

This solution should also be preferred even if you have only a single USB device.


If your system does not have /dev/serial/by-id, you can continue reading below to find a sollution

Find IDs of the USB devices when no by-id devices are present

Make sure all the usb devices are plugged in.

Run the following command:

sudo lsusb -v | grep 'idVendor\|idProduct\|iProduct\|iSerial'

and write down the:

*idVendor
*idProduct
*iSerial (if this is zero, you can ignore it)

Or use this command

dmesg
And search the list

Examples

The RFXtrx433 is reported as:

 idVendor           0x0403 Future Technology Devices International, Ltd
 idProduct          0x6001 FT232 USB-Serial (UART) IC
 iProduct                2 RFXtrx433
 iSerial                 3 07VYAR1X

Aeon ZWave USB stick is reported as:

 idVendor           0x10c4 Cygnal Integrated Products, Inc.
 idProduct          0xea60 CP210x UART Bridge / myAVR mySmartUSB light

Future Technology EnOcean USB300 stick is reported as:

 idVendor           0x0403 Future Technology Devices International, Ltd
 idProduct          0x6001 FT232 USB-Serial (UART) IC
 iProduct                2 EnOcean USB 300 DB
 iSerial                 3 FTAZ2SI

You need the part behind the '0x'. So you don't have to write down '0x0403' but '0403'. When we deal with hexadecimal numbers, they are written with a 0x in front of them so we don't confuse them with base 10 numbers.


Creating rules file

Create a rules file, with the following example content:

sudo nano /etc/udev/rules.d/99-usb-serial.rules

Please note: values in the following code are examples; put in the actual values for your system between the quotes (e.g. replace them with the idVendor, idProduct and iSerial corresponding to your USB device.

RFX-433

Put the following content in the file:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="123YAOOW", SYMLINK+="ttyUSB-RFX433-A"

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="123YX78C", SYMLINK+="ttyUSB-RFX433-B"


Some devices don't show a serial, you can remove that part in the file, so it looks like this:

SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="ttyUSB-RFX433"

Names should be ttyUSB-UNIQUENAME so you can easily identify them within Domoticz.

AeoTec Z-Stick

Gen2: SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SYMLINK+="ttyUSB-ZStick-2G"

Gen5: SUBSYSTEM=="tty", ATTRS{idVendor}=="0658", ATTRS{idProduct}=="0200", SYMLINK+="ttyUSB-ZStick-5G"

Future Technology EnOcean USB300 stick

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTAZ2SI", SYMLINK+="ttyUSB-EnOcean"

Opentherm Gateway

SUBSYSTEM=="tty", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303",SYMLINK+="ttyUSB-OTG"

P1 smartmeter

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="A7XKCMHJ", SYMLINK+="ttyUSB-P1"


When you see no device

 sudo mknod /dev/ttyUSB0 c 188 0
 sudo mknod /dev/ttyUSB1 c 188 1

And do the steps again.

Reboot your Linux system and check results

Restart (sudo reboot) with
sudo shutdown -r now
After the reboot you should have two new serial ports (ttyUSB21 and ttyUSB22), and they will always be assigned to the devices you have configured in the rules file.

To use the new ports, in Domoticz go to the Setup -> Hardware webpage, highlight the RFXtrx433, and in the Serial Port dropdown list select the new Port 'ttyUSB21'.
Then save.

Alternatively, you can reload the udev rules without rebooting by running the following command:
sudo udevadm control --reload
Then disconnect and reconnect the USB device, to see if it works.

Alternative filtering via Devpath

When there is a situation with two or more devices with the same vendor ID and productID and there is no serial number, then you can use the options below to add some other way of filtering. In this case we'll use the dev path, which is the IDs the devices have gotten in the USB hierarchy. The downside of using the devpath is that the devices will always have to stay plugged into the same physical port.

Like above, find the idVendor and idProduct codes by typing lsusb -v. Next, find the devices place in USB hierarchy. You can see this hierarchy by typing:

lsusb -t

This gives an example output of:

Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M
  |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/5p, 480M
      |__ Port 1: Dev 3, If 0, Class=Vendor Specific Class, Driver=smsc95xx, 480M
      |__ Port 2: Dev 4, If 0, Class=Vendor Specific Class, Driver=ch341, 12M
      |__ Port 5: Dev 6, If 0, Class=Vendor Specific Class, Driver=ch341, 12M

The devpath is the port hierachy. So in this specific example, using two cheap Chinese Arduino clones without proper serial numbers on a Raspberry Pi 3, we might add this to the USB Serial Rules file:

SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", ATTRS{devpath}=="1.2", SYMLINK+="ttyUSB-arduino1"
SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", ATTRS{devpath}=="1.5", SYMLINK+="ttyUSB-arduino2"

Alternative filtering via other attributes

Another option to distinguish the devices, is to look for other dissimilarities with the udevadm tool:

udevadm info -a -n /dev/ttyUSBX ..where you should replace the X with a number. You can find that number by typing ls /dev and looking for entries starting with ttyUSB.

Udevadm info starts with the device specified by the devpath and then walks up the chain of parent devices. It prints for every device found, all possible attributes in the udev rules key format. A rule to match, can be composed by the attributes of the device and the attributes from one single parent device.

Now compare the output of both devices and find some non-simulare attributes. Then add a rule based on that to the 99-usb-serial.rules file