Build a DIY Security Camera With the Twilio API and Raspberry Pi

In 2014, Dropcam was acquired by Google's Nest Labs for $555 million, and the connected camera category has only continued to grow. In this tutorial, we reproduce the basic functionality of a camera like the Nest Cam but with the unique property of being cellular connected. Cellular connectivity will enable our device to operate in remote areas, large warehouses and other non-WiFi locations. Cellular can also act as a backup for WiFi — a particularly important factor when the goal is to protect against break-ins.

You'll learn how to build a security camera that keeps watch over remote locations and alerts administrators of intrusions or safety concerns — your own round-the-clock security team.

If you find this tutorial helpful, you can visit Twilio's Programmable Wireless Blueprints to learn how to build even more IoT connected devices, such as industrial sensors and fleet trackers.

Let's break this down into four parts: Preparation, configuring the server, assembling the hardware and testing.

Preparation

Materials Needed:

  • 1x CanaKit Raspberry Pi Zero W (Wireless) Starter Kit with Official Case
  • 1x Unlocked Modem USB 4G LTE Huawei E397u-53 Worldwide Hotspot Service Required Only T-mobile in US
  • 1x Raspberry Pi Camera Module V2 - 8 Megapixel,1080p
  • 1x Arducam 15 Pin 1.0mm Pitch to 22 Pin 0.5mm Camera Cable for Raspberry Pi Zero Version 1.3 Specific (Pack of 2)
  • 1x Poweradd Slim 2 5000mAh Ultra-compact Portable Power Bank External Battery Pack with ID Identify Tech for iPhone, iPad, Samsung Galaxy and More - Black
  • 1x LoveRPi MicroUSB to USB 4 Port Black OTG Hub for Raspberry Pi Zero
  • 1x Twilio Programmable Wireless Starter Pack

All materials required can be found in the Wireless Security Camera shopping list.

Throughout this tutorial, you will see an reference to the "red + button." This is a standard user interface element that is shown throughout the user experience in Twilio's developer portal. When this tutorial refers to that button, you should be looking for something that looks like this:

red plus button reference

 

Configure Server Software

Once you've registered your Twilio SIM cards, you'll deploy scripts to Twilio Functions. The Security Camera uses Functions to host the front-end and server code. Functions is a serverless architecture that hosts the server-side code and website required to communicate with the Raspberry Pi.

Create a Twilio Sync service instance. The Security Camera will use a preview feature of Twilio Sync called Media Content Services (MCS) to store the images the camera takes. Go to Sync Services to create a new instance — click the red + button; name your new service iSecurity Camera; click the Create button and then make note of the iService Sid (starts with IS) for later.

Create a Twilio Sync service instance 1
Create a Twilio Sync service instance 2
Create a Twilio Sync service instance 3

Next you will need to purchase a Twilio phone number. Visit the Buy a Number section of the Console, enter an area code of your choice in the input field then check at least SMS from the Capabilities checkbox. You can then purchase a number from the options provided. You now have a number that you can associate with the Security Camera. Save this phone number, as we'll make use of it in the next step.

Deploy Runtime assets

Now we'll host our static assets with Twilio's Runtime Assets. The front-end is written using the AngularJS framework and compiled as a single page application. To deploy, you need to download and extract the latest version of the Security Camera repository.

To upload the assets:

  • Visit Runtime assets
  • Click the red + button
  • Locate the assets folder in the Security Camera repository you just downloaded and extracted, and select index.html
  • Click Open
  • Repeat for all files in the assets folder, using the red "+" button

Make note of the URL created when index.html is uploaded. This will be your public URL of your Security Camera. It looks like https://word-word-####.twil.io.

Runtime assets

Now you will create a new API key, which is a revocable credential used to access Twilio services. To create a new key, Visit API Keys, click the red + button, enter Security Camera as the Friendly Name and then create the key. Make a note of the Key Sid and Key Secret, then click Done.

*Note: This secret is only shown ONCE. Make note of it and store it in a safe, secure location.

Deploy Runtime Functions

There are three Functions to create:

These scripts are included in the Security Camera repository. The next few steps will require a small amount of copy and paste work to move the code from GitHub to Functions.

Deploy Runtime Functions 1

 

Create Alert Generator

  1. Create a Blank Function
  2. Enter AlertGenerator as the Functions Name
  3. Enter /alertgenerator as the Path
  4. Paste the contents of AlertGenerator.js into the Code textbox
  5. Click Save

Create User Authenticator

  1. Create a Blank Function
  2. Enter UserAuthenticator as the Functions Name
  3. Enter /userauthenticator as the Path
  4. Paste the contents of UserAuthenticator.js into the Code textbox
  5. Click Save

Create Camera Authenticator

  1. Create a Blank Function
  2. Enter CameraAuthenticator as the Functions Name
  3. Enter /cameraauthenticator as the Path
  4. Paste the contents of CameraAuthenticator.js into the Code textbox
  5. Click Save

Your Functions list should now look like the following:

Functions list

 

Configure Sync Webhook

  1. Visit Sync Services
  2. Click on the Security Camera service created in the Create a Sync Service Instance step
  3. Enter your base URL with /alertgenerator appended and include /alertgenerator into the Webhook URL textbox, as depicted in the screenshot below
  4. Click Save
Configure Sync Webhook

 

Configure Runtime Functions

The values of the variables created below are specific to your deployment, thus you need to configure the deployed functions accordingly.

  1. Visit Functions Configuration
  2. Click Enable ACCOUNT_SID and AUTH_TOKEN to enable
  3. Click the red + button
  4. Enter ALERT_FROM_NUMBER as the Key
    • Use the Twilio phone number created from the Purchase a Twilio phone number step as the Value
  5. Click the red + button
  6. Enter API_KEY & API_SECRET as the Key
    • Use the API key from the Create an API key step as the Value
  7. Click the red + button
  8. Enter SERVICE_SID as the Key
    • Use the Sync Service Instance created from the Create a Sync Service Instance step as the Value
  9. Click the red + button
  10. Enter TOKEN_TTL as the Key
    • Enter 3600 as the Value
    • This determine how often the camera or front-end should refresh their access tokens
  11. Click the red + button
  12. Enter USER_PINCODES as the Key
    • Enter {"twilio":"928462"} as the Value

Your Environmental Variables should look like the following:

Environmental Variables

 

Assemble Hardware

Download the Raspbian operating system

Next, you'll download and install the Raspbian operating system on your SD card.

Go to the Raspberry Pi Foundation website and download the latest version of their Raspbian operating system. It's recommended to get the full version of Raspbian as opposed to the Lite alternative. The full version is called Raspbian Jessie with Desktop.

Assemble the materials

Once you've installed the latest Raspbian operating system, it's time to assemble the components purchased in the Wireless Security Camera shopping list. You will need a USB keyboard, mouse, and HDMI connected monitor in addition to the components in the shopping list above. The additional computer peripherals are necessary to configure the Raspberry Pi and download and install the necessary scripts to run this Blueprint. Follow the steps below:

Insert SIM in USB modem

  • Open the SIM protective cover on the right side of the USB modem
  • Locate the SIM card that you activated in the Get started with Programmable Wireless section by following the instructions printed on the SIM
  • Place the middle punch (2FF) of the SIM card into the USB modem
  • Press the SIM card in until it locks in place
  • Set USB modem aside for now

Connect camera cable

  • Remove the white camera ribbon that came pre-attached to the Raspberry Pi camera
  • Replace the camera ribbon with the newly purchased ribbon cable from the shopping list

Attach the USB cellular modem, USB keyboard and USB mouse to the USB hub.

Plug the following cables into the Raspberry Pi

  • Plug the USB hub cable into the port labeled USB on the Raspberry Pi
  • Plug the 5.1V power adapter into the port labeled PWR on the Raspberry Pi
  • Plug an HDMI monitor cable into the micro HDMI to HDMI adapter on the Raspberry Pi
  • Pug the micro HDMI to HDMI adapter into the port labeled HDMI on the Raspberry Pi

Your device is now powered up! We'll come back later and remove the USB hub and power adapter to make this a portable device.

*Note: The default user for the Raspberry Pi is named pi and the password is raspberry. The default password is fine for testing but must be changed if you ever put these devices out in the field.

Set up the Raspbian operating system and Security Camera scripts

Now it's time to set up the Raspbian operating system and run the Security Camera scripts. You will need to install additional packages on the Raspbian operating system.

Configure the cellular USB modem
The Raspberry Pi Zero W was chosen because it is Wifi enabled — a huge advantage during the configuration process. In the link below, you will download and install the necessary packages to enable the USB cellular modem for remote access with Twilio Programmable Wireless.

The following section requires an Internet connection. You can use either Cellular or Wifi connectivity during this step.

Add your Security Camera to the front-end

  1. Visit the front-end site created in the Deploy scripts to Functions section
  2. Click the Add Camera button at the top right corner of the website
  3. Click the Add New Camera button
  4. Enter the Twilio phone number provisioned in the Deploy scripts to Functions section
  5. Click the Submit button
  6. Make a note of the ID generated you will add this to the Raspbian Security Camera script
  7. Make a note of the Token generated you will add this to the Raspbian Security Camera script

Install required Linux packages
Open a terminal and type the following commands:

csudo apt-get update
sudo apt-get install nodejs libopencv-dev

Enable the camera

  • Type the following in the command line:
sudo raspi-config
  • Scroll down to Enable Camera in the menu system
  • Press Enter to enable the camera
Raspberry Pi Software Configuration Tool

*Note: It's recommended to reboot Raspberry Pi at this point.

Install Raspbian Security Camera scripts:

  1. Make the camera directory in /home/pi
mkdir /home/pi/camera
mkdir /home/pi/camera/images
  1. Download the Node.js script and dependency file from the Security Camera repository into /home/pi/camera
wget https://raw.githubusercontent.com/twilio/wireless-security-camera/master/pi/security-camera.js
wget https://raw.githubusercontent.com/twilio/wireless-security-camera/master/pi/package.json
  1. Install Node.js modules from package.json
npm install
  1. Open security-camera.js to configure front-end credentials
sudo leafpad /home/pi/camera/security-camera.js
  1. Change the clientBootstrapUrl variable on line 13 of security-camera.js to the URL where your CameraAuthenticator Function is deployed from the Deploy scripts to Functions section
  2. Change the cameraId variable on line 11 to the ID you used when adding the camera from the Add your camera to the front-end section
  3. Change the cameraSecret variable on line 12 of security-camera.js to the token generated from the Add your camera to the front-end section
  4. Run the script by typing the following in a terminal
cd /home/pi/camera
npm start

Configure security-camera.js to run at startup
You can manually run the script every time you want to use the camera or have it run when the Raspberry Pi boots up.
To have isecurity-camera.js run at startup:

  1. Type the following commands in a terminal
sudo leafpad /etc/rc.local
  1. Add the following line right before irc.local reads "exit 0"
node /home/pi/camera/security-camera.js
  1. Save and exit leafpad

The script is set to run every time the Raspberry Pi boots up. On to testing!

Time To Test

To make the security camera portable, we're going to remove the unnecessary components from the Assemble the materials section. This will leave you with the minimal amount of hardware required to operate the Security Camera.

  • Remove all USB cords from the USB hub
  • Insert the USB / micro USB cord that came with the USB power adapter into the USB power adapter
  • Insert the appropriate end of the Cable On The Go adapter from the shopping list into the USB modem
  • Plug the USB modem into the port labeled USB on the Raspberry Pi
  • Plug the power adapter into the port labeled PWR on the Raspberry Pi

The Security Camera is now ready to test in the wild! Place your new Security Camera around any environment you want to monitor.

The code you deployed in the Deploy scripts to Functions section created a public, addressable, webpage URL. Navigate your browser to that URL, now. It will look similar to https://xxx-yyyy-7677.twil.io/.

You're Done!

That's it! Now you have a security camera that keeps watch over dangerous and remote locations and alerts you to intrusions or safety concerns.

Be sure to read the next Security article: What Airport and API Security Have in Common

 

Comments (0)