{"id":5154,"date":"2015-02-24T15:39:40","date_gmt":"2015-02-24T15:39:40","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=5154"},"modified":"2020-03-05T01:24:19","modified_gmt":"2020-03-05T01:24:19","slug":"programming-the-beaglebone-black-with-python","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/programming-the-beaglebone-black-with-python\/","title":{"rendered":"Programming the BeagleBone Black with Python"},"content":{"rendered":"<div style=\"padding: 10px; line-height: 1.2; text-align: center; background-color: #f5f5f5;\">This entry is part 4 of 4 in the series <a href=\"https:\/\/randomnerdtutorials.com\/category\/bbb-series\/\">Getting Started with BeagleBone Black<\/a>. This post\u00a0was written by\u00a0Rui Santos and Lu\u00eds Perestrelo authors of <a href=\"https:\/\/amzn.to\/2TrwWEJ\" target=\"_blank\" rel=\"noopener noreferrer\">BeagleBone For Dummies<\/a> *<\/div>\n<p>\u00a0<\/p>\n<p>Building a surveillance system with a PIR sensor, the BeagleBone Black and Python.The BeagleBone Black is an outstanding tool for projects that involve the Internet. Access is easy (simply connect it to the router through an Ethernet cable ), and both Python and JavaScript feature libraries that greatly simplifies matters.<\/p>\n<p>PIR sensors are quite neat devices that are able to detect motion. Their output pin, which will be connected to the <a href=\"http:\/\/amzn.to\/1xukLSK\" target=\"_blank\" rel=\"noopener noreferrer\">BeagleBone Black<\/a> * sends a digital signal HIGH whenever somebody passes by, and is LOW whenever else.\u00a0Automatic lights in public places\u00a0are built on this concept.<\/p>\n<p>The project\u00a0sends you an email whenever motion is detected.<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-5159\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/beaglebone-black-pir-motion-sensor.jpg?resize=479%2C358&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"beaglebone black pir motion sensor\" width=\"479\" height=\"358\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/beaglebone-black-pir-motion-sensor.jpg?w=648&amp;quality=100&amp;strip=all&amp;ssl=1 648w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/beaglebone-black-pir-motion-sensor.jpg?resize=300%2C224&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/p>\n<h2>Parts Required<\/h2>\n<p>Here&#8217;s a list with everything you need:<\/p>\n<ul>\n<li>BeagleBone Black (<a href=\"http:\/\/amzn.to\/1xukLSK\" target=\"_blank\" rel=\"noopener noreferrer\">Click to see on Amazon.com<\/a> *)<\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\">PIR Motion Sensor<\/a><\/li>\n<\/ul>\n<h2>Schematics<\/h2>\n<p>NOTE: You can read my tutorial\u00a0on\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/modifying-cheap-pir-motion-sensor-to-work-at-3-3v\/\">how to modify a cheap PIR motion sensor to operate\u00a0at 3.3V<\/a>.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-5157\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/bbb_pir_sensor.png?resize=408%2C590&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"bbb_pir_sensor\" width=\"408\" height=\"590\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/bbb_pir_sensor.png?w=1044&amp;quality=100&amp;strip=all&amp;ssl=1 1044w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/bbb_pir_sensor.png?resize=208%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 208w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/bbb_pir_sensor.png?resize=709%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 709w\" sizes=\"(max-width: 408px) 100vw, 408px\" \/><\/p>\n<h2>Python Code<\/h2>\n<p>You can <a href=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/surveillance.zip\" target=\"_blank\" rel=\"noopener noreferrer\">click here to download<\/a> this Python script.<\/p>\n<p>NOTE: You can only use this example with Gmail. And don&#8217;t forget to replace the example below with your own credentials&#8230;<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">#!\/usr\/bin\/python\n \n#import libraries\nimport smtplib\n\nimport Adafruit_BBIO.GPIO as GPIO\nimport time\n \n#create a variable called PIR, which refers to the P8_11 pin\nPIR = &quot;P8_11&quot;\n \n#initialize the pin as an INPUT\nGPIO.setup(PIR, GPIO.IN)\nGPIO.add_event_detect(PIR, GPIO.RISING)\n\ndef send_email():\n    server = smtplib.SMTP('smtp.gmail.com', 587)\n    server.ehlo()\n    server.starttls()\n    server.ehlo()\n    my_email = &quot;REPLACE_WITH_YOUR_EMAIL@gmail.com&quot;\n    my_password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n    destination = &quot;REPLACE_WITH_YOUR_EMAIL@gmail.com&quot;\n    text = &quot;Motion has been detected at your house!&quot;\n    \n    server.login(my_email, my_password)\n    server.sendmail(my_email, destination, text)\n    server.quit()\n    print(&quot;Your email has been sent!&quot;)\n       \n#loop forever\nwhile True:\n    #sends an email when motion has been detected\n    if GPIO.event_detected(PIR):\n        send_email() \n    time.sleep(0.05) #loop every 50 miliseconds to not overburden the CPU\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/BeagleBone\/BBB-Surveillance-Python\/surveillance.py\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>Now you&#8217;ve built your own surveillance system that sends you an email when the PIR sensor detects movement!<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-medium wp-image-5160\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/email.png?resize=300%2C152&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"email\" width=\"300\" height=\"152\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/email.png?resize=300%2C152&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/email.png?w=902&amp;quality=100&amp;strip=all&amp;ssl=1 902w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>If you have any questions post them in the comments\u00a0below.\u00a0Special thanks to Lu\u00eds Perestrelo for helping Rui Santos putting this series together!<\/p>\n<p style=\"text-align: center;\"><strong>Want to\u00a0learn more about the BeagleBone Black?<\/strong><\/p>\n<p style=\"text-align: center;\"><strong>You can buy our book <a href=\"https:\/\/amzn.to\/2TrwWEJ\" target=\"_blank\" rel=\"noopener noreferrer\">BeagleBone For Dummies<\/a> * on Amazon!<\/strong><\/p>\n\n\n<p><em>* We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This entry is part 4 of 4 in the series Getting Started with BeagleBone Black. This post\u00a0was written by\u00a0Rui Santos and Lu\u00eds Perestrelo authors of BeagleBone For Dummies * \u00a0 &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Programming the BeagleBone Black with Python\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/programming-the-beaglebone-black-with-python\/#more-5154\" aria-label=\"Read more about Programming the BeagleBone Black with Python\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":5159,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305,217,270,204,205,264,206],"tags":[],"class_list":["post-5154","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-other","category-bbb-series","category-beaglebone-project","category-beaglebone","category-beaglebone-black","category-project","category-b-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/beaglebone-black-pir-motion-sensor.jpg?fit=648%2C484&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/5154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=5154"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/5154\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/5159"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=5154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=5154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=5154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}