Getting Started

This page will help you get started with Meshblu MQTT. You'll be up and running in a jiffy!

🚧

You must register a device with Meshblu using one of the other APIs (HTTP, COAP, WebSocket) to receive a UUID and token to use with these examples. Our MQTT API does not yet support RPC-style request/response calls.

For your convenience, we also have a meshblu-mqtt client node.js npm module.

npm install meshblu-mqtt

Here are the basics:

var Meshblu = require('meshblu-mqtt');
var config  = require('./meshblu.json');

// Config Example
// {
//   "uuid": "5632dd4a-e66b-43c7-bbbd-b264903e20bd",
//   "token": "c84bdb43febc2702110fc7d6a9aa91cc6b783ec1",
//   "hostname": "meshblu.octoblu.com",
//   "port": "1883"
// }

var meshblu = new Meshblu(config);
console.log('starting...');

meshblu.connect(function(response){
  console.log('ready', response);
  // Update Device - response emits event 'config'
  meshblu.update({uuid: config.uuid, skynet: 'rules'});

  // Message - response emits event 'message'
  var message = {
    devices: ['5682bd69-0199-46eb-b79b-0f585e5994e5'],
    topic: 'hello',
    payload: {ilove: 'food'}
  };
  meshblu.message(message);

  // Reset token - response emits event 'token'
  meshblu.resetToken({uuid: 'some-uuid'});

  // Generate New Session Token - response emits event 'generateAndStoreToken'
  meshblu.generateAndStoreToken({uuid: 'some-uuid'});

  // Get Public Key - response emits event 'getPublicKey'
  meshblu.getPublicKey({uuid: 'some-uuid'});

  // Whoami - - response emits event 'whoami'
  meshblu.whoami();
});
var client, meshbluJSON, mqtt;

mqtt = require('mqtt');
// meshblu-util register > meshblu.json
meshbluJSON = require('./meshblu.json');

client = mqtt.connect("mqtt://" + meshbluJSON.server, {
  username: meshbluJSON.uuid,
  password: meshbluJSON.token
});

client.on('connect', function() {
  console.log('connected');
  client.subscribe(meshbluJSON.uuid);
  client.publish('message', JSON.stringify({
    devices: ['*'],
    topic: 'status'
  }));
});

client.on('close', function() {
  return console.log('disconnected');
});

client.on('message', function(topic, message) {
  return console.log(topic, message.toString());
});