2012-05-20

Python FreeDNS client (on Raspberry Pi)

I'm using FreeDNS - a dynamic DNS provider for my Raspberry Pi. If you sign up and follow the instructions on their site you can quickly have a nice name for your site like mine: Jerbly

To keep your external dynamic address updated with FreeDNS there are a number of clients (scripts) available. Since this is Raspberry Pi though I had to have one in Python.
#!/usr/bin/python

import urllib2
import os.path

OLDIP_FILE = '/var/lib/misc/oldip'

def updatedns(ip):
    print urllib2.urlopen("http://freedns.afraid.org/dynamic/update.php?YOUR-SECRET-KEY-HERE").read().strip()
    f = open(OLDIP_FILE, 'w')
    f.write(ip)
    f.close()

newip = urllib2.urlopen("http://ip.dnsexit.com/").read().strip()

if not os.path.exists(OLDIP_FILE):
    updatedns(newip)
else:
    f = open(OLDIP_FILE, 'r')
    oldip = f.read()
    f.close()
    if oldip != newip:
        updatedns(newip)

NOTE: You will have to change the YOUR-SECRET-KEY-HERE to the one supplied by FreeDNS.

I've just put this script (called simply "freedns") in my /etc/cron.hourly directory which is good enough for my site.

2012-05-18

Raspberry Pi webcam

*** UPDATE NOTE: I am now using Motion instead of ffserver see this new post: Battery powered, Wireless, Motion detecting Raspberry Pi ***

Here's a walk-through of the steps I took to get a webcam stream on the Internet from my Raspberry Pi.

Equipment:
 Raspberry Pi
 8G class 4 Sandisk SD card
 5V 1A MicroUSB power supply
 Ethernet cable
 HDMI cable
 USB keyboard
 Webcam (PS3 EyeToy)

Prepare the OS
 Arch Linux ARM - you can get the image file from the Raspberry Pi download page.
 Write the image to the SD card. (I use Win32DiskImager)
 Expand the main partition to fill the SD card. From my Windows laptop I booted to a GParted Live CD to do this.

Go headless
 Change the root password!

passwd root
 Set up a fixed IP address. (You may need to change your router settings to define a fixed IP address range) Edit /etc/rc.conf using nano:
nano /etc/rc.conf
 Follow the instructions to set up your fixed IP something like this:
interface=eth0
address=192.168.0.101
netmask=255.255.255.0
broadcast=192.168.0.255
gateway=192.168.0.1
 Also change resolv.conf to use your router for name resolution:
nameserver 192.168.0.1
 Since this Pi is running headless we don't need so much RAM allocated to the GPU. So swap the default start.elf for the 224 version:
cd /boot
mv start.elf orig-start.elf
cp arm224_start.elf start.elf
 Unplug the keyboard and HDMI, reboot and then ssh to your Pi. (I use PuTTY)

Start streaming
 Update your packages and install mplayer and ffmpeg:

pacman -Syu
pacman -S mplayer
pacman -S ffmpeg
 Create an ffserver.conf configuration file in /root. Here's mine:
Port 8090 
BindAddress 0.0.0.0 
MaxClients 4
MaxBandwidth 10000 
NoDaemon 

<Feed webcam.ffm> 
File /tmp/webcam.ffm 
FileMaxSize 5M 
</Feed> 

<Stream webcam.asf> 
Feed webcam.ffm 
Format asf 
VideoCodec msmpeg4 
VideoFrameRate 2
VideoBufferSize 80000 
VideoBitRate 200 
VideoQMin 1 
VideoQMax 10 
VideoSize qvga
PreRoll 0 
Noaudio 
</Stream>

<Stream webcam.mjpeg>
Feed webcam.ffm
Format mpjpeg
VideoSize qvga
VideoFrameRate 2
VideoIntraOnly
Noaudio
Strict -1
</Stream>
 Start the streaming server:
ffserver -f /root/ffserver.conf & ffmpeg -v 2 -r 5 -s 640x480 -f video4linux2 -i /dev/video0 http://localhost:8090/webcam.ffm
 You should now have two streams available from your Raspberry Pi. One is an asf stream which you can connect to from players like Windows Media Player or VLC. The other is an mjpeg stream which you can view from a web browser without requiring any plugins.
 The mjpeg URL is:
http://192.168.0.101:8090/webcam.mjpeg
 The asf stream is:
http://192.168.0.101:8090/webcam.asf

Put it on the Internet
If you just want to see this on the Internet temporarily simply open up port 8090 on your home router firewall settings. Set up an Inbound Service on port 8090 to 192.168.0.101. Now find your external ip address and you should be able to connect through that. Your external address will probably be shown somewhere in your router web interface or you can find it by going to here: http://ip.dnsexit.com/
Your external address will change occasionally unless you have a fixed IP address. In another post I'll show how to set up Dynamic DNS with an address updater so you can reach your Pi on the net through a name rather than address. The updater ensures that if your external address changes the Dynamic DNS provider is given the new one.

*** UPDATE NOTE: I am now using Motion instead of ffserver see this new post: Battery powered, Wireless, Motion detecting Raspberry Pi ***

My Raspberry Pi webcam stream is sometimes available here: http://jerbly.uk.to/picam
Please be gentle. If you don't see a stream try again another time!