I borrowed a Tessel from a friend @leighghunt to play with some embedded node.js. My first project is to listen to a twitter stream and blink it out as Morse code on the Tessel’s LEDs.

Getting Started

npm install -g tessel

Initialising the project: npm init

I like CoffeeScript, so to test the Tessel out I adapted the demo code from here to a file called twitter_morse_code.coffee: tessel = require('tessel')``led1 = tessel.led[0].output(1) led2 = tessel.led[1].output(0)``setInterval( -> console.log("I'm blinking! (Press CTRL + C to stop)") led1.toggle() led2.toggle() , 100)

This code will rapidly toggle two LEDs on the board. To run it just plug in the tessel: coffee -c twitter_morse_code.coffee tessel run twitter_morse_code.js

Soon the Tessel will support CoffeeScript

WiFi

To connect to twitter we can use the onboard tessel WiFi. It can be connected with: tessel wifi -n [network name] -p [password] -s [security type*]

The Tessel remembers these settings and will try to connect on startup

The WiFi can be checked with:

tessel wifi -l

Twitter to Morse Code

The code is available at https://github.com/grahamjenson/tessel_twitter_to_morse

The Dependencies:

I changed the code in the file twitter_morse_code.coffee tessel = require('tessel') keys = require './twitter_keys.json' MorseCode = require("morsecode"); morseConverter = new MorseCode(); Twitter = require('node-twitter'); q = require('q')

Morse code methods: led1 = tessel.led[0] led2 = tessel.led[1]``toggle = (led, time) -> led.write(true) q.delay(time) .then( -> led.write(false) )``dot = -> console.log 'dot' toggle(led1, 100)``dash = -> console.log 'dash' toggle(led2, 200)``morse_blink = (message, text) -> console.log "BLINKING", text promise = q.fcall( -> ) for char in message if char == '.' promise = promise.then( -> dot()) else if char == '_' promise = promise.then( -> dash())``promise

The dot and dash methods return a promise to turn on and off one of the tessel LEDs.

The morse_blink function uses javascript promises like a stack of asynchronous events, where it takes a string made of dots . and dashes _ and returns a promise to blink the the message out.

Twitter stream: twitterStreamClient = new Twitter.StreamClient(keys.key, keys.secret, keys.akey, keys.asecret);``promise = q.fcall( -> )``twitterStreamClient.on('tweet', (tweet) -> text = tweet.text morse = morseConverter.translate(tweet.text) console.log text console.log morse promise = promise.then( -> morse_blink(morse, text)) );``twitterStreamClient.start(['grahamjenson']);

As above, promises are used like a stack to make sure that we wait till the previous message is finished before we start blinking a new one.

Problems with undefined v.s. null console.log(typeof undefined) console.log(typeof null)

in node 0.12 returns undefined object

but on the tessel it returns: undefined undefined

This means that lines like this: if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')

from the request package dependency of node-twitter will break. Delete that line and it will work :)

Running

After executing: coffee -c twitter_morse_code.coffee tessel run twitter_morse_code.js

The tessel will listen for any tweet with grahamjenson in it and then blink it out as Morse on the tessel. The output will look like: test grahamjenson _ . . . . _ _ _ . . _ . . _ . . . . . _ _ _ . _ _ _ . _ . . . . _ _ _ _ . BLINKING test grahamjenson dash dot dot dot dot dash dash dash ...

Conclusion

The Tessel is a great little device to play with embedded Node. It is simple to get started and easy to debug. If you want to have Javascript interact with the world or you want to teach someone Javascript in a more pratical way, I recommend the Tessel.

*Also: Tim Pietrusky: Nerd Disco Talk *