AberCam – Raspberry Pi Webcam

Bought a few more Raspberry Pis, and have got one gazing at the sea outside my window here in Aberystwyth. You can view it (if its on) here.

Pi Camera in its Poundland tupperware box

Pi Camera in its Poundland tupperware box

Changing Public IP address

With TalkTalk, at least every time our router reboots we get a new public IP address – which is difficult when the abercam webpage is looking for our router at a specific address. To combat this:

I used curl to get the webpage output of: http://ipecho.net/plain – which displays your public IP address. (Could use wget/lynx command line browser like: lynx –dump)

 curl  http://ipecho.net/plain > <filename>.php

Then put the result of this into a file, and uploaded it to my website

 curl -T <filename containing ip address>.php ftp://<ftp server name(.co.uk)>/public_html/ –user <username>:<password>

Where the filename is the name of the file where your IP address is written, ftp server name is your website name (matthewrobbins.co.uk), and the username and password are the ftp details for your website.

Now in the HTML page where the router address was used, I inserted a PHP include of the recently uploaded file containing the IP address. It now looks like:

<img style=”-webkit-user-select: none;” src=”http://<?php include(“<filename>.php”); ?>:<port>/?action=stream” />

Where the filename is the name of the file with the IP address in it, and the port is the one you have set your router to forward incoming connections to the Raspberry Pi (e.g 8080).

Last thing, set a cronjob to run the script every hour or so, bingo. Might be worth adding in an ‘if new_ip == old_ip, end. Else, continue and upload the new one..’ to save unneeded ftp connects and file overwrites. My end bash script looks like: (make sure to put in FULL file paths!)

curl http://ipecho.net/plain > /home/pi/Documents/code/static/ipaddress.php
curl -T /home/pi/Documents/code/static/ipaddress.php ftp://matthewrobbins.co.uk/public_html/ –user <username>:<password>



The PiCam worked fine outside my window, but when I moved it sideways, the webcam stream became very jolty and stopped. Pings also stopped returning to the Pi. I believe this is due to the puny looking BT homehub that we have providing us WiFi (it doesn’t even have an external antenna!), so I’ve plugged in my trusty Tenda access point, which promptly set itself up and started working without any help needed. After changing it from WEP to WPA/WPA2 (I know, I know.. but some of the laptops in my house used to only support WEP…), I then had the fun challenge of not only getting one port forward working correctly, but two – as traffic had to be directed through the router (homehub) to the access point (Tenda), then to the Pi.

After setting this all up logically (Homehub forwards port 8080 to the external IP of the Tenda access point, then the Tenda forwards 8080 to the Pi), to my great surprise, everything started working after a couple of reboots (that both devices did NOT tell me I must do). I also found this great guide on portforward.com on double router port forwarding, which is a similar situation.

Update 2

Changed my IP address updater script to ensure that it will always upload an IP address (rather than a 404 or 503 error from the webpage):

(—- to indicate indentation)

ip=$(curl http://ipecho.net/plain)

if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
—-echo ‘Good IP’
—-echo $ip
—-echo $ip > /home/pi/Documents/code/static/ipaddress.php
—-curl -T /home/pi/Documents/code/static/ipaddress.php ftp://matthewrobbins.co.uk/public_html/ –user <username>:<password>
—-echo ‘Bad IP. Exiting’
—-echo $ip

Also attached little rubber feet to the bottom of the tupperware box, as well as adding a safety rope tied to a bag of shoes in my room, as redundancy in case the wind gets stronger.