• MQTT on Tessel

    Wednesday, September 24, 2014

    9/24/2014– Jon McKay

    Using MQTT on Tessel

    MQTT is a lightweight publish/subscribe protocol designed for machine to machine communication. The architecture features one central server that manages the subscriptions and publications from each of its numerous clients. Clients can publish data without having to be aware of who is subscribed (much like Node’s EventEmitter class).

    source: Eurotech

    MQTT is also designed to require minimum protocol overhead for each packet in order to preserve bandwidth for resource-constrained embedded devices. It’s a really simple framework for managing mesh networks of TCP-enabled devices.

    Tessel-MQTT Compatibility Backstory

    As far as I could tell, there is only one MQTT implementation in Node (surprising, right?!) and it’s really well maintained. Up until a few weeks ago, the library couldn’t run on Tessel due to a couple of Node and JS incompatibility issues. The biggest problem was the use of Function constructors, used primarily for speed improvements, which isn’t supported on Tessel yet.

    In JavaScript, the Function constructor accepts a string of JavaScript code which it processes into an actual function:

    var f = new Function(foo, bar, “console.log(‘the arguments are’, foo, bar)”);
    f(‘a trivial’, ‘example’); // prints ‘the arguments are a trivial example'

    It’s often used as a faster way to template a function, replacing Function.prototype.bind which runs abnormally slow on V8.

    Tessel’s runtime translates JavaScript to Lua on a host PC before being pushed to Tessel and run on a Lua Virtual Machine. We’ll need to push compilation functionality over to Tessel before Function constructors (or eval) can work on the device. Tim has made stready progress on a compiler written in C (as opposed to JavaScript) that will reside on Tessel itself and compile JavaScript to LuaJIT bytecode. But it isn’t quite ready yet, so we looked for another way around the issue.

    Matteo Collina, one of the primary maintainers of the MQTT.js library, and I started a discussion about the MQTT library’s compatibility with Tessel. He was kind enough to accept a pull request that replaced the Function constructors so that we wouldn’t have to wait for the C-based compiler to be complete to use MQTT on Tessel. I just had to fix one more issue with Buffer parsing and the library worked without a hitch on Tessel!

    Code Example

    Tessel can act as either an MQTT server or a client, and the client code is impressively simple in practice. In this code snippet, I publish the current temperature to an MQTT server every five seconds:

    var mqtt = require('mqtt')
      , host = ''
      , port = 1883
      , client = mqtt.createClient(port, host, {keepalive: 10000})
      , tessel = require('tessel')
      , climate = require('climate-si7020').use(tessel.port['A']);
    climate.on('ready', function ready() {
      console.log('climate ready');
      setInterval(function() {
        climate.readTemperature(function(err, temperature) {
          if (!err) {
           // You can only publish strings
            client.publish('temperature', temperature.toString());
      }, 5000);

    You can find the entire example on this Github Gist.

    If you have a project that requires the use of multiple data gathering clients interacting through a single server, consider trying out MQTT because it’s one of the simplest protocols for M2M communications.


    #mqtt #js #javascript #node #tessel #technical machine #temperature #home automation #climate #jon #mckay #matteo #collina #mqtt.js #node.js

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