At PubNub, we’ve learned a lot from IoT customers like Samsung, August Home, Wink, and Logitech about the types of features they require. We’re excited to announce that we have taken all our expanded PubNub Data Stream Network (DSN) features, like Functions, topic wildcard support, and simple setup, and have made them available to our customers with MQTT devices.
It is now super easy to integrate MQTT devices into the PubNub DSN. No code changes are required; simply put your pub/sub credentials into your device identifier, and PubNub takes care of the rest. This blog will walk you through the steps involved in this integration.
MQTT and IoT Background
What is MQTT in IoT?
If you aren’t familiar with MQTT, the Message Queuing Telemetry Transport (MQTT) is a publish/subscribe protocol aimed specifically at low-powered embedded devices. MQTT targets low-powered embedded device environments where resources are scarce, and strives to reduce battery, CPU, and bandwidth consumption.
Is MQTT good for IoT?
Originally developed at IBM, MQTT has picked up a lot of traction in the IoT industry, especially for its lower power requirements. There are plenty of documentation and open source client implementations if you wish to dive further into the MQTT IoT protocol itself. Other reasons why MQTT is good for IoT devices are:
MQTT has very low bandwidth usage, ideal where devices might have a weak or intermittent connection.
Low power consumption, meaning IoT device batteries need changing less often, reducing maintenance costs.
Scalability, as the number of IoT devices in your deployment increases, MQTT will be able to cope with a need to rearchitect your networking
MQTT is a very reliable protocol, capable of handling safety-critical or real-time data.
Which IoT devices use MQTT?
MQTT is used by a wide range of IoT devices from consumer smart home devices such as lights, thermostats, and security cameras to industrial devices for inventory tracking or healthcare remote monitoring systems.
PubNub and MQTT for IoT
You may already have deployed MQTT-based IoT devices. Or you may be considering a new IoT deployment. Either way, the question arises, “When should I use MQTT, and when should I use PubNub?” And when does it make sense to use both?
If you are considering robust, bi-directional communication options for your MQTT-based devices, PubNub provides enterprise-grade security, scalability and reliability, and many value-added features too, like functions-as-a-service and message storage and playback. The PubNub DSN is available worldwide with <250ms latency, and can scale to support all your Internet-connected devices.
Like the proverbial peanut butter and chocolate, PubNub and MQTT together can elegantly solve many embedded device use cases. You can leverage PubNub with MQTT to satisfy use cases like low-latency, efficient anomaly detection, and integrating to machine learning services.
[Read more: MQTT Use Cases]
If MQTT is not part of your current infrastructure, you might consider leveraging the PubNub DSN directly along with PubNub’s numerous IoT SDKs for all of your Internet of Things connectivity. Or the PubNub DSN with MQTT can be a great choice for low-powered devices.
How to Connect to PubNub’s Data Stream Network Using MQTT for IoT
So let’s dive into the details. How do you use MQTT with PubNub?
We have recently built out a much more scalable MQTT interface, with support for TLS security, topic wildcards, and more. In the steps below, we use MQTT.fx to simulate an embedded MQTT device, but any MQTT-supported client would work.
You’ll first need to sign up for a PubNub account, and get your unique publish and subscribe keys. You can get your keys in the PubNub Admin Dashboard. When using PubNub, you simply create channels (aka, “topics” in the MQTT world) and publish messages across those channels with those keys. You’ll do the same with MQTT.
The only two things you need to do to connect your device to the PubNub Network are:
1. Use a broker address of mqtt.pndsn.com. Use the standard ports – for unsecured connections use 1883, for TLS secured connections use 8883 – both are supported.
2. Use a client ID composed like this:
Create an MQTT-PubNub “Hello World!”
Once you’ve set up your MQTT client, initiating a “Hello World” demo should be pretty straightforward. If you’re using MQTT.fx, you can connect to the PubNub Network at mqtt.pndsn.com, and publish a “Hello” message from the client in the “Publish” tab. If you then go to the PubNub Admin Dashboard, you will see “Hello” published.
View from MQTT.fx
View from PubNub Admin Dashboard
Composing Message Back to MQTT Client
If you are experiencing any problems with this Hello World-type initialization, check the channels in the PubNub Admin Dashboard to ensure they match the topic set up in the MQTT client.
MQTT IoT Wildcard Topic Support
PubNub’s MQTT support includes topic wildcard support, so developers can leverage the application flexibility within the MQTT protocol.
When you set up wildcard support in MQTT, you use “/”, but in PubNub you use “.”. So if you want to provide access across the “a” channel/ topic, you would type “a/#” in the MQTT client, but “a.*” in the PubNub Admin Dashboard. The single-level and multi-level wildcard nomenclature remains the same, respectively “+” and “#”.
Here are examples of wildcard subscriptions from the PubNub Admin Dashboard:
// Single-level wildcard will be translated to: .*
/+/sensor
// Multi-level wildcard will be translated to: .house.sensor.*
/house/sensor/#
Note that PubNub only supports QoS level 0 at this time. Good luck with your IoT/ MQTT initiative!