Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Mongoose More Than an Embedded Web Server: MQTT Example

DZone's Guide to

Mongoose More Than an Embedded Web Server: MQTT Example

In case you haven't heard, Mongoose is a robust, lightweight server. Here's how you can add a well-known M2M protocol to Mongoose. Happy IoT!

· IoT Zone
Free Resource

Build an open IoT platform with Red Hat—keep it flexible with open source software.

Continuing on our example journey for “Mongoose more than an Embedded Server”, this week I’d like to show you how you can use Mongoose to add one of the most popular M2M protocols to your product: MQTT. 

I’m not going to spend much time singing the praises of MQTT, as you can find more in-depth material about it online already. Yet, there are couple of things that are worth spelling out before you head out Googling for the merits of MQTT over whatnot:

  • Lightweight: Simple binary protocol, small overhead, built to be friendly to small devices, where there is not much room for big buggers nor sophisticated code.
  • Simple: You can actually read the spec. It’s not like reading a phone book, you know, a lot of characters but a lousy plot; no, the MQTT spec just tells it all, right there.
  • Standard: Born at IBM, now MQTT is an OASIS standard. There are a bunch of different implementations of MQTT brokers and client libraries, with good interoperability.
  • Pub/Sub: The underlying communication model is that of message queues and the publish-subscribe paradigm. Queuing means you can reach your target even if it was offline. Pub-Sub means you don’t need to know whom to send what; whoever is interested in what you have to say will subscribe to the topic.

So let’s dig straight into our example: mqtt_client.c

In order to run the example, you need to run an MQTT broker, the server software that coordinates. There are many open source MQTT brokers out there. Mongoose is a generic library MQTT protocol library and can be used to implement brokers as well.

For convenience we also provide a simple broker implementation in mqtt_broker.c; here’s how you can run it:

$ cd examples/mqtt_broker
$ make
$ ./mqtt_broker

Once you have a broker running, let’s try out our example client:

$ cd examples/mqtt_client
$ make
$ ./mqtt_client
Subscribing to '/stuff'
Subscription acknowledged, forwarding to '/test'

The goal of the example MQTT client app is to subscribe to messages coming from a topic called “/stuff”, and forward each received message to the topic “/test”. In order to achieve this, it performs a TCP connection to the MQTT broker listening on port 1883 and sends a SUBSCRIBE command signaling its interest for the "/suff" topic to the broker.

Enough jibber jabber, let’s see this in action!

While we are at it, let’s also use a MQTT client from another vendor, for example the very well known Mosquitto client, from the Eclipse project. Open a new shell and after having installed mosquitto client, type:

$ mosquitto_pub -t /stuff -m foo

What happens here is that mosquitto_pub connects to the Mongoose mqtt_broker process and sends a PUBLISH command on the topic “/stuff” with the message “foo”. The broker knows that our mqtt_example has subscribed to that topic, and thus forwards that PUBLISH command through the connection established previously between the mqtt_client process and the broker.

The Mongoose mqtt_client example will now receive this message and log the following message:

Got incoming message /stuff: foo
Forwarding to /test

Since nobody subscribed to the “/test” topic, the broker will just swallow this message, unless somebody subscribes to it as well.

If you want to see the forwarded message with your own eyes, why don’t you download Mongoose, clone mqtt_example.c and write a simple toy app that will subscribe to “/test” and print out all it gets there ?

Have fun!

You can find more info in our MQTT reference documentation.

We even have a JavaScript MQTT API for our Mongoose IoT Platform.

DOWNLOAD AND TEST MONGOOSE

References:

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

Topics:
server ,mongoose ,embedded ,iot

Published at DZone with permission of Marko Mikulicic, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}