Fudge Cam – Python Code
This project achieves many objectives. It gives me a chance to learn how to use Python to control external devices (in this case a very cheap motion detecting sensor) and it also makes my girlfriend happy. Even though she does (rightly as there is a time and a place..!) complain about my “enthusiasm” for getting a pin to connect to a software interrupt. Anyway. Here it is:
Capturing the antics of Fudgie the Hamster!
Detects motion using a PIR Motion Detection GPIO sensing kit to leverage the GPIO pins on the Raspberry Pi, then takes photos using the Raspberry Pi Camera Module and updates Twitter with a random rant from Fudgie himself.
You will need to install “python-picamera”, which is a pure Python interface to the Raspberry Pi camera module for Python 2.7 (or above) or Python 3.2 (or above). The python-picamera library is available in the Raspbian archives. Install with apt:
sudo apt-get update sudo apt-get install python-picamera
Alternatively, the Python3 package is installed with sudo apt-get install python3-picamera. An offline version of the documentation is available with
sudo apt-get install python-picamera-docs.
Note: Fudge Cam will soon be expanded to record video using Python. This can be displayed on a webpage:
camera.start_recording('video.h264') sleep(5) camera.stop_recording()
Meanwhile here is the current code:
import RPi.GPIO as GPIO import random, time, os import tweepy import picamera pir = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(pir, GPIO.IN) ### TWITTER SETTINGS ### # Set access keys via https://apps.twitter.com api_key = 'your_api_key_number' api_secret = 'your_api_secret_number' access_token = 'your_access_token_number' token_secret = 'your_token_secret_number' auth = tweepy.OAuthHandler(api_key, api_secret) auth.set_access_token(access_token, token_secret) api = tweepy.API(auth) my_twitter = api.me() print my_twitter.name, "is connected! Press CTRL + C to quit." # Three statuses. We'll pick one at random to go with our pic tweet_text = ['Aargh! I cannot sleep because I am nocturnal!', 'This'll teach her! I wanted Sunflower seeds for dinner!!', 'Let's party! I am drunk on fermented apples!'] ### CAMERA SETTINGS ### camera = PiCamera() cam_res = (1024, 768) camera.led = False # Turn off LED so we don't scare Fudge! pics_taken = 0 time.sleep(1) ### MAIN FUNCTIONS ### def motion_sense(pir): print "Fudge action! Motion detected... Taking picture!" take_picture(cam_res) def take_picture(resolution): global pics_taken camera.resolution = resolution # Capture a sequence of frames camera.capture(os.path.join( 'pics', 'image_' + str(pics_taken) + '.jpg')) pics_taken += 1 print "Gotcha! Picture taken! Tweeting it now...""" update_twitter() def update_twitter(): api.update_with_media(os.path.join( 'pics', 'image_' + str(pics_taken -1) + '.jpg'), status = random.choice(tweet_text)) print "Status updated!" #We don't want to tweet more than once per minute! time.sleep(60) ### MAIN PROGRAM LOOP ### try: GPIO.add_event_detect(pir, GPIO.RISING, callback=motion_sense) while True: time.sleep(60) except KeyboardInterrupt: print "\nQuitting" finally: camera.close() GPIO.cleanup()
The stuff you need to do this is obtained from Amazon: