IoTa + MQTT
MQTT: The other protocol
The open-source Particle Photon (and it's CoAP protocol implementation) is a great IoT platform, with plenty of options for scalability and support for transitioning your prototypes into reality. This is especially true if you intend to spin off your projects into mass-produced commercial ones.
MQTT is another open-source implementation. It has much less hardware integration compared to the Particle ecosystem because it's simply an elegant, lean messaging protocol that allows any hardware capable of TCP/IP to leverage it. What you get is an easy-to-use, standardised means to communicate between hardware devices, in particularly low-power, intermittently-connected ones.
More information about MQTT can be accessed here:
Processing MQTT data on IoTa
You'll need to have a device that is already configured to send MQTT data to your MQTT server. Refer to this recipe for an Arduino IDE-based example sketch using the mote mini. Please note that the server names and ports are specific to my studios taught and are unique to the custom configuration I have set up for the class groups.
Now that you have a connected device subscribed to an MQTT topic and publishing data, you need to set up a flow to connect to the MQTT server. This will then allow you to relay messages between devices and machines through the IoTa interface.
Using the MQTT nodes in IoTa/node-RED
Below is a very basic example of how MQTT message transmission can be understood. You have an MQTT input/send node, and an MQTT output/receive node. The two inject nodes are sending basic text strings: a “hello” message and a numerical timestamp. The MQTT receive node, if configured correctly, will ‘catch’ these messages and send them on through to the Debug node, which is a handy way of tracing various points in your flow:
This flow looks self-explanatory; what's more crucial are the settings that you must configure within the MQTT nodes. You should also refer to the Information sidebar to get further online help on what the node is supposed to do.
Server – clicking on the edit (pencil) button brings you to the server configuration node (see next section below)
Topic – topics form a key component of message relay in IoTa/node-RED. Like the name suggests, a topic is a text string that you can declare so that only devices subscribed to the same topic will receive it. This is particularly useful if you have different functions you want triggered on a device, or if you need to send the same command to multiple devices (subscribed to the same topic). MQTT topics also support wildcarding, which allows you to trigger devices running different parent topics. For example:
For further reading on wildcards, head to this article by HiveMQ
QoS stands for Quality of Service. MQTT supports 3 QoS levels:
- 0: ‘fire-and-forget’, ‘at most once'
the server shoots a message once
- 1: ‘at least once'
the server will strive to send the message once (and only once)
- 2: ‘exactly once'
the sending device and the MQTT server does a call-and-response handshake to ensure the message is received by the server once. (NOTE: this is not supported by @imroy's PubSubClient in the Arduino IDE!)
For further information on QoS, head to this article by HiveMQ
- 0: ‘fire-and-forget’, ‘at most once'
Retain allows a sent message to be kept ‘in memory’; devices that connect after this message is sent will get this message instead of waiting for the next published message.
For further information on the retain property, head to this article by HiveMQ
Name is just any name you'd like to call your node. Try to be specifically concise!
Server configuration is done through what we call a configuration node – an ‘invisible’ node that stores secure credentials and settings that other relevant nodes can connect to. This allows you to connect re-use the same configuration and avoid entering credentials repeatedly.
In our BORB example, I have created a server configuration named
iota.mdit.space. Click on the edit icon to open the configuration screen:
Testing basic operation
Once you have the nodes added and configured, click on the red Deploy button on the upper right corner of the interface. The flow will now execute on the server and become ‘active’. Open the Debug tab on the sidebar (the one that looks… like a bug). When you click on the inject buttons, you should be able to see the output emerging from the debug nodes.
Once you have this basic connectivity tested, you now have yourself a blank canvas for sending data streams between multiple devices! Have a break and holler out your window if you'd like!
Basic MQTT operation
The following video walks you through the above process of configuring your MQTT server. In addition to what was covered above, it also demonstrates the use of MQTT wildcards to allow message filter by virtue of the message topic:
Putting it all together: IoTa MQTT + mote mini
This is the final fun part in getting everything to come together.