树莓派上安装 Mosquitto 实现 MQTT
1 在树莓派上安装Mosquitto
在树莓派上安装Mosquitto和其他平台类似,如果在树莓派平台直接编译时间可能会稍长些。Mosquitto源代包不大,所以编译时间尚可忍受。
2 安装
截止2014年9月,最新版本为mosquitto-1.3.4。在树莓派中新建一个目录,例如software
# 下载源代码包
wget http://mosquitto.org/files/source/mosquitto-1.3.4.tar.gz
# 解压
tar zxfv mosquitto-1.3.4.tar.gz
# 进入目录
cd mosquitto-1.3.4
# 编译
make
# 安装
sudo make install
2.2 安装注意点
编译找不到openssl/ssl.h
解决方法:安装openssl
sudo apt-get install libssl-dev
编译过程找不到ares.h
解决方法:修改config.mk中的WITH_SRV:=yes,改为WITH_SRV:=yes
使用过程中找不到libmosquitto.so.1
error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解决方法:修改libmosquitto.so位置
# 创建链接
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
# 更新动态链接库
sudo ldconfig
make: g++:命令未找到
解决方法:安装g++编译器
sudo apt-get install g++
3 简单的例子
设计一个简单的测试案例,在PC机上运行MQTT代理服务器,而树莓派订阅主题为gpio的消息,PC机发布同主题消息,消息的内容为JSON数据包,数据包格式为{“index”:17,“value”:0},index代表树莓派GPIO的编号,value代表打开或关闭状态。
image.png
本例中PC机IP地址为 192.168.1.110,树莓派的IP地址为192.168.1.106
3.1在PC机中开启MQTT服务
mosquitto -v
3.2 在树莓派中订阅消息
mosquitto_sub -v -t gpio -h 192.168.1.110
3.3 在PC机中发布消息
mosquitto_pub -t gpio -h 192.168.1.110 -m "{\"pin\":17,\"value\":0}"
#-h指定MQTT代理服务器主机,指向PC机IP地址192.168.1.110
3.4 消息被推送到树莓派中
最后在树莓派中输出以下内容:
gpio {"index":17,"value":0}
在PC机MQTT服务器控制台中输出
1410600001: mosquitto version 1.3.4 (build date 2014-09-13 15:55:06+0800) starting
1410600001: Using default config.
1410600001: Opening ipv4 listen socket on port 1883.
1410600001: Opening ipv6 listen socket on port 1883.
1410600062: New connection from 192.168.1.106 on port 1883.
1410600062: New client connected from 192.168.1.106 as mosqsub/3063-raspberryp (c1, k60).
1410600062: Sending CONNACK to mosqsub/3063-raspberryp (0)
1410600062: Received SUBSCRIBE from mosqsub/3063-raspberryp
1410600062: gpio (QoS 0)
1410600062: mosqsub/3063-raspberryp 0 gpio
1410600062: Sending SUBACK to mosqsub/3063-raspberryp
1410600122: Received PINGREQ from mosqsub/3063-raspberryp
1410600122: Sending PINGRESP to mosqsub/3063-raspberryp
1410600152: New connection from 192.168.1.110 on port 1883.
1410600152: New client connected from 192.168.1.110 as mosqpub/9793-EasyARM (c1, k60).
1410600152: Sending CONNACK to mosqpub/9793-EasyARM (0)
1410600152: Received PUBLISH from mosqpub/9793-EasyARM (d0, q0, r0, m0, 'gpio', ... (22 bytes))
1410600152: Sending PUBLISH to mosqsub/3063-raspberryp (d0, q0, r0, m0, 'gpio', ... (22 bytes))
1410600152: Received DISCONNECT from mosqpub/9793-EasyARM
1410600182: Received PINGREQ from mosqsub/3063-raspberryp
1410600182: Sending PINGRESP to mosqsub/3063-raspberryp
4使用MQTT远程控制GPIO
下面借助python-gpio扩展库,通过消息推送的方式实现GPIO端口的远程控制。
4.1 安装paho-mqtt
使用pip工具安装paho-mqtt,输入以下指令即可
sudo pip install paho-mqtt
4.2 树莓派订阅代码 simple.py
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import json
# BCM GPIO编号
pins = [17,18,27,22,23,24,25,4]
def gpio_setup():
# 采用BCM编号
GPIO.setmode(GPIO.BCM)
# 设置所有GPIO为输出状态,且输出低电平
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
def gpio_destroy():
for pin in pins:
GPIO.output(pin, GPIO.LOW)
GPIO.setup(pin, GPIO.IN)
# 连接成功回调函数
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# 连接完成之后订阅gpio主题
client.subscribe("gpio")
# 消息推送回调函数
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
# 获得负载中的pin 和 value
gpio = json.loads(str(msg.payload))
if gpio['pin'] in pins:
if gpio['value'] == 0:
GPIO.output(gpio['pin'], GPIO.LOW)
else:
GPIO.output(gpio['pin'], GPIO.HIGH)
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
gpio_setup()
try:
# 请根据实际情况改变MQTT代理服务器的IP地址
client.connect("192.168.1.110", 1883, 60)
client.loop_forever()
except KeyboardInterrupt:
client.disconnect()
gpio_destroy()
4.3 启动服务并发布消息
在PC机中启动MQTT代理服务
mosquitto -v
在树莓派中运行脚本
sudo python simple.py
在PC机中发布消息
# 打开GPIO17
mosquitto_pub -h 192.168.1.110 -t gpio -m "{\"pin\":17,\"value\":1}"
# 关闭GPIO17
mosquitto_pub -h 192.168.1.110 -t gpio -m "{\"pin\":17,\"value\":0}"
运行结果
树莓派GPIO17根据PC机推送的消息点亮或熄灭。