This is the first post in a new mini-series, where we build a home surveillance system, mainly using Raspberry Pis. In this post, we use a Raspberry PI zero and create an IP camera that streams video over RTSP. The output video is of a much higher quality compared to the previous example and even the esp32-cam board. By default, it is set to output 1080 video at 30fps with a bit rate of 2Mbps, but all of these can be updated to suit your needs.
The video above guides you through the entire process and I would recommend watching that first, to get an overview of how it all comes together.
Step 1: Gather the ComponentsWe will be using a Raspberry Pi zero here, mainly becuase it is compact and cheaper compared to the other variants. However, this will also work with any other Raspberry Pi variant.
Here are the main components that we would need:
- Raspberry Pi board
- microSD card
- Camera module
- Suitable camera cable
- Suitable power source
- USB card reader to access the card and it's contents
The first thing we need to do is load an operating system on the microSD card. To do this, head over to the downloads section of the Raspberry Pi website and download the Raspbian Lite OS.
https://www.raspberrypi.org/downloads/raspbian/
Then, download and install Etcher, if you do not already have this. Select the image you downloaded, make sure you have selected the microSD card and then click the Flash button. Wait for it to complete.
We then need to enable WiFi networking by creating the wpa_supplicant.conf file in the boot drive. You can also download the following template and update it with your details - country code, network name and password. It is recommended to use a text editor like notepad++ or sublime to do this.
https://www.bitsnblobs.com/wp-content/uploads/2020/05/wpa_supplicant.txt
The last thing left to do is enable SSH. This allows us to remotely access and control the Raspberry Pi, over a network. Doing this is simple. Simply use one of the text editors mentioned above to create a new file, and then save it to the bootdrive with the name "ssh". You do not need to add anThis is the first post in a new mini-series, where we build a home surveillance system, mainly using Raspberry Pis. In this post, we use a Raspberry PI zero and create an IP camera that streams video over RTSP. The output video is of a much higher quality compared to the previous example and even the esp32-cam board. By default, it is set to output 1080 video at 30fps with a bit rate of 2Mbps, but all of these can be updated to suit your needs.
Step 3: SSH Into & Prepare the Operating SystemOpen up a command prompt window by typing in "cmd' in the start menu. You can use the terminal if you are on Mac OS. Then, type in "ssh pi@192.168.1.35" and hit enter. Remember to use the IP address corresponding to your board. It will ask you if you want to authenticate/store the key. Type in yes and hit the enter key. It will then ask you for a password, so enter "raspberry" which is the default password, and then hit enter again. This will log you into the board.
Before we can work on the actual script, we need to enable the camera module. This can be done by running the "sudo raspi-config" command. Navigate to the "Interfacing Options", then "Camera" and hit enter. Select "yes" when it asks you if you want to enable the camera and then navigate to the "Finish" option. It will ask you if you want to reboot. Select "yes" and then wait for the board to reboot. Give the board a minute and then ssh back into it like before.
Next, it is a good idea to update the OS so type in "sudo apt update" and hit enter. Then type in sudo apt full-upgrade" and hit enter. Follow the onscreen prompt to install the updates. This may take a while depending on your internet connection, so do have some patience.
Finally, we need to install vlc and this can be done by running the "sudo apt-get install vlc" command. Follow any prompts and wait for it to complete. You can now move on to creating the script.
Step 4: Create & Test the ScriptIn order to create the script, run the following command "sudo nano rtsp-stream.sh". This will open up a text editor and you can then type in the contents of the script which is shown below:
#!/bin/bash
raspivid -o - -t 0 -rot 180 -w 1920 -h 1080 -fps 30 -b 2000000 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/stream}' :demux=h264
We are simply creating a video stream using the raspivid command and we then make this available over the network using VLC. You can update the resolution, frame rate and bitrate to suit your needs. The following link will take you to the relevant page which contains some documentation.
https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspivid.md
Once this is done, press the "CTRL+X" keys and it will prompt you to save the File. Type "y" and hit enter to save. We then need to make this script executable and that can be done by running the "chmod +x rtsp-stream.sh" command. To execute the script, simply type in "./rtsp-stream.sh" and hit enter. This will enable the stream.
In order to view the stream, you will have to download and use VLC. Once done, select the "Open Network" option from the File menu and enter the following URL:
"rtsp://192.168.1.35:8554/stream"
Again, be sure to use the IP address of your board. Then, hit the open button and you should be able to view the stream.
If you want to check the resource usage, then you can open up a new command prompt window, SSH into the board and then run the "top" command. Please watch the video for further details.
Step 5: Automate the StreamNow that we know that the stream works, we simply need to automate it such that it runs on startup. Doing this is simple, simply run the following command "sudo nano /etc/systemd/system/rtsp-stream.service". This will open up the text editor and you will have to type in the following contents:
[Unit]
Description=auto start stream
After=multi-user.target
[Service]
Type=simple
ExecStart=/home/pi/rtsp-stream.sh
User=pi
WorkingDirectory=/home/pi
Restart=on-failure
[Install]
WantedBy=multi-user.target
Once done, save the file by pressing the "CTRL+X" keys, then Y, then Enter. We then need to enable the service by running the following command "sudo systemctl enable rtsp-stream.service". That's all we need to do. The next time the board boots, it will automatically execute the service which will call the script. You can also manually control the service using the following commands:
To Start: "sudo systemctl start rtsp-stream.service"
To Stop: "sudo systemctl stop rtsp-stream.service"
View Status: "sudo systemctl status rtsp-stream.service"
Reboot the board by running the "sudo reboot" command. Give it a minute to boot and then open up VLC to view the stream.
And that's how you can create an IP camera using the Raspberry Pi zero. Everytime the board boots, it will automatically create the stream and you can view this remotely. In an upcoming post, we will learn how to create an NVR which will allow us to view multiple streams and save them to some storage. If you like this kind of projects, then please consider subscribing to our YouTube channel as it helps us continue creating projects like these.
YouTube: https://www.youtube.com/channel/UCbWiK1A5RqAugSquBHuyBdA
Thank you for reading!
Comments