Accessing Raspberry Pi4 USB port with Python - python

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

Related

How to program a atMega328p MCU with a Raspberry Pi 4 (Python)

I been looking everywhere online for this exact configuration but can't find much.
I want to program my AtMega328p MCU (its on a breadboard) using Python from my Raspberry Pi 4 but I am not sure how to check if communication is going on between them? I have the MISO, MOSI, SCLK, and CE0 pins from the Pi connected to the MISO, MOSI, SCK, SS pins on the AtMega328p respectively.
I understand I have to use SPI communication, however how can I exactly send data from the Raspberry Pi to the MCU to ensure there is communication between the two? Maybe some code to send to the MCU and receive it back? I been using the SPI Dev Python libraries but can't find much info on it. Thank you in advance!!
To load code onto the AVR (program it), you want to use existing software like avrdude that already speaks the AVR ISP protocol. avrdude already has support for using the RPi SPI headers - just use the linuxspi programmer type.
Here's an article by the author that explains it (although there are probably more recent articles if you search around) http://kevincuzner.com/2013/05/27/raspberry-pi-as-an-avr-programmer/
Is there a reason you want to do this from Python specifically? Or are you referring to communicating between the uC firmware and some Python code on the Pi?

WIFI Pineapple Nano and RPi GPIO

I just got a wifi pineapple nano, and have been a longtime raspberry pi enthusiast, so I'm super stoked to start joining the two. I've already got it connected to my Pi and have played around a bit with the setup. My specific curiosity right now is whether it's possible to write a program to perform GPIO tasks on the raspberry pi based on events occurring on a wireless LAN broadcast by the pineapple. An example would be to turn an LED on if a specific MAC address is detected, and said client is within some power threshold. My lack of knowledge at this stage is how the pineapple fits with the pi (or any other "host" for that matter).
Specifically, do I write my program (probably python, and later some html and JavaScript served over flask) and host it on the pi? If so, how does it interact with the pineapple to get the MAC address and connection power dB? If not, do I host it on the pineapple? If so, how does the program interact with the pi to utilize its GPIO pins? How would I compile my code so that the pineapple can use it? Are there modules already available that can get me most of the way to my goal?
I don know Pineapple Nano very much, but I found, there is an API with module Tracking.
API You should be able to set script which will run when client with specific MAC address connect to AP. In script you can call some method API/Flask on your Pi.
Next solution can be gpiozero library, which has module pingserver doc,
where you can checking some URL if is online. If you set on your AP static IP addresses for clients you want to track, you can use this module to determine, if is online (connected) or offline (disconnected)
example:
from gpiozero import PingServer, LED
from signal import pause
client= PingServer('192.168.0.155')
led = LED(4)
led.source_delay = 60 # check once per minute
led.source = client.values
pause()
In this example, when client with IP address 192.168.0.155 go online, LED will turn on.
Maybe it will help you.
Peace

Using Yun bridge library on Raspberry pi

I am new here and this is my first post. I am not 100% sure where to post this. I think it is something on the OS level so I chose to post it here. Moderators please move it to the correct place if it does not belong here.
I am developing a controller to control a industrial type process. I started off using the Arduino Yun that is a all in one type board with a Linux processor and Arduino controller on one board (if you dont know that already). It uses what they call the Bridge to communicate between the 2. Basically it talks over the com port and you don't have to build your own protocol, it does it for you.
Now the Yun is turning out to be to low specked for what I want to do so now I want to use a Raspberry Pi and hookup an Arduino Mega to do all the low level sensor stuff.
As luck would have it Paul Stoffregen ported the library so it can work on any linux type system using any Arduino board
(Arduino Code) https://github.com/PaulStoffregen/Bridge
(Python Code for the linux side) https://github.com/PaulStoffregen/BridgeUSB
So what it does is you set-up the Pi so when you connect the Arduino it will auto login on the comm port as root (via the usb connection) and then it should start the Bridge.py script. Now here is my problem, in fact I have 2 of them
1) The Bridge.py script does not fire (it does not start up)
2) The Arduino does not auto connect on a Pi reboot.
I can see the Arduino is connecting as root when I plug it in
root#RedPi:~# who
root ttyACM0 2016-03-10 18:01
I have the Arduino side working as I can see it is sending the data.
If I cat the port I can see it is sending the data
cat /dev/ttyACM0
run-bridge
▒XX100▒q▒XX100▒q▒XX100▒q▒XX100▒q▒XX100▒q▒XX100▒q▒X ...........................
But it does not run or execute the "run-bridge" command. But if I type it as root on a ssh session it runs.
And then if I reboot the Pi I have to pull out the plug and push it back to connect to ttyACMo again. But when I do a lsusb I can see the Arduino there. I guess I have to add a bit of config to tell linux to do it at startup.
You have to add this bit. And I was hoping it would take care of the USB at boot as well
# UDEV Rules automatically start a shell on a USB Serial device.
#
# This file must be placed at:
#
# /etc/udev/rules.d/00-bridge.rules
#
KERNEL=="ttyACM*", RUN+="/usr/bin/run-bridge-udev", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{MTP_NO_PROBE}="1"
I hope you can help me. I do not want to rewrite all my code that is working on the Yun. So if I can port it it will be great.

Reading Analog Data from sensor connected to USB (Python)

I may be confusing a few concepts here so any help is appreciated.
Q1: Is it possible to attach any sensor in the world to the USB on my computer as long as it gives me analog data, and read its output? (e.g. pH, temperature, oxygen sensor etc as long as it gives me analog data)
Q2: If so, then what is the simplest way in python for me read such data.
Comment: I am trying to bypass using PLC's, and trying to see if I can get the output from the sensor directly to the PC. (I do not have drivers for these sensors)
Actual Need: I have an oxygen sensor connected to my computer via a USB. The oxygen sensor is able to send out analog data. The obvious way is to go through a PLC. However, I would like a solution which by-passes PLC's so I can connect the sensor directly to my PC via USB.
The USB port on your computer cannot read analog data because USBs work with digital signals. You would need an analog-to-digital converter (ADC).
as #digitaLink answered, it is not possible directly via USB and yes, the obvious way is to use a PLC.
I would go the PLC way - in fact, I did it a few times in the past - and start with an Arduino and later develop a custom PCB, put it in a box and done.
Another possibility is to use a raspberry pi (or similar SBC), which has the GPIOs you can use for analog read.
Edit: there is another possibility.
The sensor you use now is _very_likely_ a PLC in itself, that is, the sensor is attached to a microcontroller that uses the USB port for serial communication. Now, the drivers you are missing do nothing else but decode the data coming through the serial port. Take a look inside your harware and try to find out what components there are in.
So what you could do is to try to find out how to communicate with the sensor via a serial terminal. It is probably possible to monitor serial communication (although I must admit, I don't know how to do that), reverse engineer the code and write your own driver in python. You could learn a lot from this, even if you don't succeed.

Determine which USB device gives the input in Raspberry Pi

I'm working on a project with Raspberry Pi. I have two identical keyboard emulator devices as inputs. In my program, I need to know which one gave the input. Is there a way to do this in Python? Thank you!
Do you have control over these devices? Could you change the USB protocol to something more reasonable, like a USB CDC ACM virtual serial port?
Do they have to by identical? If not, I would do something simple like have one of the devices only send capital letters and have the other device only send lower-case, but I guess that doesn't extend so well if you need to send a number.
With two keyboard emulators, you have to worry about what happens if the messages overlap. For example, if device 1 tries to type "banana" and device 2 tried to type apple, there is nothing to prevent your python program from reading something like "applbaneana".

Categories