{"__v":9,"_id":"5567496d7acd550d0075eb09","api":{"auth":"required","params":[],"results":{"codes":[]},"settings":"","url":""},"body":"[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"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.\"\n}\n[/block]\nFor your convenience, we also have a meshblu-mqtt client node.js npm module.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"npm install meshblu-mqtt\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Here are the basics:\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var Meshblu = require('meshblu-mqtt');\\nvar config  = require('./meshblu.json');\\n\\n// Config Example\\n// {\\n//   \\\"uuid\\\": \\\"5632dd4a-e66b-43c7-bbbd-b264903e20bd\\\",\\n//   \\\"token\\\": \\\"c84bdb43febc2702110fc7d6a9aa91cc6b783ec1\\\",\\n//   \\\"hostname\\\": \\\"meshblu.octoblu.com\\\",\\n//   \\\"port\\\": \\\"1883\\\"\\n// }\\n\\nvar meshblu = new Meshblu(config);\\nconsole.log('starting...');\\n\\nmeshblu.connect(function(response){\\n  console.log('ready', response);\\n  // Update Device - response emits event 'config'\\n  meshblu.update({uuid: config.uuid, skynet: 'rules'});\\n\\n  // Message - response emits event 'message'\\n  var message = {\\n    devices: ['5682bd69-0199-46eb-b79b-0f585e5994e5'],\\n    topic: 'hello',\\n    payload: {ilove: 'food'}\\n  };\\n  meshblu.message(message);\\n\\n  // Reset token - response emits event 'token'\\n  meshblu.resetToken({uuid: 'some-uuid'});\\n\\n  // Generate New Session Token - response emits event 'generateAndStoreToken'\\n  meshblu.generateAndStoreToken({uuid: 'some-uuid'});\\n\\n  // Get Public Key - response emits event 'getPublicKey'\\n  meshblu.getPublicKey({uuid: 'some-uuid'});\\n\\n  // Whoami - - response emits event 'whoami'\\n  meshblu.whoami();\\n});\",\n      \"language\": \"javascript\"\n    },\n    {\n      \"code\": \"var client, meshbluJSON, mqtt;\\n\\nmqtt = require('mqtt');\\n// meshblu-util register > meshblu.json\\nmeshbluJSON = require('./meshblu.json');\\n\\nclient = mqtt.connect(\\\"mqtt://\\\" + meshbluJSON.server, {\\n  username: meshbluJSON.uuid,\\n  password: meshbluJSON.token\\n});\\n\\nclient.on('connect', function() {\\n  console.log('connected');\\n  client.subscribe(meshbluJSON.uuid);\\n  client.publish('message', JSON.stringify({\\n    devices: ['*'],\\n    topic: 'status'\\n  }));\\n});\\n\\nclient.on('close', function() {\\n  return console.log('disconnected');\\n});\\n\\nclient.on('message', function(topic, message) {\\n  return console.log(topic, message.toString());\\n});\\n\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","category":"5567496c7acd550d0075eb07","createdAt":"2015-05-28T16:59:25.747Z","excerpt":"This page will help you get started with Meshblu MQTT. You'll be up and running in a jiffy!","githubsync":"","hidden":false,"link_external":false,"link_url":"","order":999,"project":"5567496b7acd550d0075eb03","slug":"getting-started","sync_unique":"","title":"Getting Started","type":"basic","updates":[],"user":"5564f227f0f70f0d00a9ab20","version":"5567496c7acd550d0075eb06","childrenPages":[]}

Getting Started

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

[block:callout] { "type": "warning", "body": "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." } [/block] For your convenience, we also have a meshblu-mqtt client node.js npm module. [block:code] { "codes": [ { "code": "npm install meshblu-mqtt", "language": "shell" } ] } [/block] [block:api-header] { "type": "basic", "title": "Here are the basics:" } [/block] [block:code] { "codes": [ { "code": "var Meshblu = require('meshblu-mqtt');\nvar config = require('./meshblu.json');\n\n// Config Example\n// {\n// \"uuid\": \"5632dd4a-e66b-43c7-bbbd-b264903e20bd\",\n// \"token\": \"c84bdb43febc2702110fc7d6a9aa91cc6b783ec1\",\n// \"hostname\": \"meshblu.octoblu.com\",\n// \"port\": \"1883\"\n// }\n\nvar meshblu = new Meshblu(config);\nconsole.log('starting...');\n\nmeshblu.connect(function(response){\n console.log('ready', response);\n // Update Device - response emits event 'config'\n meshblu.update({uuid: config.uuid, skynet: 'rules'});\n\n // Message - response emits event 'message'\n var message = {\n devices: ['5682bd69-0199-46eb-b79b-0f585e5994e5'],\n topic: 'hello',\n payload: {ilove: 'food'}\n };\n meshblu.message(message);\n\n // Reset token - response emits event 'token'\n meshblu.resetToken({uuid: 'some-uuid'});\n\n // Generate New Session Token - response emits event 'generateAndStoreToken'\n meshblu.generateAndStoreToken({uuid: 'some-uuid'});\n\n // Get Public Key - response emits event 'getPublicKey'\n meshblu.getPublicKey({uuid: 'some-uuid'});\n\n // Whoami - - response emits event 'whoami'\n meshblu.whoami();\n});", "language": "javascript" }, { "code": "var client, meshbluJSON, mqtt;\n\nmqtt = require('mqtt');\n// meshblu-util register > meshblu.json\nmeshbluJSON = require('./meshblu.json');\n\nclient = mqtt.connect(\"mqtt://\" + meshbluJSON.server, {\n username: meshbluJSON.uuid,\n password: meshbluJSON.token\n});\n\nclient.on('connect', function() {\n console.log('connected');\n client.subscribe(meshbluJSON.uuid);\n client.publish('message', JSON.stringify({\n devices: ['*'],\n topic: 'status'\n }));\n});\n\nclient.on('close', function() {\n return console.log('disconnected');\n});\n\nclient.on('message', function(topic, message) {\n return console.log(topic, message.toString());\n});\n", "language": "javascript" } ] } [/block]