I am stuck at a very unusual problem. I am making a project in which i am using two USB ports of RaspberryPi for attaching a GPS module and a S2 Xbee. The code of python i am using is using both the GPS and the S2 Xbee. The biggest problem i am facing is whenever i switch off my raspberryPi and start it again, the ports gets interchanged. Basically, for GPS i am using USB0 and for Xbee i am using USB1, but they gets interchanged.
Please suggest me some solution. I searched a lot but could not find much.
Each USB device has a unique vendor id and product id combination. The command 'lsusb -v' or 'udevadm info --query=all -n /dev/bus/usb/001/00x' x = [1..4] on raspi, will identify the device name where they are mounted by the raspi.
Here is another way without starting linux commands from python:
Simple way to query connected USB devices info in Python?
Related
I am using Raspberry Pi 4 which comes with 4 USB ports. For my project, I have two mics and both will be connected to Rsp via a USB port. I need to be able to choose which Mic needs to be ON. So basically I need to be able to switch/choose which USB port I want the sound to come in.
I need to be able to choose Mic1 or Mic2. I am writing my code in Python. Is there any library that gives me access to choose a specific UBS port? I really can't find anything.
Appreciate your help.
Regards
With the help of some tutorials I wrote a Python script that lists network details of the devices in the network.
I however only detect certain devices if I run the Python script. I already saw when searching that certain devices (e.g. iOS devices) don't respond (but are occasionally detected), but the strange thing is:
macOS (Catalina, 10.15.2) does find these devices with "arp -a"
Raspbian (10, buster) also finds a number of devices when doing "arp -a" but does not find the iOS devices
Python (3/2.7) does not find the Arduino devices which Raspbian and macOS do find, I run the Python script from the mac.
Running the Python script on the Pi does give me the Arduino devices as well.
I use Scapy for the ARP messages in Python (based on this tutorial: https://www.thepythoncode.com/article/building-network-scanner-using-scapy):
def find_devices(gateway):
_ip_items = gateway.split(".")
target_ip = "%s.%s.%s.%s" % (_ip_items[0], _ip_items[1], _ip_items[2], "0/24")
# IP Address for the destination
# create ARP packet
arp = ARP(pdst=target_ip)
# create the Ether broadcast packet
# ff:ff:ff:ff:ff:ff MAC address indicates broadcasting
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
# stack them
packet = ether/arp
result = srp(packet, timeout=3, verbose=0)[0]
# a list of clients, we will fill this in the upcoming loop
clients = []
for sent, received in result:
# for each response, append ip and mac address to `clients` list
clients.append({'ip': received.psrc, 'mac': received.hwsrc})
print("{:16} {}".format(received.psrc, received.hwsrc))
Of course I could use ARP -A directly (e.g. execute that from Python and parse the results), but I am much more curious about the reasons that the Raspberry Pi seems to detect more devices than the Python code and macOS detects even more (are they cheating and using bonjour e.g.?) but how come both detect the Arduino's (Nano 33 IoT boards with MQTT pub/sub code on them) while the script on macOS does not detect those.
I actually found out that I can get even the iOS devices and Arduino devices when I chance the timeout to a much larger value (e.g. 15). It is still much more slow than macOS (arp -a gives an instant response) but they probably draw it from some cache or something(?). It does produce all devices that I see in arp -a also from the Python code even though it then becomes really slow.
I need to run both zigbee and SMS dongle on a raspberry pi, that means having to run both ports in a python script. Since xbee is always on ttyUSB0, is there a way I could set the dongle on ttyUSB1 or ttyUSB2 so that i dont have to configure it via gammu command everytime it is plugged in?
The best thing to do here is use a udev rule. This will allow you to symlink the device to a specific port, something like /dev/ttyXBEE and /dev/ttySMS in your case.
To accomplish this, you need to query the devices using udevadm to find some unique identifying information, then create a file /etc/udev/rules.d/99-usbserial.rules that will tell the Pi how to map the device.
This thread has an example.
I'm connecting a several identical USB-MIDI devices and talking to them using Python and pyportmidi. I have noticed that when I run my code on Linux, occasionally the MIDI ports of the devices are enumerated in a different order, so I send messages to the wrong devices. As the devices do not have unique identifiers, I am told that I should identify them by which USB port they are connected to.
Is there any way to retrieve this information? My app will run on Linux, but Mac OS support is useful for development.
It's annoying because they usually enumerate in a sensible order - the first device in the hub is the first device in portmidi, but sometimes they don't - usually the first 2 devices are switched. I have to physically move the devices without unplugging to fix them.
lsusb should do the trick. All devices and their respective hubs are listed there.
I am using an RS232 Serial - USB Converter to connect to a controller. I am trying to connect to this port programmatically.
The device is getting listed as below when I give lsusb
Bus 003 Device 003: ID 05ad:0fba Y.C. Cable U.S.A., Inc.
But I am not able to see it's device path (something like /dev/ttyUSB*).
I would like to know the way to get the path from device id (or) a way to create / find the path which can be used to open the port from a program (Ruby, Python, C)
Centos has a fairly old kernel. It may be lacking in support for your device.
Are you able to try with a newer distribution (Fedora, Ubuntu, etc) to test whether it works or not? Failing that, do you have a different dongle you can use?
You can Google for "Centos 05ad:0fba" or similar combinations to find out whether it's supported.