Michel Parreno

@michelparreno

How to access your Raspberry Pi Camera from anywhere

This article is part of a series about how to spy your pet with Raspberry Pi, don’t miss the first step here: Spy your pet with a Raspberry Pi Camera Server where I describe how to set up a camera stream server!

Final result of the tutorials : a full connected cage !

In order to access your Raspberry Pi from anywhere, you need to be able to enter your home network! What is the only way of accessing it from anywhere? Your IP address! It’s basically like your home address but on the internet.

However, most of home networks IP adresses change dynamically after a certain time. So let’s say you use it directly to access your PI, it would only work for a short time and then you’ll need to go home and check it again.

Oh! Then could we create a script in our Raspberry Pi that checks our IP address and mail it back to us?

You could! But assuming you want to build a website or an app that uses this IP to access your PI, you will have to continuously do updates to change the IP. Which is very annoying right?

Then, maybe I can ask to have a fixed IP address at home ?

Yes ! But most internet providers still charge fixed IP adresses. That’s why I used a free DDNS tool.

Dynamic DNS with no-ip

To access my PI from anywhere, I used a service called Dynamic DNS or Domain Name Server. What does it involve?

  • You choose a name for your home network that will redirect requests to an IP address stored on no-ip servers. I chose http://mochi.ddns.net
  • You install the no-ip script in your Raspberry PI. This will make sure that your home IP address stored on no-ip is always updated.
I used no-ip services because you can have one free dynamic DNS. There are plenty other offers online and you can choose whatever service suits you. The principle remains the same.

Step 1: Create your domain name

After subscribing, in the dashboard, simply go in the “Dynamic DNS” section. Then “Create Hostname”. Choose a name on the next screen, let the options by default and validate. Now let’s go back on our Pi!

Step 2: Install the no-ip script on your PI

Let’s download the script from no-ip first. On the Raspberry PI terminal:

# Create the directory, step into, and download the script
mkdir /home/pi/noip
cd /home/pi/noip
wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz
tar
vzxf noip-duc-linux.tar.gz

This should have created another directory with a folder named by the last version of no-ip script.

# Install no-ip script!
cd noip-
<the_version_you_have_downloaded>
sudo make install

You are going to be prompted to login with your no-ip account. After that, when asked how often you want the updates to be triggered, I recommend 15min. Now let’s launch the service!

sudo /usr/local/bin/noip2
# To confirm the service is running properly
sudo noip2 ­-S

Last step, we want to automate the service launch. If we don’t, you will have to do it after every single reboot of the Pi.

# Open the boot file
sudo nano /etc/rc.local
# Add this line at the bottom of the file (just before exit 0)
/usr/local/bin/noip2

Done! Now we have an automated system that updates our domain name IP alone! Here comes the tricky part.

Configuring your internet box

Ok, we have a domain name that always redirects to our home network. However, your internet box (a.k.a router) doesn’t know how to deal with the incoming requests! We need to ask “Hey please, redirect incoming requests to the Raspberry Pi”. All routers are different, the best thing to do is to search “Port forwarding” on the web with your brand and model. I will highlight here the main things you’ll have to do.

Connect to your router administration panel

On a web browser, type your router IP address in the URL bar. Typically it’s 192.168.1.1 or 192.168.1.254. If not, it should be somewhere in your router manual.

  1. Login. By default credentials are often admin & admin or 0000.
  2. In the router admin page, find the port forwarding section.

Configure the port forwarding to your PI

After finding the port forwarding, you’ll need to add a new rule. Here’s an example with the parameters and their meaning.

French BBOX port forwarding configuration
French Livebox port forwarding configuration with a additional rule for a web server (80). From left to right : Application, internal port, external port, protocol, device, active

Internal Port: The port on the raspberry Pi where requests should be redirected to. In our case, the video stream server is at 8081 if you followed the first tutorial.

External Port: We don’t want ALL requests redirect to our PI. Only those that are targeting the camera server. Since we are going to use a browser, or an app to access our Pi, we can request our home network with any port we want. (for example we will use http://mochi.ddns.net:XXXX). To make it simple, I used the same as the video stream server: 8081.

Protocol: Set this to ALL.

IP Address / Device: The IP of the Pi in your home network. That’s where we are going to redirect the incoming requests so they can reach our Pi. To find it, type the following command (on your Pi):

ifconfig

In the command line response, find the “inet addr” which will give you the Raspberry Pi IP

wlan0 Link encap:Ethernet HWaddr 80:1f:02:aa:12:58
inet addr:192.168.1.8 Bcast:192.168.1.255 Mask:255.255.255.0
...
We are actually telling our router: Please redirect all outside world request with port 8081 to the raspberry Pi on port 8081.

You may have to restart your router after adding the rule, and finally we’re all set!

Access your Raspberry PI Camera stream from anywhere

On the previous tutorial we learned how to install Motion Pi. Remember the step where you configured the port for the stream server? That port is the entrance gate on your system to the video stream. Now we’re going to use that… and it’s dead simple!

⚠️ Try this with a different connection than your home. For example, on your smartphone. When being inside your local network, trying to use the DDNS may not work. This is a limitation with routers.

Test your camera from the internet! https://<your_ddns_name>.ddns.net:<motion_port>

Authenticate the camera server

You can add a username and a password to your camera in order to protect it. If you have followed the previous tutorial and use motion, you can do :

sudo vim /etc/motion/motion.conf
# Add this line in the file
webcontrol_authentication username:password

See motion documentation for more details and options.

All this tutorial also works for any exposed service on your raspberry PI. For example, a web server where you could have a website. (port would be 80 then).

Capture from a distant browser
Connected food dispenser! (Next article)

In my next article, I will show you how to build a food dispenser with your Raspberry Pi and then trigger it from anywhere with a webserver !

If you’ve liked this article and that it helped you building your own remote system on your Pi, please post a picture of it =D!

Thank you so much for reading. See you soon.

More by Michel Parreno

Topics of interest

More Related Stories