Home Security with BeagleBone

Create an Internet-connected, home security system with a microphone, piezo speaker, an LED and a BeagleBone Black.
  • Matt Dobson
  • calendar Sep 18, 2014
  • upwardsbarchart experienced
  • stopwatch 1-3 hours
  • download Source Code
  • nodes Project

Directions

  1. Setup Zetta on the BeagleBone
  2. Blink the LEDs
  3. Link to the Cloud
  4. Buzz the Buzzer
  5. Soundcheck the Microphone
  6. Run the Security App

Goal

The goal for this project is to create a simple home security system by assembling a microphone, a piezo speaker and an LED into a Zetta app running on a BeagleBone Black. We will connect the app to the Internet by linking the BeagleBone with a second Zetta server running in the cloud.

The Connected Microphone

Microphone Hookup Diagram

downloadcloud Download the Fritzing diagram for the finished project: home_security_system.fzz.

Parts

All Materials

cart Buy the parts for the Home Security project from SparkFun.

Step #1: Setup Zetta on the BeagleBone

Connect the BeagleBone

  1. Connect your BeagleBone to the internet. For help, check out this topic.

Clone the Starter Code to the BeagleBone

  1. Open the browser-based Cloud9 IDE at http://beaglebone.local:3000.

  2. Click your mouse in the BeagleBone’s IDE console. Cloud9 Splash Screen

  3. From the Cloud9 IDE console, clone the Zetta starter project to a new home-security directory on your BeagleBone.

    git clone https://github.com/zettajs/zetta-starter-project home-security
    

    help Are you seeing error: Couldn't resolve host 'github.com'? Be sure to run dhclient on the BeagleBone, restart your browser and don’t be shy about rebooting the BeagleBone and your PC. Read How to Connect a BeagleBone to the Internet via a PC for more details.

Install Zetta

  1. From the Cloud9 IDE console, cd to home-security.

    cd home-security
    
  2. From the Cloud9 IDE console, install Zetta with NPM.

    npm install
    

    clock Running npm install on the BeagleBone can take several minutes.

Write Zetta Server Code

  1. From the Cloud9 IDE workspace, click on the arrow to the left of the home-security folder. Then double-click on the file server.js to open it in the Cloud9 IDE editor.

    Saving a file

  2. In the server.js file, write code to require Zetta, give your server a name and listen on server port 1337.

    info Choose a name for your server. Consider using your first and last name.

    var zetta = require('zetta');
    
    zetta()
      .name('FirstName LastName')
      .listen(1337, function(){
         console.log('Zetta is running at http://beaglebone.local:1337');
    });
    
  3. From the Cloud9 IDE, click File > Save to save the changes you made to server.js.

  4. From the Cloud9 IDE console, run the Zetta server.

    node server.js
    

    Notice the console output indicating the server is running.

    Apr-23-2014 18:14:23 [server] Server (beaglebone) beaglebone listening on http://127.0.0.1:1337
    Zetta is running at http://beaglebone.local:1337
    

Call the API

  1. In the Cloud9 IDE, open a new terminal tab.

    New terminal tab

  2. In the Cloud9 IDE console, call Zetta’s web API.

    curl http://beaglebone.local:1337 | python -m json.tool
    
    { "actions": [{
        "fields": [
          { "name": "server", "type": "text"},
          { "name": "ql", "type": "text"}],
        "href": "http://beaglebone.local:1337/",
        "method": "POST",
        "name": "query-devices",
        "type": "application/x-www-form-urlencoded"}],
      "class": ["root"],
      "links": [
        {"href": "http://beaglebone.local:1337/", "rel": ["self"]},
        {"href": "http://beaglebone.local:1337/servers/FirstName%20LastName",
          "rel": ["http://rels.zettajs.io/server"], "title": "FirstName LastName"},
        {"href": "http://beaglebone.local:1337/peer-management",
          "rel": ["http://rels.zettajs.io/peer-management"]}]}
    

    info As we use devices in server.js they will appear in the web API. For the following steps we’ll access the API via the Zetta Browser. However, the curl command is a helpful way to use the API from the command line.

Step #2: Blink the LEDs

Write the LED Code

  1. In the Cloud9 IDE console, ensure your working directory is home-security. Install the Zetta device driver for BeagleBone LEDs.

    npm install zetta-led-bonescript-driver --save
    
  2. In the server.js file, write code to require and use the BeagleBone user LEDs: ‘USR0’, ‘USR1’, ‘USR2’ and ‘USR3’.

    Add line 2:

    var LED = require('zetta-led-bonescript-driver');
    

    Add line 6:

    .use(LED, 'USR0', 'USR1', 'USR2', 'USR3')
    
  3. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-bonescript-driver');
    
    zetta()
     .name('FirstName LastName')
     .use(LED, 'USR0', 'USR1', 'USR2', 'USR3')
     .listen(1337, function(){
       console.log('Zetta is running at http://beaglebone.local:1337');
    });
    
  4. From the Cloud9 IDE, click File > Save to save the changes you made to server.js.

  5. In the Cloud9 IDE, stop (CTRL-c or COMMAND-c) and restart the Zetta server.

    node server.js
    
  6. When Zetta discovers the LEDs, it will log messages about the devices to the Cloud9 IDE console.

    {timestamp} [scout] Device (led) {id} was discovered
    {timestamp} [scout] Device (led) {id} was discovered
    {timestamp} [scout] Device (led) {id} was discovered
    {timestamp} [scout] Device (led) {id} was discovered
    
  1. Open the Zetta Browser. Point it to the BeagleBone server. http://browser.zettajs.io/#/overview?url=http:%2F%2Fbeaglebone.local:1337

  2. Ensure your LED devices are listed.

    Zetta Browser with LEDs

  3. Click the turn-on button for the various LEDs.

  4. Ensure the LEDs on your BeagleBone turned on and that the device state changed in the Zetta Browser visualization.

Step #3: Link to the Cloud

At this point, your LED API is only available locally. Let’s make the LED API available from the cloud.

  1. In the Cloud9 IDE console, open the server.js file. Write code to link your Zetta server on the BeagleBone to a Zetta server running in the cloud.

    Add line 7:

    .link('http://hello-zetta.herokuapp.com/')
    
  2. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-bonescript-driver');
    
    zetta()
      .name('FirstName LastName')
      .use(LED, 'USR0', 'USR1', 'USR2', 'USR3')
      .link('http://hello-zetta.herokuapp.com/')
      .listen(1337, function(){
        console.log('Zetta is running at http://beaglebone.local:1337');
    });
    
  3. From the Cloud9 IDE, click File > Save to save the changes you made to server.js.

  4. From the Cloud9 IDE console, stop (CTRL-c or COMMAND-c) and restart the Zetta server.

    node server.js
    
  5. Ensure the peer connection to the cloud is established and the console log includes notifications that the peer was established.

    {timestamp} [peer-client] WebSocket to peer established (ws://hello-zetta.herokuapp.com/peers/beaglebone)
    {timestamp} [peer-client] Peer connection established (ws://hello-zetta.herokuapp.com/peers/beaglebone)
    

info By linking your Zetta server on the BeagleBone to a Zetta server running in the cloud, you can access your devices via a web API from anywhere in the world.

  1. Open the Zetta Browser. Point it to the cloud server. http://browser.zettajs.io/#/overview?url=http:%2F%2Fhello-zetta.herokuapp.com

  2. Ensure your LED devices are listed.

  3. Click the turn-on button for the various LEDs.

  4. Ensure the LEDs on your BeagleBone turned on and that the device state changed in the Zetta Browser visualization.

world Now anyone in the world can control the LEDs on your BeagleBone. Try it. Copy the cloud URL and send it to friends so they can control your LEDs from afar: http://browser.zettajs.io/#/overview?url=http:%2F%2Fhello-zetta.herokuapp.com.

Step #4: Buzz the Buzzer

Assemble the Buzzer Hardware

Piezo Hookup Diagram

  1. Attach the piezo buzzer to the breadboard.

    From To
    Buzzer - pin Breadboard A3
    Buzzer + pin Breadboard A6

    help New to solderless breadboards? Read the Adafruit Guide To Excellent Soldering.

  2. Create a circuit between the BeagleBone and the buzzer.

    From Wire To
    Breadboard E3 White BeagleBone P9_14
    Breadboard E6 Black Breadboard -
    Breadboard - Black BeagleBone P9_01

    help Are the BeagleBone pin numbers confusing? See the Pinout section in the BeagleBone topic.

After assembling the buzzer hardware, your project should look similar to the images below.

The Connected Piezo Buzzer The Connected Piezo Buzzer

Write the Buzzer Code

  1. From the Cloud9 IDE console, install the Zetta device driver for the buzzer.

    npm install zetta-buzzer-bonescript-driver --save
    

    caution Ensure the BeagleBone Cloud9 IDE console is in the correct working directory when you run npm install. The Cloud9 IDE console prompt should be: root@beaglebone:/var/lib/cloud9/home-security/#

  2. In the server.js file, write code to require and use the Buzzer on pin ‘P9_14’.

    Add line 3:

    var Buzzer = require('zetta-buzzer-bonescript-driver');
    

    Add line 8:

    .use(Buzzer, 'P9_14')
    
  3. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-bonescript-driver');
    var Buzzer = require('zetta-buzzer-bonescript-driver');
    
    zetta()
     .name('FirstName LastName')
     .use(LED, 'USR0', 'USR1', 'USR2', 'USR3')
     .use(Buzzer, 'P9_14')
     .link('http://hello-zetta.herokuapp.com/')
     .listen(1337, function(){
       console.log('Zetta is running at http://beaglebone.local:1337');
    });
    
  4. From the Cloud9 IDE, click File > Save to save the changes you made to server.js.

  5. In the Cloud9 IDE, switch to the first console tab. Stop (CTRL-c or COMMAND-c) and restart the Zetta server.

    node server.js
    
  6. When Zetta discovers the buzzer, it will log a message about the device to the Cloud9 IDE console.

    {timestamp} [scout] Device (buzzer) {id} was discovered
    

Buzz the Buzzer

  1. Open the Zetta Browser:

    http://browser.zettajs.io/#/overview?url=http://browser.zettajs.io/#/overview?url=http:%2F%2Fhello-zetta.herokuapp.com

  2. Ensure your Buzzer device is listed. Zetta Browser with Piezo Attached

  3. Click the beep button.

  4. Ensure your buzzer buzzed and the device state changed in the Zetta Browser visualization.

    help Didn’t hear a beep? Double check your wiring and make sure there were no errors reported in the Cloud9 IDE console.

Step #5: Soundcheck the Microphone

Assemble Microphone Hardware

Microphone Hookup Diagram

  1. If your microphone does not have headers attached, solder them in place so the microphone can be attached to the breadboard.

    help New to soldering? Read the How to Solder guide.

  2. Attach your microphone to the breadboard.

    From To
    Microphone VCC Breadboard F18
    Microphone GND Breadboard F19
    Microphone AUD Breadboard F20
  3. Create a circuit between the BeagleBone and the microphone.

    From Wire To
    Breadboard H18 Red BeableBone P9_32
    Breadboard H19 2.2kΩ Resistor Breadboard -
    Breadboard H20 Green BeableBone P9_36

    help Don’t know how to read resistor values? Read the How to Read Resistor Values guide.

After assembling the microphone hardware, your project should look similar to the images below.

The Connected Microphone The Connected Microphone

Write Microphone Software

  1. From the Cloud9 IDE console, install the Zetta device driver for the microphone.

    npm install zetta-microphone-bonescript-driver --save
    
  2. In the server.js file, write code to require and use the Microphone driver on BeagleBone pin P9_36.

    Add line 4:

    var Microphone = require('zetta-microphone-bonescript-driver');
    

    Add line 10:

    .use(Microphone, 'P9_36')
    
  3. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-bonescript-driver');
    var Buzzer = require('zetta-buzzer-bonescript-driver');
    var Microphone = require('zetta-microphone-bonescript-driver');
    
    zetta()
      .name('FirstName LastName')
      .use(LED, 'USR0', 'USR1', 'USR2', 'USR3')
      .use(Buzzer, 'P9_14')
      .use(Microphone, 'P9_36')
      .link('http://hello-zetta.herokuapp.com/')
      .listen(1337, function(){
        console.log('Zetta is running at http://beaglebone.local:1337');
    });
    
  4. From the Cloud9 IDE, click File > Save to save the changes you made to server.js.

  5. From the Cloud9 IDE console, stop (CTRL-c or COMMAND-c) and restart the Zetta server.

    node server.js
    
  6. When Zetta discovers the microphone, Zetta will log a message about the device to the Cloud9 IDE console.

    {TIMESTAMP} [scout] Device (microphone) {id} was discovered
    

    Running Your Server

Soundcheck the Microphone

  1. Open the Zetta Browser:

    http://browser.zettajs.io/#/overview?url=http:%2F%2Fhello-zetta.herokuapp.com

  2. Ensure the Microphone device is listed. Zetta Browser root with Microphone

  3. In the Zetta Browser, click on the Microphone link to open a detailed view of the device.

    Zetta Browser root with Microphone

  4. Make a noise near or gently tap on the microphone.

  5. Ensure the values and waveform for the :volume characteristic in the Zetta Browser are streaming over time and change as you make noise.

Step #6: Run the Security App

Write Security App Code

  1. From the Cloud9 IDE console, create the app file and directory.

    touch apps/app.js
    
  2. From the Cloud9 IDE, double-click on the new apps/app.js file to edit it.

  3. Write the app logic.

    module.exports = function(server) {
    
      var ledQuery = server.where({type: 'led'});
      var buzzerQuery = server.where({type: 'buzzer'});
      var microphoneQuery = server.where({type: 'microphone'});
    
      server.observe([ledQuery,buzzerQuery, microphoneQuery], function(led, buzzer, microphone){
        microphone.streams.volume.on('data', function(msg){
          if (buzzer.state === 'off' && msg.data > 30) {
            led.call('turn-on-pulse', function() {});
            buzzer.call('turn-on-pulse', function() {});
            setTimeout(function(buzzer) {
              buzzer.call('turn-off', function(err) {})
              led.call('turn-off', function(err) {})
            }, 3000, buzzer);
          }
        });
      });
    }
    

Use Security App

  1. From the Cloud9 IDE, edit the server.js file. Add code to require and use the app from the apps folder.

    Add line 5.

    var app = require('./apps/app');
    

    Add line 12.

    .use(app)
    
  2. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-bonescript-driver');
    var Buzzer = require('zetta-buzzer-bonescript-driver');
    var Microphone = require('zetta-microphone-bonescript-driver');
    var app = require('./apps/app');
    
    zetta()
      .name('FirstName LastName')
      .use(LED, 'USR0', 'USR1', 'USR2', 'USR3')
      .use(Buzzer, 'P9_14')
      .use(Microphone, 'P9_36')
      .use(app)
      .link('http://hello-zetta.herokuapp.com/')
      .listen(1337, function(){
        console.log('Zetta is running at http://beaglebone.local:1337');
    });
    
  3. From the Cloud9 IDE, click File > Save to save the changes you made to server.js.

Run the Security App

  1. From the Cloud9 IDE console, restart the Zetta server by pressing CTRL-c or COMMAND-c and then running node server.js

    node server.js
    
  2. Make a noise near or gently tap on the microphone.

  3. Ensure the alarm sounds for approximately 3 seconds.

  4. Open the Zetta Browser to observe state changes:

    http://browser.zettajs.io/#/overview?url=http:%2F%2Fhello-zetta.herokuapp.com

Congratulations!

Congratulations. You built a home-security system that is connected to the Internet and programmable from anywhere in the world.

Author

Matt Dobson

Connect with Matt and other Zettanauts. Join the Zetta community discussion.

File under: BeagleBone

Have a project to share? Read How to Share a Project