树莓派 - MQTT
安装mosquitto
下载源代码包
wget http://mosquitto.org/files/source/mosquitto-1.5.tar.gz
解压
tar zxfv mosquitto-1.5.tar.gz
进入目录
cd mosquitto-1.5
编译
make
安装
sudo make install
安装问题
handle_connect.c:33:25: fatal error: uuid/uuid.h: No such file or directory
# include <uuid/uuid.h>
解决:
pi@raspberrypi:~/mosquitto-1.5 $ sudo apt-get install uuid-dev
找不到openssl/ssl.h
解决:sudo apt-get install libssl-dev
error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解决:
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
sudo ldconfig
在linux 虚拟机上也安装mosquitto, sudo apt install mosquitto
测试
在raspberryPi上运行mosquitto -v
pi@raspberrypi:~ $ mosquitto -v
1533737054: mosquitto version 1.5 starting
1533737054: Using default config.
1533737054: Opening ipv4 listen socket on port 1883.
1533737054: Opening ipv6 listen socket on port 1883.
1533737096: New connection from 192.168.0.197 on port 1883.
1533737096: New client connected from 192.168.0.197 as mosqpub|9828-ubuntu (c1, k60).
1533737096: No will message specified.
1533737096: Sending CONNACK to mosqpub|9828-ubuntu (0, 0)
1533737096: Received PUBLISH from mosqpub|9828-ubuntu (d0, q0, r0, m0, 'gpio', ... (20 bytes))
1533737096: Received DISCONNECT from mosqpub|9828-ubuntu
1533737096: Client mosqpub|9828-ubuntu disconnected.
1533737126: New connection from 192.168.0.197 on port 1883.
1533737126: New client connected from 192.168.0.197 as mosqsub|9829-ubuntu (c1, k60).
1533737126: No will message specified.
1533737126: Sending CONNACK to mosqsub|9829-ubuntu (0, 0)
1533737126: Received SUBSCRIBE from mosqsub|9829-ubuntu
1533737126: gpio (QoS 0)
1533737126: mosqsub|9829-ubuntu 0 gpio
1533737126: Sending SUBACK to mosqsub|9829-ubuntu
1533737170: New connection from 192.168.0.197 on port 1883.
1533737170: New client connected from 192.168.0.197 as mosqpub|9832-ubuntu (c1, k60).
1533737170: No will message specified.
1533737170: Sending CONNACK to mosqpub|9832-ubuntu (0, 0)
1533737170: Received PUBLISH from mosqpub|9832-ubuntu (d0, q0, r0, m0, 'gpio', ... (20 bytes))
1533737170: Sending PUBLISH to mosqsub|9829-ubuntu (d0, q0, r0, m0, 'gpio', ... (20 bytes))
1533737170: Received DISCONNECT from mosqpub|9832-ubuntu
1533737170: Client mosqpub|9832-ubuntu disconnected.
1533737186: Received PINGREQ from mosqsub|9829-ubuntu
1533737186: Sending PINGRESP to mosqsub|9829-ubuntu
1533737228: New connection from 192.168.0.197 on port 1883.
1533737228: New client connected from 192.168.0.197 as mosqpub|9835-ubuntu (c1, k60).
1533737228: No will message specified.
1533737228: Sending CONNACK to mosqpub|9835-ubuntu (0, 0)
1533737228: Received PUBLISH from mosqpub|9835-ubuntu (d0, q0, r0, m0, 'gpio', ... (20 bytes))
1533737228: Sending PUBLISH to mosqsub|9829-ubuntu (d0, q0, r0, m0, 'gpio', ... (20 bytes))
1533737228: Received DISCONNECT from mosqpub|9835-ubuntu
1533737228: Client mosqpub|9835-ubuntu disconnected.
1533737235: New connection from 192.168.0.197 on port 1883.
1533737235: New client connected from 192.168.0.197 as mosqpub|9836-ubuntu (c1, k60).
1533737235: No will message specified.
1533737235: Sending CONNACK to mosqpub|9836-ubuntu (0, 0)
1533737235: Received PUBLISH from mosqpub|9836-ubuntu (d0, q0, r0, m0, 'gpio', ... (20 bytes))
1533737235: Sending PUBLISH to mosqsub|9829-ubuntu (d0, q0, r0, m0, 'gpio', ... (20 bytes))
1533737235: Received DISCONNECT from mosqpub|9836-ubuntu
1533737235: Client mosqpub|9836-ubuntu disconnected.
1533737246: Received PINGREQ from mosqsub|9829-ubuntu
1533737246: Sending PINGRESP to mosqsub|9829-ubuntu
在PC机上分别开两个终端运行
mosquitto_sub -v -t gpio -h 192.168.0.135
mosquitto_pub -t gpio -h 192.168.0.135 -m "{\"pin\":17,\"value\":0}"
树莓派的IP地址为192.168.0.135.
发布者发布数据:
~$ mosquitto_pub -t gpio -h 192.168.0.197 -m "{\"pin\":17,\"value\":0}"
~$ mosquitto_pub -t gpio -h 192.168.0.135 -m "{\"pin\":17,\"value\":0}"
~$ mosquitto_pub -t gpio -h 192.168.0.135 -m "{\"pin\":17,\"value\":0}"
订阅者接收到数据:
~$ mosquitto_sub -v -t gpio -h 192.168.0.135
gpio {"pin":17,"value":0}
gpio {"pin":17,"value":0}
gpio {"pin":17,"value":0}
#
Reference
https://github.com/eclipse/mosquitto
https://blog.csdn.net/xukai871105/article/details/39255089