Raspberry Pi with Stretch – Static IP, Video Streaming, and CRONTAB Scheduling programs to start and stop
If you hit this page you probably already learned there is a tremendous amount of misinformation and overly complicated comments out there about using the Raspberry Pi.
I wanted to do a fairly simple thing: stream an IP surveillance camera to my 65” television. Well, I may as well have wanted to travel to Mars. Every single piece of information I Googled was either flat wrong or didn’t work. Part of this problem is that the Debian Linux for PI has changed twice in the past two years and is completely different from earlier versions. Another issue is that so called ‘gurus’ always like to leave out parts of the commands and codes that are critical – either thinking you already know them or just to be funny. So here is my step by step procedure based on the latest version of Raspbian Stretch.
SETTING A STATIC IP
First I needed to set a wired static IP address for my Pi so I could find it on my network and hard code that IP into another program. Don’t believe anything you’ve read because it doesn’t work. The only way I’ve been able to set a permanent static IP is the following:
On the console terminal or SSH window – type ifconfig
You’ll get back a lot of info but the important one is the number at the top starting with enx
Next type in: sudo nano dhcpcd.conf
Using the DOWN arrow key move the cursor all the way past the last line and type in the following:
interface enxb827eb40e363 (this is the number you got on the first step)
static ip_address=192.168.0.75/24 (this is the IP address you want to be static – the /24 is critical)
static domain_name_servers=192.168.0.1 (if your router is doing the DNS, other wise whatever DNS server you want.)
Type CNTL X, then Y, then RETURN (ENTER)
Type sudo reboot and the PI will reboot. When it starts it should have the IP address you specified above.
Now we want to stream the video from the camera. I’m using an Axis camera so all the info is based on that. If you are using a different one get the RTSP stream parameters for that camera. We will use omxplayer to do the streaming.
At the terminal, console, or SSH type:
omxplayer –avdict rtsp_transport:tcp rtsp://your username:firstname.lastname@example.org:554/axis-media/media.amp
This command assumes the camera is at 192.168.0.101. Substitue your camera’s IP. This command also forces the omxplayer to use TCP instead of UDP. TCP is the only format that works. UPD (the default) will result in a pixelated video display. Your camera should now be displayed on your television (assuming you have the HDMI output of the PI connected to it.
To stop the program, type ESCAPE or Q.
SCHEDULING A PROGRAM TO START AND STOP
Next I wanted to schedule the video display when I was in front of my television – ON at 5pm off at 10pm. Again, after weeks of trying and failing and getting wrong information, here is my method that works:
At the terminal console or SSH, type:
Using the DOWN ARROW key, move the cursor to past the last line and type:
00 17 * * * usr/bin/omxplayer –avdict rtsp_transport:tcp rtsp://admin:email@example.com:554/axis-media/media.amp
00 22 * * * pkill omxplayer
Press DOWN ARROW key once (crontab must have a newline at the end of the file)
Type CNTL X, then Y, then RETURN (ENTER)
The number 17 is 5pm in military or 24 hour time format. The 22 is 10pm. The asterisks mean every day, every month, every year.
Because the crazy Raspbian won’t tell you out of the box if something is going wrong with your crontab jobs, it’s a good idea to install an email server because that’s where the error messages will go. I didn’t want to go to all that trouble, but I did learn an easier way to get the info.
Type sudo apt-get install postfix
When the configuration menu comes up, select LOCAL ONLY at the bottom. This avoids all the tedious email setup and sends email to your local Pi account. When you log on the Pi will tell you if you have new mail. You can read the mail by typing: cat /var/mail/pi
One important final note: if you want to run a program other than omxplayer then use this command to find the exact path and edit the above lines accordingly-
Which (command or program). For example which date
Will give you /bin/date and that is what should be put in the crontab file.
By the way an UP ARROW will repeat the last line you typed in terminal.
Don’t ask me why these specific commands work and all the others around don’t – I don’t know. This was all trial and error. The code above works for me and is in use every day without a hitch.