PiCam2

Posted on Tue 24 April 2018 in RaspberryPi

RaspberryPi CCTV Camera, it has to be done.

The RaspberryPi was built to have a camera attached, literally. It's small footprint, low power consumption and ease of use make it a perfect project.

As far as I can work out, there are three ways to go about creating a motion activated camera;

* coding each line, using a PIR sensor and camera
* installing MotionEyeOS 
* using motion

I'd like to get to a point where I can code my own project, independent of precompiled packages and using external sensors. I just don’t have the time right now.

I've previously used https://github.com/ccrisan/motioneyeos/wiki/Installation.

It's ridiculously simple to set up and use, works very well and has a great interface.

I strongly recommend you look in to it, if you're looking for a polished, OOtB experience.

However, I had an issue whereby the cameras would disconnect and require a reboot to get working again. Not ideal. I also had issues with false positives. Hundreds of emails and pictures a day being uploaded daily, defeating the object entirely. It wasn't the OS's fault I'm sure, but with limited access to the read only OS, I struggled to troubleshoot. MotionEyeOS is built on motion. Motion, installed on Raspbian allows for a completely configurable system.

I have three cameras that are motion activated, upload the videos and stills to Dropbox and keep themselves tidy.

I'll add other notifications at a later date, email or PushBullet no doubt.

From start to almost finished, this is how I built my camera

You'll need:

* Raspberry Pi W Zero + Micro SD, Power
* Camera Module
* Putty
* Notepad++
* Win32 Disk Imager
* Dropbox Access

I'm using a 16GB Micro SD card however have used 8GB with no issue

I've used PiCam2, NoIR Cam and PiZero NoIR Cam, all seem to work will with varying image results.

Download Raspbian Stretch from https://www.raspberrypi.org/downloads/

Download Win32Disk Imager - sourceforge usually has it hosted

Raspbian Stretch

Open Win32DiskImager, select your SD card, downloaded .img and click write.

alt text

WiFi & SSH If you're building headless, without a screen, you'll need to create two files to activate WiFi and SSH.

This needs to be done before the Pi is booted for the first time.

SSH

In the SD card (should be called boot) create a file with no extension called ssh. Right click, new Text Document, call it ssh, delete the .txt off the end. You'll end up with a no icon file called ssh.

Wifi

Create a file called wpa_supplicant.conf - same as with SSH but instead of deleting the .txt, rename it .conf. Edit the file in a text editor, I use Notepad++, copy and paste the below, including the quotes, replacing with your network name and password. Copy it over to the root of your SD card.

country=GB
        update_config=1
        ctrl_interface=/var/run/wpa_supplicant

network={
    scan_ssid=1
    ssid="MY SSID"
    psk="MY PASSWORD"
}

Pop the SD card in the Pi and let it boot. Using a terminal emulator, I use Putty, connect to the Pi via it's IP address.

Finding the Pi IP

If you have a DHCP server, check the leases, if you have a standard home router, check the attached devices or use a tool like Advanced LAN scanner to scan for the Pi's IP address.

Once connected to the Pi, you'll need to update it and change the Pi password.

The apt-get dist-upgrade will take a while.

Setup Pi

sudo apt-get update

sudo apt-get dist-upgrade

sudo passwd pi

password

password

sudo apt-get update

sudo raspi-config

Edit your Keyboard, Timezone and Localisation options, also enable the camera under interfacing options.

The config will ask you to reboot once the options have been changed, if it does not;

sudo reboot

Once the Pi is up to date and you've changed the password, time to install Motion.

Working Directory

When working, make sure you're at ~, home, this will avoid issues with permissions.

If at the start of your command prompt you see pi@raspberrypi:/ $

cd ~

You should see pi@raspberrypi:~ $ - this is Pi's home folder and an ls should show Desktop, Documents, Music etc.

Motion

cd ~

Create a folder for the images

mkdir motion

Install Motion using apt-get

sudo apt-get install motion

Edit the Motion config file

sudo nano /etc/motion/motion.conf

Edit the below settings (ctrl+W to search)

    Daemon = on
    stream_localhost  off
    webcontrol_localhost off
    Stream_quality 100
    width 640 height 480
    locate_motion_mode preview
    locate_motion_style box
    event_gap  10
    quality 80
    text_changes yes
    framerate 100
    threshold  5000
    noise_level 85
    Llghtswitch 85
    minimum_motion_frames 10
    pre_capture 5
    post_capture  5
    max_movie_time 360
    target_dir /home/pi/motion/

Fine Tuning

The threshold, minimum frames, light switch detection and noise settings are used to decrease false positives. Adjust as needed.

The pre and post capture are how many stills are included in the motion event, I have 5 to decrease the upload size.

A full list of what does what can be found here

https://motion-project.github.io/motion_guide.html

Start the daemon on boot

sudo nano /etc/default/motion

start_motion_daemon = yes

Register the camera module

sudo modprobe bcm2835-v4l2

Reboot

sudo reboot

Testing

Open IP address of your Pi with the port 8080 at the end in a web browser. eg 192.168.1.11:8080

You should see whatever your camera is pointing at! If you have a grey screen, check your /etc/modules file.

You should see bcm-2835-v4l2 listed, if not add that line at the bottom.

Edit the modules file:

sudo nano /etc/modules

add the line

bcm2835-v4l2

reboot

sudo reboot

At this point you should have a fully functioning Pi Zero CCTV camera!

Dropbox Uploader

I've added a Dropbox upload so that I am able to save and view the files remotely.

Still working from ~ install the Dropbox Uploader by Andrea Fabrizi

sudo git clone https://github.com/andreafabrizi/Dropbox-Uploader.git

cd ~/Dropbox-Uploader

Create a dropbox App access token:

Go to https://dropbox.com/developers/apps ,complete the app set up and generate an access token

alt text

alt text

Run the script and add your access token:

./dropbox_uploader.sh

CD into your Dropbox Uploader folder, make the script executable

cd ~/Dropbox_Uploader/

sudo chmod +x dropbox_uploader.sh

List the saved stills so far

ls /home/pi/motion

Test your uploader, replace the .jpg with one of your own

./dropbox_uploader.sh upload /home/pi/motion/06-20180408150408-01.jpg /

To break the script up:

upload script - command - upload folder - file - remote folder (in this case root or /)

Automate

I found an awesome script online that will automate the upload. It checks if the file already exists, if not uploads it.

There is an option to delete the file once uploaded, I'm having permission issues with it so am not using the delete function.

The script is available here https://www.raspberrypi.org/forums/viewtopic.php?t=164166

Create a python file for the script

sudo nano ~/home/pi/Dropbox-Uploader/uploader.py

Copy and paste the script in, it'll take a min or so to render. Save it once it's done.

Edit the #Path to the sync dir, where your images are being saved.

If the dropbox uploader script is saved elsewhere, point to it using #Path to the Dropbox-uploaded shell script.

Test the script is working

python ~/Dropbox-Uploader/uploader.py

More files should start appearing in your Dropbox!

As I'm not using the delete function in the script, I needed something to clean up the picture folder, else it would fill up.

Create another python file and copy the below script in. It's a simple list and delete script.

sudo nano ~/Dropbox-Uploader/cleanup.py

    #!/bin/bash

    import os

    import os.path


    d='/home/pi/motion/'

    files = [os.path.join(d,f) for f in os.listdir( d )]

    for f in files:

        os.remove(f)

Cronjobs

You now have two scripts. One to upload the files to Dropbox and one to clean up the folder.

Need to make them run themselves.

As scheduled tasks are to Windows, Cronjobs are to Linux.

Open the cronjob editor

crontab -e

https://crontab.guru/ Will help you understand the cron timings.

I have these two cronjobs. The upload runs every 30 mins. The clean-up, every 40.

*/30 * * * * python /home/pi/Dropbox-Uploader/uploader.py

*/40 * * * * python /home/pi/Dropbox-Uploader/cleanup.py

Part 2

So, eveything has been running well for roughly a week now. I've had a few issues but nothing show stopping. The cam with the Zero Cam in, seems to want a reboot regularly. Which is semi-annoying. The official RPi Cams seem to be ok, both NOIR and IR.

I've added an extra cron job to schedule the reboot for me:

0 0 * * * /sbin/shutdown -r now

Making a Webpage

So checking the camears via the IP address is fine, opening each in a different window or tab wasn't ideal.

I made a locally hosted website that will show the three cams, quite nicely, in one window.

It's not as beautiful in Chrome but works well in Firefox. Something to do with the .css webkit. IDK, it's "working".

<!DOCTYPE html>
<html>
<body>

<style type="text/css">
#iframe {
    zoom: 1;
    -moz-transform:scale(1);
    -moz-transform-origin: 0 0;
    -o-transform: scale(1);
    -o-transform-origin: 0 0;
    -webkit-transform: scale(1);
    -webkit-transform-origin: 0 0;
}
</style>

<iframe src="http://192.168.10.100:8081"  scrolling="no" height="455" width="590"></iframe>
<iframe src="http://192.168.10.101:8081"  scrolling="no" height="455" width="590"></iframe>
<iframe src="http://192.168.10.102:8081"  scrolling="no" height="455" width="590"></iframe>

</body>
</html>

You can pretty much CP that in to a blank HTML file, edit the IP address and be good to go!