Saturday, February 8, 2014

BeagleBone Black TTL serial camera home security system with private motion activated tweets

In 2013, I bought my first BeagleBone Black with the intention to jump head first into new robotics projects.  I had been reading a lot of books and blogs on robotics over the past few years, with my career starting taking precedence to my passion projects, but it was time to get back into it.  My last robot was a maze navigating and flame detecting & extinguishing mobile robot.  Embedded devices have evolved quite a bit since that HC12 project with the recent popularity of Arduino, Raspberry Pi, BeagleBone and similar products.  It's a perfect storm of embedded electronics fun.

To get started, I bought the BeagleBone Black starter kit from Adafruit (awesome company) and a series of parts and tools from Amazon and The Robot Shop (Canadian!) - more on this in another post.

I also needed a good introductory project to follow the classic embedded equivalent to Hello World: make a LED blink.  Since I was about to head out on a vacation out of the country for two weeks, I decided to build a home security system.

Here's my part list:
  • Weatherproof TTL Serial JPEG Camera with NTSC Video and IR LEDs
  • Adafruit Beagle Bone Black Starter Pack 
  • USB Wifi dongle
  • 2 port USB hub
  • 32Gig micro SD card
  • That's it!
I've always believed in code over hardware in design decisions.  Even with my electrical engineering background, I find it less costly, more flexible and more robust to code solutions rather than to rely on hardware.

Here's a Python code snippet of my project.

 enable_tty01 = 'sudo sh -c \'echo ttyO1_armhf.com > /sys/devices/bone_capemgr.9/slots\''   
 os.system(enable_tty01)   
 filepath = "/home/ubuntu/"  
 def initialize():   
   resp = ""  
   time.sleep(1)  
   while(serial.inWaiting() > 0):  
       data = serial.read()  
       resp += data  
       if "Init end\r\n" in resp:  
           print "Ready"  
           break  
   # Set image size to 640 x 480  
   serial.write(b'\x56\x00\x54\x01\x00')  
   resp = ""  
   time.sleep(1)  
   while (serial.inWaiting() > 0):  
     data = serial.read()  
     resp += data  
     if b'\x76\x00\x54\x00\x00' in resp:  
       print "Size set"  
       break  
 #Picture function  
 def takePic():  
   print "Take Picture"  
   # Take picture  
   cam.takephoto()  
   #Get JPG size  
   serial.write(b'\x56\x00\x34\x01\x00')  
   resp = ""  
   #time.sleep(1)  
   print "Get image size from serial device"  
   bytes = cam.getbufferlength()  
   # Write image to file  
   print "Read image from serial device"  
   return cam.readbuffer(bytes)  
 # Initialize serial connection.  
 serial = serial.Serial("/dev/ttyO1", baudrate=38400)  
 serial.write(b'\x56\x00\x26\x00')  
 cam = VCam(serial)  
 # Initialize the camera settings for the first picture.  
 initialize()  
 tweet = Tweet()  
 cam.enablemotion()  
 while 1:  
   # Continuously check for motion detection.  
   if (cam.motionDetected()):  
     # If motion is detected, take a pic.      
     cam.disablemotion()  
     frame1 = takePic()  
     string_frame = ''.join(frame1)  
     now = datetime.datetime.now()  
     filename = "%d.%02d.%02d.%02d.%02d.%02d.jpg" % \  
     (now.year,now.month,now.day,now.hour,now.minute,now.second)  
     try:  
       f = open(filepath + filename, 'w')  
       f.write(string_frame)  
     except:  
       print "Error writing the file to the system."  
     else:  
       f.close()  
     #Post the image to Twitter in a new thread to resume motion detection.  
     t = Thread(target = tweet.postimage, args = (filepath, filename))  
     t.start()  
     cam.reset()  
     initialize()  
     cam.enablemotion()  


I had a number of challenges in getting my program to work.  The main two include:


  1. The camera's baud rate is rather slow so I wasn't able to implement the motion detection on the computer and had to rely on the built in feature from the camera.  In the future, I will use a USB or IP camera instead.
  2. The wifi dongle was difficult to install but with enough googling, I was able to find working steps.  While I was working on this project, the BeagleBone Black was still relatively new compared to the Raspberry Pi so there were limited examples available online.  This is no longer the case.


And finally, the working prototype!



While I was away, I had pictures of the sun rise and sun set lighting change against the wall of my home office tweeted to me on a daily basis (they were detected as motion).  It was nice to see everything was good at home while being away.

Until next time!


24 comments:

  1. This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. alarm companies port-aransas texas

    ReplyDelete
  2. I needed to thank you for this awesome read!! I certainly getting a charge out of each and every piece of it I have you bookmarked to look at new stuff you post. Visit Now

    ReplyDelete
  3. Woh I am cheerful to discover this site through google.
    House Alarms

    ReplyDelete
  4. However, these can be extremely expensive to purchase and install. In this article you are going to learn about a cheap camera security system that you can install in your home for less than security camera system installation NY

    ReplyDelete
  5. Or he almost accidentally wins a Darwin award, can’t tell. www.forbrukeretaten.no/

    ReplyDelete
  6. This will rely upon the nature of the update and the increases you make to your kitchen. As indicated by the land operators, a completely useful and present day kitchen will support the estimation of your property drastically. hardwood Floor Refinishing overland park ks

    ReplyDelete
  7. You create sense out of the foremost complex topics.24 Hour Locksmith Pros

    ReplyDelete
  8. You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. Home Security

    ReplyDelete
  9. You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. full color night vision security camera

    ReplyDelete
  10. The Cori HD Smart Home Security Camera joins the Momentum camera lineup including the Axel HD Smart Camera, Aria LED Floodlight Camera and the Niro Garage Door Controller. Along with all Momentum Wi-Fi cameras, the Cori cameras can be controlled and viewed from the same mobile app, as well as other home-based consumer electronics from NEST.
    Complete Security Alarms System

    ReplyDelete
  11. Great! We will be connecting to this enormous post on our site. Continue the good writing.Marietta locksmith

    ReplyDelete
  12. Surveillance camera frameworks give a lot of assistance in keeping various unlawful acts and have contributed towards reclassifying home security, however security at organizations and working environments. Surveillance cameras that accompany a wide scope of highlights and choices are a
    building intercom system upgrade

    ReplyDelete
  13. There are also scene modes included with compact cameras that allow you to choose a setting to match the scene you are shooting.https://www.optics1.eu

    ReplyDelete
  14. Your articles make whole sense of every topic.
    IP camera systems

    ReplyDelete
  15. The superb highly informative blog I’m about to share this with all my contacts.
    self storage security

    ReplyDelete
  16. Picking a play kitchen set relies upon the age of the youngster, your financial plan and your setting. It likewise relies upon your own rationality. Where is that play kitchen made? pressure cooker directions

    ReplyDelete
  17. When the flow through a space is blocked, weak, or misdirected the buyer's perceived relationship to the home suffers, resulting in an unsuccessful tour. Dark, dirty, or stale spaces can choke the positive reaction to a home. Coupons & Discounts in San Diego

    ReplyDelete
  18. I truly like the diversion your distribute has advertised. I cherished this site an awesome arrangement. Continue posting article, for example, this. It's enjoyable. Pleasant photography excessively !
    vert shock reviews

    ReplyDelete
  19. In the contemporary context, one of the ingenious thing one can do with a home's equity is to put it right back into the home. Home improvement is besides providing you with the much needed changes, increases the equity of your home.home renovations Calgary

    ReplyDelete
  20. At the point when one discussions about setting aside cash through effective air conditioning frameworks the subject of SEER for the most part comes up. Airco Antwerpen

    ReplyDelete
  21. The monthly mortgage payment mainly pays off principal and interest. But most lenders also include local real estate taxes, home owner's insurance, and mortgage insurance (if applicable).canadian real estate

    ReplyDelete
  22. Molds are present in every home and in the fresh exterior air. If a mold spore is present on a food source such as drywall, wood trim, wood or even on dust it will not necessarily grow to a problem level. If that same spore and location were to have moisture added, now you have conditions where molds can grow and become an issue.Top Real Estate Agents in Clinton Missouri

    ReplyDelete
  23. Going through the final part of this article, you will see just how important budgeting and home improvement can be done which will help save you money and improve your home.portable shampoo bowl

    ReplyDelete
  24. Complete woodworkers have to have a wonderful eye when developing the design, joints and also general appearance of a job or space which suggests the option of that to work with could be vital for an effective job or improvement required for the general job success.my blog

    ReplyDelete