• USB communication between Tessel and Node on your PC

    Tuesday, July 29, 2014

    7/29/2014– Kevin Mehall

    This post is an introduction to using USB to communicate between Tessel and Node. Find updates and more examples here.

    In addition to loading code, you can also use the USB connection to Tessel to pass data between your Tessel and Node.js on your PC. Combine your Tessel’s easy access to hardware and your computer’s processing power to do things neither device can do alone.

    Messages are JavaScript objects. It uses structured clone, which beyond JSON, supports cyclic structures and Buffer, RegExp, and Date objects.

    We’ve built an example for you to expand on:

    git clone https://github.com/tessel/docs.git
    cd docs/tutorial/usb_messages
    npm install
    

    The directory structure looks like:

    ./
      /host.js         -- script that runs on the computer
      /package.json
      /node_modules    -- npm modules for the computer
        /tessel        -- the Tessel host-side USB module
      /device          -- this entire directory will be deployed to Tessel
        /package.json
        /node_modules  -- all the libs that run ON Tessel go here (camera, ambient, IR, etc.)
        /device.js     -- the main script that runs on Tessel
                       -- other JS files for Tessel go here
    

    Both Tessel and Node run JS, but you don’t want all your host-side code on the Tessel. The host and device each have their own node_modules for npm packages. Only devices/ is sent to the Tessel, because it has its own package.json.

    The tessel command line tool functionality is also exposed as a library. host.js uses that library to take the place of tessel run, and not only deploys your code to the Tessel, but also communicates with it.

    The Tessel code, device/index.js, simply demonstrates sending and receiving messages.

    To try this example, run node host.js. It sends the device-side code on the Tessel as it starts.


    Here’s the code that runs on the PC: host.js

    // When we `require('tessel')` here, this is the library out of `node_modules`
    // for USB communication. It's not the same as the `tessel` module built into
    // the Tessel firmware that code on the Tessel uses for hardware access --
    // that's only available to JS that executes on the Tessel itself.
    var tessel = require('tessel');
    
    var script =  require.resolve('./device/index.js');
    
    var opts = {
      // Stop existing script, if any
      stop: true,
      // Serial number (`undefined` picks the first one)
      serial: process.env.TESSEL_SERIAL,
    };
    
    var args = [];
     
    // `tessel.findTessel` finds a Tessel attached to this computer and connects.
    tessel.findTessel(opts, function(err, device) {
        if (err) throw err;
    
        // Once we've found a Tessel, we tell it to run our script. This works just
        // like `tessel run` and bundles the `device/` directory. It bundles only
        // `device/` and not the host code because `device/` has its own
        // `package.json`.
        device.run(script, args, {}, function () {
              // Connect the stdout and stderr of the process running on Tessel to
              // the console, so that our `console.log` messages show.
              device.stdout.resume();
              device.stdout.pipe(process.stdout);
              device.stderr.resume();
              device.stderr.pipe(process.stderr);
     
              var count = 0;
    
              // `device.send(msg)` sends an object to Tessel. It supports JSON
              // compatible objects as messages, with the addition that `Buffer`s,
              // `Date`s and cyclic data structures are also handled properly.
              setInterval(function(){
                device.send({count:count++, data: {obj: 'demo'}})
              }, 4000);
    
              // `device.on('message', function (msg) { ... })` receives an event
              // when an object is received from Tessel.
              device.on('message', function (m) {
                console.log('[PC] Message from Tessel:', m);
              });
     
              // Exit cleanly on Ctrl+C.
              process.on('SIGINT', function() {
                // Try to stop the process on the Tessel
                device.stop();
    
                setTimeout(function () {
                  // But if that fails, just exit
                  logs.info('Script aborted');
                  process.exit(131);
                }, 200);
              });
     
              // When the script on Tessel exits, shut down
              // USB communications and exit
              device.once('script-stop', function (code) {
                device.close(function () {
                  process.exit(code);
                });
              });
        });
    });
    

    The code that runs on Tessel is very simple: device/index.js

    // The `tessel` module built-in to the Tessel firmware for access to hardware
    var tessel = require('tessel');
    
    //When a message is received from the computer, this event triggers.
    process.on('message', function(msg) {
      console.log("[Tessel] Message from PC:", msg);
    });
    
    var counter = 0;
    
    // Every 5 seconds...
    setInterval(function() {
      // Send a message to the computer
      process.send({count: counter++});
    }, 5000);
    
    // Keep the event loop alive 
    process.ref();
    

    When a message is received from the computer, the process.on('message', function(msg) { event triggers.

    To send a message to the computer, call process.send(msg)


    Code examples in this document are placed in the public domain.

    See the code on Github here.

    #kevin mehall #docs #USB #Tessel #Technical Machine #Node #Examples #code

February 2017

November 2016

October 2016

September 2016

August 2016

July 2016

June 2016

April 2016

March 2016

February 2016

November 2015

September 2015

August 2015

July 2015

June 2015

May 2015

March 2015

February 2015

January 2015

December 2014

November 2014

October 2014

September 2014

August 2014

July 2014

June 2014

May 2014

April 2014

March 2014

February 2014

January 2014

December 2013

November 2013

October 2013

September 2013

August 2013

July 2013