HiveMQ integration!

HiveMQ is an Enterprise MQTT broker, designed to handle large amounts of messages with low latency. This guide shows you how to integrate your SmartMesh IP network into HiveMQ.

This recipe focuses on the integration of SmartMesh IP into HiveMQ. If you don't know what HiveMQ and/or MQTT is, we recommend the excellent getting started guides from HiveMQ.

The goal of this recipe is to get you started integrating your SmartMesh IP network into HiveMQ. After following these steps, you will be able to explore all the possibilities HiveMQ offers.

Overview


MQTT is a system which allows computers to publish messages to a broker. Each message is attached a topic (e.g. "dustcloud/temperature"). MQTT also allows computers to subsribe to certain topics. MQTT is scalable (i.e. many computers can be publishing/subscribing to many messages at the same time) and has several security options.


HiveMQ is a company which sells an MQTT broker with all the features (security, scalability, support...) appropriate for Enterprise solutions.

In this recipe, we show you atomic integration examples between your SmartMesh IP network and MQTT, enough to get you started on more complete integration for your application.

Tutorials

Publish Temperature to HiveMQ

We start with the most straightforward use case of HiveMQ: (sensor) data publishing.

Here is what the system you will build does:

  1. a SmartMesh IP mote generates some (sensor) data
  2. the mote sends it, through the SmartMesh IP mesh network, to the manager
  3. the manager publishes the data through its serial port, as a data notification
  4. this data notification is received by the JsonServer application (part of the SmartMesh SDK)
  5. JsonServer sends it to a local instance of Node-RED
  6. Node-RED publishes it to a HiveMQ broker, on topic "dustcloud/temperature"
  7. anybody in the world who is connected to the same broker - and subscribed to that topic - gets a notification

In a nutshell, add publish/subscribe capabilities to your SmartMesh IP network in a couple of clicks!

Connect to the HiveMQ WebSocket Client

What just happened?

Your browser is now connected to HiveMQ's demo MQTT broker.

Easy, right?

It doesn't work :-(

It is possible that you get the following message after clicking the "Connect" button. This is because your firewall prevents you from communicating with the HiveMQ demo MQTT broker over port 8000. Contact you network or system administrator.

Subscribe to the dustcloud Topic

On the right-hand side, add a new subscription to the "dustcloud/temperature" topic.

What just happened?

You will get a notification anytime someone connected to the same MQTT broker sends a message to this topic.

That's exactly what we will do below.

Prepare your SmartMesh IP network

  • Switch on your SmartMesh IP manager and motes

    We assume all SmartMesh IP motes are running the default firmware in master mode, and publish temperature data every 30 seconds.

  • Start the SmartMesh IP JsonServer application and Node-RED, and connect the JsonServer application to your SmartMesh IP manager's serial API port.

    See the SmartMesh IP and Node-RED, revisited recipe to see how to do that.

What just happened?

Your SmartMesh IP network is running, and the JsonServer application is forwarding all temperature measurements to your local Node-RED instance.

All you have to do now is have Node-RED publish those sensor measurements to your HiveMQ demo MQTT broker!

Publish Temperature to HiveMQ

Almost there, this is the last step!

All we need to do is create a Node-RED flow which:

  • listens for an HTTP POST request to URL /oap to receive the OAP notifications form the JsonServer
  • filters temperature notifications, and extracts:
    • the MAC address of the mote publishing
    • the temperature
  • publishes that information to the HiveMQ demo MQTT broker

This is what the resulting Node-RED flow looks like:

JSON version of the flow

[{"id":"b78f42ff.b21b2","type":"http in","z":"6b763b8c.728c44","name":"oap from motes","url":"/oap","method":"post","swaggerDoc":"","x":166.11111450195312,"y":138.72734832763672,"wires":[["29ec8374.075f0c","559dd3c4.d7410c"]]},{"id":"29ec8374.075f0c","type":"http response","z":"6b763b8c.728c44","name":"http response","x":324.38377380371094,"y":97,"wires":[]},{"id":"559dd3c4.d7410c","type":"function","z":"6b763b8c.728c44","name":"extract mac and temperature","func":"if (msg.payload.name==\"oap\" && msg.payload.fields.channel_str==\"temperature\") {\n    return {\n        \"payload\": {\n            \"mac\":         msg.payload.mac,\n            \"temperature\": msg.payload.fields.samples[0]/100.0\n        }\n    }\n}","outputs":1,"noerr":0,"x":392.3836212158203,"y":151.00001525878906,"wires":[["cf146a4f.330678"]]},{"id":"cf146a4f.330678","type":"mqtt out","z":"6b763b8c.728c44","name":"","topic":"dustcloud/temperature","qos":"","retain":"","broker":"418adc3f.9a5994","x":646.5201873779297,"y":153.63636016845703,"wires":[]},{"id":"418adc3f.9a5994","type":"mqtt-broker","z":"6b763b8c.728c44","broker":"broker.hivemq.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

To import the flow above in your Node-RED instance:

  • copy the string above (triple-click to select, then copy with Ctrl+C)
  • in your Node-RED editor, on the hamburger menu on the upper right, select "Import" > "Clipboard"
  • paste the string (Ctrl+V) and click "Import"
  • activate the flow by clicking on the "Deploy" button

You can now see the notifications on any MQTT client in the world which is connected to this broker and subscribed to the "dustcloud" topic:

using HiveMQ's websocket's clientusing a smart phone app (here MQTTool for iOS)

What just happened?

That's it! You are now pushing data into one the most convenient and efficient publish/subscribe systems!

Enjoy!

(but keep reading to see another use case and some important discussion)

Actuate LEDs from the Cloud

Your SmartMesh IP mote can interface to virtually any sensor and actuator through analog and digital interfaces. The default firmware which is pre-loaded allows you to emulate this by doing the simplest remote actuation: setting an LED.

The TempMonitor sample application (part of the SmartMesh SDK) allows you to actuate a mote's LED from the manager. In this section, we take it one step further, and allow your to actuate and LED from the cloud!

Setup

We use the exact same setup as before, all we will do is change the Node-RED flow.

Subscribe to the "dustcloud/led" topic on HiveMQ

All we need to do is create a Node-RED flow which:

  • connects to the HiveMQ demo MQTT broker and subscribes to the "dustcloud/led" topic
  • when receiving a notification, issues the appropriate HTTP request to the JsonServer

This is what the resulting Node-RED flow looks like:

JSON version of the flow

[{"id":"1a18583b.e07288","type":"mqtt in","z":"125e241b.c9181c","name":"","topic":"dustcloud/led","qos":"2","broker":"35dfdcfe.e4f334","x":100.8367919921875,"y":65.80557250976562,"wires":[["5156a084.5cf12","75684a8d.2e16f4"]]},{"id":"b85e3baf.1f1bb8","type":"http request","z":"125e241b.c9181c","name":"","method":"use","ret":"txt","url":"","tls":"","x":449.83677673339844,"y":66.55903625488281,"wires":[["4455fdaa.f482f4"]]},{"id":"5156a084.5cf12","type":"function","z":"125e241b.c9181c","name":"set LED","func":"payload = JSON.parse(msg.payload)\n\nvar js_url = \"http://127.0.0.1:8080/api/v1/oap/\"\njs_url    += payload.mac+\"/digital_out/INDICATOR_0\"\n\nreturn {\n    url:     js_url,\n    method:  \"PUT\",\n    headers: {\n        \"content-type\":     \"application/json\",\n    },\n    payload: {  \n        \"value\":            payload.value\n    }\n};\n","outputs":1,"noerr":0,"x":281.8401641845703,"y":66.52430725097656,"wires":[["b85e3baf.1f1bb8","22a1da22.45a7b6"]]},{"id":"75684a8d.2e16f4","type":"debug","z":"125e241b.c9181c","name":"","active":true,"console":"false","complete":"false","x":283.83677673339844,"y":270.1145935058594,"wires":[]},{"id":"22a1da22.45a7b6","type":"debug","z":"125e241b.c9181c","name":"","active":true,"console":"false","complete":"false","x":423.8402557373047,"y":187.125,"wires":[]},{"id":"4455fdaa.f482f4","type":"debug","z":"125e241b.c9181c","name":"","active":true,"console":"false","complete":"false","x":586.8298797607422,"y":137.11805725097656,"wires":[]},{"id":"35dfdcfe.e4f334","type":"mqtt-broker","z":"","broker":"broker.hivemq.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

To import the flow above in your Node-RED instance:

  • copy the string above (triple-click to select, then copy with Ctrl+C)
  • in your Node-RED editor, on the hamburger menu on the upper right, select "Import" > "Clipboard"
  • paste the string (Ctrl+V) and click "Import"
  • activate the flow by clicking on the "Deploy" button

Actuate your mote's LED from anywhere in the world!

On your HiveMQ websocket client, publish the following message to the "dustcloud/led" topic

{
    "mac":   "00-17-0d-00-00-38-07-18",
    "value": 1
}

Set value to "1" to switch the LED on, set to "0" to switch it off.

Of course, replace the MAC address (00-17-0d-00-00-38-07-18) by the MAC address of a mote in your network.


Tada, the LED changes state!

Any MQTT client connected to that broker can issue the same message. This means your can trigger actuation from anywhere in the world, using any type of device, including your smart phone.


Discussion

This recipe doesn't even scratch the surface of what HiveMQ and MQTT allow you to do.

Things you can build now:

  • create a "control loop", where the data sent by one mote triggers an actuation on a different mote
  • connect multiple SmartMesh IP networks logically together by having them publish/subscribe to the same HiveMQ broker
  • use HiveMQ's websocket feature to include notifications directly into your web pages
  • plug in analytics, databases, visualization tools, etc. to the MQTT broker to build a complete solution
  • etc.

About Security

Security MUST be taken seriously in all applications, including end-to-end IoT solutions like the one we are building here. This recipe is minimal by design, and doesn't detail nor contain the level of security you need for a production system.

Things we haven't touched upon:

  • proper end-to-end security between Node-RED and HiveMQ
    • in particular, in the Node-RED flow above, transactions are done without authentication with the HiveMQ demo MQTT broker
    • a production HiveMQ MQTT broker comes with authentication, which you should use
  • proper security between the JsonServer and Node-RED
    • in particular, in this recipe, the JsonServer doesn't authenticate to Node-RED

When building a production system, we strongly recommend implementing industry-standard security methods.

Some links for your further exploration.