MQTT


Repository| Issues

MQTT is an Internet of Things connectivity protocol featuring a lightweight publish/subscribe messaging transport. It is useful for it's small code footprint and minimal network bandwidth usage.

For more info about the MQTT adaptor click here.

How to Install

Install cylon-mqtt through NPM:

$ npm install cylon cylon-mqtt

Before using cylon-mqtt, you'll need to have a MQTT broker running in order to connect/publish/subscribe to messages.

A good, simple broker is mosca. The developers have a tutorial on using Mosca as a standalone service.

How to Use

There's two different ways to use the cylon-mqtt module.

You can use the connection object only, in which case you have pub/sub access to all available MQTT channels:

Cylon.robot({
  connections: {
    server: { adaptor: 'mqtt', host: 'mqtt://localhost:1883' }
  },

  work: function(my) {
    my.server.subscribe('hello');

    my.server.on('message', function (topic, data) {
      console.log(topic + ": " + data);
    });

    every((1).seconds(), function() {
      my.server.publish('hello', 'hi there');
    });
  }
});

Or, you can use the device object, which restricts your interaction to a single MQTT channel. This can make it easier to keep track of different channels.

Cylon.robot({
  connections: {
    server: { adaptor: 'mqtt', host: 'mqtt://localhost:1883' }
  },

  devices: {
    hello: { driver: 'mqtt', topic: 'hello' }
  },

  work: function(my) {
    my.hello.on('message', function (data) {
      console.log("hello: " + data);
    });

    every((1).seconds(), function() {
      my.hello.publish('hi there');
    });
  }
})

Simple

var Cylon = require('cylon');

Cylon.robot({
  connections: {
    server: { adaptor: 'mqtt', host: 'mqtt://localhost:1883' }
  },

  devices: {
    hello: { driver: 'mqtt', topic: 'greetings' }
  },

  work: function(my) {
    my.hello.on('message', function (data) {
      console.log(data);
    });

    every((1).seconds(), function() {
      console.log("Saying hello...");
      my.hello.publish('hi there');
    });
  }
}).start();
var Cylon = require('cylon');

Cylon.robot({
  connections: {
    mqtt: { adaptor: 'mqtt', host: 'mqtt://localhost:1883' },
    firmata: { adaptor: 'firmata', port: '/dev/ttyACM0' }
  },

  devices: {
    toggle: { driver: 'mqtt', topic: 'toggle', adaptor: 'mqtt' },
    led: { driver: 'led', pin: '13', adaptor: 'firmata' },
  },

  work: function(my) {
    my.toggle.on('message', function(data) {
      console.log("Message on 'toggle': " + data);
      my.led.toggle();
    });

    every((1).second(), function() {
      console.log("Toggling LED.");
      my.toggle.publish('toggle');
    });
  }
}).start();

For more examples, please see the examples folder.

How to Connect

var Cylon = require('cylon');

Cylon.robot({
  connections: {
    server: { adaptor: 'mqtt', host: 'mqtt://localhost:1883' }
  },

  work: function(my) {
    my.server.subscribe('hello');

    my.server.on('message', function (topic, data) {
      console.log(topic + ": " + data);
    });

    every((1).seconds(), function() {
      console.log("Saying hello...");
      my.server.publish('hello', 'hi there');
    });
  }
}).start();

Authentication

mqtt: { adaptor: "mqtt", host: "mqtt://localhost:1883", username: "iamuser", password: "sosecure" },

Drivers

There is only one driver for the MQTT.