Wednesday, June 1, 2016

Make your own IP cam with a Raspberry PI

This is going to be a lengthy post.........

I wanted to show my Ledstrip controlled by an ESP8266-01 to some of my friends. However as most of them have jobs like I do, and therefore not a lot of spare time. And some do not live next door. It is not easy to have them visiting me for just showing part of a project I am working on. 

I therefore needed a way to show them things without having them to visit me. And that is where an IP-Cam is the way to go. 
Skype could be a solution but then my computer always has to be standby and I have to be there to service it. With an IP cam they can just have a quick peek without me even being present. So having my computer permanent on for servicing a web-cam is out of the question. There should be another way. And there is.

The Raspberry PI

I have to admit it. I own several of them, but I don't tinker a lot with them. Too little time is what I have. I am talking about the famous Raspberry PI.

For those not familiar with the Raspberry PI I urge you to get to know it. It is a small Linux computer which has USB ports, an Ethernet port an Audio port and a HDMI port. You only add an SD card as memory a screen (HDMI or analog or even a tv) and a keyboard and mouse and you are set to go with a full fledged Linux computer. All this for about 40 euro. For more info look here: Raspberry Pi foundation.

So if I do not tinker with them why do I own several. Well one of these little gems is running Ponterface as the heart of my 3D printer system, another one is setup as a media player / game computer, one is  my inhouse printerserver and one is destined to be the heart of my domotic system.

And now I have set up a Raspberry as an IP-cam.

What is an IP-Cam

For those of you not familiar with the name I will give a small explanation.
Every one knows the ordinary web-cams. Most laptops have them build in nowadays and they are great for skype and other ways of communication.

They only have one small problem: they need your computer to operate. In comes the IP-Cam.

An IP cam is a webcam that is connected to a small computer and attached to your home network. You can buy dedicated IP cams that look as a small box. They indeed house a cam and a small dedicated computer. As they are attached to your home network you can access them from any computer, laptop, phone or tablet in your house. Hell you could use one as a dedicated door-bell. But they can also communicate through the internet with the whole world. So you can look at things in your house from any place in the world: from your work, during a holliday abroad etc etc etc. You can use them as surveillance cams, for following certain processes, trafic cam, bird cam or just to keep an eye on your wife.........

The setup

What I wanted to do is to make an IP-Cam with a Raspberry PI that did not need a mouse or keyboard attached. It does need a housing, a power supply, an SD card, an ethernet connection (to your network router or switch) and a USB webcam.

To program the Raspberry PI you do need a keyboard a mouse and a screen. But they will be removed later when all functions well. This means that the Raspberry will function standalone without a mouse, keyboard or screen. This is what we call headless.

The Start

First make sure you have a fresh Raspberry Pi setup with a fresh made Raspbian distribution. I am not going to tell you how to do that because there are plenty tutorials on that.

Installing the IP-Cam software.

Open in the Raspbian main screen your terminal screen and make sure you got the latest updates and upgrades by typing in the terminal screen:

sudo apt-get update
sudo apt-get upgrade

Now this may take a while so be patient. After the update and upgrade have finished we have to install the IP-Cam software. There are several programs suitable for this, but I'll be using Motion.
Motion is a lot more as just an IP-Cam. It can make snapshots and best of all it can detect.......motion. I will not be going into this last option at this stage as I am not using it for this project. So let's start with installing Motion.

sudo apt-install motion 

For the next step we need to edit the setup files of the Motion software. We'll do that by adressing the Nano editor. So still in your terminal screen give the following command:

sudo nano etc/motion/motion.conf

Now find (by scrolling or by using Ctrl-W) the following lines and edit them in the following (bold are the new values):
deamon on
webcam_localhost off
framerate 5

The last one is dependend on your network upload capabilities. My provider gives me a good download speed but a very low upload speed so I set it to 5. You might tweek it for your own purposes.

Dont forget to save the new settings.

One last part is to make sure that everytime the Raspberry is powered up the Motion software will start immediately we can do that by the following command, which you'll have to type in the terminal window:

sudo nano etc/default/motion

and find the following line and edit the value as follows


and that's it. The cam is installed. Now we can test it.

Testing the IP-Cam

Well first we need to know the IP adress where the cam is located on you network.In the terminal window type:

hostname -I

The Raspberry will promt you with it´s IP adress. It will be something like

This setup will use port 8081 on your Raspberry PI.

So now we know the ip-adress and the port it functions on we can have a look at what the cam sees.
In any computer on your network and even on the PI itself open a web-browser and put the following URL in:

Remember to substitute the IP adress with the adress you found in the lines above.

That is it. You should see a picture of whatever the cam is aiming at. Updates will be a few frames per second depending on the afore mentioned settings.

You can now access your PI from any computer or laptop in you home network.

Fine Tuning

Ok this is fine. But everytime the PI starts it will start in graphical mode. That is fine for testing purposes but not needed for every day use.

So open your terminal program again and type the following line:

sudo raspi-config

And the original Raspberry configuration screen opens.
Now change the setting Enable boot to Desktop into boot to Console text.

The next time the PI boots it boots in console mode and does not have to load the graphical environment which will make it slightly faster.

So put the power off. Detach the mouse, keyboard and screen and reboot your PI. Wait a few minutes and open a web-page in another computer system. Use the IP adres mentioned above and you should see what the cam sees.

Connect to the world

This all works fine within the confinement of your won network. But we want to be able to see the webcam from anywhere in the world. So here are the next steps.

First lookup your own ip-adress. You can do that in your router or through a webiste like this:

This site will immediately show your own IP-Adress. You can also find it in your router.

Best thing is anyhow to look it up in your router as you will need to alter the settings of your router anyway. Most likely you can access your router from your webbrowser by using the following IP adress: If not consult the routers manual.

Your IP adress will be something like being for sets of 3 figures.

We will need this -outide- ipadress in the future. But not now.

Now we need to setup your router for access from the outside world.

Look for the settings in your router that contol port-forwarding. In my router which is a Huawei it is found under the firewall settings. The external port we will be using on the router is 48461. We will re-route that port to Raspberry's IP-Adress which is in my example and the port which is 8081. In my router it is done like the following picture shows:

Close the router and that's it.

Does it work ????

Well here is a screenshot. Beware that I am using el-cheapo webcam. I bought a few of them for 5 euro a piece so the quality is not the best. But it works.

And for those that are wondering what the hell it is I am showing. It is a Nodemcu. That is an ESP8266 devellopment board. I have attached it to the internet (just like my IP-Cam) and now it can be accessed from anywhere in the world. But that is another project..........

More finetuning

There are still a few things to do:
- When the Raspberry resets (by cutiing the power or whatever reason) it can get a new IP adress. So I need to give it a fixed IP adress in my network
- The port number 48461 is a bit difficult to remember so I need to set that in the Raspberry to something more easy to remember.
- Maybe I will use a free DNS service that will rename my IP-Adress for the outside world in something more easy to remember like or so.

All things that I can write about in future updates in thsi web-log.

So, till next time
have fun

Luc Volders