Security Camera
3. Deploy scripts to Functions
3 / 8

The Security Camera uses Functions to host the front and back end code.

What is Twilio Functions?

Functions is a serverless development environment; it will host the server side code and website required to communicate with the Raspberry Pi.

Visit our Glossary to learn more about serverless architectures.

What is Twilio Runtime?

Twilio Runtime is a suite of value-added tools which help you build, scale and operate your application, including helper libraries, API keys, asset storage, debugging tools, as well as a comple Node.js based serverless hosting environment Twilio Functions.

You will have the following accomplished by the end of this section:

  • A security camera front-end web interface that runs on Twilio Runtime
  • One or more compatible security cameras that can be added to the front-end
  • A security camera configured to send an SMS to a Twilio phone number when a threat is detected
  • Archived images of alerts to be reviewed

Pricing note: Security Camera uses a preview feature of Twilio Sync called Media Content Services (MCS) to store the images the camera takes. MCS is free during the Developer Preview.

What is Sync?

Sync enables you to share data across multiple apps and devices, securely and efficiently. Using Sync means that you avoid building the real-time infrastructure to do this yourself. Twilio terminates and authenticates the websocket connections, manages permissions and access, and propagates data updates to app and device endpoints.

Visit Sync's documentation to learn more.

Create a Sync Service Instance

Twilio Sync is used for synchronization and data storage for this security camera solution.

  • Visit Sync Services to create a new instance
  • Click the red + button
  • Name your new service Security Camera
  • Click the Create button
  • Make note of the Service Sid for later
    • The Sid starts with IS

You will be using a Twilio phone number to send text messages when the device detects intrusions that threaten assets or safety.

What is a Twilio phone number?

Twilio allows you to instantly provision national, mobile, and toll-free phone numbers in more than 70 countries that you can use with developer-friendly APIs. This Blueprint uses a phone number when sending text messages to notify stakeholders of intrusions.

Visit the phone numbers documentation to learn more about provisioning and best practices.

Purchase a Twilio phone number

You will need a Twilio phone number to send SMS alerts.

  • Visit the Buy a Number section of the Console
  • Enter an area code of your choice in the input field
  • Check at least SMS from the Capabilities checkbox
  • Click Search

security-camera-buy-numbers-medium

You should see a list of available numbers to purchase.

  • Select a number of your choice
  • Click Buy This Number

You now have a number that you can associate with the Security Camera. We’ll make use of this phone number in the next step.

Deploy Runtime assets

Runtime assets are used to host the front-end of this Blueprint. The front-end is written using the AngularJS framework and compiled as a single page application. To deploy, you need to download the latest version of index.html and index.min.js which you can find in the Security Camera repository.

To upload the assets:

  • Visit Runtime assets
  • Click the red + button
  • Locate index.html
  • Click Open
  • Locate index.js
  • Click Open
  • Repeat the same process for styles.js and vendor.js

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://xxx-yyyy-7677.twil.io/.

Create a new API key

An API key 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
  • Click Create API Key
  • Make a note of the Key Sid
  • Make a note of the Key Secret
  • Click Done

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

Deploy Runtime Functions

It’s time to deploy the scripts to 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.

To create the Functions:

  • Visit Functions
  • Click the red + button
  • Click the Blank option
  • Click Create

security-camera-functions-blank

Alert Generator

  • Enter AlertGenerator as the Functions Name
  • Enter /alertgenerator as the Path
  • Paste the contents of AlertGenerator.js into the Code textbox
  • Click Check for valid Twilio signature
  • Click Save

security-camera-functions-alertgenerator

Why enable “Check for valid Twilio signature?”

Enabling Check for valid Twilio signature provides additional authorization benefits. An arbitrary caller on the public Internet will not be able to invoke the Alarm endpoint now.

User Authenticator

  • Create a Blank Function
  • Enter UserAuthenticator as the Functions Name
  • Enter /userauthenticator as the Path
  • Paste the contents of UserAuthenticator.js into the Code textbox
  • Click Save

Camera Authenticator

  • Create a Blank Function
  • Enter CameraAuthenticator as the Functions Name
  • Enter /cameraauthenticator as the Path
  • Paste the contents of CameraAuthenticator.js into the Code textbox
  • Make note of this URL
  • You will use this URL in the next step
  • Click Save

Your Functions list should now look like the following:

security-camera-functions-dashboard-small

Configure Sync webhook

  • Visit Sync Services
  • Click on the Security Camera service created in the Create a Sync Service Instance step above
  • Enter the full URL from the AlertGenerator Functions step above into the Webhook URL textbox
  • Click Save

security-camera-sync-instance-configure

Configure Runtime Functions

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

  • Visit Functions Configuration
  • Click Enable ACCOUNT_SID and AUTH_TOKEN to enable
  • Click the red + button
  • Enter ALERT_FROM_NUMBER as the Key
    • Use the Twilio phone number created from the Purchase a Twilio phone number step above as the Value
  • Click the red + button
  • Enter API_KEY as the Key
    • Use the API key from the Create an API key step above as the Value
  • Click the red + button
  • Enter API_SECRET as the Key
    • Use the API secret from the Create an API key step above as the Value
  • Click the red + button
  • Enter SERVICE_SID as the Key
    • Use the Sync Service Instance created from the Create a Sync Service Instance step above as the Value
  • Click the red + button
  • 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
  • Click the red + button
  • Enter USER_PINCODES as the Key
    • Enter {"twilio":"928462"} as the Value

Your Environmental Variables should look like the following:

security-camera-functions-configuration

That’s it! In this section you:

  • Created a functional front-end that runs on Twilio Runtime
  • Created serverless scripts using Functions
  • Configured the camera to send an SMS to a Twilio phone number when a threat is detected

Continue to the next section to download and install the Raspbian operating system on your SD card.