官术网_书友最值得收藏!

Subscribing to topics with a command-line tool

A drone is an IoT device that interacts with many sensors and actuators, including digital electronic speed controllers linked to engines, propellers, and servomotors. A drone is also known as an unmanned aerial vehicle (UAV), but we will definitely refer to it as a drone. Let's imagine that we have to monitor many drones. Specifically, we have to display their altitude and the speed for each of their servomotors. Not all of the drones have the same number of engines, propellers, and servomotors. We have to monitor the following types of drone:

Each drone will publish its altitude every 2 seconds to the following topic: sensors/dronename/altitude, where dronename must be replaced by the name assigned to each drone. For example, the drone named octocopter01 will publish its altitude values to the sensors/octocopter01/altitude topic and the drone named quadcopter20 will use the sensors/quadcopter20/altitude topic.

In addition, each drone will publish the speed for each of its rotors every 2 seconds to the following topic: sensors/dronename/speed/rotor/rotornumber, where dronename must be replaced by the name assigned to each drone and rotornumber must be replaced by the rotor number for which the speed is going to be published. For example, the drone named octocopter01 will publish its speed values for its rotor number 1 to the sensors/octocopter01/speed/rotor/1 topic.

We will use the mosquitto_sub command-line utility included in Mosquitto to generate a simple MQTT client that subscribes to a topic and prints all the messages it receives. Open a Terminal in macOS or Linux, or a Command Prompt in Windows, go to the directory in which Mosquitto is installed, and run the following command:

mosquitto_sub -V mqttv311 -t sensors/octocopter01/altitude -d

If you want to work with Windows PowerShell instead of the Command Prompt, you will have to add .\ as a prefix to mosquitto_sub.

The previous command will create an MQTT client that will establish a connection with the local MQTT server and then will make the client subscribe to the topic specified after the -t option: sensors/octocopter01/altitude. We specify the version of the MQTT protocol that we want to use when the client establishes the connection with -V mqttv311. This way, we indicate to the MQTT server that we want to use MQTT version 3.11. We specify the -d option to enable debug messages that will allow us to understand what happens under the hood. We will analyze additional options for connection and subscription later.

The Terminal or Command Prompt window will display debug messages similar to the following lines. Take into account that the generated ClientId will be different from the one shown after Client mosqsub|17040-LAPTOP-5D:

Client mosqsub|17040-LAPTOP-5D sending CONNECT
Client mosqsub|17040-LAPTOP-5D received CONNACK
Client mosqsub|17040-LAPTOP-5D sending SUBSCRIBE (Mid: 1, Topic: sensors/octocopter01/altitude, QoS: 0)
Client mosqsub|17040-LAPTOP-5D received SUBACK
Subscribed (mid: 1): 0

The Terminal or Command Prompt window will display messages published to the topic to which we subscribed as they arrive from the MQTT server to the MQTT client. Keep the window open. You will see that that the client sends PINGREQ packets to the MQTT server and receives PINQRESP packets from the MQTT server. The following lines show examples of the messages displayed for these packets:

Client mosqsub|17040-LAPTOP-5D sending PINGREQ
Client mosqsub|17040-LAPTOP-5D received PINGRESP
主站蜘蛛池模板: 扎赉特旗| 那坡县| 武安市| 财经| 闽侯县| 绿春县| 苏州市| 汶川县| 册亨县| 门头沟区| 庆阳市| 德兴市| 天水市| 甘肃省| 封丘县| 甘肃省| 西城区| 休宁县| 沐川县| 定西市| 吐鲁番市| 卢龙县| 淳化县| 彩票| 出国| 永新县| 宣威市| 凌云县| 靖江市| 清涧县| 托里县| 揭东县| 中方县| 柳江县| 商丘市| 新巴尔虎左旗| 花莲县| 禄丰县| 安达市| 白水县| 河北区|