brandorf.com The everyday ramblings of one nerd.

20May/13

Annoying people with a Raspberry Pi

The Raspberry Pi is a neat little device. First and foremost, it's a complete little computer, secondly it's very easy to program for, allowing a software guy like myself to have a much lower learning curve to AVR-style programming than you would have on an Arduino or Launchpad. I've had one for a while, but nothing immediately sprung to mind about anything fun to do with it, so it disappeared into the big bucket of projects-that-might-be.

I eventually decided to use it for evil. To sic Navi on the poor victim, like a more annoying version of the Annoy-a-tron
(If that's possible?).

Principal ingredients for this is the Pi itself, and some speakers. I'm using one of those speaker case jobs designed for a MP3 players, runs off of 2 AA batteries for a very long time, unfortunately this won't power the Pi, so the Pi still needs to be plugged in (and the Pi can only provide a wimpy 50ma at 5v on pins, not enough to power a speaker). That's really it on the hardware department, the rest of the magic is software - namely Python. I've never worked with python, but the more languages you learn the easier new ones become (both spoken languages and programming). So let's bang something out and see what we can get it to do.

photo (1) photo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
 
import time
import random
import os
import subprocess
 
random.seed()
command = ["mpg321", "/etc/Navi/Navi-WatchOut.mp3"]
print "Watch Out!"
with open(os.devnull, "w") as fnull:
	result = subprocess.call(command, stdout=fnull, stderr=fnull)
time.sleep(30)
 
while (True):
	command = ["mpg321", "/etc/Navi/Navi-Listen.mp3"]
	with open(os.devnull, "w") as fnull:
		result = subprocess.call(command, stdout=fnull, stderr=fnull)
	sleepTime = random.randrange(-15,15)
	time.sleep((30 + sleepTime) * 60)
	print "Hey! Listen"
	print "Sleeping for " + str(30 + sleepTime) , " minutes."

For those of you who can't decipher this what it does is play the "Hey Listen!" sound clip at random intervals between 15 and 45 minutes between each one. It's using the linux app mpg321 to play the audio files with most of the extra bits there to hide the output from mpg321 from spamming the console. Not really needed as the prank use will not have the Pi connected to a display but hey, why not.

To make sure that this script starts each time the PI is booted, we need to add this script to Init.d I must confess I'm not entirely sure what this is doing except at a high level; registering our python script as a service, and therefore runs even if you don't log-in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#! /bin/sh
 
case "$1" in
  start)
    echo "Starting NAVI"
    # run application you want to start
    sudo amixer cset numid=1 90%
    python /etc/Navi/Navi.py &
    ;;
  stop)
    echo "Stopping NAVI"
    # kill application you want to stop
    killall python
    ;;
  *)
    echo "Usage: /etc/init.d/example{start|stop}"
    exit 1
    ;;
esac
 
 
exit 0

Save the scripts, chmod them if needed run update-rc.d and restart the Pi, you'll get a warning of "Lookout" and then you've got a minimum of 15 minutes to plant your annoyance bomb. Try not to get caught.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.