Skip to content

How Zetta Works

Kevin Swiber edited this page May 29, 2015 · 3 revisions

How Zetta Works

This reference assumes that the reader has completed the Hello World project.

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

    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"}]}]}

    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

    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

Blink the LED from the PC

  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.

Write the Link Code

  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/')
  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)

    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.

Blink the LED from the Cloud

  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

    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.

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)
      .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

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{:.zoom}

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

Command Line

Zetta uses WebSockets to stream device data. Use a command line tool to subscribe to the WebSockets from the cloud.

  1. Install wscat

    npm install -g ws
  2. Use the Zetta Browser to determine the URL of the photocell intensity WebSocket by clicking on the photocell link and searching for ws:. The first WebSocket URL you find should be for monitoring the intensity.

  3. Connect to the WebSockets stream with the URL. The URL will use your FirstName, LastName and a device id.

    wscat --connect ws://hello-zetta.herokuapp.com/servers/{FirstName%20LastName}/events?topic=photocell%2F{id}%2Fintensity

Step #5: Run the Dusk to Dawn Light App

Write the Dusk to Dawn Light App Code

  1. Create an apps directory in the how-zetta-works 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 the Dusk to Dawn Light 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{:.zoom}

Congratulations!

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

Clone this wiki locally