I spi with my little eye…

Sebcam.
A word that sounds ridiculous. Made up. Well, that’s because it is.
A word that makes sense to a few (probably both of you reading this post) but I’ll explain it for any who don’t know… and I’m sorry, for my casual reader it might get a bit techy (like that’s a surprise)

Many years ago I was gifted a fish tank (long story) and a fish, who my brother had named Seb (although I am still to this day unsure if he’d caught the 2012 bug and named the fish after Seb Coe or the F1 driver Seb Vettel).

Fast forward five years or so, and I had (after many false starts) two fish who I had managed to keep alive for quite some time (turns out fish can be tricky to keep alive and need a fair bit of maintenance). Anyhoo, it appears that the two fish (Chas and Dave) fell in love and had lots of babies (So it would seem one of them was actually Shaz or Davina). Seven of which survived their mother. This left me with eight fish in a now rather small tank.

After a few weeks living in a sieve, the babies were large enough to live in the tank on their own.

Now, I must have been a good boy this year, because Father Christmas, Santa Claus, the big fat man with the long white beard, brought me a much larger fish tank, with light and heater and the whole shebang. As well as a new Raspberry Pi Zero W, and camera module.

My geeky brain put the two together and came up with a webcam for the fish. A webcam, for Seb. A Sebcam!
And lo, the Sebcam was born.

I set out and had a play with the Pi and the camera module.
A quick install of Raspbian, updated and ready to go and I was ready to start playing.
I had to install the picamera software from Pimoroni.

sudo raspi-config

Then navigated to interfacing options.

Enable the camera option. Exit the config.

Power off the raspberry pi and install the camera module by plugging the cable into the pi zero. Simple enough.

Power on the raspberry pi and cross my fingers.
Everything came back up and on the TV screen I can fire up the terminal.
Now at this point I have to admit to getting a bit cocky – I fired up python and began to write my basic code. I figured a simple few lines to take an image and save it should suffice as a starting point.

#!/usr/bin/env python                 #Setup the environment

import picamera                       #Import the picamera tools
from time import sleep                #Import sleep command to pause the code    

camera = picamera.PiCamera()          #Setup an alias for picamera tools
camera.resolution = (2592,1944)       #Max out the camera resolution

camera.capture('image.jpg')           #Capture and save an image

(Don’t ask me why the I imported the sleep command – I have just noticed this and will need to update the code to v.02 soon)

Save the file and run. Nothing happened. Well, nothing came up in the terminal, which (in this case) was a good thing.

Quick check in the folder and there was a very nice image file as expected.

It works!

Excellent. Right, time to edit the timer file (crontab for those who understand these things) and add a quick line to get this code to run every 15 minutes or so. Brilliant. The code runs and overwrites the original image file with the latest photo.

That’s great, but how to get the image from the pi zero?
Well, this is where things get a bit… dodgy.

I have a desktop PC on my home network that is on 24/7. This Windows PC already doubles up as a kind of media server and so is on all the time, with most of the user folders shared on the network (Because I’m lazy and like working on my laptop, but not having to shuffle files with memory sticks). So all I needed to do was connect to the network share from the pi and drop the files into the folder synced with Dropbox.

In theory, the image should sync with Dropbox, and then I can view the file from my phone, tablet or laptop simply by accessing the Dropbox site.

This was a little more complicated because it turns out that mounting a Windows share in Raspbian wasn’t as straightforward as I had hoped.

Eventually things got sorted and I had access to the share. Well kind of. I could view files but not write to the folder. I am sure there are much more elegant was to deal with this but sudo seemed like the quickest way to test stuff and it worked. Running the camera code with a sudo meant that the image was saved in the Dropbox folder and synced online. The updated image appeared on the iphone app within a couple of seconds of the code running. It’s a bit of a cludgey workflow but it does work.

  • Camera takes photo
  • Photo is saved the file on the Windows desktop Dropbox folder
  • Dropbox folder syncs with the website
  • Image updates on the app

Hey, I’m as impressed as you are.

Two more steps were required to complete my planned picamera fishcam, some sort of stand to hold the pi zero and a page on this very blog to show the world my fish.

I am a fan of nice cases, and there are many out there… nice 3D printed ones, modified tuna tins… who knows what. My preference leans towards the wonder of Lego. Well, in my case because I’m a cheapskate, Wilko Blocks (Compatible with the leading brand) and a quick trip out in my lunch hour yielded this…ย 

I had myself a bag of black shapes that could be used to build a simple case to house the zero, with suitable openings for power and the camera.

It’s not master builder level, but it does the job. It keeps the camera steady and in place to take regular photos of the fish.

A quick search on the interwebs tells me that, as long as I am updating the same file (that is, replacing the same file, with the same file name, in the same place) Dropbox will keep the link for the file the same. This means it is relatively simple to embed the latest Dropbox image in my blog (this site).

Off I go, add the image, a bit of blurb about it (I wasn’t sure if I should auto-refresh the page or get visitors to manually refresh – I went with manual refresh, it was simpler) and fired off a tweet showing off my new page and how brilliant I am.

Except I’m not. Brilliant, that is.

Every time a new visitor arrives at the page, they get the latest image. Every time a repeat visitor views the page, they see their original image. This stumped me for a while (and much to my other half’s consternation) I spent brunch, and a train ride, trying to work out what was wrong.

It turns out that Jetpack (the standard WordPress monitor plugin) has an option to cache images. It’s designed to save hits on the storage provider. A brilliant idea and I had obviously turned this on to save on my bandwidth. Except that meant that every time someone visited the page their first image was cached and every time they reloaded they got this file again (because the original filename hadn’t changed – logical but not what I wanted to happen).

Turning this setting off fixed the issue, and it manually grabs the image from Dropbox every time the page is loaded, or reloaded.

This meant I could enjoy the planned afternoon visit to pub in peace (as could Claire).

The camera is now in place looking into the new fish tank, which at the time of writing is fishless, although hopefully, once the water is ready to go then there will hopefully be fish enjoying their new home on camera.

You can view the latest image from the Sebcam by clicking on the Sebcam link at the top of this page (although at present it isn’t very exciting).

I am also thinking that from time to time I could use the camera as a security camera and use it to check on the house when I am away. Or maybe I’ll get a second picamera and use that for home security…

Since writing the first draft of this post (and with some feedback from those who I shared the page with) I have made a couple of changes – The page should now refresh automatically. It was suggested that I include a countdown timer to show people when there should be a new image. I looked at a few WordPress plugins and found this which gives the option to redirect to a web page when the timer runs to 00:00 – this is probably frowned upon in “proper” web development, but it does mean the page automatically reloads when the timer runs out, and should show the latest image.

There are also two fish in the tank, exploring new frontiers. As these baby fish never had a name, they have been called Neil and Buzz. (Armstrong and Aldrin, not Diamond and Lightyear). Keep an eye out and you might see them ๐Ÿ˜‰

2 thoughts on “I spi with my little eye…

  1. Hey dude! Sebcam is go! You’ve been wanting this for ages, and I’m totally jealous of the sweet Lego case ๐Ÿ™‚

    Re: the image caching; instead of turning off Jetpack and losing caching everywhere on your site, you could try adding a salt to the image URL so *looks* different, which may cause Jetpack to ignore it. Try something like

    dropbox.com/image/path/image.jpg?random=INSERT-LONG-RANDOM-NUMBER-HERE

    1. I’ll have a look and see if I can do that, but my next version might be to get it to save the last 5 images (or so) in case the fish aren’t on the latest grab.

Get in touch...