Webcam Over WAN: Raspberry Pi 2 and Raspberry Pi Camera Module (B) Rev 2.0


The Raspberry Pi Camera module is fantastic device! However, to make this webcam work required way too much research time and effort so I thought I would take a few moments to provide everyone my “lessons learned” pulled from a sea of confusion in the hope this will help make the lives of other novices, like myself, better. Pick out the tid-bits you like from the discussion. This represents the easiest solution for enjoying excellent live HD video from anywhere in the world – from your laptop, Android or iDevice – without any third party service fees.

The goal of this project was simply to view live weather conditions from hundreds of miles away.  Click here to see a sample Vimeo video of what the camera is capable of, and see details of an assembled unit (3:24 long).  As discussed at the bottom of this post, both the package and Python code have been significantly simplified and made more reliable than shown in the video.

 

Short Falls of Older Tutorials

Many of the old Raspberry web cam tutorials found on the web were created with older Raspberry Pi configurations and fell apart as new revisions hit the market that left out key software components rendering the tutorial useless for today’s projects. Many others explain how to create live video with Local Area Networks (LAN, which is restricted inside your home) which is of no use to me. Still others would let you access your web cam over the Wide Area Network (WAN provides access from anywhere in the world) but would disconnect the camera from the web cam when you logged off the computer you were making changes from; this option put your web cam on the same network as your computer somewhere else in the world and shut down the cam when you closed your computer down since it lost the internet connection. Other solutions required Linux be loaded on my laptop but my computer is old and very memory challenged, so that was not an option for me.

Still, others solutions required knowledge that I simply don’t have at the moment but may be “standard operating procedure” for many of you more advanced readers. For example, “simply download and cross compile the software form this link”. I don’t know how to do that yet and could not find a tutorial to explain how to do that, but I have a hunch it requires Linux on my laptop.

 

Create a Live Web Cam Accessible Anywhere In the World

The following 3 step solution works great! However, I cannot take credit for any of the technical content of what I am about to provide; that credit goes to the people who spent the time creating the websites I reference. A suggestion would be to open the following links in your Raspberry Pi browser so you can copy and paste most of the code.

Before you get started, be aware that there is about a 17 second delay time between what shows in front of the camera until it shows up on the YouTube Live stream. While this is fine for watching weather conditions change, it may not be good at all if using it for a backup camera for you new SUV and boat trailer. The quality of the HD video on YouTube Live is excellent, especially when considering the relative low cost of the components.

 

Step 1. Basic Raspberry Pi 2 Startup

If you have not done it yet, here is a good video to get the latest upgrades, updates, enable your camera, enable SSH, and more.
https://www.youtube.com/watch?v=qLo-0Tmmlrc by Peter Oakes.

Step 2. YouTube Live Account

Create your own YouTube Live account since we will need information from that account later. From your YouTube Live Stream page, get the encoder information provided at the bottom left of the YouTube Live page to enter into your Raspberry launch command later, and later you will see it go live using the Share link also provided at the bottom of the YouTube Live page. This should get you started: https://support.google.com/youtube/answer/2474026?hl=en

 

Step 3. Install FFMPEG

Many tutors on the internet complain that it takes 9 hours to download FFMPEG but it only took me a little over an hour and a half (very reasonable to me). My Verizon FiOS internet speed is about 50 to 75 Mbps upload and download as a reference. Val Mueller’s link worked flawlessly for me and provided good explanations along the way, including definitions of different segments of the script. Except Val Mueller’s actual syntax used to stream to my YouTube Channel was close, but didn’t quite work (perhaps too much for YouTube to handle). Between Val Mueller and Mr. Schlatter I was able to make it work…These links are provided by Val Mueller and Wolfgang Schlatter.

Load FFMPEG from Val Mueller’s site:
http://www.valmueller.net/stream-video-with-raspberry-pi-to-youtube/

But use the syntax command from Mr. Schlatter’s site to launch the video to YouTube Live:
https://plus.google.com/+wolfgangschlatter/posts/2L5fRou17AX

EXCEPT on Mr. Schlatter’s command line, remove the “http://” and the “./” infront of ffmpeg from the command so it looks like this:

raspivid -o – -t 0 -w 1280 -h 720 -fps 25 -b 4000000 -g 50 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i – -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/(Your_YouTube_Live_Stream_Name_Key)

Don’t put the brackets around the words: Your_YouTube_Live_Stream_Name_Key, just cut and paste the long alpha numeric Stream Name Key from your YouTube account and place it right after… live2/. Note:  If the email address you used to open your YouTube account is myemail@gmail.com, and your live stream name key is hqp2-4tb6-86Z5-LJM8, you may find that you will need to enter your Your_YouTube_Live_Stream_Name_Key like this: myemail.hqp2-4tb6-86Z5-LJM8 rather than just entering hqp2-4tb6-86Z5-LJM8.

Remember to launch this syntax from this directory: cd /usr/src/ffmpeg and you don’t need “sudo” in front of the syntax command to launch the video.

Once you enter the syntax you should see live streaming on your YouTube Live account and then you will be able to view it from anywhere in the world from the YouTube Live account! Again, note there is about a 17 second delay from what goes on in front of the camera until it goes live, but otherwise you get a clean, crisp picture without a lot of buffering. You don’t need to worry about creating a New Live Event or using any of their suggested software items. Just launch it from your Raspberry and view it live. If it doesn’t come up live right away, check your YouTube Video Manager to see if one of your previous viewings is processing (delete it if you wish or wait out the processing time), then reboot the Raspberry Pi, and refresh your YouTube Live screen.

 

Other Useful Key Items

You might find it desirable to:  Work with your Raspberry Pi desktop with a wireless internet connection;  view the Rpi2 desktop on your own laptop rather than another monitor; create a Static IP so the camera doesn’t stop working when the Dynamic IP changes; have the ability to turn off the camera light so it doesn’t reflect into the camera in low light conditions; or make router changes to allow you to work on the camera from miles away using your Windows Remote Desktop Connection. Here is how to accomplish those items and more…

Additional Lessons Learned

  1. Remote Desktop Connection with Raspberry Pi
  2. How To Use Remote Desktop Connection Anywhere In The World
  3. Create A Static IP Address For Your Raspberry Pi
  4. How to Turn Off The Glaring Raspberry Pi Cam Red LED Light
  5. Wireless Internet Connection
  6. How to Automatically Run your Camera Script in the Background at Startup
  7. Automatically Run Python Script for Your Stepper Motors in the Background at Start Up
  8. “Kill” Scripts You Automatically Launched At Startup
  9. Share Your Files from Raspberry Pi 2 to Your Windows PC (back and forth)
  10. Clone Your SD Card to Create a Backup Copy
  11. Using Stepper Motors for Camera Pan/Tilt Functions
  12. How Does a Stepper Motor Work – For Those With Inquiring Minds
  13. Rpi2 General Purpose Input Output (GPIO) Connector Wire Diagram
  14. Program A Stepper Motor to Find Home Position w/ Two Photo Resistors & Two LED’s
  15. Having Problems with YouTube Live Not Showing Your Video?
  16. Bit Rate and Resolution Information for YouYube Live
  17. Collection of Often Used Commands
  18. Pan/Tilt Script Used, the stepper motors used, and finding home without LED’s and photo resistors

 

1. Remote Desktop Connection with Raspberry Pi

Using the xrdp on the Raspberry Pi with Windows Remote Desktop Connection (comes with Windows) is the best solution. This allows you to view your Raspberry Pi desktop and make changes to your camera from anywhere in the world and not have camera shut down when you close you remote laptop (as happens using other tutorials and free services).
https://www.jeremymorgan.com/tutorials/raspberry-pi/how-to-remote-desktop-raspberry-pi/

    A Note on iPhone Apps:

As of this post, all iPhone apps that share desktops seem to have buttons that don’t work. For example, the missing space bar action is an important defect I ran into. Though you can see and work on your shared desktop you can’t even type in the most basic “sudo reboot” command without the space bar. It has to be an issue with Apple because too many apps have this same problem. People have been reporting this problem on the web for over a year. Desktop sharing using your laptop and its own keyboard works fine though.

So we have a simple iPhone app work-around…  All I really want to use my iPhone desktop share app for is to reboot my camera when I am away from home with the “sudo reboot” command. When a good friend of mine was presented with my issue, he simply said just create a short cut, or an Alias as he called it, so you don’t need to type a space in “sudo reboot”. It works great!!! To create an Alias, follow these instructions:

http://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html and

https://stackoverflow.com/questions/36093910/alias-to-launch-python-py-script and

http://stackoverflow.com/questions/36136971/least-complex-method-to-a-kill-python-script-running-in-the-background

Type: sudo nano ~/.bashrc and then near the bottom (below the alias definition comments and before the ‘if’ statements) enter

alias reboot=’sudo reboot’ and then save your changes with ctrl-x, y, enter.  Or if a script, then

alias runstepper=’sudo python /home/pi/moveit.py’ and then save your changes with ctrl-x, y, enter.

Now when you type ‘reboot’, it will be the same as if you typed ‘sudo reboot’.

FYI, I found the iFreeRDP Remote Desktop Client (has the blue rabbit-like creature icon) to be easier to use than Microsoft RD Client; neither are great apps but work ok for simple things like rebooting your Raspberry Pi.

 

2. How To Use Remote Desktop Connection Anywhere In The World

Here is how to make changes to your router to accomplish this. The tutorial explains it so well that I can even understand it.
http://www.circuitbasics.com/access-raspberry-pi-desktop-remote-connection/

 

3. Create A Static IP Address For Your Raspberry Pi

Note: Your internet cord needs to be plugged in during this process.
https://thepihut.com/blogs/raspberry-pi-tutorials/16683276-how-to-setup-a-static-ip-address-on-your-raspberry-pi

 

4. How to Turn Off The Glaring Raspberry Pi Cam Red LED Light

At first I would recommend using this tutorial: http://thepihut.com/blogs/raspberry-pi-tutorials/16043032-stealth-cam-how-to-disable-the-raspberry-pi-camera-led
However, it appears that the latest software version, again, has disabled this simple command. So my solution is to just resort to an old fashion hardware change: simply put a drop of silicone adhesive on top of the LED. 

5. Wireless Internet Connection

From the Raspberry Pi Desktop, just click on the icon of two computers in the upper right hand corner, find your internet from the drop down and enter the password for your internet connection. Then you see the familiar radial internet signal you can unplug the internet cord.

 

6. Automatically Run your Camera Script in the Background at Startup

http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/scripts

Once you read through the link above, you may find the key items are:

mkdir ./bin
cd ./bin

sudo nano script_auto_run

#!/bin/bash
# Script to start our camera application
echo “Doing autorun script . . . “
(sleep 2;raspivid -o – -t 0 -w 1280 -h 720 -fps 25 -b 4000000 -g 50 | ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i – -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/Your_Secret_YouTube_Live_Key) &

Then save this with Ctrl+X, Y, Enter. Note: Oh, and the “ &” at the end makes the script run in the background (put a space between the last parenthese and the &).

sudo chmod 755 script_auto_run (this makes the above scripts executable).

sudo nano /etc/rc.local

Add the following line: /home/pi/bin/script_auto_run

Then save this with Ctrl+X, Y, Enter.

 

7. Automatically Run Python Script for Your Stepper Motors in the Background at Start Up

This is an easy way to automatically launch a Python script at start up, and is what was used for the pan/tilt stepper motors. Running it in the background lets you continue to work on the Raspberry terminal to make program modifications, etc. while the camera and steppers are running.
http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/

 

8. “Kill” Scripts You Automatically Launched At Startup

Once script is automatically started in the background after start up, how do you stop it? Use these commands to find the process number and then kill that process:

ps ax – – Find the process number(s) that you are running in the background that you want to end (i.e., 605), then kill it with this command (to kill multiple processes just type them in with a space . . . ) like this:
sudo kill –s kill 605 701 702

What ps ax processes look like when the camera is running properly on YouTube.

See the link below for  sudo pkill -f /home/pi/mystepper.py

http://stackoverflow.com/questions/36136971/least-complex-method-to-kill-python-script-running-in-the-background

 

9. Share Your Files from Raspberry Pi 2 to Your Windows PC (back and forth)

If you have a PC or laptop connected to your workgroup, you should be able to see your Raspberry Pi in Windows Explorer under Network. raspberry\pi and then raspberry

http://raspberrypihq.com/how-to-share-a-folder-with-a-windows-computer-from-a-raspberry-pi/

Note: If Windows ever loses your Raspberry Pi share folder in your windows explorer (now that would never happen would it), here is how to reconnect to your Raspberry Pi from a Windows machine. Open the “Computer” window (on Windows 7) or “This PC” (on Windows 8) and click the “Map Network Drive” button on the toolbar (Windows 7) or click the “Map Network Drive” button under “Computer” (on Windows 8) or open the “This PC” app in Windows 10.

In the Map Network Drive dialog, select and unused drive letter from the “Drive:” drop-down list and enter \\raspberrypi\pi in the “Folder:” field and click “Finish”.

Enter pi in the Username field when you are asked to enter the network credentials. Enter the password you set previously in the password field and click OK.

These instructions were improvised from:
http://www.maketecheasier.com/turn-raspberry-pi-into-file-server

 

10. Clone Your SD Card to Create a Backup Copy

This shows you how to create a copy of the script on your SD card so it can be downloaded in the event your SD card somehow becomes corrupted.
http://computers.tutsplus.com/articles/how-to-clone-your-raspberry-pi-sd-cards-with-windows–mac-59294

 

11. Using Stepper Motors for Camera Pan/Tilt Functions

Here is how to use stepper motors to build a camera pan/tilt platform:
http://raspberry-python.blogspot.ro/2013/01/pyhacking-step-by-step.html

 

12. How Does a Stepper Motor Work – For Those With Inquiring Minds

https://www.youtube.com/watch?v=Dc16mKFA7Fo

 

13. Rpi2 General Purpose Input Output (GPIO) Connector Wire Diagram

This diagram is helpful for wiring devices and components to your Raspberry Pi2:
http://elinux.org/RPi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29

 

14. Program A Stepper Motor to Find Home Position w/ Two Photo Resistors & LED’s

A non-contact photo resistor/LED scheme is used to locate the home position on my camera at power up so it begins the pan/tilt process from the same starting point. If it can’t find home, the script will stop and blink LED’s indicating that assistance is required. You will need to pay attention to the ambient light levels too. Here is the photo sensor wire diagram:
http://www.raspberrypi-spy.co.uk/2012/08/reading-analogue-sensors-with-one-gpio-pin/

The photo resistor is a good device for sensing is relative light or dark conditions which is what we need in this application. However, there are other techniques better suited if you need accurate light level measurements that compare readings to a standard.

At the bottom of this post I provide script on how to eliminate the photo resistors and LED’s, and find home after startup simply by storing the stepper motors last position on your SD card. This was a second iteration due to ambient light conditions. It resulted in a more simplified and more reliable solution.

 

15. Having Problems with YouTube Live Not Showing Your Video?

There are at least two things that I have found:
1) If YouTube is processing a prior video, you may at times have a problem viewing your live video at the same time. To get around that, go to your Video Manager, find the video that is processing, check the box next to the video and under actions click delete.
2) If it is still not showing your live video, then reboot the web cam.

 

16. Bit Rate and Resolution Information for YouYube Live

Here is some useful information on tweaking your YouTube Live video quality. https://support.google.com/youtube/answer/2853702?topic=2853713&ctx=topic
. . . and more on streaming to YouTube – Buffering: -vb
http://www.tmplab.org/wiki/index.php/Streaming_Video_With_RaspberryPi

17. Collection of Often Used Commands

sudo raspi-config
sudo apt-get update
sudo aft-get upgrade
sudo apt-get install rpi-update
sudo rpi-update
sudo reboot
sudo apt-get install xrdp
sudo ifconfig
startx
raspistill to take stills
raspistill –o image.jpeg
raspivid to take movies
raspivid –o to testvideo.h264 –t 10000
sudo apt-get install libav-tools
sudo apt-get install ffmpeg
sudo nano libav-tools.conf
sudo nano /usr/local/nginx/conf/nginx.conf to the edit file
rtmp://a.rtmp.youtube.com/live2/
ctrl+C = stop streaming
cd /usr/src/ffmpeg

Plugged into internet
sudo ifconfig
sudo route –n

sudo nano init-script
put the hash sign # infront of “OPT…” to comment it out
sudo nano /etc/rc.local
sudo kill –s kill 605
ps ax = list of running processes; find the one to kill, like 605 which has the scripts I’m running

 

18. Pan/Tilt Script (and stepper motors)

To view the pan/tilt script used, see the bottom of my post on this Raspberrypi.org forum link: https://www.raspberrypi.org/forums/viewtopic.php?t=134141   This code uses LED’s and photo resistors to find home position.

https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=140352  This code located in a different RaspberryPi.org forum link was my second iteration due since the variable ambient lighting was such a significant error factor while locating home. This happens to be a much better solution, however. Now it stores last position on the RPi2 SD card and then uploads and uses that last position data on startup to find home. This eliminates the need for any LED’s, photo resistors, about a third of the packaging wires, and removes a third of the Python code. This is actually more robust, simpler to build, and more reliable mechanically.

Both scripts provide useful pieces of code that can be cut and pasted into your own unique project.

Side note using time control in Python: https://stackoverflow.com/questions/36208132/how-to-use-the-current-local-time-of-day-to-control-a-process-without-dates

The Step Motor’s 28BYJ-48 were with supplied with their own separate control board (A package of 5 was found priced at $14/package on Amazon). The motors are used for controlling HVAC dampers. etc..

Note: See this link to get a better understanding of sudo crontab -e for auto launching Python script, and use of *.txt files for storing data on your Raspberry Pi SD Card:

http://stackoverflow.com/questions/35997444/crontab-no-longer-runs-raspberry-pi-python-script-in-background-but-still-runs-m/36018025?noredirect=1#comment59716325_36018025

 

 

 

 

Tags: , , , , , , , , , , , , , , , , , , , , , ,

5 Responses to “Webcam Over WAN: Raspberry Pi 2 and Raspberry Pi Camera Module (B) Rev 2.0”

  1. Barry Says:

    Mark….dude…finally!!! Thanks a trillion for scouring the Net and finding the best method for A: installing ffmpeg and B: getting it to stream to Youtube without major issues.

    Absolute perfect job! I can’t begin to explain how many effing dead ends I’ve run into….but you already know that.

    Best regards,
    -Barry

  2. Val Says:

    Great Recap and overview of this. This is “Mr. Mueller”…. 🙂

    I too had to scour the internet to find the information to fit my needs and project.

    My final project is live from time to time and is located here on my YouTube channel: https://www.youtube.com/watch?v=tiUf-H5X4rw

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: