Car Speed Tracker System

Create an Internet-connected, car speed tracker system with a phillips hue bulb, mock car.
  • Anil Sagar
  • calendar Feb 03, 2015
  • upwardsbarchart experienced
  • stopwatch 1-2 hours
  • download Source Code
  • nodes Project

Directions

  1. Setup Zetta on the Machine
  2. Car Sensor System
  3. Link to the Cloud
  4. Setup Phillips Hue Bulb and Hub
  5. Run the Car Speed Alert App

Goal

The goal for this project is to create a simple car speed tracker system by assembling a mock car, a phillips hue LED Bulb into a Zetta app running on a PC. We will connect the app to the Internet by linking the laptop with a second Zetta server running in the cloud.

A simple use case we try to demonstrate is if a car speed goes above 100 mile/hour then family at home will be alerted by turning Phillips HUE LED bulb into red in color at home. For Example, Using this app a father knows his teenage son is driving too fast sitting at home.

Parts

This project requires

  1. A PC with an Internet connection and Node.js.
  2. Phillips Hue Bulb & Hub

Step #1: Setup Zetta on the PC

Initialize the Project

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

    mkdir zetta-speed-tracker
    
  2. Change to the project directory.

    cd zetta-speed-tracker
    
  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-speed-tracker project folder.

    touch 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. Open the Zetta API in a web browser: http://127.0.0.1:1337.

  2. Confirm the API looks like the response 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. For the following steps we’ll access the API via the Zetta Browser.

Step #2: Car Sensor System

Write the Car Sensor System Code

  1. Install the mock car sensor driver from npm.

    npm install zetta-car-mock-driver --save
    

    info Zetta driver names follow the pattern zetta-[device]-[platform]-driver. The Car Speed Tracker 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 CAR.

    Add line 2:

    var CAR = require('zetta-car-mock-driver');
    

    Add line 6:

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

    var zetta = require('zetta');
    var CAR = require('zetta-car-mock-driver');
    
    zetta()
    .name('FirstName LastName')
    .use(CAR)
    .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 CAR, it will log a message about the device.

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

Operate the CAR 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 CAR is listed.

    Zetta Browser with CAR

  3. Click the StartCar button and ensure the car state changed from idle to started.

  4. Click the accelerateCar button and ensure the car state changed from idle to accelerating. Notice speed increasing gradually.

  5. Click the releaseAccelerator button and ensure the car state changed from accelerating to cruising. Notice speed of the car becomes constant.

  6. Click the brakeCar button and ensure the car state changed from cruising to braking. Notice speed of the car decreasing gradually.

  7. Click the releaseBrake button and ensure the car state changed from braking to cruising. Notice speed of the car becomes constant.

Step #3: Link to the Cloud

At this point, the CAR API is only available locally. Let’s make the CAR 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/')
    
  2. Ensure server.js looks like the code below.

    var zetta = require('zetta');
    var CAR = require('zetta-car-mock-driver');
    
    zetta()
    .name('FirstName LastName')
    .use(CAR)
    .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.

Control the CAR 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

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

  2. Ensure the CAR is listed.

  3. Click the startCar button for the CAR and ensure the CAR state changed in the Zetta Browser visualization.

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

Step #4: Setup Phillips Hue Bulb And Hub

  1. Power Up the Hub

    Phillips Hue Hub

  2. Connect LAN Cable to Hub

  3. Power Up the Phillips Hue Bulb

    Phillips Hue Bulb

  4. Make sure zetta server & phillips hue bulb hub are in same network

Write Phillips Hue Software

  1. Install the Zetta device driver for the phillips hue.

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

    Add line 3:

    var Hue = require('zetta-hue-driver');
    

    Add line 8:

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

    var zetta = require('zetta');
    var CAR = require('zetta-car-mock-driver');
    var Hue = require('zetta-hue-driver');
    
    zetta()
    .name('Anil Sagar')
    .use(CAR)
    .use(Hue)
    .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 hue hub & bulbs, it will log a message about the device.

    {timestamp} [scout] Device (huehub) {id} was discovered
    
  6. Open the Zetta Browser and point it at the PC server:

    http://browser.zettajs.io/#/overview?url=http://127.0.0.1:1337

  7. Ensure the Hue Hub is listed.

    Zetta Browser with CAR & Hue

  8. Click on Register button to register hub and press centre circular button in hue hub device to register hue hub & bulbs with zettajs application.

  9. Refresh browser & click on register to see discovered bulbs.

    Zetta Browser with CAR, Hue, Bulbs

  10. Since we got only one bulb connected to hub, by trial find out which one by blinking the bulb using Zetta Browser and note down bulb name to build app.

    Zetta Browser Blink Bulb

Sense Car Speed with Mock Car Driver

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 car device is listed.

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

    Car speed Detail Page

  4. Ensure the values and waveform for the :speed characteristic in the Zetta Browser change over time and stream like a exponential graph. Click on Start Car & Accelerate car button to see graph.

Step #5: Run the Car Speed Alert App

Write the Car Speed Alert App Code

  1. Create an apps directory in the zetta-speed-tracker directory.

    mkdir apps
    
  2. Create the car_speed_alert.js file.

    touch apps/car_speed_alert.js
    
  3. Write code in apps/car_speed_alert.js to find the hue bulb and the car, monitor the car speed and toggle the hue bulb as the speed changes.

  4. Make sure you change the name of the bulb in below code. Use the bulb name which is powered up.

    module.exports = function(server) {
      var carQuery = server.where({ type: 'car' });
      var bulbQuery = server.where({type: 'huebulb', name: 'Hue Bulb Hue Downlight 1'});
      server.observe([carQuery, bulbQuery], function(car, bulb){
        car.streams.speed.on('data', function(m) {
          if(m.data > 100) {
            if (bulb.available('turn-on')) {
              bulb.call('color', '#ff0000');
              bulb.call('turn-on');
            }
          } else {
            if (bulb.available('turn-off')) {
              bulb.call('turn-off');
            }
          }
        });
      });}
    

Use the Car Speed Alert App

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

    Add line 5.

    var carSpeedAlert = require('./apps/car_speed_alert');
    

    Add line 11.

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

    var zetta = require('zetta');
    var CAR = require('zetta-car-mock-driver');
    var Hue = require('zetta-hue-driver');
    var carSpeedAlert = require('./apps/car_speed_alert');
    
    zetta()
    .name('Anil Sagar')
    .use(CAR)
    .use(Hue)
    .use(carSpeedAlert)
    .link('http://hello-zetta.herokuapp.com/')
    .listen(1337, function(){
      console.log('Zetta is running at http://127.0.0.1:1337');
    });
    
    

Run the Car Speed Alert 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 Hub Bulbs are turned Off.

  4. Start the Car & Accelearte the car.

  5. Ensure the Hue Bulb turns on in red in color once the speed crosses 100 mark.

    Screenshot of Zetta browser with car speed alert

  6. Release the Car accelerator & brake the car. Notice speed getting decreased.

  7. Ensure the Hue Bulb turns off once the speed dips below 100 mark.

Congratulations!

Congratulations. You built a car speed alert system that is connected to the Internet and programmable from anywhere in the world.

Author

Anil Sagar

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

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