-
Notifications
You must be signed in to change notification settings - Fork 114
How Zetta Works
This reference assumes that the reader has completed the Hello World project.
-
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. -
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 inserver.js
they will appear in the web API.
-
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 somock
is considered to be the platform. -
In the
server.js
file, write code torequire
anduse
the mockLED
.Add line 2:
var LED = require('zetta-led-mock-driver');
Add line 6:
.use(LED)
-
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'); });
-
Stop and restart the Zetta server by pressing
CTRL-C
then runnode server.js
.node server.js
-
When Zetta discovers the mock LED, it will log a message about the device.
{timestamp} [scout] Device (led) {id} was discovered
-
Open the Zetta Browser and point it at the PC server: http://browser.zettajs.io/#/overview?url=http://127.0.0.1:1337
-
Ensure the LED is listed.
-
Click the
turn-on
button and ensure the LED state changed fromoff
toon
. -
Click the
turn-off
button and ensure the LED state changed fromon
tooff
.
At this point, the LED API is only available locally. Let's make the LED API available from the cloud.
-
In the
server.js
file, write code tolink
the Zetta server on the PC to a Zetta server running in the cloud.Add line 7:
.link('http://hello-zetta.herokuapp.com/')
-
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'); });
-
Stop and restart the Zetta server by pressing
CTRL-C
then runnode server.js
.node server.js
-
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
link
ing 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.
-
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.
-
Ensure the LED is listed.
-
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.
-
Install the Zetta device driver for the mock photocell.
npm install zetta-photocell-mock-driver --save
-
In the
server.js
file, write code torequire
anduse
thePhotocell
driver.Add line 3:
var Photocell = require('zetta-photocell-mock-driver');
Add line 8:
.use(Photocell)
-
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'); });
-
Stop and restart the Zetta server by pressing
CTRL-C
then runnode server.js
.node server.js
-
When Zetta discovers the mock Photocell, it will log a message about the device.
{timestamp} [scout] Device (photocell) {id} was discovered
Streaming data in Zetta is done via WebSockets.
-
Open the Zetta browser and point it at the Zetta cloud server:
http://browser.zettajs.io/#/overview?url=http:%2F%2Fhello-zetta.herokuapp.com
-
In the Zetta Browser, ensure the photocell device is listed.
-
Click on the photocell link to see a detailed view.
{:.zoom}
-
Ensure the values and waveform for the
:intensity
characteristic in the Zetta Browser change over time and stream like a sine wave.
Zetta uses WebSockets to stream device data. Use a command line tool to subscribe to the WebSockets from the cloud.
-
Install
wscat
npm install -g ws
-
Use the Zetta Browser to determine the URL of the
photocell intensity
WebSocket by clicking on thephotocell
link and searching forws:
. The first WebSocket URL you find should be for monitoring the intensity. -
Connect to the WebSockets stream with the URL. The URL will use your
FirstName
,LastName
and a deviceid
.wscat --connect ws://hello-zetta.herokuapp.com/servers/{FirstName%20LastName}/events?topic=photocell%2F{id}%2Fintensity
-
Create an
apps
directory in thehow-zetta-works
directory.mkdir apps
-
Create the
dusk_to_dawn_light.js
file.touch apps/dusk_to_dawn_light.js
-
Write code in
apps/dusk_to_dawn_light.js
to find theled
and thephotocell
, monitor thephotocell intensity
and toggle theled
as theintensity
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'); } } }); });}
-
Edit the
server.js
file. Add code torequire
anduse
thedusk_to_dawn_light
app from theapps
folder.Add line 5.
var duskToDawnLight = require('./apps/dusk_to_dawn_light');
Add line 11.
.use(duskToDawnLight)
-
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'); });
-
Stop and restart the Zetta server by pressing
CTRL-C
then runnode server.js
.node server.js
-
Open the Zetta Browser and point it at the Zetta cloud server:
http://browser.zettajs.io/#/overview?url=http:%2F%2Fhello-zetta.herokuapp.com
-
Ensure the LED turns
on
andoff
based on thephotocell intensity
.{:.zoom}
Congratulations. You built a dusk to dawn lighting system that is connected to the Internet and programmable from anywhere in the world.
Need help? Visit the Zetta Discuss List !
Need help? Visit the Zetta Discuss List ! |
---|
About Zetta
Videos and webcasts
- NEW! Building with Zetta
Tutorials
- NEW! Zetta tutorial series
- Quick start
- Configure a simple device
- Build a mock LED device
- Use the browser client
- Deploy a Zetta server to Heroku
Understanding Zetta
Writing Zetta drivers
- Finding Zetta device drivers
- Create a device driver from starter code
- More coming soon...
Using streams
Reference