pHATBEAT Python script works in IDLE but not from command line


#1

I’m wanting to make a standalone device that plays a .wav file when a button is pressed, using a pHATBEAT and a Pi Zero W. The simple Python 3 script I’ve written so far works fine when executed in IDLE, but if I try to run it from the command line I get silence, and a couple of random LEDs light on the pHAT. If I remove the button handlers and just run the bit that plays the sound file, it’s fine. I’m new to all this so it’s hopefully something simple!

import pygame
import time
import phatbeat
import os
pygame.init
whistle = pygame.mixer.Sound("sounds/whistle.wav")
@phatbeat.on(phatbeat.BTN_PLAYPAUSE)
def playpause(pin):
     pygame.mixer.Sound.play(whistle)
     time.sleep(5)
@phatbeat.on(phatbeat.BTN_ONOFF)
def onoff(pin):
     os.system("sudo shutdown -h now")

Any ideas please?


#2

What command are you using to run it, and what do you see if you run the pwd and ls commands?


#3

I run it using
python whistle.py
from the directory containing the script; pwd returns
/home/pi/GPIOMB
and ls returns
sounds whistle.py
sounds is the directory containing the .wav file(s).


#4

I have just tried typing the commands in whistle.py directly on the command line. All is OK until I get to

whistle = pygame.mixer.Sound(“sounds/whistle.wav”)

This returns

Traceback (most recent call last):
   File <"stdin">, line 1, in module
MemoryError

I guess that is the problem, but how to solve it? The .wav file is 1.0MB. Thanks.


#5

Is the “sounds/whistle.wav” directory in your current working directory?

“MemoryError” seems like it might be a red herring- I can’t see a 1.0MB wav file being an issue, unless it’s accidentally loaded hundreds of times or some other weirdness.


#6

Could it be a $path problem? You can try to spell out the full path to the wav file.

Try changing
whistle = pygame.mixer.Sound(“sounds/whistle.wav”)

to
whistle = pygame.mixer.Sound("/home/pi/GPIOMB/sounds/whistle.wav")


#7

Thanks both. I’ve tried mov the .wav file to the same directory as the .py file and also specifying the full path name. Both result in silence and a return to the command prompt when run from the command line, and the same MemoryError if I type the commands in separately. I’m assuming the pygame library is well proven and unlikely to be the issue, is that reasonable? Is there a log file somewhere that might offer a clue? I’ve a couple of RPi books on order which I’m hoping will help clarify how the Pi works.

I’m more used to programming microcontrollers in assembly language when it’s much easier to see exactly what’s going on, and it’s 30 years or more since I used Unix!


#8

This works fine from the command line:

import pygame
import time
import phatbeat
import os
pygame.init
whistle = pygame.mixer.Sound(“sounds/whistle.wav”)
#@phatbeat.on(phatbeat.BTN_PLAYPAUSE)
#def playpause(pin):
pygame.mixer.Sound.play(whistle)
time.sleep(5)

So it would appear to be something to do with the (commented out) phatbeat code?


#9

I’ve abandoned the phatbeat module and am now using gpiozero instead. It’s working OK :)