Eclipse Mosquitto MQTT Server in the Cloud: How to Build a Personal IoT Network

By | September 20, 2017
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

eclipse mosquitto mqtt serverNowadays, MQTT is one of the most widely used protocols for IoT (Internet of Things) deployments. It utilizes a lightweight publish/subscribe messaging model, which is optimal for machine-to-machine (M2M) connectivity, especially in conditions of a low network bandwidth and/or limited resources capacities. Herewith, the base of any MQTT infrastructure is represented with a central messaging broker (server), intended to manage notifications interchange, and connected to it instances (clients).

In this article, we’ll consider the Eclipse Mosquitto one-click installation package by Jelastic, which allows to set up such MQTT broker in just a minute. Being designed to handle a big number of simultaneous clients with minimal RAM usage, this solution is equally suitable for administration of both full-powered machines and simple sensors or actuators. So, let’s investigate how to quickly get your personal Mosquitto message broker inside a Cloud and to take the full control over all devices in your IoT network with its help.

Eclipse Mosquitto Package Specifics

The Eclipse Mosquitto package is intended to set up a ready-to-work Mosquitto MQTT message broker in one click. Upon its installation, an environment with the following topology specifics is created:

  • contains a single Docker container with deployed eclipse-mosquitto image (latest tag)
  • the default resource scaling limit for Eclipse Mosquitto container is set to 8 dynamic cloudlets, allowing to use up to 1 GiB of RAM and 3.2 GHz of CPU (these confines can be manually adjusted any time through the environment topology wizard, if needed)
  • a dedicated endpoint is automatically attached to a broker node, making it possible to access it from anywhere over the Internet (however, for production use, we recommend to assign Public IP address to this instance after the installation)

How the Device Interaction via MQTT Protocol Works

The MQTT protocol principles are somewhat similar to the client-server model, since it’s operating with a single central broker and multiple clients, connected to it. Herewith, all interaction is performed through the subscribe/publish messaging. This allows to keep publishers and subscribers (i.e. clients) unaware of each other, as they are never connected directly – through the MQTT server only.build personal iot network with Mosquitto

The general approach is quite simple – any MQTT client enables user to subscribe to a topic and/or publish on it. Herewith, each new topic message passes through a broker to be retranslated only to the clients that are subscribed to this particular thread. This way, inner message interchange between connected devices is performed implicitly, whilst you get all the essential information on the prescribed operations’ execution & device state almost instantly, gaining the full control over your IoT network

How to Deploy Eclipse Mosquitto MQTT Server to the Cloud

With pre-packaged Eclipse Mosquitto solution by Jelastic, all the required actions on server setup and the appropriate software installation are accomplished by the system automatically. The only thing you need to do is to initiate the corresponding package deployment – let’s consider this in more details.

1. Log into your Jelastic account and proceed with one of the following flows:

  • or, import the manifest.jps link from the appropriate repository:

https://github.com/jelastic-jps/mosquitto/blob/master/manifest.jpsimport mosquitto iot githubClick Import to continue.

2. Within the opened frame, customize parameters of the dedicated Eclipse Mosquitto environment that would be created upon this solution installation.mqtt server in cloudHere, the fields should be filled in as follows:

  • Environment – set the name for your environment
  • Display Name – provide an environment alias, if needed (optionally)
  • Region – select the preferable hardware region from the list (if several ones are available)

Click Install and wait for Jelastic to configure everything for you.

3. In a minute, you’ll see a popup frame informing you about the successful package installation.mosquitto mqtt serverThat’s it! Your Eclipse Mosquitto broker is ready for work now. Subsequently, the provisioned domain name and required port can be used to connect clients to this MQTT server.

Operating IoT Devices with Mosquitto Broker

Now, let’s consider a simple example of MQTT messaging through the Eclipse Mosquitto server. To follow the tutorial below, you’ll need several MQTT clients (i.e. devices to constitute your IoT network – we’ve created a set of containers within a separate environment for this purpose) with the mosquitto-clients package being installed at each of them (is required to enable execution of the subscribe and publish commands).

Being truly universal, the MQTT protocol allows to configure monitoring and automation for almost any device – e.g., would you like to have some hot toasts after a hard working day in office? No problem, all you need to do is to interconnect the appropriate devices (we’ll use aliases to designate used containers as Car, Toaster and Mobile phone):mosquitto mqtt exampleThe scenario we’ll implement is as simple as it’s useful, being aimed to save you from the redundant routine actions. Just 3 operations in a chain:

  • Car sends notification when you arrive home
  • Toaster reacts on it by preparing toasts
  • Mobile phone signals when the food is ready

So, let’s see how to accomplish such device interaction.

1. We’ll start with the Mobile phone instance. This one is interested only in a Toaster status, so you just need to subscribe it to the appropriate topic – for that, execute the following command when being connected to the appropriate container via SSH Gate:

mosquitto_sub -t {topic} -h {host} -p {port}mosquitto mqtt clientwhere:

  • {topic} – topic name to subscribe the current instance for (e.g. demo/toast-status)
  • {host} – domain address of the Eclipse Mosquitto environment
  • {port}public port of your endpoint (can be found within the Environment Settings > Endpoints section) for connection from outside of the Jelastic Platform

    mosquitto mqtt broker tutorial

Tip: If working within Platform internal network, the port value can be skipped – the 1883 one will be used by default.

2. To automate tasks execution on the Toaster component, the following simple bash scripts will be used:

  • scripttracks messages in the demo/car-location topic to run the second script

while read msg;
do
bash $msg;
done < <(mosquitto_sub -t demo/car-location -h {host} -p {port})

  • homeexecutes custom commands you define within it (for this example, we just sleep the Toaster for several seconds to imitate running of some operation) and publishes a message to the demo/toast-status topic

echo “Preparing toasts…”
sleep 10s
echo “Done!”
mosquitto_pub -t demo/toast-status -m “Toasts are ready!” -h {host} -p {port}

Now, run the main (first) script with the bash script command:building m2m iot projects3. Everything is set up now, so let’s publish a message on the Car node “channel” and see what will happen:

mosquitto_pub -t demo/car-location -m “home” -h {host} -p {port}

  • Car – message is sentiot machine to machine messaging
  • Toasternotification from the Car instance triggers execution of the appropriate custom scripts; as a result, another message is sent to Mobile Phonebuilding m2m iot projects
  • Mobile phonetoast status message is successfully received, initiating the appropriate alarmeclipse mosquitto message broker

Great! Everything works fine and you can enjoy the just-made hot toasts right after your arrival.

Although that’s just the simplest example of IoT networking implementation through the Eclipse Mosquitto message broker, it’s enough demonstrative about the capabilities and benefits this tool provides. Use it as a base to build your own, more complex IoT solutions within Jelastic Cloud right now!

Need any assistance on Eclipse Mosquitto installation or maintenance? Write us at Stackoverflow and Jelastic technical experts will do their best to help you with the met issues.
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Subscribe to get the latest updates