A Talking Seat - Script Help (Raspberry Pi) - python

Project Aim: Create a seat that when you sit on it, audio starts. When you leave, the audio stops. Next time someone sits down, the audio begins again. Each time it is a random track, different from the last.
Context: Outside with access to power
How it can work (I think): I’m wondering if it is possible with using a Raspberry Pi and PIR Sensor (plus some other bits and pieces?). Basically, when someone moves into the range of the PIR sensor, this event is detected and sends an input to a GPIO pin on the Pi. With the right script, this can then fire an event – e.g. play a random audio file from micro SD card. Does this sound correct/doable?
Help!: I believe its not too abstract an idea or function, that has probably been done before. But I just can’t seem to find exactly what I’m looking for, where someone has already written a script I can use/modify. So I’d love to know, do you think this idea is doable/achievable? And, where acatly do you recommend I can track down a pre-made script/instruction on making it a reality.
If you know someone who could write such a script, please do put me in contact.
Thanks!

This is actually not that hard to realize. You have to come up with an idea for a sensor that you can build into your chair, I would recommend just a simple one that works like a push button: when someone sits down, you got a connection the RasPi can recognize. After doing that make sure, your RaspberryPi has power, a GPIO connection to the sensor and speakers attached.
The Script is also quite simple and its structure would look like this:
import RPi.GPIO as GPIO
import simpleaudio # just as an example, was the first one I found online
sound = 'path/to/your/soundfile.wav'
def playSound(directory):
play = simpleaudio.WaveObject.from_wave_file(directory)
play.play()
try:
while True:
if GPIO.input(numberofyourgpio):
play_sound(sound)
except KeyboardInterrupt:
pass
Finally you can set this to start after booting the RaspberryPi and you will never have to care about it not being started anymore.

Related

How to make python talk to puredata?

This is my first post. I am trying to make a drum kit using a raspberry pi. The drum triggers are some peizoelectric sensors. I have used pygame inside python to play sounds. When the script detected gpio inputs, it played drum sounds. It was fine for the most part but due to latency issues, I took another approach and decided to use puredata.
I want python to detect gpio inputs and then send signals to puredata, which will play the desired sounds.
My question is, how can I get python to send simple signals to puredata??? I am running both python and pd on the same pi.
Also, what do you think of this approach i.e. using puredata directly from gpio inputs? Can having a microcontroller like arduino to send the signals as midi signals to pd affect anything???
Thank you in advance!
I'm going back about a decade but I used Python to send data from the 3d modelling software Blender to PureData using the socket library.
This article might help a bit more with packaging up and sending across.
If you know what your components within PD are expecting as an input that will help you know how best translate your input triggers into packages to ship over socket to PD.

Looking for help writing python code for Raspberry pi to make an interactive old school phone

I am an absolute N00b when it comes to Python and I have no idea how to turn my 'sudo'- code into actual code.I'm also having issues with the GPIO pins in my pi.
I would very much like some help coding this project.
I've tried making separate codes to test the GPIO pins but they never seem to work properly, I'm not using any resistors and I probably should even though I have no idea which ones and where.
I have tried to use the Bluetooth speaker with my pi but it always seems to prefer the HDMI even though I try to force it to Bluetooth, it just won't go.
I have been learning python in my free time as much as I can but since I am a full-time student, I haven't been able to learn very much and my knowledge of c++ has been entirely useless so far.
This is my sudo code
loop(called while on)
if(handset is on the cradle)
Do nothing and loop “while on” again
else
Clear Code
loop(called dialing loop)
If (time<3seconds)
Play dialtone.mp3
if(number indicator is off)
Stop playing dialtone.mp3
Reset time
Count number of pulses from #pulser
Assign number to the next letter of Code
Exit to dialing loop
Else
Start counting time
Exit to dialing loop
Else
Search thumbstick for Code.mp3
if (Code.mp3 exists)
Play Code.mp3
Exit to while on loop
Else
Play dropped call.mp3 for 3 seconds
Exit to dialing loop
My goal is to use an old rotary phone with a raspberry pi that will let you dial a number (arbitrary length) an audio file associated with that number (one file per number-one number per file) will be played through the handset. My restrictions are: the files and codes must be read from a thumb drive, there must not be a cap on the number of files, there must not be a cap on the length of the number you can dial, all the outward facing parts of the phone must look like the original phone, it must be a raspberry pi. the dial on the phone uses two leaf switches, one that pulses an amount of times based on the number that is dialed and one that is active until a user starts to dial a number. There is a needle switch attached to the cradle that will detect whether or not the handset is on the cradle and the audio is being played through a Bluetooth speaker.
You could always start here: https://leanpub.com/RPiMRE
You can read it online for free: Here's a section on GPIO:
https://leanpub.com/RPiMRE/read#leanpub-auto-pin-header-1
Again it's free and a good resource with code examples on how to map what you're looking to do for your project. It might not be straight away what you're looking for but might have some bits that can help you. Good Luck!

Raspberry Pi 3: Unusual behaviour with mpd, PiFi DAC+ audio

I'm looking for some help 'cause I'm getting a bit frustrated on this... :-(
I have a headless Raspberry PI 3 with a PiFi DAC+ audio card, basically an HiFiBerry clone. On the PI I installed mpd and mpc as a client.
On top of those I wrote a python script that invokes some mpc commands to control the underlying mpd daemon (load a playlist, play a stream,...).
Now the issue.
The overall audio setup based on the hifiberry-dacplus overlay works well, the sound is good and I'm fine with it. Mpc & mpd work, I can control all the functionalities of mpd (at least the ones I need) through mpc without a flaw...but, if I try to run my python script suddenly I cannot hear anything anymore, even if no specific errors are traced.
The 'scary' thing is that, after aborting the script execution, I'm no more able to play any sound (I tried with several wav files using aplay), and again no specific errors show up in the log files...looks like someone just 'muted' the volume, but alsamixer shows all playback levels to 100%. I need to reboot the PI to get my sound back.
I checked for clues in the usual places:
/var/log/messages
/var/log/syslog
dmesg
boot.log
/var/log/mpd/mpd.log
I also run aplay -vvv when audio was blocked and compared the output with a session where audio was running fine but I didn't notice any difference...
I know it would be very difficult to diagnose the problem without having access to my system, but do you have any ideas on where else to look to understand if something went wrong?
Just for info, here's my aplay -l output:
**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ HiFi pcm512x-hifi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
Thank you!
Michele
EDIT: seems like there is some incompatibility between the audio board and a 16x2 LCD display I'm using to show the name of the stream I'm playing. The display is a very common one, based on the HD44780 chip.
My code uses the AdaFruit python library available here to drive it and I still have to figure where the problem is: the audio board, as per HiFiberry docs is connected through GPIO 2,3,18,19,20,21 (plus ground & +5V for power), so it shouldn't cause any conflict with the LCD which uses different pins, but I wouldn't bet on it.
Anyway, removing the LCD management part from the python code (but leaving the display physically attached to the RaspBerry pins) apparently solved the problem...
I'll keep this question updated, maybe could be useful for someone else, who knows!
Ok, I got it. As usual, I just went too fast with CTRL-C & CTRL-V without properly reading the code...
I didn't notice I left this statement in my python code
lcd_backlight = 2 #GPIO pin to control lcd backlight
Actually The GPIO 2 (which is one of the two I2C enabled pins on the Raspberry) is not connected to the LCD, but it is used by the audio board for configuration purposes: this way, whenever I tried to initialize the LCD, the audio board was somehow reconfigured, making it "mute". The only way to reset the faulty configuration was to reboot the PI itself.
Just leaving the default 'None' value for the backlight control pin (I do not need it) did the trick.

Real Time Python program

I have a project in my job.
The target is to prepare 14 SD cards for provisioning any Raspberry Pi 3.
So I have to found a solution to do it automatically and follow which SD card is ready to start and which one is complete.
I have the idea to build with a Python3 program and a tkinter interface because I know a little bit Python and not others languages...
The program should work like this :
List every Windows drives where SD card is mounted
Push a button in front of the letter of the SD card drive to start the provisioning.
The provisioning is all steps to make the SD cards bootable with an OS. So I have to pass some DISKPART commands or equivalent in Python I think, if you have any suggestions ?
Show a statut in front of each drive to follow if the drive is pending, working, complete, etc.
I have a huge interogation about this. My program has to refresh every informations. I mean the program should work in real time or not ? What is the best way to proceed ?
To be clear, I don't want someone building to me this program, I just want to have some good idea to implement.
Thank you
You can use this module to execute commands. For example:
import subprocess
completed = subprocess.run(['ls', '-1'])
print('returncode:', completed.returncode)
I can't help with the Python part, but if you have a WinAPI window with message handling (so the WindowProc thing), WM_DEVICECHANGE is the message, here are the actual event categories and RegisterDeviceNotification is how you subscribe to it. Complete (but C) MSDN example is here
While implementing it may require some work, viability itself depends on getting access/not getting access to the message queue (from Python). Based on this and this it seems to be possible, but I have no experience with it.

Is it possible to detect when the system is recording a sound and then perform some action on Python?

I began learning Python a few days ago, and i was wondering about a practical use for a program.
Then i came up with the following: if my brother is in his room recording himself playing guitar, a led plugged to the usb and wired so it's outside his door lights up, and then i'll know he's recording and i'll take care not to make any noises.
The main questions are:
How Python can detect any recording going on in the system?
How would i interface with the usb so i can actually turn the led on?
to interact with USB, you can read this related question.
to detect recording, you will have to tell us on which operating system your program is intented to run (but i am not sure it is possible to detect that another application is recording)

Categories