Monday, November 17, 2014

Critter Monitoring on the Homestead with Raspberry Pi

Successfully captured a freshly laid egg:

  • Objectives
    • Discover what is happening with our chicken eggs. We went from 2-3 eggs per day to no eggs at all. As was mentioned in previous posts it turned out that the stop in eggs was from a chicken virus, but this project was started before that discovery. We needed to know where our eggs were going despite seeing the chickens nesting in the boxes and in the middle of the coop, and one bird possibly going broody.
    • The solution needed to be remotely accessible, and it needed to be wireless. In other words it need to be monitored from the comfort of my couch.
    • The solution needed to be asynchronous. In other words it needed to record activity and allow me to review in a fraction of the real rime that was monitored = time lapse video.
  • Equipment
    • Raspberry Pi (I used model B+, but any model would work)
    • 2.1 Amp USB power supply (smart phone charger)
    • USB WiFi (because who really wants to trench CAT 5 cable through their yard)
    • USB webcam
    • USB memory card (or a really big SD card)
    • dust and water barrier (1 quart sandwich bag works)
  • Software
    • Raspbian (any unix version running on your Pi could work)
    • fswebcam (capture images from the webcam)
    • mencoder (converts images to an avi)
    • x11vnc (for remote desktop on the pi, this is easy enough to setup, but tightVNCserver is even easier, has more publicity and will work in most cases)
    • Remmina Remote Desktop Client (remote gui / remote desktop)
    • SSH from a terminal window on my desktop (remote command line access)
  • My coop set up
    • Outside
    • Inside
    • Camera placements
    • Camera point of view
  • My scripts
    • Image capture
    • Video creation
    • Still image clean up
      • flushold.sh
      • my stills are stores on a USB drive, and I'm deleting images that are 2 days old or older. This script is scheduled to run once per week.
    • Crontab entries
      • crontabsample.txt
      • 4 tasks are in my cron table, 
        • fast images, 
        • slow images, 
        • video creation, 
        • still image clean up
      • you edit your cron jobs with "sudo crontab -e
    • 2 commands from my home machine
      • grab the new video
      • SSH - image flushing, I'll write a bash script and add it to the crontab eventually
  • Tips and things you will become familiar with
    • As soon as you have it mostly working start making image copies of your Raspberry Pi SD card. If you don't, you will eventually do something that messing up your card and you will have to re-install and configure everything again. I had to do this twice before I learned my lesson while getting everything to autostart on boot.
    • Cron jobs
      • These are basically scheduled tasks, but they can only be triggered to run as fast as once per minute. Chickens move around a lot in 1 minute.
      • 24/7 video recording doesn't make sense when your observable area is empty for 6 hours and black for 12 hours.
      • Use the crontab to record different rates during different time windows
    • Bash scripting
      • because typing 12 characters on 1 line 20 times is way better than 100's of characters on multiple lines 20 times is much easier
      • because updating 1 file (script) is much better than updating the same time 20 times.
      • because bash has flow control, logic, and 'pause'
      • because you can use variables
    • Performance issues
      • Image resolutions
        • size conversions take processor power and time, make everything the same resolution. If your webcam is 720p set your image capture resolution to 1280x720, and set your video output to be 1280x720. This will make your webcam capture faster so you can take more pictures and have better video, and the video creation will be much faster. If you need to change resolutions do it first at the camera capture and try to scale by an integer like 2 or 4 especially if making things smaller. Computers have an easier time dividing evenly without remainders to deal with, can pictures scale faster and better when the process doesn't have to deal with partial pixels. If you scale during video creation be prepared to wait more than an hour for it create the video from the day. Without scaling my setup with create the video file at approximately 15fps, and if there is scaling to be done that drops to 3fps or slower. The other problem with video scaling is it needs to create a temporary file for each image that it scales and the disk access speeds on the Pi is slow (limited by the speed of your SD card or USB stick).
      • WiFi Range and interference
        • walls add up, you make need an access point outside or you may be able to us an USB WiFi with external antennae. My smart phone had WiFi at the the chicken coop, but had no signal from inside the chicken coop. Now you could just set a static IP on your Pi and go outside next to your coop with a laptop configured with a static IP in the same domain as the Pi and get your video, but that's a horrible option when it's below freezing, or really sunny (can't view your screen), or raining. For me, it is really worth it to get your remote Pi onto your home network. Despite being able to see my home router through a window in the house I still needed to use an external antennae on my Pi and I have the antennae poking out through a roof vent to maintain good stable WiFi signal.
        • Chicken wire is a bad RF blocker in general, but it really causes signal degradation in WiFi zones. I have WiFi signal inside my garage which is further from the router than the coop is. My next desperate attempt to have coop connectivity was to run CAT5 from the coop to the garage and put an access point in the garage.
      • Start with your camera far away as possible. You want to see the whole area first and not just the small area you think the issue is at. There is a lot that happens off camera if you start the other direction. I learned that the hard way so you won't have to.
      • If your camera is placed where your critters can touch it, you need to protect it. With my chickens they really couldn't hurt it, and they mostly ignored it. The ignored the camera so much so that they roosted on it, giving me 1/2 a day of taking pictures of the bedding 12 inches from the camera and 4 hours of feathers on a chicken butt.
    • A day in a chicken coop (sample video)
      • At about 2:30 clock time an egg appears This mean the birds will get to stay for at least another week.
  • Future add-ons to this project
    • streaming video with audio
    • temperature and humidity monitoring
    • automatic coop door

No comments:

Post a Comment