树莓派做mqtt服务器(三部曲之一)在树莓派上安装mosquitto mqtt服务器

 

网上有很多安装mosquitto mqtt 服务器的文章,但由于树梅派版本和软件时效性的原因,往往会发生各种问题。

今天找个比较简洁,步骤清晰的文章。加上我的注解(汉字部分)并做了一些简化,推荐。

转载:https://www.switchdoc.com/2018/02/tutorial-installing-and-testing-mosquitto-mqtt-on-raspberry-pi/

https://learn.adafruit.com/diy-esp8266-home-security-with-lua-and-mqtt/configuring-mqtt-on-the-raspberry-pi

Installing the MQTT  “mosquitto”

Unfortunately, the Raspberry Pi normal “apt-get” archives do not contain the latest version of the Mosquitto software.  If you don’t install the latest version of the broker, you will get odd errors (because of version compatibility errors) and it will not work.  So, the first thing is to open a terminal window (or log in using ssh) to your Raspberry Pi and do the following:

考虑到原文写作时间,实际上我只做了后两行:

 
sudo wget https://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo apt-get update
sudo apt-get install mosquitto
 

Next we can install the three parts of Mosquitto proper.

  • mosquitto – the MQTT broker (or in other words, a server) //这应该是前面做过的。
  • mosquitto-clients – command line clients, very useful in debugging  //这是要安装的客户端程序,对数据的pub和sub是通过客户端完成的,否则系统没有相关的sub与pub命令:

    sudo apt-get install mosquitto mosquitto-clients
  • paho-mqtt – the Python language bindings  //这个我没有安装,以后用python时再说。

 

If you’re going to use MQTT in a Python project, you’ll have to install paho-mqtt, which replaces the old Mosquitto Python module.  //以下三行我没有安装,以后用python时再说

sudo apt-get install mosquitto mosquitto-clients
sudo apt-get install python-pip
sudo pip install paho-mqtt

As is the case with most packages from Debian, the broker is immediately started.  Since we have to configure it first, stop it.

sudo /etc/init.d/mosquitto stop

Configuring and Starting the Mosquitto Server

Before using Mosquitto, we need to set up the configuration file. The configuration files is located at /etc/mosquitto.

Open the file as follows:
sudo nano /etc/mosquitto/mosquitto.conf

向其中加入黑体字表示的内容:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
 
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
 
log_dest topic
 
log_type error
log_type warning
log_type notice
log_type information
 
connection_messages true
log_timestamp true
 
include_dir /etc/mosquitto/conf.d

Starting the Mosquitto Server

Now start the mosquitto server:
sudo /etc/init.d/mosquitto start

Testing the Mosquitto server

Open up two more terminal windows.

In Terminal window 1 type:
mosquitto_sub -d -t hello/world

n Terminal window 2 type: mosquitto_pub -d -t hello/world -m "Hello from Terminal window 2!"

When you have done the second statement you should see this in the Terminal 1 window.~ $ sudo mosquitto_sub -d -t hello/world

Client mosqsub/3014-LightSwarm sending CONNECT
Client mosqsub/3014-LightSwarm received CONNACK
Client mosqsub/3014-LightSwarm sending SUBSCRIBE (Mid: 1, Topic: hello/world, QoS: 0)
Client mosqsub/3014-LightSwarm received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/3014-LightSwarm received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (32 bytes))
Greetings from Terminal window 2

Now you are running the Mosquitto broker successfully.

按上述操作后(有省略),成功。
测试结果:
sudo mosquitto_sub -d -t hello/world
其一,需要先执行上述sub,否则pub发生连接错误:
mosquitto_pub -d -t hello/world -m "Hello from Terminal window 2!"
Client mosqpub|4147-raspberryp sending CONNECT
Client mosqpub|4147-raspberryp received CONNACK (0)
Client mosqpub|4147-raspberryp sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (29 bytes))
Client mosqpub|4147-raspberryp sending DISCONNECT
这一点和我的想象不一样,我原来理解为如果没有订阅,pub发出的信息会在队列中等待。
sub执行后,如果没有pub发出信息,它会耐心等待,并在等待时,每隔一小段时间就会向
服务器发出请求,并且得到服务器的响应,并显示:
Client mosqsub|4421-raspberryp sending PINGREQ
Client mosqsub|4421-raspberryp received PINGRESP
 
第二,测试了从一个窗口发出pub,多个窗口可以收到信息。

你可能感兴趣的:(树莓派做mqtt服务器(三部曲之一)在树莓派上安装mosquitto mqtt服务器)