CircuitPython: No module named 'usb_hid' - raspberry-pi-zero

I installed CircuitPython on my Raspberry Pi Zero W and tried to import usb_hid in Python and it threw the error: ModuleNotFoundError: No module named 'usb_hid'. I've spend all day trying to solve this issue to no avail.
I'm running the default Raspberry Pi OS and I used this tutorial to install the CircuitPython libraries.
My end goal is to use my Raspberry Pi as a mouse that controls another device. I modified the code from this tutorial to come up with this code:
import time
import usb_hid
from adafruit_hid.mouse import Mouse
mouse = Mouse(usb_hid.devices)
time.sleep(5)
while True:
mouse.move(x=100)
time.sleep(0.5)
mouse.move(x=-100)
time.sleep(0.5)
I'm very new to Raspberry Pi's / Python, so maybe I'm just missing something obvious.

Related

Raspberry pi interfacing rfid module rc522

i have been trying to interface the rc522 rfid module but i cant seem to make it work. can someone help?
I am using this tutorial: https://pimylifeup.com/raspberry-pi-rfid-rc522/
I have enables SPI
None of my GPIOs are fried, i have checked them using this guide: https://forums.raspberrypi.com/viewtopic.php?t=268806
I have downloaded the libraries needed!
I have wired it this way:
The only difference is that I am using a rpi zero 2w
I have installed all libraries and i have written the code, but the rfid module doesnt seem to read or write
Can someone help me?
Discord: Yasen#7752 This is the pins i have wired the module on my rpi zero 2w.
I tried running this code:
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
text = input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")
finally:
GPIO.cleanup()
but it didn't seem to work!
This is an example output:
pi#raspberrypi:~/pi-rfid $ sudo python3 Write.py
New data:pimylifeup
Now place your tag to write
Written
My program just doesn't print "Written". I am assuming it has something to do with the rfid modules.
Once again the tutorial i am using is: https://pimylifeup.com/raspberry-pi-rfid-rc522/. I have almost no experience as I am only 14, but i would really use some help! Thanks!

Playing audio in a remote Jupyter Notebook (like Google Colab)

I'd like to play audio on a remote Jupyter Notebook (like in Google Colab or a Binder-hosted Jupyterlab) but I can't figure out how. What I would like to get working is something like this:
from pydub import AudioSegment
from pydub.playback import play
start = 1000
end = 3000
audio = AudioSegment.from_file("someaudio.flac")
audio_piece = audio[start:end]
play(audio_piece)
With a playback package like simpleaudio installed, everything works fine on my local machine. But when I try to run this code in Google Colab, for example, I get this error message:
SimpleaudioError: Error opening PCM device. -- CODE: -2 -- MSG: No such file or directory
I tried several other audio packages but I always ran into some trouble. The only thing that works is IPython.display -> Audio. But I can't use this for my project because I don't want a player displayed (and because it doesn't seem to have the option to play segments of an audio file).
Does anyone know a solution for this?
For MyBinder-served sessions, you have to connect the audio playing ability to IPython abilities imported into the notebook or link the playing of audio to ipywidgets running in the notebook.
Example of playing a wav file in a MyBinder-served notebook
Go to here and press 'launch Binder'.
When the session spins up, paste this into a cell and run that cell:
# PLAY A WAV. BASED ON
# https://ipython.org/ipython-doc/stable/api/generated/IPython.display.html#IPython.display.Audio
from IPython.display import Audio, display
Audio("http://www.nch.com.au/acm/8k16bitpcm.wav") # From URL
# see https://ipython.org/ipython-doc/stable/api/generated/IPython.display.html#IPython.display.Audio
# for other options such as a file you upload to the remote MyBinder-served session
That will show a controller you can click 'play' to play the file found at the URL as the wav file.
See the source linked in the comments for more information, such as how you could play your own wav file.
To demo this in JupyterLab:
If you are starting fresh, click here to launch directly into JupyterLab. When that sessions spins up, open a new notebook and paste in the code above.
If you are already in a session provided by MyBinder in the classic notebook, click the 'Jupyter' logo in the upper left side above the notebook. The page will refresh and the JupyterLab interface will load and you can open a notebook there and paste in the above code.
The environment specified for those sessions isn't overly complex as you can see here.
Example of playing a tone in a MyBinder-served notebook
Similar to everything above; however, use this as the code block you paste into a cell:
# Generate a sound. BASED ON
# https://ipython.org/ipython-doc/stable/api/generated/IPython.display.html#IPython.display.Audio
import numpy as np
from IPython.display import Audio, display
framerate = 44100
t = np.linspace(0,5,framerate*5)
data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t)
Audio(data,rate=framerate)
Example if an interactive control via ipywidgets of audio-generation in a MyBinder-served notebook.
Click here to launch a session with the JupyterLab interface back by an environment with the necessary modules installed, and then run the following in a cell:
!curl -OL https://raw.githubusercontent.com/mlamoureux/PIMS_YRC/master/Widget_audio.ipynb
That will fetch a notebook that you can run, based on code described here.
Alternatively, you can use the classic interface by launching fresh to one or switching from the JupyterLab inferface back by using from the menubar, 'Help' > 'Launch Classic Notebook'.
(I believe the notebook used in this example is based on here, or vice versa. When I tried that one in the ipywidgets docs from a session that already had ipywidgets installed & not much else, I had to also install matplotlib along with ipywidgets via running %pip install matplotlib, because of the line import matplotlib.pyplot as plt.)
UPDATE: use pydub to edit audio via MyBinder and hear it played
This was added in response to the comments to the general answer. Specifically, pydub use is demonstrated using a different enviornment, since pydub needs ffmpeg.
Go here and click on 'launch binder' to spin up a session where ffmpeg is installed in the backing environment. pydub needs ffmpeg or equivalent.
You can run the following line in a notebook and then open the notebook that it gets to work though that demonstration:
!curl -OL https://gist.githubusercontent.com/fomightez/86482965bbce4bbbb7adb4c98f6cd9e6/raw/d31473699d8a2ec6d31dbf1d9590b8a0ef8972db/pydub_edit_plays_via_mybinder.ipynb```
Or step through the equivalent demonstration code in a notebook in JupyterLan by following these steps.
First enter in a cell the following:
```python
%pip install pydub
Get an audio file to use for testing by running this:
!curl -OL http://www.nch.com.au/acm/8k16bitpcm.wav
Edit that file and playback the result in the notebook without interacting with it, by running this in a cell:
from pydub import AudioSegment
from pydub.playback import play
start = 1000
end = 3000
audio = AudioSegment.from_file("8k16bitpcm.wav")
audio_piece = audio[start:end]
audio_piece.export("test_clip.wav", format='wav')
from IPython.display import Audio, display
Audio("test_clip.wav", autoplay=True)

How to use neopixel lights and sound output on Raspberry together?

I am working on a program to play a song and at the same time light up a 45 led Neopixel strip. The problem is when I am running the code using sudo the sound is not working (I am using a soundwave USB to audio sound card) but the lights work (i tried it using a separate test program for just lights) and on the other hand when I run the program without sudo in terminal using (python3 test_light_led.py) the sound works but now the lights don't work.
Has anyone worked on something like this? I have been searching a lot of places for a solution but couldn't find one.
THis is my current code. Any help would be appriciated.
import os
import playsound
import neopixel
import board
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.color import AMBER
pixel_pin = board.D18
pixel_num = 45
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness = 1, auto_write = False)
pulse = Pulse(pixels, speed = 0.1, color = AMBER, period = 3)
while True:
playsound.playsound("Happy_Voice.wav")
pulse.animate()

Adapting Selenium Script for Raspberry Pi

Preface: I have very limited experience with linux and selenium in general. Also this is a multipart question.
Hi there,
I've written a script that I want to run on my Raspberry Pi model 3B which is running Raspbian OS.
I originally built it on a Windows machine (pretty stupid mistake to build it for the wrong platform but anyway) and now need to adapt it so it runs on the Pi.
The main thing I am concerned about is the usage of selenium and chromedriver. I have downloaded chromium-chromedriver (chromium-chromedriver 34.0.1847.116-0ubuntu2 in armhf (Release)) from this repository.
First Question
The file downloads as a .deb which I have never seen before. It opts to install, however I have no idea where it installs as it does not give the option to install to a different directory. I've tried search for it with find, but nothing comes up. Where does it go by default in Raspbian OS?
Second Question
I know I will have to change some expressions because I am using chromium-chromedriver instead of chrome driver, but I am unsure which ones as I don't know selenium that well. This is the main instance here. Do I just change all instances of "Chrome" including "chrome_options" to "Chromium/chromium_options"?
from selenium.webdriver import Chrome
from contextlib import closing
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
#Scrape out the table information
with closing(Chrome(chrome_options=chrome_options)) as driver:
Lastly, for the purpose of finding the process with psutil and ending it with process.kill(), the text ID of the driver in task manager should just be chromium-chromedriver.exe right?
Any and all help and advice appreciated.
Thank you all for your time.
L

Raspberry pi to excel

I've created this code on python to do some data analysis. It runs fine on my desktop but when I transfer it to my Raspberry pi it doesn't create any Excel files, other wise it runs fine and reports no errors. The part that I think is causing concern is here.
def subjectanalysis(sub):
writer=pd.ExcelWriter(sub+".xlsx")
cleandf.to_excel(writer,"rawdata")
data=list(data_sets.keys())
for i,v in enumerate (data_sets.values()):
try:
dataset=fromtarget(sub,v)
df5=pd.DataFrame(dataset)
df5.to_excel(writer,data[i])
except:
print("this data does not exist for "+sub)
subjectanalysis("M")
I'm using berryconda and have Pandas and openpyxl installed. Any reason it doesn't create Excel file on the Raspberry pi?

Resources