Skip to content

Create Device Driver

wwitman edited this page Dec 22, 2015 · 2 revisions

Introduction

This topic explains how to create a Zetta device driver using a set of starter files that you can modify to suit your particular device. Let's get started!

Know your device and platform

Zetta drivers are created for specific devices on specific platforms. The Zetta community uses a naming convention for drivers that incorporates the device and platform names separated by hyphens: zetta-{device}-{platform}-driver.

Know the names for the device and platform you are making:

  • device
  • platform

For example, the Zetta driver for an LED on Arduino is named: zetta-led-arduino-driver.

Clone the starter project from GitHub

Clone the Zetta starter driver to a new directory called zetta-{device}-{platform}-driver:

git clone https://github.com/zettajs/zetta-starter-driver zetta-{device}-{platform}-driver

Replace {device} with the name of the device and {platform} with the name of the platform for the driver you are making.

Run the example server

  1. Change to the new driver directory.

    cd zetta-{device}-{platform}-driver

  2. Install default Zetta driver dependencies.

    npm install

  3. Change to example directory.

    cd example

  4. Install default Zetta server dependencies.

    npm install

  5. Run the driver’s example server.

    node server.js

  6. Ensure you see expected startup messages in the terminal.

    {timestamp} [scout] Device (starter) {id} was discovered
    {timestamp} [server] Server (server.name) server.name listening on http://127.0.0.1:1337
    {timestamp} [Starter-log] DEFAULT: ./app.js is running
    {timestamp} [device] starter transition do   
    
  7. Ensure you see Starter in the Zetta browser and that the the state visualization updates.

    http://browser.zettajs.io/#/overview?url=http:%2F%2F127.0.0.1:1337

You now have a working device driver, scout, example server and example app running on your development machine.

Search and replace the starter project code

Adapt the starter code to your device.

  1. Change directory to project root.

    cd ..

  2. Edit the following files and replace references to starter and Starter with the device you are making.

    • Scout: index.js
    • NPM package: package.json
    • README: README.md
    • Driver: starter.js
  3. Rename the driver file to the name of your device.

    git mv starter.js {device}.js

    Replace {device} with the name of the device you are making. For example, if you are creating the driver for an LED, you would execute git mv starter.js led.js. There is no need to add zetta or driver to the name of the driver file in this context.

  4. Restart the example server.

    node example/server.js

  5. Ensure you see your {device} labeled in the Zetta browser and that the the state visualization updates.

    http://browser.zettajs.io/#/overview?url=http:%2F%2F127.0.0.1:1337

Update the GitHub repo

After renaming the starter device you will likely want to push changes to your own git repo.

  1. Create a git repository for the new driver on your git repository of choice. Follow the Zetta naming convention: zetta-{device}-{platform}-driver.

    The Zetta team uses GitHub as the source code repository for Zetta.

  2. Change the remote of the git repository.

    git remote set-url origin {your git repo url}

    In the case of GitHub, your {git repo url} would look something like https://github.com/{username}/zetta-{device}-{platform}-driver.git.

  3. Commit changes to the repository.

    git commit -a

  4. Push changes to the new origin.

    git push origin master

Get inspired!

There are many Zetta drivers that can serve as starting point for creating a device driver.

  • Query NPM for drivers that have been created for other devices by the community:

https://www.npmjs.com/search?q=zetta-*-driver

  • Query GitHub for drivers that have been created by the core Zetta team:

https://github.com/zettajs?query=driver

  • Narrow the search by device type: zetta-led-*-driver

  • Narrow the search by platform type: zetta-*-edison-driver.

The Zetta Discuss Google Group is the ideal spot for getting the broader community’s help when creating device drivers:

https://groups.google.com/forum/#!forum/zetta-discuss

Build the driver

Starting with the {device}.js driver file and the index.js scout file, write the code that models your device. Use the Zetta Browser to validate your work as you go.

Publish with NPM

Follow these steps to publish your device driver to NPM:

https://gist.github.com/coolaj86/1318304

Clone this wiki locally