Saturday, May 10, 2014

Raspberry Pi Cam, VNC, Wifi

It's here, the Raspberry Pi camera.  Here meaning here in my living room, it has been available for quite a while now.  Also, a lot of other things.  Here is what I'll be covering today, I learned all of it from browsing other blogs and some Python knowledge, but want to compile today's work into one post and one page for you to learn from.

First: remote access.
    I love to have the TV on, so I don't like using it as a monitor for my Pi.
ssh would not work for me and VNC allows me access from afar.
Setting up VNC:
On the Pi I plugged it into the ethernet to get VNC working.
I started here: http://pihw.wordpress.com/guides/direct-network-connection/ but never got it working.
So I looked at VNC again and started here: http://www.rasptut.co.uk/files/raspberry-pi-vnc.php

Hook up your Pi to a monitor with it's own input devices. On your Pi, in the terminal:
sudo apt-get update
sudo apt-get install vnc-server

To start the VNC-server on your, in your terminal type:
vncserver -geometry 1024x768

Add an 8 character password and it is your choice to add a viewer password.
Note that the first time you run this (the first time since a restart) it is producing a call for :1 (remember this for later)

Now on your laptop/desktop THIS IS FOR LINUX (download from: http://www.realvnc.com/download/viewer/ for MAC OR WINDOWS) download VNC-viewer....which doesn't exist, through apt-get and when you grab the .gz or binaries...well I couldn't get it to work.  This did:

sudo apt-get install xtightvncviewer

On your Pi type in
ifconfig
to get your ip address.  It will be under the eth0 header, we will take this ip address from wlan0 soon.

Back to your laptop, in the terminal:
xtightvncviewer

The GUI fro this is an undersized window that wants the ip address AND :1
It does not look like other shots I've seen of VNC, just this:
Type in the ip: 192.000.0.000:1      -- the ip plus the :1 (which is the port we are accessing on the Pi)

This took me a while to get.  A mix of iffy internet connections here, not reading everything thoroughly.  If you start vncserver on the Pi again you will connect with ip:2 (192.000.0.000:2) When the Pi is restarted it will be 192.000.0.000:1 again.

You will be prompted for the password that you put into your Pi earlier.

That is it, your done!

However, now that you can control your Pi with a remote GUI, you are still stuck with it wired to ethernet.

Wifi was a pain last year.
Wifi is simple now.
I read this: http://www.maketecheasier.com/setup-wifi-on-raspberry-pi/
Then I plugged in the lil wifi dongle, clicked on the Wifi icon that is on the newest release of Wheezy, selected my network and it was online.
Done.

So now you can connect to your Pi over Wifi.

It is time to get the camera running.  I did this all over the Wifi.
Guide: http://thepihut.com/pages/how-to-install-the-raspberry-pi-camera
Before you follow the guide, watch this video: https://www.youtube.com/watch?v=T8T6S5eFpqE At the 2 minute mark it shows how to pull up the tabs and insert the camera's ribbon. Not rocket science, but helpful to see.

Launch xtightvncviewer from your laptop
Start Leafpad up on the Pi
raspistill -o myfirstpipicture.jpg

A few seconds later you will have your first snap shot.
I wanted to do some time lapse and read a few sites but found it way easier to use my Python knowledge (learned http://learnpythonthehardway.org/ and codecademy.com) I wrote a script to take one picture, mostly to test my scripting:

import os #to pass the arguments into the terminal - at least that is how I understand it
os.system("raspistill -o imagesnap.jpg") #within the " " is what you would normally put into the terminal.

Time Lapse:

import os
import time

i = 0

while i <= 15:
           imageNum = str(i)  #to number your images
           os.system("raspistill -o image%s.jpg"%(imageNum))
           i += 1
          time.sleep(15)

Yes, it is fixed in the number of shots it takes and the time it sleeps.  I'll be writing a new script to take these as input arguments when the timelapse.py is started.  The ones I read were messy (in my opinion) but they work.  More importantly, it is easy to script all of this with Python and there are plenty of different ways to do it.

I had everything setup at sunset, but it was dark by the time that the timelapse script ran correctly (messed up the " " placement as I didn't understand that everything within "raspistill -o image%s.jpg" was what would be passed on to the Terminal to be run.  It seems perfectly obvious now.

The script ran and I got 15 pictures of a dark window and Janet cutting up vegetables and closing the curtains.  It works, now I've just got to capture something fun with the time lapse.

But Wait!
There's more!

Getting VNC to start on the Pi automatically so it never needs to see a monitor again.
Ugh I did a few different, long things that didn't work.  Even the parts that said: if you get the following error, try this....   I was still stuck.
Ada to the rescue: https://learn.adafruit.com/adafruit-raspberry-pi-lesson-7-remote-control-with-vnc/running-vncserver-at-startup
it is easy easy easy, but took a lot of searching to find this guide. I won't retype anything from the Ada guide here because there is a lot of fun stuff over at Ada Fruit so it is worth your browsing time.

-Jeff

No comments:

Post a Comment