{"id":2453,"date":"2015-08-04T21:20:39","date_gmt":"2015-08-04T21:20:39","guid":{"rendered":"http:\/\/www.richardmudhar.com\/?p=2453"},"modified":"2015-08-05T08:29:02","modified_gmt":"2015-08-05T08:29:02","slug":"wildlife-camera-with-raspberry-pi-motion-and-temperature-sensing","status":"publish","type":"post","link":"https:\/\/www.richardmudhar.com\/blog\/2015\/08\/wildlife-camera-with-raspberry-pi-motion-and-temperature-sensing\/","title":{"rendered":"Wildlife camera with Raspberry Pi, Motion, and temperature sensing"},"content":{"rendered":"<p>I&#8217;m toying with the idea of going along to the <a href=\"http:\/\/www.ipswichraspberryjam.co.uk\/\">Ipswich Raspberry Jam<\/a> on Saturday 8th Aug and figure it&#8217;s be nice to have something to show. There&#8217;s of course our <a href=\"http:\/\/www.richardmudhar.com\/2014\/07\/remote-farm-camera-experimenting-with-wifi\/\">farm Raspberry Pi cameras<\/a> which are in service and this one is riffing a bit off an idea <a href=\"http:\/\/wildlifegadgetman.com\/\">Wildlife Gadget Man<\/a> is playing with. He&#8217;s the guy with the wildlife &#8211; I only have sparrows[ref]I like my sparrows but they aren&#8217;t going to pose long enough for the camera, and presumably they have their heads under their wings in a hedge somewhere now, a hedgehog in a hog box is the sort of target that would work well here[\/ref] so I have to make do with a stuffed toy stoat \ud83d\ude42<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2455\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/stoatpi-550x413.jpg?resize=550%2C413\" alt=\"stoatpi\" width=\"550\" height=\"413\" srcset=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/stoatpi.jpg?resize=550%2C413&amp;ssl=1 550w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/stoatpi.jpg?resize=624%2C468&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/stoatpi.jpg?w=960&amp;ssl=1 960w\" sizes=\"auto, (max-width: 550px) 85vw, 550px\" \/><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/temp.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2456\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/temp-550x252.png?resize=550%2C252\" alt=\"temp\" width=\"550\" height=\"252\" srcset=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/temp.png?resize=550%2C252&amp;ssl=1 550w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/temp.png?resize=624%2C285&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/temp.png?w=866&amp;ssl=1 866w\" sizes=\"auto, (max-width: 550px) 85vw, 550px\" \/><\/a><\/p>\n<p>There&#8217;s nothing earth-shatteringly new in here, but the ability to make a box which gives you video, snapshots and a temperature plot taken from one of those Chinese waterproof DB18B20 probes is good for mammals.<\/p>\n<p><!--more--><\/p>\n<p>The reason the picture is soft is because the camera is fixed focus and 15cm from the stoat; it needs reading glasses or a close-up lens.<\/p>\n<p>It&#8217;s not demanding of hardware; I run this on an <a href=\"http:\/\/elinux.org\/RPi_HardwareHistory\" target=\"_blank\">old-skool Raspberry Pi Model A<\/a> with the big board headless and a nano USB dongle doing WiFi connectivity<\/p>\n<p>Basically you run Motion on a Pi &#8211; that used to be hard but it&#8217;s as simple as :<\/p>\n<p># <a href=\"http:\/\/raspberrypi.stackexchange.com\/questions\/10480\/raspi-camera-board-and-motion\" target=\"_blank\">get your Pi camera onto \/dev\/video<\/a><\/p>\n<p>sudo nano \/etc\/modules<\/p>\n<p>append<\/p>\n<p>bcm2835-v4l2<\/p>\n<p>like the man says, then sudo reboot, then<\/p>\n<blockquote><p>sudo apt-get update<\/p>\n<p>sudo apt-get upgrade<\/p>\n<p>sudo apt-get install motion<\/p>\n<p>sudo nano \/etc\/motion\/motion.conf<\/p><\/blockquote>\n<p>change resolution to 960 by 720<\/p>\n<blockquote><p># Rotate image this number of degrees. The rotation affects all saved images as<br \/>\n# well as mpeg movies. Valid values: 0 (default = no rotation), 90, 180 and 270.<br \/>\nrotate 0<\/p>\n<p># Image width (pixels). Valid range: Camera dependent, default: 352<br \/>\nwidth 960<\/p>\n<p># Image height (pixels). Valid range: Camera dependent, default: 288<br \/>\nheight 720<\/p><\/blockquote>\n<p>set snapshot to every minute<\/p>\n<blockquote><p># Make automated snapshot every N seconds (default: 0 = disabled)<br \/>\nsnapshot_interval 60<\/p><\/blockquote>\n<p>ice this<\/p>\n<blockquote><p># Restrict webcam connections to localhost only (default: on)<br \/>\nwebcam_localhost off<\/p><\/blockquote>\n<p>make it always overwrite one file<\/p>\n<blockquote><p># File extension .jpg or .ppm is automatically added so do not include this.<br \/>\n# Note: A symbolic link called lastsnap.jpg created in the target_dir will alwa$<br \/>\n# point to the latest snapshot, unless snapshot_filename is exactly &#8216;lastsnap&#8217;<br \/>\n# snapshot_filename %v-%Y%m%d%H%M%S-snapshot<br \/>\nsnapshot_filename lastsnap<\/p><\/blockquote>\n<p>use the temporary file system to not trash your SD card<\/p>\n<blockquote><p># Target base directory for pictures and films<br \/>\n# Recommended to use absolute path. (Default: current working directory)<br \/>\n#target_dir \/tmp\/motion<br \/>\ntarget_dir \/run\/shm<\/p><\/blockquote>\n<p>mask off all motion detection you can<a href=\"http:\/\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/blank960x720.pgm\" target=\"_blank\"> use this black PGM file (600k)<\/a> if you are running 960&#215;720<\/p>\n<blockquote><p># PGM file to use as a sensitivity mask.<br \/>\n# Full path name to. (Default: not defined)<br \/>\n; mask_file value<br \/>\nmask_file \/home\/pi\/blank960x720.pgm<\/p><\/blockquote>\n<p>then run it. Use a cron job to upload \/run\/shm\/lastsnap.jpg to your website however many minutes you want and you&#8217;re sorted. You may view the motion jpg video on http:\/\/yourPiIPaddress:8081<\/p>\n<p>To add the temperature display you follow <a href=\"https:\/\/learn.adafruit.com\/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing\/hardware\" target=\"_blank\">Simon Monk&#8217;s instructions<\/a> over on adafruit. I did a gonzo install using a 16-way header which is a nice cheap way of getting onto your Pi GPIO as long as you take care to get it the right way round and match up the start<\/p>\n<figure id=\"attachment_2454\" aria-describedby=\"caption-attachment-2454\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/DSCN2747.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-2454\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/DSCN2747-550x413.jpg?resize=550%2C413\" alt=\"Quick'n'dirty onewire isntall\" width=\"550\" height=\"413\" srcset=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/DSCN2747.jpg?resize=550%2C413&amp;ssl=1 550w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/DSCN2747.jpg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/DSCN2747.jpg?resize=624%2C468&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2015\/08\/DSCN2747.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"auto, (max-width: 550px) 85vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2454\" class=\"wp-caption-text\">Quick&#8217;n&#8217;dirty onewire install<\/figcaption><\/figure>\n<p>but there&#8217;s nothing stopping you going the neater Adafruit way if that lights your fire. Now go<\/p>\n<p>&nbsp;<\/p>\n<p>sudo apt-get install rrdtool which will do all the display for you, and make an rrd directory under the \/home\/pi dir. I used this shell script to actually set up the rrd database<\/p>\n<pre>#!\/bin\/bash\r\nDIR=\"\/home\/pi\/rrd\/\"\r\nDS=\"SP\"\r\nSTEP=600\r\n# step is 10m = 10*60= 600\r\n# save 365*24 raw samples of 60 mins (=8760 of 60m), 797 daily (24*4 samples =\u00a0 96)\r\n# one day is 48 steps, image with = 800 so raw is 16 2\/3 days for 800 samples\r\n# last 66 days is consolidate to avearge fo 4\r\n# one year is consolidate to average of 32, make this an average of 48 and save three years (1152 days)\r\nrrdtool create $DIR\/$DS.rrd --start N --step $STEP \\\r\nDS:$DS:GAUGE:$((STEP*3)):-10:50 \\\r\nRRA:AVERAGE:0.5:1:800 \\\r\nRRA:AVERAGE:0.5:4:800 \\\r\nRRA:AVERAGE:0.5:24:1152 \\\r\nRRA:MAX:0.5:48:1152 \\\r\nRRA:MIN:0.5:48:1152<\/pre>\n<p>I hacked Simon&#8217;s code thusly<\/p>\n<pre>import os\r\nimport glob\r\nimport time\r\nfrom commands import getstatusoutput\r\n\r\nID=\"\/home\/pi\/rrd\/SP.rrd\"\u00a0\u00a0 # where rrd databases are held \r\nos.system('modprobe w1-gpio')\r\nos.system('modprobe w1-therm')\r\n\u00a0\r\nbase_dir = '\/sys\/bus\/w1\/devices\/'\r\ndevice_folder = glob.glob(base_dir + '28*')[0]\r\ndevice_file = device_folder + '\/w1_slave'\r\n\u00a0\r\ndef read_temp_raw():\r\n\u00a0\u00a0\u00a0 f = open(device_file, 'r')\r\n\u00a0\u00a0\u00a0 lines = f.readlines()\r\n\u00a0\u00a0\u00a0 f.close()\r\n\u00a0\u00a0\u00a0 return lines\r\n\u00a0\r\ndef read_temp():\r\n\u00a0\u00a0\u00a0 lines = read_temp_raw()\r\n\u00a0\u00a0\u00a0 while lines[0].strip()[-3:] != 'YES':\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 time.sleep(0.2)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lines = read_temp_raw()\r\n\u00a0\u00a0\u00a0 equals_pos = lines[1].find('t=')\r\n\u00a0\u00a0\u00a0 if equals_pos != -1:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 temp_string = lines[1][equals_pos+2:]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 temp_c = float(temp_string) \/ 1000.0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #print \"\/usr\/bin\/rrdtool update \"+ID+\" N:\"+str(temp_c)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 getstatusoutput(\"\/usr\/bin\/rrdtool update \"+ID+\" N:\"+str(temp_c))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return temp_c\r\n\u00a0\u00a0 \u00a0\r\nfile = open(\"tempc.txt\", \"w\")\r\nfile.write(\"StoatPi - temp: \"+\"{:.1f}\".format(read_temp()) + \"C time: \" + time.strftime(\"%c\"))\r\nfile.close()<\/pre>\n<p>and call it from cron every 10 minutes, offset from 0 by 1 (ie 1min, 11 min, 21 min etc), which updates the \/home\/pi\/rrd\/SP.rrd database and also drops in a caption for the picture in<\/p>\n<p>\/home\/pi\/tempc.txt<\/p>\n<p>while probably also ought to go in the temp filesystem.<\/p>\n<p>I deleted the original updating cron job. and put in another every 1 minutes and 11 etccalling this with a cron job, you have to sudo python that because you a putzing about with the hardware<\/p>\n<p>Finally two minutes later (on a 3, 13, 23&#8230; raster)\u00a0 I get another cron job to make the graph from the previously updated temperature and caption the picture<\/p>\n<pre>#!\/bin\/bash\r\nDIR=\"\/home\/pi\/rrd\"\r\nDIROUT=\"\/run\/shm\"\r\nID=\"SP\"\r\n\r\ntime_readible=$(echo $(date) | sed 's\/\\:\/\\\\:\/g')\r\n\r\n#define the desired colors for the graphs\r\nINTEMP_COLOR=\"#FF0000\"\r\nMIDTEMP_COLOR=\"#FF00FF\"\r\nOUTTEMP_COLOR=\"#0000FF\"\r\nAQUA=\"#00FFFF\"\r\nBROWN=\"#A52A2A\"\r\nCORAL=\"#FF7F50\"\r\nCRIMSON=\"#DC143C\"\r\nFORESTGREEN=\"#228B22\"\r\nGOLD=\"#FFD700\"\r\nRED=\"#FF0000\"\r\nORANGE=\"#FFAF00\"\r\nGREEN=\"#00FA00\"\r\nGREY=\"#808080\"\r\nLGREY=\"#C0C0C0\"\r\nBLUE=\"#0000FF\"\r\nMAUVE=\"#FF00FF\"\r\n\r\n# combined detail\r\n#hourly 1 day\r\nrrdtool graph $DIROUT\/temp.png --start end-2d\u00a0 --width 800 --height 300 \\\r\n-a PNG -E\u00a0 --disable-rrdtool-tag\u00a0 \\\r\nDEF:$ID=$DIR\/$ID.rrd:$ID:AVERAGE LINE:$ID$CRIMSON:\"Temp\" \\\r\nHRULE:6$LGREY:\"T min (6 C)\":dashes \\\r\nVDEF:SPmin=SP,MINIMUM \\\r\nVDEF:SPmax=SP,MAXIMUM \\\r\nVDEF:SPnow=SP,LAST \\\r\nCOMMENT:\" \\n\" \\\r\nCOMMENT:\"Stoat \" \\\r\nCOMMENT:\" \\n\" \\\r\nCOMMENT:\"Temp now \" \\\r\nGPRINT:SPnow:\"%2.1lf C\" \\\r\nCOMMENT:\"Temp min \" \\\r\nGPRINT:SPmin:\"%2.1lf C\" \\\r\nCOMMENT:\"Temp max \" \\\r\nGPRINT:SPmax:\"%2.1lf C\" \\\r\nCOMMENT:\"\\n\" \\\r\nCOMMENT:\"\\n\" \\\r\nCOMMENT:\"\\n\" \\\r\nCOMMENT:\"Last Update\\: $time_readible\" \\\r\nCOMMENT:\"\\u\" \\\r\ntime_readible=$(echo $(date) | sed 's\/\\:\/\\\\:\/g')\r\n\r\nconvert \/run\/shm\/lastsnap.jpg\u00a0 -pointsize 24 -fill white\u00a0 -annotate +30+710\u00a0 '@\/home\/pi\/tempc.txt' -gravity Center $DIROUT\/stoatpi.jpg\r\nscp <strong>insert your details here<\/strong> $DIROUT\/stoatpi.jpg $DIROUT\/temp.png<\/pre>\n<p>and job done. There&#8217;s no error checking which there really should be, but for a first cut it&#8217;ll do<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m toying with the idea of going along to the Ipswich Raspberry Jam on Saturday 8th Aug and figure it&#8217;s be nice to have something to show. There&#8217;s of course our farm Raspberry Pi cameras which are in service and this one is riffing a bit off an idea Wildlife Gadget Man is playing with. &hellip; <a href=\"https:\/\/www.richardmudhar.com\/blog\/2015\/08\/wildlife-camera-with-raspberry-pi-motion-and-temperature-sensing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Wildlife camera with Raspberry Pi, Motion, and temperature sensing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[10],"tags":[313],"class_list":["post-2453","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi","tag-ipswich-raspberry-jam"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5aOO7-Dz","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/2453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/comments?post=2453"}],"version-history":[{"count":11,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/2453\/revisions"}],"predecessor-version":[{"id":2467,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/2453\/revisions\/2467"}],"wp:attachment":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/media?parent=2453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/categories?post=2453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/tags?post=2453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}