Top 5 Application Examples for Mongoose (Excerpt: MQTT)
Want to get started with Mongoose, but need a little help? In this post, we'll provide an example covering how to use Mongoose with MQTT.
Join the DZone community and get the full member experience.Join For Free
Mongoose is known as one of the best embedded web servers currently available. From starting as a single-click-to-run executable to now being an embedded networking library, its functions range from WebSocket to MQTT and more.
To make sure you get off to an easy start with Mongoose, we have examples for almost all of its functionality. You can easily follow these, amend them, and use them for your integration project.
In this ebook, you'll find the top 5 application examples with full code outlines and additional reference.
Learn how to set up:
- WebSocket Chat
- RESTful Server
- Big Upload
- MQTT Broker and Client
- Captive DNS Server
Find the code and reference compiled in one handy ebook: Top 5 Mongoose Embedded Web Server Examples.
Now for the sample excerpt...
MQTT Broker and Client
Here, we’d like to show you how you can use Mongoose to add one of the most popular M2M protocols to your product: MQTT.
We’re not going to spend much time singing the praises of MQTT, as you can find much 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 phonebook, you know, a lot of characters but a lousy plot; no, the MQTTspec 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 "/stuff" topic to the broker.
Enough jibber jabber, let’s see this in action!
While we are at it, let’s also use an 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 Mongoosemqtt_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_clientprocess 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?
Go ahead and download the full e-book: Top 5 Mongoose Embedded Web Server Examples.
Published at DZone with permission of , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.