Software apps and online services
Hand tools and fabrication machines
A few weeks ago we released a project called balena Cam where @mbalamat developed an application running on balenaOS that initiates a WebRTC video streaming connection between the server (a Raspberry Pi, for example) and the client. You can check the full blog post here.
Once the main core of the application was stable, we decided to challenge ourselves and take the project down a slightly different route; turning a fake CCTV camera into a real working CCTV camera, where we control the software running on it 🤯.
Come with us in this step-by-step tutorial showing how you can build the hardware and software of this fake/real CCTV camera!
Let’s go!Hardware required
Here’s a list of what you need for this project:
- Raspberry Pi Zero W:~$10 - This is the main brain of the operation, the device will process the images from the camera, and transmit them via WiFi to be accessible over the internet via WebRTC.
- Wide Angle – Camera Module for Raspberry Pi Zero:~ $17 - We chose to use this wide-angle lens for the Raspberry Pi Zero for its minimal form factor so that it fits on the cylinder of the fake camera. It has a 5MP sensor (2592x1944 pixels) and can display 53.50 degrees horizontal, 41.41 degrees vertical field of view. Perfect for our project!
- Fake Surveillance Security Camera:~ $9 - We looked for a cheap fake camera that looks real enough (this one even includes a flashing red LED).
- microSD card: 16GB recommended.
- USB Power Supply and micro-USB cable: 5V and 1A recommended.
- You will also need a soldering iron, glue gun, and one 330Ω resistor.
Just as you need hardware for this project, you also need software! Here’s what you need:
- A balenaCloud account: You can sign up for free here. You will use this account to manage your Raspberry Pi device.
- balenaEtcher: balenaEtcher is a desktop application that you will use to flash balenaOS onto your microSD or standard SD memory card. You can download balenaEtcher here.
- Balena CLI: You will use the balena cli to push the application code to the device. Check the installation guide for different operational systems here.
In this section, we will go through the process of taking the barebone fake camera, mount our Raspberry Pi Zero W with the wide-angle lens camera and transform it into a real surveillance camera. Let’s go! 💪
First up, take the camera apart, you will notice that the hardware itself is extremely simple. There is one battery support for two AA 1.5V batteries, which powers a red LED (this is a special led, that blinks when supplied power).
Step 1 - Taking the camera apart
Step 2 - Adding the camera to the setup
Once you have all the parts of the camera separated, we are ready to add our own hardware. What we have to do now is to find a way to insert our own lens inside the open hole of the cylinder.
The easiest way to make sure the camera module is placed in the center of the cylinder is by designing a small plastic container to place the camera, as displayed in the image below. (If you have a 3D printer available, you can download the part from thingiverse.
- In case you don’t have access to a 3D printer, you can make use of a hot glue gun or even white craft glue to place and secure the camera inside the cylinder.
Step 3 - Adapting the resistor for the Raspberry Pi
Once the camera is placed in the right position, we can focus on the red LED for a second. We can simply disconnect (unsolder or cut the wires) from the battery socket.
We want to power the LED from our Raspberry Pi, and for that, we will solder a 330Ω resistor on the tip of one of the LED pins and solder two female jumper-cable in order to make it easy to connect to our device.
Now we can connect the LED between any of the 3.3V or 5V (01, 02, 04, 17) pins to the GND (06, 09, 14, 20, 25, 30, 34, 39). And we are done! This LED has a specific feature where when power is applied it automatically starts to blink. 😉
Step 4 - Connecting the hardware
It is time to assemble everything back together, first (as shown in the above image).
Make sure you gently attach the flex cable to the connector on the Pi as shown in the image below. To make the whole setup more stable, you can also screw or hot-glue the device to the plastic casing.
You need to replace the fake cable on the camera with a real micro-USB cable that you will then use to power the Raspberry Pi.
Before assembling everything back together, check out the next steps on how to setup the software and make sure everything works.
Below we’ll walk you through each of the steps required for setting up the software on your device to deploy the camera application.
Step 1 - Sign up for a free balenaCloud account
The first thing you’ll need is to sign up for an account. If you’ve already got a GitHub or a Google accountyou can use that to log as well.
Step 2 - Install and link the balena CLI to your account
The next step is to download and install the balena-cli on your computer. The documentation for the CLI tools is the best place to start and covers the installation and setup of both the npm package and the standalone binaries.
When you have the CLI installed and working, the first step is to log in to balenaCloud by issuing the
balena login command:
Once you’ve reached this point, and have a working CLI which has been logged in to your account, you're ready to start pushing code to your device.
Step 3 - Create a balenaCloud application
Once logged in your balenaCloud account, click on the green “Create application” button near the top left-hand side of the interface. Next, give your application a name, select the device model of your Raspberry Pi, in our case a Raspberry Pi (v1 or Zero). You can select the “Starter” account package, as this account type is free and will have everything you need. Once you create the application, the main dashboard for the application will load.
Step 4 - Add a device and download the balenaOS disk image
After creating the application, the next step is to add a new device. Click the green “+ Add device” button on the top left section of the application interface and follow the instructions. When adding the new device:
- Use the recommended build version of balenaOS.
- Select the “Development” edition of the recommended balenaOS version. The development edition has a variety of testing and troubleshooting tools that are often useful. Read more about the differences between the development and production editions here.
- If you plan to connect your device to a WiFi connection, enter your WiFi credentials (note that the Raspberry Pi Zero W is not capable of connecting to 5GHz WiFi networks unless you use an external WiFi adapter that supports it).
Once you select the configuration options, click the blue “Download balenaOS (~137 MB)” button. This will download the custom balenaOS disk image to your desktop.
Step 5 - Flash your SD card with the balenaOS disk image
After you have successfully downloaded the balenaOS disk image, you now need to flash this disk image to your SD card. We recommend using our own balenaEtcher for this purpose.
To flash your SD card, connect it to your computer via an internal or external SD reader/writer drive. Open balenaEtcher, select the recently downloaded balenaOS disk image and select your connected SD card as the destination drive. Next, click “Flash” to write the disk image onto your SD card. This writing process can take several minutes.
After you successfully flash balenaOS onto your SD card, safely eject the SD card from your computer and insert the SD card into your Raspberry Pi. Next, connect your Raspberry Pi device to a power outlet. The device should then boot up with the balenaOS operating system. Once the device boots up, you will see the device listed as “Online” on the balenaCloud dashboard.
Step 6 - Deploy the balenaCam application to your Raspberry Pi
Now that you have the balena-cli working on your computer and your device is online and showing on the dashboard, it’s time to deploy the code. ✨✨✨
With the file downloaded and unzipped to your computer, using the terminal, go to the newly created folder and push the code live with the command
balena push <application-name>, with
<application-name> being the name you created on the dashboard.
Step 7 - Configuring your device
At this point, your device will start downloading and installing the containers. Meanwhile, go to Device Configuration on the dashboard and add the following Custom Variables:
Set all of these, so your Pi can autoselect the gpu_mem allocation automatically depending on the amount of RAM available and the start configuration.
- BALENA_HOST_CONFIG_start_x = 1
- BALENA_HOST_CONFIG_gpu_mem_256 = 192
- BALENA_HOST_CONFIG_gpu_mem_512 = 256
- BALENA_HOST_CONFIG_gpu_mem_1024 = 448
With the custom variables set, go to the device main page and toggle the
PUBLIC DEVICE URL and click on the arrow icon. It will open a new browser tab and you will now be streaming the images from your “FAKE” surveillance camera.
Congratulations, you have just finished turning a fake surveillance camera into a real and working one with a Raspberry Pi and balenaCloud! The next steps for this project would be to improve the software running on balena to record video (remember that currently, we are only streaming the video feed), detect movements and more. Let us know what you have in mind, maybe we can build it together!
Thanks for reading, we hope you found the project useful and fun to build. If you’ve tried it we’d love to hear from you on the forums, on Twitter @balena_io, on Instagram @balena_io or on Facebook where we'd be more than happy to help!