Using the MQTT IoT protocol for unusual but useful purposes

Thing Minder

For Programmers

The mosquitto_* utilities are essential for tests or for embedding in shell scripts. MQTT APIs exist for many languages, from Java and JavaScript, to C, C#, Objective-C, Lua, Perl, and Python. The subscriber written in Python initially arranges a callback to be invoked when a message arrives (Listing 3).

Listing 3

Subscriber in Python

01 #!/usr/bin/env python
02
03 import paho.mqtt.client as paho   # pip install paho-mqtt
04
05 def on_message(mosq, userdata, msg):
06     print "%s (qos=%s, r=%s) %s" % (msg.topic, str(msg.qos), msg.retain, str(msg.payload))
07
08 mqtt = paho.Client()
09 mqtt.on_message = on_message
10
11 mqtt.connect("localhost", 1883, 60)
12 mqtt.subscribe("linux/+", 0)
13
14 mqtt.loop_forever()

The client now connects to the broker and goes into a continuous loop in a controlled manner. If a message arrives from the broker with the right topic, the callback is executed, which outputs the message and stores it in a database.

Anyone wanting to monitor whether the client is still alive can insert a last will and testament into the script from Listing 3:

# Arrange LWT
mqtt.will_set('alerts/attention', payload="Died: %s" % sys.argv[0], qos=0, retain=False)

The user now receives information as soon as the client terminates (with Ctrl+C).

The project mqttwarn [4] provides a Python tool with many plugins for such monitoring (Figure 3). Mqttwarn links topics that it follows with services it invokes. The whole thing is controlled using a configuration file, as shown in Listing 4.

Figure 3: As per the last will and testament, Mqttwarn sends a warning via pushover.

Listing 4

Mqttwarn Configuration

01 [defaults]
02 launch   = log, pushover
03
04 [config:log]
05 targets = {
06     'info'   : [ 'info' ],
07   }
08
09 [config:pushover]
10 targets = {
11     'linux'  : ['ml1234567890..', 'buzQx...'],
12   }
13
14 [alerts/attention]
15 targets = pushover:linux, log:info
16 title: Linux Magazine
17 format = {payload}

WebSockets

Modern web browsers can use MQTT with the help of WebSockets. The Paho project [5] provides a JavaScript module for MQTT. All brokers have WebSockets support on freely configurable ports. Note that communication via WebSockets does not run via the standard MQTT port 1883.

The need to perform HTTP polling and refreshes is eliminated when you have WebSockets in the browser: The browser receives messages as soon as MQTT clients publish them via the broker. This process provides interactive applications: With just a bit of skill in HTML and JavaScript, you can develop interesting monitoring panels, such as a panel for representing sensor or position data for mobile devices in real time.

In Real Life

MQTT is used outside of the IoT in various products. An MQTT plugin was recently developed for the statistics daemon Collectd [6] and for the logging system Graylog [7]. The open source project OwnTracks [8] supplies iOS and Android clients that provide positioning data to the MQTT broker via MQTT; friends and family members can find each other (see Figure 4).

Figure 4: Owntracks provides position information on your acquaintances via MQTT.

Anyone who deals with home automation and openHAB can also benefit from MQTT [9]: You'll discover a variety of techniques for controlling openHAB using MQTT or obtaining openHAB status information via MQTT. MQTT is also in use with all the major companies: Facebook used MQTT for its messenger service due to the lower latency [10].

More Info from the 10th OSMC

There will be more information on this and many other exciting topics in the field of monitoring from the Open Source Monitoring Conference, which took place from November 16 to 19 and was hosted by the company Netways for the 10th time in Nuremberg. The archives will be available online: https://www.netways.de/en/events_trainings/osmc/

The Author

Jan-Piet Mens is an independent Unix/Linux consultant, system administrator and author who has primarily worked on infrastructure issues since 1985. One of his specialties is DNS, which he has written a book about. He also launched the MQTT-based, open source project OwnTracks.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=