Raspberry Pi CCTV with netcat and mplayer

Adam Derewecki
4 min readSep 22, 2019

I’ve been playing with different ideas for Raspberry Pis with cameras, and was surprised how quick and easy it was to set up a very basic / proof of concept level closed-circuit television system at my house. Using a camera on a Pi, you can stream a video signal over netcat to an mplayer process on a different Pi connected to an LCD display, with essentially two simple commands.

Camera

While searching for inexpensive Pi Zero Ws on Amazon, I ran across the OctoCam kit which included the Pi, 5 megapixel camera, and (as the description says) a “custom red octobpus case” with suction cup mounts.

The suction cups are what drew me to this originally, as a quick/cheap solution to stick a camera almost anywhere. Because I mounted to my front gate, I ended up not using them — but the pre-drilled suction cup holes worked great for zip ties.

Display

I had a leftover 7" LCD touchscreen display and RPi 3 leftover from the decommissioned Raspberry Pi Bus Schedule. The touchscreen is a little overkill, but it’s what I had on hand.

Streaming

Camera

Start with the latest Raspbian image loaded onto a micro SD card. You need to turn on SSH and set up the wifi while you have the card mounted on your computer:

$ touch /Volumes/boot/ssh
$ cat <<EOF > /Volumes/boot/wpa_supplicant.conf
> ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
> network={
> ssid="YOUR_NETWORK_NAME"
> psk="YOUR_PASSWORD"
> key_mgmt=WPA-PSK
> }
> EOF

Now insert the micro SD card into the Pi and power it on. Using your router’s DHCP client list, you should see a new Pi. If you’re unsure which one is the Pi:

  • the hostname should be raspberrypi
  • the hardware MAC address begins with b8:27:eb
  • the lease time should be just a few minutes short of 24 hours

You should be able to ssh into it with ssh pi@192.168.0.X at this point (default password raspberry). When connected, run sudo raspi-config and change the hostname to picam to make it more recognizable. Also go into interfaces and enable the camera. When you finish you’ll need to restart the Pi.

Create a script in the home directory named raspivid.sh with:

#!/bin/bashwhile true
do
raspivid -a 12 -fps 10 -t 0 -w 800 -h 480 -o - | nc lcdtouchscreen.local 5000
done
  • -a 12 overlay the date and time
  • -fps tells raspivid to capture at ten frames per second
  • -t 0 capture for an infinite amount of time
  • -w, -h scale to width and height
  • -o — output to STDOUT
  • | nc pipe STDOUT to netcat
  • lcdtouchscreen.local the hostname of the display (using .local for Bonjour lookup)
  • 5000 the port to connect to on the display

It’s wrapped in a while true; do; done loop because if the display’s netcat process terminates, this netcat command terminates. The loop will restart the netcat command after that happens and wait for the display’s netcat listening process to start up. (There are more robust ways to do this, but quick and dirty is fine for this proof of concept).

To have this script run on start-up, use sudo nano or sudo vi to open /etc/rc.local and add the line:

bash /home/pi/raspivid.sh

Display

Follow the same steps as the camera, except:

  • make the hostname lcdtouchscreen
  • don’t enable the camera

Create a script in the home directory named mplayer.sh with:

#!/bin/bashwhile true
do
netcat -l -p 5000 | mplayer -fs -fps 10 -cache 2048 -
done
  • netcat -l p 5000 netcat listens for connections on port 5000
  • | mplayer pipes the video stream from netcat to an mplayer process
  • -fs -fps 10 full screen at ten frames per second
  • -cache 2028 fills a buffer before beginning the stream
  • - tells the mplayer process to read from SDTOUT

Similarly, use sudo nano or sudo vi to open /etc/rc.local and add the line:

bash /home/pi/mplayer.sh

Fisheye Upgrade

The 5mp lens that came with the OctoCam ended up not being wide enough angle for my purposes attached to the front gate. I swapped out that cam for a Miuzei Raspberry Pi 5 Megapixels Wide-Angel Fish-Eye Camera.

--

--

Adam Derewecki

Hi! I’m Adam. I live in San Francisco, write code, take pictures, and practice yoga.