作者:xi曦 | 来源:互联网 | 2023-08-06 11:36
转载:https:www.switchdoc.com201802tutorial-installing-and-testing-mosquitto-mqtt-on-r
转载: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
- paho-mqtt – the Python language bindings
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.
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
You should see the following:
# 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.examplepid_file /var/run/mosquitto.pidpersistence true
persistence_location /var/lib/mosquitto/log_dest file /var/log/mosquitto/mosquitto.loginclude_dir /etc/mosquitto/conf.dChange the “log_dest” line to:log_dest topic
This puts the logging information as a “topic” so we can subscribe to it later on to see what is going on in our IOTRFID system.
Next add the next six lines:
log_type error
log_type warning
log_type notice
log_type informationconnection_messages true
log_timestamp true
Now your /etc/mosquitto.conf files should look like:
# 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.examplepid_file /var/run/mosquitto.pidpersistence true
persistence_location /var/lib/mosquitto/log_dest topiclog_type error
log_type warning
log_type notice
log_type informationconnection_messages true
log_timestamp trueinclude_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
In 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.