rphgeek
Posts: 21
Joined: Sat Sep 14, 2013 12:28 pm

Rasperry Pi Zero Random Simpsons Episode Shuffler

Wed Dec 13, 2017 2:51 pm

Raspberry Pi Zero
Case
MicroSD card
Momentary Push button
2 small wires
Micro USB power supply
HDMI cable
Pi1.jpg
Pi1.jpg (34.84 KiB) Viewed 1844 times
Pi2.jpg
Pi2.jpg (62.25 KiB) Viewed 1844 times
I always wanted a dedicated device to just play random episodes of my favorite shows and since I was able to get a raspberry pi zero, I thought this would be a perfect opportunity to do just that. I don’t claim any ownership for coming up with any of the code below, I only put a few web site searches together to meet my needs. The credit goes fully to the websites I’ve references below. I hope you enjoy it!

The only physical preparation needed for this project is to drill holes in the case for the button, attach one wire from the Momentary push button to the GPIO 5 pin and another wire from the push button to the GPIO 6 pin.

Step 1: Flash OS image to MircoSD card with Etcher

Step 2: Make the pi accessible through the USB
(found at https://learn.adafruit.com/turning-your ... net-gadget)

-open config file -> add “dtoverlay=dwc2” (without the quotes) as the last line
-open cmdline file -> after “rootwait”, add a space, then “modules-load=dwc2,g_ether” (without the quotes)
-add a blank file labeled “ssh” with no extension on it to allow PuTTy to access it

Step 3: Add the power button function to pins 5 & 6
(found at: https://howchoo.com/g/mwnlytk3zmm/how-t ... spberry-pi)

-Open PuTTY and go to IP: raspberrypi.local
Default ID: Pi Password: raspberry
-After connecting to your Pi, run the following command to create a file called listen-for-shutdown.py:

Code: Select all

sudo nano listen-for-shutdown.py
-Paste the following code into that file and save it:

Code: Select all

#!/usr/bin/env python


import RPi.GPIO as GPIO
import subprocess


GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)

subprocess.call(['shutdown', '-h', 'now'], shell=False)
-Start this script on boot. So we'll place the script in /usr/local/bin and make it executable:

Code: Select all

sudo mv listen-for-shutdown.py /usr/local/bin/

Code: Select all

sudo chmod +x /usr/local/bin/listen-for-shutdown.py
-Add another script called listen-for-shutdown.sh that will start/stop our service:

Code: Select all

sudo nano listen-for-shutdown.sh
-Enter the following code in that file and save it:

Code: Select all

#! /bin/sh

### BEGIN INIT INFO
# Provides:          listen-for-shutdown.py
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting listen-for-shutdown.py"
    /usr/local/bin/listen-for-shutdown.py &
    ;;
  stop)
    echo "Stopping listen-for-shutdown.py"
    pkill -f /usr/local/bin/listen-for-shutdown.py
    ;;
  *)
    echo "Usage: /etc/init.d/listen-for-shutdown.sh {start|stop}"
    exit 1
    ;;
esac

exit 0
-Place this file in /etc/init.d and make it executable.

Code: Select all

sudo mv listen-for-shutdown.sh /etc/init.d/

Code: Select all

sudo chmod +x /etc/init.d/listen-for-shutdown.sh
-Register the script to run on boot.

Code: Select all

sudo update-rc.d listen-for-shutdown.sh defaults
-Start the code with:

Code: Select all

sudo /etc/init.d/listen-for-shutdown.sh start

Step 4: Share internet connection via your PC

-Go to networks and settings -> change adapter settings
-Right click on the Ethernet connection and choose the “share” tab
-Check the box marked “allow other network users…” and choose the RNDIS gadget (possibly Ethernet 2 or 3) from the drop down.
-Reboot the pi

Step 5: Install the video looper
(found at: https://learn.adafruit.com/raspberry-pi ... r/overview)

-Use these 3 commands to get the video looper application

Code: Select all

sudo apt-get update

Code: Select all

sudo apt-get install -y git

Code: Select all

git clone https://github.com/adafruit/pi_video_looper.git
-Use these 2 commands to install the looper application

Code: Select all

cd pi_video_looper

Code: Select all

sudo ./install.sh
Step 6: Expand the size of SD card used

Code: Select all

sudo raspi-config
- go to advanced -> expand filesystem

Step 7: Transfer all videos to the Pi via WinSCP
-Connect to the pi via WinSCP
-Add a “videos” folder in home/pi
-Transfer any videos to be shown to that folder

Step 8: Change some configurations of the video looper:

In the main boot partition, open the file marked “video_looper.ini”. Any lines with a # are ignored so you will have to change that if you’re not going to play videos off of a USB drive.

-play random playlist: true

Code: Select all

	# To play random playlist.
        is_random = true

-turn off time between videos and startup screen

Code: Select all

	#osd = true
        osd = false

-change folder for videos to home/pi/videos

Code: Select all

	#file_reader = usb_drive
        file_reader = directory
path = /home/pi/videos

User avatar
rpdom
Posts: 14448
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Rasperry Pi Zero Random Simpsons Episode Shuffler

Wed Dec 13, 2017 3:08 pm

Step 3:

Instead of all that heavy work and code, you could try adding

Code: Select all

dtoverlay=gpio-shutdown
to the config.txt file.

Step 6
Is not required. The OS automatically expands the filesystem on first boot. This is on of the reasons it takes a while to start up the first time.

rphgeek
Posts: 21
Joined: Sat Sep 14, 2013 12:28 pm

Re: Rasperry Pi Zero Random Simpsons Episode Shuffler

Wed Dec 13, 2017 3:25 pm

Thanks for the suggestions rpdom! I'm not an expert by any stretch so I didn't know there was an easier way. I'll definitely give that a shot!

User avatar
rpdom
Posts: 14448
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Rasperry Pi Zero Random Simpsons Episode Shuffler

Wed Dec 13, 2017 4:33 pm

The gpio-shutdown option was only added fairly recently.

rphgeek
Posts: 21
Joined: Sat Sep 14, 2013 12:28 pm

Re: Rasperry Pi Zero Random Simpsons Episode Shuffler

Wed Dec 13, 2017 5:44 pm

Probably a day after I put in the work to get it done the old way. Lol!

yodko
Posts: 1
Joined: Tue May 14, 2019 3:36 pm

Re: Rasperry Pi Zero Random Simpsons Episode Shuffler

Tue May 14, 2019 3:42 pm

When the video player starts up it isn't finding the videos for me even after following these steps, I'm getting 'Insert USB drive with compatible movies'. I'm not using the GPIO switch for power as I have a on/off switch on my power cable. Is there something I am missing here?

Return to “Media centres”