• Run, Push, and Erase: Developing on Tessel 2

    Wednesday, March 18, 2015

    3/18/15– Kelsey Breseman

    This post should give you a glimpse into developing on Tessel’s CLI.

    The basic interaction of a Tessel is to create some code, then send it to the device to execute.

    Current Tessel users should be familiar with tessel run to run the code one time in RAM, tessel push, which pushes the code over to Tessel’s Flash memory to be executed whenever the Tessel boots up, and tessel erase, which erases any pushed code.

    Tessel 2 runs on a totally different architecture than does Tessel 1, so we’re recreating the command line interface to act in the exact same way, but with a different implementation.

    Of course, this setup will get easier over time as developments continue.

    Setting up Tessel 2

    Tessel 2 is based on the OpenWRT architecture (& the same WiFi/processor chip), so for applications that don’t involve much hardware, I can use an OpenWRT Node or for a no hardware required dev setup, this Vagrant box to act as a Tessel 2.

    Here’s my setup:

    OpenWRT Node setup

    OpenWRT Node wired to USB serial, and powered (via a small USB hub) by microUSB. Both of those plug into another USB hub, which plugs into my computer.

    Before you can do anything, you’ll have to install the serial cable (we use this one), and then dterm (it’s on brew) to talk to it over the command line. For easy development, you can then set up an alias to set up serial comms with Tessel 2:

    alias v2="dterm /dev/tty.usbserial 115200"
    

    Assuming that’s all working properly, you can now run your aliased command v2 and hit enter to boot up. Congrats! You’re now root on your Tessel 2/OpenWRT. You should be able to save files and run them with scp -r path/to/code-folder root@ip:/path/to/dest (/tmp is good).

    Setting up CLI

    Great, now you can get Tessel 2’s CLI up and running. On your computer (not on Tessel 2), clone the V2 CLI repo and follow the instructions in the readme. Again, this is going to change and eventually be npm installable, but this works for now.

    Be sure to fill out the config.env file – a quick ifconfig on Tessel 2 should get you its IP address.

    All right! Now you should be able to run the CLI from your computer’s terminal. What fun! If you’ve gotten this far, you’re both qualified and welcome to begin contributing to the Tessel 2 CLI– check out the issues/features to make here!

    How Tessel 2 Calls Run

    The rest of this post is a walkthrough of how Tessel’s Run, Push, and Erase commands work, just to get you into the code.

    Jon actually wrote the run command, but I can walk you through it.

    The functions reside in the lib folder of the repo, and are called from bin/prime.js. So any new command will need a new file in lib and a new function call in prime.js.

    The run and push commands are both in lib/deploy.js, because they both deploy code. Note that this is an exception to the naming scheme; most functions should have their own file in lib and be called function.js.

    When someone types tessel run file.js in the command line, prime.js calls sftpDeploy and passes it the filename to deploy.

    Your computer then SSH’s into Tessel 2…

    function sftpDeploy(opts, push) {
     tessel.logs.info('Connecting to remote Tessel...');
      // Create a new SSH client connection object
      ssh.createConnection(function(err, conn) {
    

    …stops existing processes and deletes old code…

    conn.exec('/etc/init.d/tessel-app stop; rm -rf /tmp/remote_code’, function(err, stdin) {
    

    …makes a tarball of all the files relevant to your code…

    // Gather details about the file structure of the script being sent
    var ret = tessel.analyzeScript(process.cwd() + "/" + opts.entryPoint, {verbose: opts.verbose});
    // Tar up the code to improve transfer rates
    tessel.tarCode(ret.pushdir, {node: true}, function(err, bundle) {
    

    …and sends it over the WiFi, really just copying it into a temporary folder.

    conn.exec('mkdir /tmp/remote_code/; tar -x -C ' + filepath, function(err, rstdin) {
    

    Once finished, Tessel 2 runs your code by calling node app.

    The push command is only slightly different: if you specify to sftpDeploy that you are pushing code, it copies it into a different (non-temporary) folder (/app instead of /tmp/remote_code’), and also makes a file calledstart` within that folder, which Tessel 2 will execute whenever it boots up:

    conn.exec("echo '#!/bin/sh\ncd /app\nexec node .' > " + filepath + "/start && chmod +x '" + filepath + "/start'", function (err, newStream) {
    

    Tessel Erase

    If that sounded simple, it was. Tessel erase is even simpler: you call tessel erase from your command line and your computer SSH’s into Tessel 2 stops existing processes and calls rm -rf on the folder where pushed code is stored:

    function erase(opts) {
      var filepath = '/app';
    
      // Create a new SSH client connection object
      ssh.createConnection(function(err, conn) {
    
        conn.exec('/etc/init.d/tessel-app stop; rm -rf /app’, function(err, rstdin) { });
      });
    }
    

    Moving Forward

    This whole thing took only part of a day, including setting up the Tessel 2 and writing this blog post. Next, I’m tackling some of the Tessel 2 configuration to make setup simpler. We’re in a fun place right now, where you can pick a feature and build it out. Want to join?

    Tessel 2 CLI repo

    Kelsey

    #tessel 2 #tessel #tessel2 #technical #kelsey breseman #run #push #erase #tessel push #code #developer #development setup

July 2017

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