MQTT 
for monitoring (and for the IoT) 
Jan-Piet Mens 
November 2014 
@jpmens
MQTT is a standard, 
a transport, 
PUB/SUB messaging, 
designed for 
unreliable networks
transport protocol 
binary payload, 256MB, (+2 bytes), 
fast, 
lightweight, ideal for low-bandwith, 
high-latency networks
security 
TLS 
authentication 
ACLs 
TLS-PSK 
(payload encryption)
Quality of Service 
0 At most once 
1 Assured delivery 
2 Once only
more features 
keepalive 
last will & testament, 
decoupled senders/recipients, 
durable messages
topic names 
UTF-8, hierarchical, wildcards 
temperature/room/living 
devices/# 
finance/+/eur/rate
PUB/SUB cauldron
MQTT brokers 
the server bit of MQTT
Mosquitto 
C, fast, lightweight, ACLs (plugin), TLS, TLS-PSK, bridge, 
Websockets, logging via $SYS 
http://mosquitto.org
HiveMQ 
Java, plugins, Websockets, clustering, modules 
http://hivemq.com
MQTT brokers 
$SYS topic 
$SYS/broker/clients/total 1771 
$SYS/broker/messages/received 36597465 
$SYS/broker/messages/sent 39714120 
$SYS/broker/messages/stored 2941 
$SYS/broker/bytes/received 2830787008 
$SYS/broker/bytes/sent 3810653433 
$SYS/broker/version mosquitto version 1.4 
$SYS/broker/publish/messages/received 19798673 
$SYS/broker/publish/messages/sent 30622855 
$SYS/broker/publish/bytes/received 1868229299 
$SYS/broker/publish/bytes/sent 3185942282
bridging
CLI utilities 
mosquitto_sub -v 
[-h localhost] [-p 1883] 
[--cafile file] 
[--cert file --key file] 
[-u username [-P password]] 
[ --tls-version tlsv1.2 ] 
-t 'topic/#' 
subscribe 
publish 
mosquitto_pub 
... 
[-r] 
-t topic 
-m payload
languages 
Lua, Python, C, JavaScript, Perl, Ruby, Java, ...
Python API: PUB 
#!/usr/bin/env python 
import paho.mqtt.publish as mqtt 
mqtt.single('conf/hello', 'Hello MQTT') 
topic payload 
$ mosquitto_sub -h localhost -v -t 'conf/#' 
conf/hello Hello MQTT
Python API: SUB 
callbacks 
#!/usr/bin/env python 
import paho.mqtt.client as paho 
def on_connect(mosq, userdata, rc): 
mqttc.subscribe("conf/+", 0) 
def on_message(mosq, userdata, msg): 
print "%s %s %s" % (msg.topic, str(msg.payload), userdata) 
data = { 'type' : 'conference' } 
mqttc = paho.Client(userdata=data) 
mqttc.on_connect = on_connect 
mqttc.on_message = on_message 
mqttc.connect("localhost", 1883, 60) 
mqttc.loop_forever()
Python API: SUB 
$ mosquitto_pub -t 'conf/thirsty' -m 'Beertime?' 
$ mosquitto_pub -t 'conf/catering' -m 'Coffee is ready' 
$ ./sub.py 
conf/thirsty Beertime? {'type': 'conference'} 
conf/catering Coffee is ready {'type': 'conference'}
practical solutions 
alerting, metering, logging, location awareness, tracking, 
automation, and controlling, host monitoring
alerting: mqttwarn 
https://github.com/jpmens/mqttwarn
temperature: Arduino 
huh?
all good!
how does this work? 
arduino/10.0.69.105/celsius 24
critical: not good!
via MQTT to mobile
mqtt.org 
@mqttorg

OSMC 2014: MQTT for monitoring (and for the lo t) | Jan-Piet Mens

  • 1.
    MQTT for monitoring(and for the IoT) Jan-Piet Mens November 2014 @jpmens
  • 2.
    MQTT is astandard, a transport, PUB/SUB messaging, designed for unreliable networks
  • 3.
    transport protocol binarypayload, 256MB, (+2 bytes), fast, lightweight, ideal for low-bandwith, high-latency networks
  • 4.
    security TLS authentication ACLs TLS-PSK (payload encryption)
  • 5.
    Quality of Service 0 At most once 1 Assured delivery 2 Once only
  • 6.
    more features keepalive last will & testament, decoupled senders/recipients, durable messages
  • 7.
    topic names UTF-8,hierarchical, wildcards temperature/room/living devices/# finance/+/eur/rate
  • 8.
  • 9.
    MQTT brokers theserver bit of MQTT
  • 10.
    Mosquitto C, fast,lightweight, ACLs (plugin), TLS, TLS-PSK, bridge, Websockets, logging via $SYS http://mosquitto.org
  • 11.
    HiveMQ Java, plugins,Websockets, clustering, modules http://hivemq.com
  • 12.
    MQTT brokers $SYStopic $SYS/broker/clients/total 1771 $SYS/broker/messages/received 36597465 $SYS/broker/messages/sent 39714120 $SYS/broker/messages/stored 2941 $SYS/broker/bytes/received 2830787008 $SYS/broker/bytes/sent 3810653433 $SYS/broker/version mosquitto version 1.4 $SYS/broker/publish/messages/received 19798673 $SYS/broker/publish/messages/sent 30622855 $SYS/broker/publish/bytes/received 1868229299 $SYS/broker/publish/bytes/sent 3185942282
  • 13.
  • 14.
    CLI utilities mosquitto_sub-v [-h localhost] [-p 1883] [--cafile file] [--cert file --key file] [-u username [-P password]] [ --tls-version tlsv1.2 ] -t 'topic/#' subscribe publish mosquitto_pub ... [-r] -t topic -m payload
  • 15.
    languages Lua, Python,C, JavaScript, Perl, Ruby, Java, ...
  • 16.
    Python API: PUB #!/usr/bin/env python import paho.mqtt.publish as mqtt mqtt.single('conf/hello', 'Hello MQTT') topic payload $ mosquitto_sub -h localhost -v -t 'conf/#' conf/hello Hello MQTT
  • 17.
    Python API: SUB callbacks #!/usr/bin/env python import paho.mqtt.client as paho def on_connect(mosq, userdata, rc): mqttc.subscribe("conf/+", 0) def on_message(mosq, userdata, msg): print "%s %s %s" % (msg.topic, str(msg.payload), userdata) data = { 'type' : 'conference' } mqttc = paho.Client(userdata=data) mqttc.on_connect = on_connect mqttc.on_message = on_message mqttc.connect("localhost", 1883, 60) mqttc.loop_forever()
  • 18.
    Python API: SUB $ mosquitto_pub -t 'conf/thirsty' -m 'Beertime?' $ mosquitto_pub -t 'conf/catering' -m 'Coffee is ready' $ ./sub.py conf/thirsty Beertime? {'type': 'conference'} conf/catering Coffee is ready {'type': 'conference'}
  • 19.
    practical solutions alerting,metering, logging, location awareness, tracking, automation, and controlling, host monitoring
  • 20.
  • 21.
  • 22.
  • 23.
    how does thiswork? arduino/10.0.69.105/celsius 24
  • 24.
  • 25.
  • 26.