Thursday, 23 June 2016

Pi Zero CCTV with ZeroView

One of the first things I used a raspberry Pi for was to build a simple CCTV system. In fact that original model B is still up and running and today.  It is a nice compact, self contained system.

However the addition of a camera capability to the Pi Zero opened up the possibilities for even smaller photography ideas.

Those rambunctious pirates at Pimoroni were quick off the mark with their dinky Little Bro kit. This is a smart CCTV sign that has a  hole for the camera and comes with a mounting bracket for the Pi Zero which also allows you to fix the whole thing to a wall.    When I wanted to make a timelapse of some garden work that was happening I simply used some string to hang the Little Bro from the window handle. This worked fine although it was a bit wobbly (especially when the window as opened) and when I had to adjust something I didn't manage to put it back up without altering the field of view slightly (you can see that the picture jumps slightly at about 00:10). It was also difficult to get the camera as close to the glass to keep reflections to a minimum.

Then The PiHut launched the excellent ZeroView, designed for exactly this sort of thing. It uses two sucker pads to stick to a window thus keep the camera as close to the glass as possible. Because the frame can be removed from the suckers while leaving them attached to the window, it also allows for easy adjustment and replacement without altering the field of view.

Initial tests confirmed that this was genius!

 Remixing my original CCTV build (which uses Motion to detect movement and then uploads the resulting movie to Dropbox) was easy. On the first night of testing I managed to catch this wily fox sneaking through my garden at dawn.

However there were a couple of issues about making this a permanent installation that I wanted to address:

1) My experience of headless Pis and wifi is that sometimes the computer drops off the network. Although the power management for dongles seems to be much improved now, I wanted an easy way of seeing that the Zero was still connected.
2) Suckers are effective but not always reliable, especially in a window which might experience big changes in temperature.

So as a combined solution to both of these, I used a ProtoZero board, an accelerometer and an RGB LED.

Some simple python checks the accelerometer and looks for abrupt changes in the values reported, to detect if the Pi falls off the window. In this event it uploads a picture of Humpty Dumpty to DropBox. The same code also periodically checks that it is still connected to the Internet (by pinging Google) and changes the colour of the RGB LED from green to red.  There are a few other visual cues too: the LED flashes blue when the baseline accelerometer readings are being measured at start-up and also flashes green when a ping test is underway. This is run at startup via /etc/rc.local.

from gpiozero import RGBLED
from adxl345 import ADXL345
import time, numpy
from datetime import datetime
import dropbox
import urllib3.contrib.pyopenssl
import os,sys,logging

logfile = "/home/pi/cctv-"+str("%Y%m%d-%H%M"))+".csv"
logging.basicConfig(filename=logfile, level=logging.DEBUG,
    format='%(asctime)s %(message)s',
    datefmt='%Y-%m-%d, %H:%M:%S,')

led = RGBLED(26,13,6)
adxl345 = ADXL345()
client = dropbox.client.DropboxClient('<ENTER YOUR DROPBOX KEY HERE>')
#print('linked account: ', client.account_info())'linked account: ', client.account_info())
axes = adxl345.getAxes(True)
#print("ADXL345 on address 0x%x:" % (adxl345.address))"ADXL345 on address 0x%x:" % (adxl345.address))
t = 0
x_av = 0
y_av = 0
x_values = []
y_values = []
led.blink(on_time=0.1,off_time=0.1,on_color=(0,0,1), n=50,background=True)
while t < 100:
    axes = adxl345.getAxes(True)
    x = axes['x']
    y = axes['y']
#    print(x)
x_av = numpy.mean(x_values) 
y_av = numpy.mean(y_values) 
x_range = numpy.max(x_values) - numpy.min(x_values)
y_range = numpy.max(y_values) - numpy.min(y_values)'Set mean x: ' + str(x_av))'Set range x: ' + str(x_range))'Set mean y: ' + str(y_av))'Set range y: ' + str(y_range))
led.color = (0,0.2,0)
counter = 0
jiggle = 5
while True:
    axes = adxl345.getAxes(True)
    x = axes['x']
    y = axes['y']
    if ((x > (x_av + (jiggle*x_range))) or (x < (x_av - (jiggle*x_range)))) and  ((y > (y_av + (jiggle*y_range))) or (y < (y_av - (jiggle*y_range)))):
        led.color = (1,0,0)
        f = open(eggfile, 'rb')
        response = client.put_file(eggfile,f)
    counter +=1
    if counter > 6000:
        #print('Checking network comms...')'Checking network comms...')
        counter = 0
        response = os.system("ping -c 3")
        if response == 0:
  'I pinged Google successfully')
            led.blink(on_time=0.1, off_time=0.1, on_color=(0,0.5,0), n=3, background=False)
            led.color = (0,0.2,0)
  'Comms Down :-(')
            led.blink(on_time=0.1, off_time=0.1, on_color=(1,0,0), background=True)


1) I used numpy to calculate the means and ranges which is really lazy - especially as numpy takes ages to compile on a Pi Zero.

2) I started from a fresh instal of jessie-lite. In addition to the Python libraries at the top of the code, you'll also  need to install the following Raspbian packages:


3) You''l also need to enable the camera and the i2C interface through raspi-config


  1. Nice article you posted...

    Get the best CCTV HD Security Systems, Just Search in Google "Q See"

  2. This is really a very good article. Thanks for taking the time to discuss with us, I feel happy about learning this topic. keep sharing your information regularly for my future reference.

  3. Security is must to save your lives and very important things which are connected with you. We are dealing in various instruments which help to save casualties in daily life. Lets take a look for the Cctv full kit, Cctv suppliers Manchester , Cctv camera system , 8ch cctv DVR , 8 channel cctv security system , HD cctv camera or Hikvision ptz , Hikvision DVR, Hikvision NVR and Cctv for sale.

  4. Nice to read this article...... Thanks for sharing this information... cctv camera dealers in chennai

  5. Nice blog.. Atelecom with massive business understanding and know-how of this arena, we have been engrossed in delivering CCTV IP Cameras Installation Service. Delivered by a trained workforce, these offered services could be purchased from us as per our patrons’ changing needs in multiple delivering provisions. Together with this, we deliver these at most affordable rates. CCTV Installation Service in Maharashtra

  6. the growing market in the region while strengthening the supply chain cycle of the industry.”he added. Hikvision Suppliers

  7. People tired to find a relevant place where they can know real facts and myths about the topic mentioned by author of this blog. I want to help a lot of needy people through this blog to come up at my blog to know the real facts and myths regarding this topic. ติดตั้งกล้องวงจรปิด

  8. I must say this is the best post. Thankyou and have a great service related to CCTV Installation and this Advice for all affordable price.

  9. Great stuff ! i would like to appreciate this informative article. You may find the best Hidden camera manufacturers: Hidden camera manufacturers

  10. I have been really impressed by going through this awesome blog information of security cameras and CCTV.
    cctv houston

  11. The locksmith services are really important for your safety too)

  12. Hi Dear,

    i Like Your Blog Very Much..I see Daily Your Blog ,is A Very Usefull For me.

    You Can see also my services.....

    Bison security provide CCTV camera system in London. We have hikvision HD CCTV camera for Security solutions are available as sale or rental packages.

    Visit Now - Hikvision

  13. hi

    wow!!! thank you for great posts

    farshid ghasemi
    from imenshop CCTV company iran
    دوربین مدار بسته


  14. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    Boom Barriers

  15. Great Stuff thanks for sharing this post. Keep Sharing .

    If you want to Check More details on CCTV Installers Click Here CCTV Installers

  16. It is important to install Security Cameras and keep the total workstation premise under CCTV Surveillance. You can even initiate the Video Surveillance Systems in the office, which will keep a watch on the workflow of your office staffs even.

  17. The incidents of carjacking have come close to zero at places where such environment is maintained because the entry or exit is granted to an individual having the usage credentials, which means that anyone who is not having the access to the application will not be able to get the Boom Barriers Gate open.

  18. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site
    door interlocking system
    automatic bollards in Hyderabad
    rising bollards

  19. Nice Post. Don’t wait until it’s too late!!! Install a CCTV Camera at shipgig


  20. Thank you for sharing like this information. If you want more information regarding CCTV Camera visit shipgig website.

  21. We are a highly diversified and aggressive company engaged in making the world a safer place to live in.
    Melbourne Camera Systems

  22. Thanks for posting the useful information to my vision. This is excellent information,.
    boom barriers manufacturers in delhi
    boom barriers

  23. Too good information that you shared. It would be so useful post for those are searching such kind of information.

    Home security systems Deer-park

  24. Awesome,
    Thank you so much for sharing such an awesome blog...
    turnstile Systems in hyderabad