2. What is MQTT(S) ?
• Very popular in IoT devices for pub/sub data
• Open protocol and Standard ( no fees )
• Very Simple to implement as a socket TCP service
– Small devices and IoT ready sensors uses this protocol (mainly)
– Low overhead, low bandwidth use
• Robustness
– Suitable for mobile devices / unreliable connections ( QoS
selectable ,Retained messages , Last will…)
MQTT: Message Queuing Telemetry Transport
3. About MQTT…
• MQTT Server ( usually called “Broker”) is a
service that may run in a cloud or in your local
infrastructure.
• The server is responsible for receiving all
messages, filtering them, manage the client
connections and delivering the message to all
subscribed clients.
• The server may handle thousands of
concurrently connected MQTT clients. The
capacity depends of the service / network
capacity you hire .
MQTT Server
4. MQTT Server
About MQTT…
• MQTT Servers can be bridged each other
in order to exchange and grow easily.
• Devices that are connected to MQTT
server hosted in a Cloud server may
include extra integrated services
• Big Data Storage
• Analytics
• Dashboards, Reports, etc
• Notifications, alarms, events
• Machine learning
• Decisions
• Predictions…
MQTT Server
5. MQTT Main feature
M2M “unlimited*”
connections
Just one
connection
(to Server)
..is shared to
hundreds or
thousands*
MQTT Server
*Depends of the
Server/ cloud server
clients capacity
6. Some MQTT brokers
• Mosquitto (popular)
• Apache ActiveMQ
• Apollo MQTT
• JoramMQ
• RabbitMQ
• VerneMQ
• …
Performance & features comparison:
http://www.scalagent.com/IMG/pdf/Benchmark_MQTT_servers-v1-1.pdf
7. MQTT Basic Concepts
Find a very handful tutorial to learn about MQTT at
https://www.hivemq.com/blog/mqtt-essentials/
8. How does it share the data?
• All data in MQTT has to be published under a
TOPIC* in order to address to the subscribers
Tank/Pressure/#Tank/Temperature/
Publisher
Tank/Temperature/#
Subscribers
MQTT Server
9. MQTT pub/sub concept
*MQTT Server
Publish:
Temperature
Humidity
Subscribe:
Temperature
Humidity
Publish:
Air Pressure
Subscribe:
Fan Control
Air Pressure
Subscribe:
Fan Control
Fan Status
Temperature
Humidity
Air Pressure
Publish:
Fan Control
Publish:
Fan Status
*Server can be local
or remote (i.e. cloud
service)
10. Quality of Service
What is Quality of Service?
• A Sender and Receiver agrees a Quality of Service
level during publish and subscription, that guarantees
delivering / receiving a message.
• MQTT offers 3 QoS levels :
• At most once (QoS0)
• At least once (QoS1)
• Exactly once (QoS2)
MQTT Server
Qualityofservice
11. Last Will / Testament Message
• When a MQTT client connects to MQTT server, it may
deliver a Last Will message (LWT) that is delivered to
the other clients, when it disconnects from the
server.
• LWT message is set by the client when it connects to
server. It is specified by a <LWT Topic> & <LWT
message>. In order to receive a LWT message , a
client has to be subscribed to the LWT topic
• It may be used to notice to interested subscribers a
MQTT client connection loss.
LWT/Is_gone
MQTT Server
LWT/Is_gone
LWT/Is_gone LWT/Is_gone
Subs.LWT/
Subs.LWT/
Bye!
12. Retained messages
<retained>Temp/23
MQTT Server
<retained>Temp/23
23
SUSBSCRIBE
Temp/
• A retained message will be stored by the MQTT
server, even without subscribers listening.
• The server will store the last retained message ,
only one retained message per topic. New
retained message will overwrite the last one.
• In this way , the receiver will get the message
immediately after subscribing, without waiting
to publisher to send the last value
13. Persistent Session ( clean session)
• When a persistent session starts ,the MQTT server
stores* relevant information for the session such:
• Existence of a session , even if there are no subscriptions
• All existing subscriptions
• All messages in QoS1 /2, not confirmed by the client
• All new QoS1/2 messages, while the client missed while it was
offline
• All received QoS2 messages, which are not yet confirmed from
the client
• A MQTTclient may use a Persistent session to agree to
the Server that has to store the undelivered/
unconfirmed messages (in QoS1 and QoS2 only)
MQTT Server
14. Keep Alive
• A Keep Alive is a of timeout agreed from Client to
Server, when client connects to MQTT server.
• If the Server doesn’t receive any message from a
client within the Keepalive time , server will drop the
connection to the client .
• In this case , Client should connect again in order to
publish data.
• This feature helps to close automatically inactive
connections, releasing resources at the server
MQTT Server
16. Library structure
• It can be used a regular Internet Connection or a Local network
• 3 FBs ( so far) to Connect, Publish & Subscribe to a MQTT broker
17. Message queue / buffer
Operation concept
MQTT ServerPublish instances Subscribe Instances
MQTT Client
This method allow to queue several messages from different
datasources ( devices, variables, etc.. ) that are updated at
different timing.
18. MQTTPublish
Topic/Message
Publish Example
• By using a library to collect NJ data in a string variable , then
calling to a MQTTpublish instance
Message:SlaveData
SlaveData= {
"Temperature": 98.7,
"Pressure": 1568,
"PH": 7,
"Pump": true,
"Heater": false
} MQTT Server
Topic:Variables
• Temperature
• Pressure
• PH
• Pump
• heater
19. Subscription example
• NJ is subscribed to the topic Meteo/temperature/# from a
sensor at home:
MQTT Server
Subscriber
Publisher
Meteo/temperature/ Meteo/temperature/21.8
21. The JSON data format
• JSON (JavaScript Object Notation) is a
popular data format at IoT used for data
exchange.
• Compared to XML :
– Lightweight
– Structured for natural reading and easy parsing
22. The JSON data format
{
"TimeStamp":"2017-11-08T15:37:58.926Z",
"Temperature": 98.7,
"Pressure": 1568,
"Pump": true,
}
• A JSON data structure consist on one or
more pairs of names and values,
separated by comma ; and delimited by
keys {..}.
• It may include several types such
numeric, boolean, text, date,.. as an
structure
• A JSON may include a Object definition
for a key, and it may nest objects and
arrays .
{“Sensors”:
{“location”:”Bathroom”,”Measure”:88.5,”Status”:TRUE};
{“location”:”Livingroom”,”Measure”:44.5,”Status”:FALSE};
{“location”:”Bedroom”,”Measure”:32.8,”Status”:FALSE};
{“location”:”Garage”,”Measure”:66.1,”Status”:TRUE}
}
{“Process”:
{
"TimeStamp":"2017-11-08T15:37:58.926Z",
"Temperature": 98.7,
"Pressure": 1568,
"Pump": true
}
}
{ Machine:
{
Production: 55604,
Rejected: 34,
ProductionSpeed: 134,
Status:
{
OEE: 91,2,
OnProdution: TRUE,
Hours: 1255.3
}
}
}
23. JSON library : use it with MQTT
• MQTT protocol accept any type of data format
• However , JSON is convenient format and compatible for IoT
devices
• We made a library to convert Sysmac variables to JSON string
{
"TimeStamp":"2017-11-08T15:37:58.926Z",
"Stage":"On Preparation",
"Temperature": 98.7,
"Pressure": 1568,
"PH": 7,
"Pump": true,
"Heater": false
}
24. Collect data in structured way (JSON)…
…and send it by MQTT !
26. The example below shows a conversion from variables to JSON
strings , from different datasources ( at different locations), that
are sent under MQTT Topics , and finally are shown in a
dashboard:
29. Application example (1)
• Single client is connected
to a remote MQTT broker,
by using a 4G
modem/router trough
VPN connection
• Typical application for
monitoring, alarm
triggering, maintenance…
MQTT Server
MQTTPublish
Topic/Message
VPN
MQTTSubscribe
Topic/Message
3G/4G
30. Application example (2)
• The clients are connected
to a local MQTT broker
• The broker manages the
connections and
published messages, and
delivers to the
subscribers
31. Application example (3)
• In a Factory, MQTT clients shares
messages between them across the
local MQTT broker
• In addition, the broker bridges to a
external Cloud Service
• The bridged connection can be secured
from the local broker
32. TLS/SSL
Bridged
connection
• Two or more factories can
be connected from
bridged connections to a
global remote MQTT
broker ( cloud service).
• The global MQTT broker
can deliver a message
from any station to
another, bridging both
networks
• On the Cloud Service, we
can use the received data
for analysis, storage, etc