{"id":2059,"date":"2014-04-22T08:54:28","date_gmt":"2014-04-22T07:54:28","guid":{"rendered":"http:\/\/mccltd.net\/blog\/?p=2059"},"modified":"2015-03-06T10:57:56","modified_gmt":"2015-03-06T10:57:56","slug":"fudge-cam-python-code","status":"publish","type":"post","link":"http:\/\/darenmatthews.com\/blog\/?p=2059","title":{"rendered":"Fudge Cam &#8211; Python Code"},"content":{"rendered":"<p>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 &#8220;enthusiasm&#8221; for getting a pin to connect to a software interrupt. Anyway. Here it is:<\/p>\n<h5><a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2014\/04\/fudge.jpg\"><img loading=\"lazy\" class=\"alignleft wp-image-2155\" src=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2014\/04\/fudge.jpg\" alt=\"fudge\" width=\"88\" height=\"103\" \/><\/a>Capturing the antics of Fudgie the Hamster!<\/h5>\n<p>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.<!--more--><\/p>\n<p>You will need to install &#8220;python-picamera&#8221;, 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:<\/p>\n<pre>sudo apt-get update\r\nsudo apt-get install python-picamera<\/pre>\n<p>Alternatively, the Python3 package is installed with sudo apt-get install python3-picamera. An offline version of the documentation is available with<\/p>\n<pre>sudo apt-get install python-picamera-docs.<\/pre>\n<p><b>Note:<\/b> Fudge Cam will soon be expanded to record video using Python. This can be displayed on a webpage:<\/p>\n<pre>camera.start_recording('video.h264')\r\nsleep(5)\r\ncamera.stop_recording()<\/pre>\n<p>Meanwhile here is the current code:<\/p>\n<pre>import RPi.GPIO as GPIO\r\nimport random, time, os\r\nimport tweepy\r\nimport picamera\r\n\r\npir = 17\r\nGPIO.setmode(GPIO.BCM)\r\nGPIO.setup(pir, GPIO.IN)\r\n\r\n### TWITTER SETTINGS ###\r\n# Set access keys via https:\/\/apps.twitter.com\r\napi_key = 'your_api_key_number'\r\napi_secret = 'your_api_secret_number'\r\naccess_token = 'your_access_token_number'\r\ntoken_secret = 'your_token_secret_number'\r\nauth = tweepy.OAuthHandler(api_key, api_secret)\r\nauth.set_access_token(access_token, token_secret)\r\napi = tweepy.API(auth)\r\nmy_twitter = api.me()\r\nprint my_twitter.name, \"is connected! Press CTRL + C to quit.\"\r\n# Three statuses. We'll pick one at random to go with our pic\r\ntweet_text = ['Aargh! I cannot sleep because I am nocturnal!',\r\n'This'll teach her! I wanted Sunflower seeds for dinner!!',\r\n'Let's party! I am drunk on fermented apples!']\r\n\r\n### CAMERA SETTINGS ###\r\ncamera = PiCamera()\r\ncam_res = (1024, 768)\r\ncamera.led = False # Turn off LED so we don't scare Fudge!\r\npics_taken = 0\r\ntime.sleep(1)\r\n\r\n### MAIN FUNCTIONS ###\r\ndef motion_sense(pir):\r\nprint \"Fudge action! Motion detected... Taking picture!\"\r\ntake_picture(cam_res)\r\n\r\ndef take_picture(resolution):\r\n  global pics_taken\r\n    camera.resolution = resolution\r\n    # Capture a sequence of frames\r\n    camera.capture(os.path.join(\r\n      'pics', 'image_' + str(pics_taken) + '.jpg'))\r\n    pics_taken += 1\r\n    print \"Gotcha! Picture taken! Tweeting it now...\"\"\"\r\n    update_twitter()\r\ndef update_twitter():\r\n  api.update_with_media(os.path.join(\r\n    'pics', 'image_' + str(pics_taken -1) + '.jpg'),\r\n    status = random.choice(tweet_text))\r\n  print \"Status updated!\"\r\n  #We don't want to tweet more than once per minute!\r\n  time.sleep(60)\r\n\r\n### MAIN PROGRAM LOOP ###\r\ntry:\r\n  GPIO.add_event_detect(pir, GPIO.RISING, callback=motion_sense)\r\n  while True:\r\n    time.sleep(60)\r\nexcept KeyboardInterrupt:\r\n  print \"\\nQuitting\"\r\nfinally:\r\n  camera.close()\r\n  GPIO.cleanup()<\/pre>\n<p><iframe src=\"http:\/\/astore.amazon.co.uk\/horsleynews-21\" width=\"90%\" height=\"4000\" frameborder=\"0\" scrolling=\"no\"><\/iframe><br \/>\nThe stuff you need to do this is obtained from Amazon:<\/p>\n<p><a href=\"http:\/\/www.amazon.co.uk\/gp\/product\/B00E1GGE40\/ref=as_li_tf_il?ie=UTF8&amp;camp=1634&amp;creative=6738&amp;creativeASIN=B00E1GGE40&amp;linkCode=as2&amp;tag=mccltd-21\"><img src=\"http:\/\/ws-eu.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B00E1GGE40&amp;Format=_SL110_&amp;ID=AsinImage&amp;MarketPlace=GB&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=mccltd-21\" alt=\"\" border=\"0\" \/><\/a><img loading=\"lazy\" style=\"border: none !important; margin: 0px !important;\" src=\"http:\/\/ir-uk.amazon-adsystem.com\/e\/ir?t=mccltd-21&amp;l=as2&amp;o=2&amp;a=B00E1GGE40\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> <a href=\"http:\/\/www.amazon.co.uk\/gp\/product\/B00GOPJJWU\/ref=as_li_tf_il?ie=UTF8&amp;camp=1634&amp;creative=6738&amp;creativeASIN=B00GOPJJWU&amp;linkCode=as2&amp;tag=mccltd-21\"><img src=\"http:\/\/ws-eu.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B00GOPJJWU&amp;Format=_SL110_&amp;ID=AsinImage&amp;MarketPlace=GB&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=mccltd-21\" alt=\"\" border=\"0\" \/><\/a><img loading=\"lazy\" style=\"border: none !important; margin: 0px !important;\" src=\"http:\/\/ir-uk.amazon-adsystem.com\/e\/ir?t=mccltd-21&amp;l=as2&amp;o=2&amp;a=B00GOPJJWU\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> <a href=\"http:\/\/www.amazon.co.uk\/gp\/product\/B00G9AZ79O\/ref=as_li_tf_il?ie=UTF8&amp;camp=1634&amp;creative=6738&amp;creativeASIN=B00G9AZ79O&amp;linkCode=as2&amp;tag=mccltd-21\"><img src=\"http:\/\/ws-eu.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B00G9AZ79O&amp;Format=_SL110_&amp;ID=AsinImage&amp;MarketPlace=GB&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=mccltd-21\" alt=\"\" border=\"0\" \/><\/a><img loading=\"lazy\" style=\"border: none !important; margin: 0px !important;\" src=\"http:\/\/ir-uk.amazon-adsystem.com\/e\/ir?t=mccltd-21&amp;l=as2&amp;o=2&amp;a=B00G9AZ79O\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;enthusiasm&#8221; for getting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[74],"tags":[88],"_links":{"self":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2059"}],"collection":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2059"}],"version-history":[{"count":18,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2059\/revisions"}],"predecessor-version":[{"id":2156,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2059\/revisions\/2156"}],"wp:attachment":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2059"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}