Turning your TV on or off using the Pi

For this example, we are relying on a technology called Consumer Electronics Control (CEC), which is a feature of the HDMI standard to send control messages to your home electronics equipment.

To help us send these messages, we'll need a software package called libCEC. While previously we had to build the libCEC package ourselves, we can now download a version for Raspbian Jessie using the installer:

pi@raspberrypi ~ $ sudo apt-get install libcec3 cec-utils

We will be using a utility called cec-client to send CEC messages to the TV. Issue the following command to switch off your TV:

pi@raspberrypi ~ $ echo "standby 0" | cec-client -d 1 -s

Use the following command to turn your TV on again:

pi@raspberrypi ~ $ echo "on 0" | cec-client -d 1 -s

Note that your TV must be able to support these commands. Not all TVs support the standby command through the HDMI port. You can play around with the cec-client console to discover the devices and their features connected to the HDMI bus on your setup.

Scheduling a playback scare

At this stage, you already know all the individual techniques used for this example. It's simply a matter of combining what you've learned so far to achieve the effect you want.

We'll try to illustrate a bit of everything with one sweet prank: you will prepare your Pi at home, take it over to your friend's house, and sneakily hook it up to the living room TV. In the middle of the night, the TV will turn itself on and a creepy video of your choice will start to play. This freaky incident might repeat itself a couple of times during the night, or we could take the prank to phase two: whenever someone walks into the room, their presence is detected and the video is played.

Let's start prepping the Pi! We will assume that no network connection is available at your friend's house, so we'll have to create a new ~/autorun.sh script to perform our prank, together with a timer in /etc/rc.local that starts counting down when the Pi is plugged in at your friend's house.

Here's the new ~/autorun.sh script:

#!/bin/sh 
# 
# Raspberry Pi Video Prank Script 
# Use chmod +x ~/autorun.sh to enable. 
 
CREEPY_MOVIE="AJn5Y65GAkA.mp4" # Creepy movie to play, located in the Pi home directory 
 
MOVIE_LOOPS="1" # Number of times to play creepy movie (1 by default) 
 
MOVIE_SLEEP="3600" # Number of seconds to sleep between movie plays (1 hour by default) 
 
WEBCAM_PRANK="y" # Set to y to enable the motion detection prank 
 
tv_off() { 
  if [ "$(echo "pow 0" | cec-client -d 1 -s | grep 'power status: 
    on')" ]; then # If TV is currently on 
  echo "standby 0" | cec-client -d 1 -s # Send the standby command 
  fi 
} 
 
prepare_tv() { 
  tv_off # We switch the TV off and on again to force the 
    active channel to the Pi 
  sleep 10 # Give it a few seconds to shut down 
  echo "on 0" | cec-client -d 1 -s # Now send the on command 
  sleep 10 # And give the TV another few seconds to wake up 
  echo "as" | cec-client -d 1 -s # Now set the Pi to be the 
    active source 
} 
 
play_movie() { 
  if [ -f ~/"$CREEPY_MOVIE" ]; then # Check that the creepy movie 
    file exists 
  omxplayer -o hdmi ~/"$CREEPY_MOVIE" # Then play it with sound 
    going out through HDMI 
  fi 
} 
 
 
case "$1" in 
  prankon) # Signal from Motion that event has started 
    prepare_tv 
    play_movie 
    tv_off 
    ;; 
 
  prankoff) # Signal from Motion that event has ended 
    ;; 
 
  *) # Normal start up of autorun.sh script 
for i in $(seq $MOVIE_LOOPS) # Play creepy movie in a loop the number of times specified 
do 
   prepare_tv 
   play_movie 
   tv_off 
   sleep "$MOVIE_SLEEP" # Sleep the number of seconds specified 
done 
 
    start_webcam_prank # Begin prank phase 2 
    ;; 
esac

Don't forget to give the script executable permission using the following:

pi@raspberrypi ~ $ chmod +x ~/autorun.sh.

Now all we need to do is adjust /etc/rc.local to set a timer for our autorun.sh script using the at command. Type in sudo nano /etc/rc.local to open it up for editing, and adjust the following block:

if [ -x /home/pi/autorun.sh ]; then 
  sudo -u pi at now + 9 hours -f /home/pi/autorun.sh 
fi

So if you plug in the Pi at your friend's house at 6pm, strange things should start happening right around 3am in the morning.

As for what creepy movie to play, we leave that entirely up to you. There's a tool called youtube-dl that you might find useful. Install it and update it with the following sequence of commands:

pi@raspberrypi ~ $ sudo apt-get install youtube-dl
pi@raspberrypi ~ $ sudo wget https://yt-dl.org/latest/youtube-dl
-O /usr/bin/youtube-dl

Note

Be careful not to introduce line breaks within each of the command lines; otherwise, they will not run—breaks are only present in these ones for display reasons.

Now you could use it to fetch videos like this:

pi@raspberrypi ~ $ youtube-dl http://www.youtube.com/watch?v=[creepyvideoid]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.123.189