Hello World

Create an Internet-connected, dusk-to-dawn lighting system with a mock LED and a mock photocell.
  • Adam Magaluk
  • calendar Oct 13, 2014
  • upwardsbarchart beginner
  • stopwatch 1 hour
  • download Source Code
  • nodes Project

Directions

  1. Setup Zetta on the PC
  2. Blink the LED
  3. Link to the Cloud
  4. Sense Light with Photocell
  5. Run Dusk to Dawn App

Goal

The goal for the Hello World project is to create a mock dusk-to-dawn lighting system by assembling a mock LED and a mock photocell into a Zetta app running on a PC - no additional hardware required. We will connect the app to the Internet by linking the PC with a second Zetta server running in the cloud.

Screenshot of Zetta browser with dusk to dawn lighting system

Parts

This project requires a PC with an Internet connection and Node.js.

Step #1: Setup Zetta on the PC

Initialize the Project

  1. From the PC command line, create the project directory.

    mkdir zetta-hello-world
    
  2. Change to the project directory.

    cd zetta-hello-world
    
  3. Initialize the project by following the npm init utility walk-through.

    npm init
    

    action Press <ENTER> multiple times to accept the npm init defaults.

Install Zetta

  1. Install Zetta and save it as a dependency to the package.json file that was created by npm init.

    npm install zetta --save
    

Write the Zetta Server Code

  1. Create the server.js file.

    touch server.js
    
  2. In a text editor, write code in server.js to require Zetta, give the server a name and listen on server port 1337.

    info Consider replacing FirstName and LastName with your first and last name.

    var zetta = require('zetta');
    
    zetta()
      .name('FirstName-LastName')
      .listen(1337, function(){
         console.log('Zetta is running at http://127.0.0.1:1337');
    });
    
  3. Save the file and run the Zetta server from within the zetta-hello-world project folder.

    node server.js
    

    Notice the console output indicating the server is running.

    {timestamp} [server] Server (FirstName-LastName) FirstName-LastName listening on http://127.0.0.1:1337
    Zetta is running at http://127.0.0.1:1337
    

Call the API

  1. Make a HTTP GET request to the API on the running node server at http://127.0.0.1:1337.

    curl http://127.0.0.1:1337
    

    info To make an HTTP request use curl in the command line, a web browser or a REST client (like Advanced REST Client) to see the API results.

  2. Confirm the API request returns a response like the data below.

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

    info As we use devices in server.js they will appear in the web API.

Step #2: Blink the LED

Write the LED Code

  1. Install the mock LED driver from npm.

    npm install zetta-led-mock-driver --save
    

    info Zetta driver names follow the pattern zetta-[device]-[platform]-driver. The Hello World project uses mock devices so mock is considered to be the platform.

  2. In the server.js file, write code to require and use the mock LED.

    Add line 2:

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

    Add line 6:

    .use(LED)
    
  3. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-mock-driver');
    
    zetta()
      .name('FirstName-LastName')
      .use(LED)
      .listen(1337, function(){
         console.log('Zetta is running at http://127.0.0.1:1337');
    });
    
  4. Stop and restart the Zetta server by pressing CTRL-C then run node server.js.

    node server.js
    
  5. When Zetta discovers the mock LED, it will log a message about the device.

    {timestamp} [scout] Device (led) {id} was discovered
    
  1. Open the Zetta Browser and point it at the PC server: http://browser.zettajs.io/#/overview?url=http://127.0.0.1:1337

  2. Ensure the LED is listed.

    Zetta Browser with LED

  3. Click the turn-on button and ensure the LED state changed from off to on.

  4. Click the turn-off button and ensure the LED state changed from on to off.

Step #3: Link to the Cloud

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

  1. In the server.js file, write code to link the Zetta server on the PC to a Zetta server running in the cloud.

    Add line 7:

    .link('http://hello-zetta.herokuapp.com/')
    

    info This project uses the shared hello-zetta cloud instance. To create your own Zetta instance in the cloud follow the How to Deploy a Zetta Server to Heroku guide.

  2. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-mock-driver');
    
    zetta()
      .name('FirstName-LastName')
      .use(LED)
      .link('http://hello-zetta.herokuapp.com/')
      .listen(1337, function(){
        console.log('Zetta is running at http://127.0.0.1:1337');
    });
    
  3. Stop and restart the Zetta server by pressing CTRL-C then run node server.js.

    node server.js
    
  4. Ensure the console log includes notifications that the peer was established.

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

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

  1. Open the Zetta Browser and point it at the Zetta cloud server:

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

    info Notice that you are now accessing the LED on your laptop from a cloud server on Heroku.

  2. Ensure the LED is listed.

  3. Click the turn-on button for the LED and ensure the LED state changed in the Zetta Browser visualization.

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

Step #4: Sense Light with Photocell

Write Photocell Software

  1. Install the Zetta device driver for the mock photocell.

    npm install zetta-photocell-mock-driver --save
    
  2. In the server.js file, write code to require and use the Photocell driver.

    Add line 3:

    var Photocell = require('zetta-photocell-mock-driver');
    

    Add line 8:

    .use(Photocell)
    
  3. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var LED = require('zetta-led-mock-driver');
    var Photocell = require('zetta-photocell-mock-driver');
    
    zetta()
      .name('FirstName-LastName')
      .use(LED)
      .use(Photocell)
      .link('http://hello-zetta.herokuapp.com/')
      .listen(1337, function(){
      console.log('Zetta is running at http://127.0.0.1:1337');
    });
    
  4. Stop and restart the Zetta server by pressing CTRL-C then run node server.js.

    node server.js
    
  5. When Zetta discovers the mock Photocell, it will log a message about the device.

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

Sense Light with Photocell

info Streaming data in Zetta is done via WebSockets.

Zetta Browser

  1. Open the Zetta browser and point it at the Zetta cloud server:

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

  2. In the Zetta Browser, ensure the photocell device is listed.

  3. Click on the photocell link to see a detailed view.

    Photocell Wave Detail Page

  4. Ensure the values and waveform for the :intensity characteristic in the Zetta Browser change over time and stream like a sine wave.

Step #5: Run Dusk to Dawn App

Write the Dusk to Dawn App Code

  1. Create an apps directory in the zetta-hello-world directory.

    mkdir apps
    
  2. Create the dusk_to_dawn_light.js file.

    touch apps/dusk_to_dawn_light.js
    
  3. Write code in apps/dusk_to_dawn_light.js to find the led and the photocell, monitor the photocell intensity and toggle the led as the intensity changes.

    module.exports = function(server) {
      var photocellQuery = server.where({ type: 'photocell' });
      var ledQuery = server.where({ type: 'led' });
      server.observe([photocellQuery, ledQuery], function(photocell, led){
        photocell.streams.intensity.on('data', function(m) {
          if(m.data < 0.5) {
            if (led.available('turn-on')) {
              led.call('turn-on');
            }
          } else {
            if (led.available('turn-off')) {
              led.call('turn-off');
           }
         }
       });
    });}
    

Use the Dusk to Dawn Light App

  1. Edit the server.js file. Add code to require and use the dusk_to_dawn_light app from the apps folder.

    Add line 5.

    var duskToDawnLight = require('./apps/dusk_to_dawn_light');
    

    Add line 11.

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

    var zetta = require('zetta');
    var LED = require('zetta-led-mock-driver');
    var Photocell = require('zetta-photocell-mock-driver');
    
    var duskToDawnLight = require('./apps/dusk_to_dawn_light');
    
    zetta()
      .name('FirstName-LastName')
      .use(LED)
      .use(Photocell)
      .use(duskToDawnLight)
      .link('http://hello-zetta.herokuapp.com/')
      .listen(1337, function(){
        console.log('Zetta is running at http://127.0.0.1:1337');
    });
    

Run Dusk to Dawn App

  1. Stop and restart the Zetta server by pressing CTRL-C then run node server.js.

    node server.js
    
  2. Open the Zetta Browser and point it at the Zetta cloud server:

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

  3. Ensure the LED turns on and off based on the photocell intensity.

    Screenshot of Zetta browser with dusk to dawn lighting system

Congratulations!

Congratulations. You built a dusk to dawn lighting system that is connected to the Internet and programmable from anywhere in the world.

Author

Adam Magaluk

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

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