Directions
- Setup Zetta on the PC
- Blink the LED
- Link to the Cloud
- Sense Light with Photocell
- 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.
Parts
This project requires a PC with an Internet connection and Node.js.
Step #1: Setup Zetta on the PC
Initialize the Project
-
From the PC command line, create the project directory.
mkdir zetta-hello-world
-
Change to the project directory.
cd zetta-hello-world
-
Initialize the project by following the
npm init
utility walk-through.npm init
action Press
<ENTER>
multiple times to accept thenpm init
defaults.
Install Zetta
-
Install Zetta and save it as a dependency to the
package.json
file that was created bynpm init
.npm install zetta --save
Write the Zetta Server Code
-
Create the
server.js
file.touch server.js
-
In a text editor, write code in
server.js
torequire
Zetta, give the server aname
andlisten
on server port1337
.info Consider replacing
FirstName
andLastName
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'); });
-
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
-
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. -
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 inserver.js
they will appear in the web API.
Step #2: Blink the LED
Write the LED Code
-
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 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
Blink the LED from the PC
-
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
.
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
-
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/')
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. -
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)
info 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.
Blink the LED from the Cloud
-
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.
-
Ensure the LED is listed.
-
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
-
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) .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
-
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
-
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.
-
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
-
Create an
apps
directory in thezetta-hello-world
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'); } } }); });}
Use the Dusk to Dawn Light App
-
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'); });
Run Dusk to Dawn App
-
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
.
Congratulations!
Congratulations. You built a dusk to dawn lighting system that is connected to the Internet and programmable from anywhere in the world.
Have a project to share? Read How to Share a Project