IoT Tutorial Series # 1 - Getting Started with MQTT and Node JS

in #utopian-io6 years ago (edited)

Repository

MQTT : https://github.com/mqttjs/MQTT.js

What Will I Learn?

This is first part of an IOT tutorial series I will be writing to help the utopian community and developers understand the basics and essentials of the internet of things. In this tutorial we will learn about MQTT and its basic concepts Like

  • What is MQTT Protocol and mqtt.js
  • What are clients and broker
  • How to connect to a broker via Node JS
  • How to publish and subscribe to messages on a network

Requirements

  • Node JS basics

Difficulty

  • Basic

Tutorial Contents

The internet of things is a widely used buzzword in the recent technology space. along with AI and blockchain, the IoT is one of technologies of the future.

The Internet of Things (IoT) is the network of physical devices, vehicles, home appliances and other items embedded with electronics, software, sensors, actuators, and connectivity which enables these things to connect and exchange data, creating opportunities for more direct integration of the physical world into computer-based systems, resulting in efficiency improvements, economic benefits and reduced human intervention. - WikiPedia

Simply speaking, Just like the Internet is a network of computers connected across the world, the internet of things is a giant network of computers, humans and other physical devices connected via the internet.

MQTT

MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium. - Mqtt.org

MQTT is a communication protocol, built on top the TCP/IP stack.HTTP is another protocol based on the TCP/IP. However, MQTT fundamentally is a publish/subscribe protocol for machine to machine communication, while HTTP is based on the request/response model for client/server communication.

It means, MQTT allows clients to connect as a publisher, subscriber, or both. You connect to a broker that handles all the message passing. The messages are passed via topics. Topics are the way a subscriber register interest for incoming messages or how a publisher specify where he wants to publish his message. Hence these are the main components in a MQTT Application -

Publisher : A publisher is a client who publishes messages in the network. The message is sent to the broker with a topic, who forwards it to the intended listeners of the topic.

Subscriber : A Subscriber is a client who listens to incoming messages on perticular topics.

Broker : The broker is primarily responsible for receiving all messages published in the network, filtering the messages, decide who is interested in it and then publishing the message to all subscribed clients.

Topic : A topic is a string, which is used by the broker to filter messages for each connected client. A topic consists of one or more topic levels. Each topic level is separated by a forward slash (topic level separator). For instance, A client may subscribe to topic "world/of/things". Topics may also contain wildcards.

Messages : Messages are data that is published on the network by publishers.

Quality of Service(QoS) : MQTT provides three qualities of service for message delivery:

  • "At most once", where messages are delivered according to the best efforts of the operating environment. Message loss can occur. This level could be used, for example, with ambient sensor data where it does not matter if an individual reading is lost as the next one will be published soon after.
  • "At least once", where messages are assured to arrive but duplicates can occur.
  • "Exactly once", where message are assured to arrive exactly once. This level could be used, for example, with billing systems where duplicate or lost messages could lead to incorrect charges being applied.

How it really works

The MQTT protocol works by exchanging a series of MQTT control packets in a defined way. Each control packet has a specific purpose and every bit in the packet is carefully crafted to reduce the data transmitted over the network. A MQTT topology has a MQTT server and a MQTT client. MQTT client and server communicate through different control packets. Table below briefly describes each of these control packets.

PacketTable.jpg

Okay! Enough theory! Now lets dive into the code to see everything in action.

In the below segment we will be creating a NodeJS MQTT client as publisher and subscriber. We will be using a public broker provided by mosquitto.

First of all Create a new npm project by using the following command and following the on screen instructions.

Open the terminal,Make sure you have NodeJs installed by running the following command.

node -v

If you don't have NodeJS installed, You can install it from their official website. Now create a project directory mqtt-demo and start a new npm project with the following commands.

mkdir mqtt-demo
cd mqtt-demo
npm init 

Following the onscreen instructions, you will find a package.json file created in the project directory. This file holds data about the Node Js project. Create another file named subscriber.js in the same directory and open it in your favourite text editor. I use VS Code, but you can any text editor to edit .js files. Inside the index.js file we will be creating our first mqtt client. But before that, we need to import the MQTT.js library as a dependency from npm.

npm install mqtt --save

We can create a new client using mqtt.connect() method. This method requires url of the broker as a parameter.This client can then publish messages on the network or subscribe to topics. MQTT.js also provides us with client.on(event,callback) method to handle varius events that are fired when a packet is sent or received. In order to create a client we will add the following code to subscriber.js.

var mqtt = require('mqtt')
var client  = mqtt.connect('mqtt://test.mosquitto.org')

client.on('connect', function () {
  client.subscribe('mytopic');
})

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

In the above piece of code, we are creating a client who is connected to the mosca public broker. Once the connection is set up, the client will be listening to all the messages being sent on the topic 'mytopic'.

Let us now create the publiser client in publisher.js who will publish message to topic 'mytopic'

var mqtt = require('mqtt')
var client  = mqtt.connect('mqtt://test.mosquitto.org')

client.on('connect', function () {
  setInterval(function(){client.publish('mytopic', 'Hello mqtt')},1000)
})

Now, Go to the terminal and run the subscriber client by running :

node subscriber.js

Now open a new terminal and navigate to the project directory. Lets publish a message by running the publisher.js file.

node publisher.js

Upon switching back to the subscriber terminal, you should see the message "Hello World" on the console every 1 second, which means that the message is received by the subscriber. The output should be something like this :

node sub.js
1360
Hello mqtt
Hello mqtt
Hello mqtt
Hello mqtt
Hello mqtt
Hello mqtt
Hello mqtt
Hello mqtt
Hello mqtt
Hello mqtt

As a real world example use case, one of these two clients can be a temperatures sensers publishing frequent temperature data for a particular location, while the subscriber maybe a web dashboard showing live analysis of the data recieved.

Curriculum

This is the first part of the Internet of things tutorial series. The curriculum as of now is as follows :

  • How to use Mosca to create our own broker
  • Learning Async Await in Node JS with async-mqtt
  • How to create a remote door opener using MQTT
  • How to Listen to tweeter feed in node js and MQTT
  • Getting started with NODE RED
  • Building real time applications using NODE RED

Please feel free to comment if you have any query/doubts.

Additional Resources

MQTT website : http://www.mqtt.org
MQTT.js npm : https://www.npmjs.com/package/mqtt
NodeJs website : https://nodejs.org/en/

Proof of Work Done

The code used here can be found in the follwing gist.

https://gist.github.com/AneilPatel05/8cba8b914325ef9120516427558caa34

Sort:  

You have a minor misspelling in the following sentence:

on(event,callback) method to handle varius events that are fired when a packet is sent or recieved.
It should be received instead of recieved.

Thanks for the correction!

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Resources: Put more extra resources.
  • Structure of the tutorial: Improve the structure of the tutorial.

Write something more innovative and more challenging in your future tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for your review @portugalcoin. I will keep your suggestions in mind.

Maybe you want to try this in your next tutorial:

  1. Try to avoid anything that related to "Hello World", even printing the hello text itself. Sometimes it can mislead the moderator (and the reader if the subject little bit advance).
  2. Because your topics are about network and communication, adding some diagram (like sequence diagram or your own style diagram) will bring more value 🐣.

Thank you @drsensor for your valuable suggestions. I will surely Roger that.

Hey @drsensor
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hey @aneilpatel
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.033
BTC 63945.57
ETH 3135.76
USDT 1.00
SBD 4.00