Saturday 22 August 2015

Setting up my Time lapse Raspberry Pi Camera with a shut down switch

Lets do it !


Well this post is a little bit about Linux Mint as I'm using it to set up and transfer files to my Time Lapse Camera I have made from a "Raspberry Pi" Micro computer but also because it links in to my last post which was about joining the shots I'd made into a short time lapse Video.

Let's get this down while I have the tabs open on my browser I've used and it's fresh, in case I need to do it again.

About the Camera


I set up a simple camera time lapse using the camera module for the Pi and set it off connected to a mobile phone battery back up pack.

These are ridiculously cheap on e-bay from China without the batteries inside, currently about £2 including shipping. They're searchable as Rechargeable USB LED Mobile Power Bank Charger 4pcs 18650 Li-ion Battery Case

People give me old out of date or none working Laptops and so I ripped the Lithium batteries out of the battery pack of one of these. The nice Panasonic batteries were checked on my multimeter and they all seemed OK so I put 6 of them inside the battery pack casing and set it on charge through the USB port of my PC.

By the way most laptops seem to hold battery packs of the type 18650. You can buy them on e-bay but there are types with a nipple on the plus end and types that are flat on both ends. My batteries from the laptop fitted in the case but some new ones I also bought 4x 4200mAh UltraFire 18650 3.7V Rechargeable Li-ion Battery  were shorter and were loose when put in so I did not use them.

I now had a portable time lapse camera I can take anywhere and it will take a picture every one minute for as long as the batteries last.

The set up for the Camera time lapse is available as a worksheet on the Raspberry Pi  learning website
https://www.raspberrypi.org/resources/

This is just a prototype and I want to add other features to it as I go along so nice start.

What now

After setting up the Camera to capture the scene outside my window onto the street I left it running overnight to see how long the batteries would last before they cut power to the Pi. The shots were saved as in the worksheet instructions to save date and time so I would know how many hours by just checking the time stamp on the pictures.

Well that all worked and I took the mini SD card (or TF card if you're from China) from out of the Pi and connected it to my PC, copying over the "Camera" folder to my Linux desktop and then using imagemagic in TERMINAL as described in my last post.

Now to the crux

The battery lasted enough time to get a nice sunrise over my car and onto the street but then I had the issue of shutting down the Pi. (Switches to present tense)

I don't want to damage the file system on the SD card by too many hard close downs from loss of power. I can do this by adding something to my script to fix the time and amount of shots taken before the script tells the Pi to shutdown the operating system (don't know how to do that yet but it is something I will research and learn). 

I also would like to be able to just press a button and ask the Pi to stop taking shots and shutdown the operating system itself before the battery does it for it by dying.

The little portable setup won't be connected to a monitor and keyboard and also I don't want to always have it connected to a ethernet cable or have a WiFi receiver plugged in. 

The only choice is a soft shutdown button.

Now there are a load of pages on the Internet on how to do this but my problem with them is if they are written by experts, they forget the simple stuff or miss vital bits out expecting you to know it.

If they are written by novices they seem to do everything the longwinded way with un-neccsary steps that just add to the confusion.

It's difficult to find something at my "tinkering" level. Oh well, that's life.

So this is How I have done it after lots of wasted hours.

The actual Process

This is for the Raspberry Pi model B+.  I have one of the earlier models, the B but I don't like doing anything that could screw it up with it.

First all the pages I looked at had everybody doing the complicated stuff on the Pi first connected to a monitor. For desktop stuff I think the Pi is just way to slow to do these things. After my initial setup of the card and Pi using a monitor I noted the IP address of the Pi and enabled SSH in (sudo) raspi-config so I could access it from my main computer.

All my editing of files were done by cutting the code from my browser then pasting into gedit the linux mint notepad, saving this file to my desktop and using Filezilla the FTP program to transfer that file to the correct folder on the Pi.

I used PUTTY to  do any other things such as making the files I had sent over read or writable using chmod to change the permissions, and for shutdowns.

The best resource I found was an article on the Rasbperry Pi forum but to me I felt it was over complicated. There were lots of comments made to improve the result but all left out important bits leaving a guide which seemed to be do it wrong then change this bit by bit until it works this way.

I had to go through everything to pull out a relevant and simple process.

First rip a small monetary switch from some old press button electronics board or buy new. They are tiny and just connect when pressed down for a moment. 

Then check they open and close forming a circuit. I used a light and battery to create a simple series circuit to check the legs of the switch as I could only find square 4 leg switches in my scrap box and didn't know which leg connected to which.

I then cut of the wires from an old PC fan connector that fits into the headers on a motherboard. They are small and thin black power connectors. You'll see them and know they are perfect to connect to the headers on the Pi.

Solder the micro switch to the two wires from the flat black connector (it may have two or 3 holes in it depending where you got it from) and you have your plug in switch. This should just slide gently over the pins 5 and 6 on the Model B+

Count in from the top left by the mounting hole, use a guide first to see where they are. 2 , 4 , 5 and 6.

Now cut and paste the code from the forum post below into gedit. I used the code by "ame" at the end of page one as the later code didn't work for me. 

Save it as shutdown.py.

FTP the file to the Home/Pi/    folder

Use PUTTY to access the Pi and in TERMINAL go to the Home/Pi/ folder where everything is and chmod +x shutdown.py to change file permissions.

Then enter the Cron system that sets up timed events by crontab -e

Go to the bottom of the file and add at the end

@reboot /home/pi/shutdown.py


Save this. Shutdown the Pi and disconnect, fit the switch wherever you want it and connect it all up again.

After the reboot you should just be able to press the microswitch quickly and the Pi will shutdown from within with a shutdown call. the lights will flicker then go to a solid red. One more tap and the Pi will come out of hibernate.





Links to resources I've used and were useful.

https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/

Shows GPIO points on the Raspberry Pi B+ I used, they're different from the B model

https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=106410

Called "Solderless clean shutdown/boot switch" if it moves.

http://www.trevorappleton.blogspot.co.uk/2013/11/creating-time-lapse-camera-with.html

https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=67109

Friday 21 August 2015

Rotating timelapse Images in bulk & converting to AVI using ImageMagic

CD to your folder  eg.

TERMINAL

cd Desktop/camera/



Make a bash script. My images are upside down so I need the rotation 180% using ImageMagic's convert.


TERMINAL


for file in *.jpg; do convert $file -rotate 180 rotated-$file; done


This script takes files all files (* wildcard) within the folder that are jpg format and rotates them 180 degrees. It then renames the file by adding a "rotated-" to the start of the file name.

I just remove the unrotated jpg's cos I'm lazy then

TERMINAL

ls *.jpg > stills.txt


List all jpg files and send that down a pipe to a text file called stills.txt

I can then use memcoder (if installed) to convert the timelapse pictures to a short avi video

mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o timelapse.avi -mf type=jpeg:fps=24 mf://@stills.txt