Brainleak

Blog as external memory

Detect a USB Device on Linux With Udev

I needed a way to start an application when a device is connected to the RPi. In this example the barcodescanner software should start when we connect the scanner. Adding a udev rule should do the trick.

  • First get the ID of the specific device
lsusb

    Bus 001 Device 006: ID 05f9:2214 PSC Scanning, Inc. 
    Bus 001 Device 004: ID 0a5f:0015 Zebra
  • Add the rules file located in /etc/udev/rules.d/, for instance /etc/udev/rules.d/85-custom_usb_device_rules.rules

  • Add the rule to detect scanner and start software, the idVendor and idProduct point to the ID we got from lsusb

ACTION=="add",SUBSYSTEM=="usb",ATTR{idVendor}=="05f9",
    ATTR{idProduct}=="2214",RUN+="/etc/init.d/barcodescanner start"
  • Reload the rules or reboot
udevadm control --reload-rules

List of ID’s

Device ID ID
Barcodescanner 05f9:2214
Zebra Printer 0a5f:0015
Dacal DC-300 04b4:5a9b 04b4:5203