3

I'm very new to Raspberry Pi/Linux/Python world and I'm trying to identify one of the ports on my RPi, so if someone for example, connects a USB Flash drive (or anything) to that specific port an action will occur like turning on a LED and if it's disconnected the LED will turn off. I want the program to run infinitely until interrupted. I've already downloaded PyUSB, and I tried coding but it doesn't work because the only way I figured out how to detect if something is connected to the port is by identifying the idVendor and idProduct of the thing I'm going to connect and then if the program sees that the product is connected then it turns on the LED.

Since I won't always connect the same device into the USB port, I don't think that :

dev = usb.core.find(idVendor=0x****, idProduct=0x****)

will work.

Thanks

2 Answers 2

5

lsusb will show the USB devices. This has a number of options e.g. lsusb -t will print a tree.

/:  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=vend., Driver=smsc95xx, 480M
        |__ Port 3: Dev 4, If 0, Class=HID, Driver=usbhid, 1.5M
        |__ Port 4: Dev 5, If 0, Class=vend., Driver=rt2800usb, 480M
        |__ Port 5: Dev 6, If 0, Class=HID, Driver=usbhid, 1.5M
        |__ Port 5: Dev 6, If 1, Class=HID, Driver=usbhid, 1.5M
2
  • yeah, but if I already know the Port that I want to designate for the turn on/off of the LED when a device is plugged into it how can I command it to notify me when is being used? Oct 30, 2014 at 13:36
  • Because for finding an specific device being used (one that I know its id) I can use usb.core.find(idVendor=0x****, idProduct=0x****), but I don't know what to use for a port... Oct 30, 2014 at 13:40
0

One thing you could do, sorta building off of @Milliways answer, is use a python library like popen or another similar tool to run lsusb and parse the output, looking for whether your port or your device shows up. This would allow you to react to different devices being plugged in as well as port because lsusb gives a lot of info. I think there are other ways to do this as well, but that's one way that's definitely do able.

Also you asked "how can I command it to notify me right when it is being used?" and I believe the only way to achieve this functionality (without like editing drivers or doing something low level like that) is to have a program that is always running and continuously checks, so that it can react immediately. If you need to run other stuff on your pi and don't want it to be constantly checking you can also check on a timer, ie have it check every minute or so. Good Luck!

3
  • 1
    ICYI: The question was posted over 6 years ago. You probably already knew this, but when you closed with Good Luck!... I just wanted to make sure you knew that.
    – Seamus
    Nov 15, 2020 at 20:33
  • So I didn't know when I answered, but I noticed right as I hit post lol. Sorry for reviving an ancient question, but maybe my answer will help the next person who wants to do this
    – Dyskord
    Nov 15, 2020 at 21:53
  • No need to apologize, you didn't revive it... the man behind the curtain did :) I only mentioned it because IMO, new answers to old questions are rarely noticed. Several reasons for this; e.g. the Q has become irrelevant. I've decided they're largely a waste of time (hmmm...), but I still answer an "oldie" on occasion.
    – Seamus
    Nov 15, 2020 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.