Help With Sense Hat Python Script

Dec 22, 2023 Last reply: 2 years ago 4 Replies

I have a Sense Hat and want to run this oython script at boot:



from sense_hat import SenseHat from pydub import AudioSegment from pydub.playback import play



song = AudioSegment.from_wav("/home/pi/redalert.wav")



sense = SenseHat()



x = 0.1 y = 204,0,0 z = 0,0,153 y1 = 25,0,0 z1 = 255,255,0



while True: play(song) play(song) sense.show_message("!!! Intuder Alert ", x, y, z)



Now if I boot the Pi and then run the script it runs fine and the sound plays.



However if I use



@reboot python3 /home/pi/intruder.py via a cron job, the message is displayed but no sound.


I think I had this issue. The permissions on sound channels are weird. I wanted the web server (user www-data) to play sounds - it wouldn't until I did something to it...

OK I just logged in to that machine

in /etc/group is this line:

audio:x:29:me,www-data,pulse,root

I suggest you add at least root and probably pulse to the audio group

Hi - thanks for your help - tired that but still no sound

That is *TRIED* but still no sound!!!

Strange. You did reboot etc afterwards?

Try making the script do a 'who am i' so you can see who is in fact the owning process.

And look in the log files.

Looking in stack overflow it seems that a 60 second sleep before executing the script may allow stuff to settle down, but you probably ought to be using systemd to start the script..e.g

/usr/lib/systemd/system/intruder.service

[Unit] Description=Intruder Server [Install] WantedBy=multi-user.target [Service] ExecStart=/bin/bash /home/you/yourscript.py Type=simple User=root Group=root WorkingDirectory=/home/you Restart=on-failure

Put this in a file /usr/lib/systemd/system/intruder.service. Then run systemctl daemon-reload (once, and whenever you change intruder.service to tell systemd to re-read the configuration) and systemctl start intruder.service to start the e server.

If that works as expected, you can use systemctl enable intruder.service to make sure it starts at boot.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required