Eclipse Mosquitto MQTT Server in the Cloud

DZone 's Guide to

Eclipse Mosquitto MQTT Server in the Cloud

See how you can use Eclipse Mosquitto and Jelastic Cloud to create your own IoT system using the MQTT protocol, starting with this simple example of making toast.

· IoT Zone ·
Free Resource

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

In this article, we’ll consider the Eclipse Mosquitto one-click installation package by Jelastic, which allows you to set up such an 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 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 a deployed eclipse-mosquitto image (latest tag)
  • The default resource scaling limit for the Eclipse Mosquitto container is set to 8 dynamic cloudlets, allowing you 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 assigning a Public IP address to this instance after the installation)

How Device Interaction via MQTT Protocol Works

The MQTT protocol's principles are somewhat similar to the client-server model, since it’s operating with a single central broker and multiple clients connected to it. All interaction is performed through the subscribe/publish messaging. This allows us 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 users to subscribe to a topic and/or publish on it. 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 and device state almost instantly, gaining the full control over your IoT network

How to Deploy Eclipse Mosquitto MQTT Server to the Cloud

With the 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.

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

  • find the Eclipse Mosquitto package within Jelastic Marketplace and click on Installeclipse mosquitto installation
  • or, import the manifest.jps link from the appropriate repository:

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

Within the opened frame, customize the 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 With IoT Devices With the 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 (required to enable execution of the subscribe and publish commands).

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

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

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

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

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

  • {topic}: sets the topic name to subscribe (e.g. demo/toast-status).
  • {host}: provides the domain address of the Eclipse Mosquitto environment.
  • {port}: specifies the public port for your endpoint (can be found within the environment settings), which is required upon connecting from outside Jelastic Platform.mosquitto mqtt broker tutorial
Tip: If working within the Platform's internal network, the port value can be skipped – the required 1883 port is used by default and can be omitted.

To automate tasks on a Toaster, we’ll use a couple of the following simple bash scripts:

script – tracks messages in the demo/car-location topic to run the second script

while read msg;

bash $msg;

done <<(mosquitto_sub -t demo/car-location -h {host} -p {port})

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

echo “Preparing toast…”
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 projectsEverything is set up now, so let’s publish a message on the Car node “channel” and see what happens:

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

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

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

Although that’s just the simplest example of IoT networking implementation through the Eclipse Mosquitto message broker, it’s enough to demonstrate the capabilities and benefits this tool provides.

iot ,mqtt ,iot cloud ,mosquitto ,tutorial

Published at DZone with permission of Tetiana Markova , 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 }}