Mosquitto

An Open Source MQTT v3.1/v3.1.1 Broker

Pachube (now Cosm) has recently announced beta support for publishing and receiving data to their service using MQTT. This is great news and something I know that a lot of people have been hoping for. Well done Pachube!

Command Line Examples

Publishing Data

In this example we’re connecting to host api.xively.com, using our xively api-key as the username, publishing to feed /v2/feeds/504 using the csv format and are updating datastream 0 with the value 29. Another way to achieve the same thing would be to do:

The “-l” option reads messages from stdin, sending a separate message for each line. This means that our imaginary executable sensor_read that is reading data from a sensor must be printing each reading as a text line.

Retrieving Data

In this example, mosquitto_sub will print a text line containing the csv data for datastream 0 of feed 504 every time it is updated.

Last Will and Testament

The last will and testament or just “will” is a very nice feature of MQTT. When your client connects to the MQTT broker/server, it can give the broker this will, which consists of a topic and a message. If the client is disconnected from the broker unexpectedly, that is to say without sending a disconnect message, then the broker publishes the will message on the will topic.

This provides a very simple mechanism for client connection monitoring. When your client connects it could publish a message “1” to a topic. If it also set a will to send a message “0” to the same topic on unexpected disconnect, then it would be possible to determine whether that client was connected by monitoring the topic.

In the context of Xively, the same approach is possible, but using a trigger to indicate that the client had disconnected.

The mosquitto_sub client provides support for wills as shown in the example below:

In this example, the Xively broker would publish the value “0” to datastream 0 of feed 12345 if mosquitto_sub disconnects unexpectedly. This isn’t the most useful example because of the limitations of what mosquitto_sub provides.

Writing Your Own Clients

In practice, to get the full benefit of the advantages that MQTT provides you will probably want to write your own MQTT client to connect to Xively for your specific application. The mqtt.org software page lists client implementations for lots of different programming languages including the mosquitto client libraries in C/C++, libraries in Java, Python and also device specific implementations for Arduino and other low power devices.

MQTT Beyond Xively

The Xively offering is a slightly restricted MQTT offering. “Full” MQTT offers a bit more scope for doing fun things using topic wildcards for example, something that wouldn’t really make sense for Xively.

As such alot of the above will stop working if not already, someone smart needs to update this article or write a new one ;0

Roger2013/06/06

It shouldn’t matter too much, they’ve been good about keeping the old names around and working. I’ve updated the page anyway. Thanks for the note!

yuvraj2015/11/30

on basic level…i have installed mqtt,mosquito on my rpi….now second step i did was i did a simple led experiment(led glowing),now at third step i want to send the led data that means when its glowing data sent or displayed is on and when off data displayed on the terminal is switched off)…now this is the thing i want to know how i can get this data update through mqtt broker and get it displayed