文档结构  
翻译进度:18%     翻译赏金:0 元 (?)    ¥ 我要打赏

目前,MQTT是物联网部署中使用最广泛的协议之一。它利用轻量级的发布/订阅消息传递模型,该模型对于机器对机器(M2M)连接是最佳的,特别是在低网络带宽和/或有限资源的情况下。因此,任何MQTT基础架构的基础都由一个中央消息传递代理(服务器)来表示,旨在管理连接到实例(客户端)的通知交换。

在本文中,我们将考虑Jelastic的Eclipse Mosquitto一键安装包,它允许您在一分钟内设置这样的MQTT代理。该解决方案旨在以最少的RAM使用量处理大量同步客户端,同样适用于全功率机器和简单传感器或执行器的管理。因此,让我们来研究如何在云中快速获得个人Mosquitto消息代理,并在其帮助下全面控制物联网网络中的所有设备。

第 1 段(可获 1.91 积分)

Eclipse Mosquitto 封装细节

Eclipse Mosquitto package 软件包旨在通过单击即可设置一个准备就绪的Mosquitto MQTT消息代理。 安装后,将创建具有以下拓扑特性的环境:

  • 包含一个带有部署的 eclipse-mosquitto 图像的单个Docker容器(最新标签)
  • Eclipse Mosquitto容器的默认资源扩展限制设置为8个动态云端,允许使用高达1 GiB的RAM和3.2 GHz的CPU(如果需要,可以随时通过环境拓扑向导手动调整这些限制)
  • 专用端点会自动连接到代理节点,从而可以从互联网上的任何位置访问它(但对于生产用途,我们建议在安装后为此实例分配公用IP地址)
第 2 段(可获 1.64 积分)

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

第 3 段(可获 1.85 积分)

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:
第 4 段(可获 1.15 积分)

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.

第 5 段(可获 1.05 积分)

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).

第 6 段(可获 1.54 积分)

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
第 7 段(可获 1.4 积分)

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
第 8 段(可获 1.25 积分)

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.

第 9 段(可获 1.15 积分)


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
第 10 段(可获 0.89 积分)

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.

第 11 段(可获 0.56 积分)

文章评论