【1】编译找不到openssl/ssl.h
【解决方法】——安装openssl
sudo apt-get install libssl-dev
【2】编译过程找不到ares.h
【解决方法】——修改config.mk中的WITH_SRV:=yes,改为WITH_SRV:=yes
【3】使用过程中找不到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
【4】make: g++:命令未找到
【解决方法】
安装g++编译器
sudo apt-get install g++
2 简单的例子
设计一个简单的测试案例,在PC机上运行MQTT代理服务器,而树莓派订阅主题为gpio的消息,PC机发布同主题消息,消息的内容为JSON数据包,数据包格式为{“index”:17,“value”:0},index代表树莓派GPIO的编号,value代表打开或关闭状态。
图1 示例
本例中PC机IP地址为 192.168.1.110,树莓派的IP地址为192.168.1.106
2.1 在PC机中开启MQTT服务
mosquitto -v
2.2 在树莓派中订阅消息
mosquitto_sub -v -t gpio -h 192.168.1.110
【-h】指定MQTT代理服务器主机,指向PC机IP地址192.168.1.110
2.3 在PC机中发布消息
mosquitto_pub -t gpio -h 192.168.1.110 -m "{\"pin\":17,\"value\":0}"
【-h】指定MQTT代理服务器主机,指向PC机IP地址192.168.1.110
2.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
3 使用MQTT远程控制GPIO
下面借助python-gpio扩展库,通过消息推送的方式实现GPIO端口的远程控制。
3.1 安装paho-mqtt
使用pip工具安装paho-mqtt,输入以下指令即可:
sudo pip install paho-mqtt
3.2 树莓派订阅代码——simple.py
-
-
import paho.mqtt.client as mqtt
-
import RPi.GPIO as GPIO
-
import json
-
-
-
pins = [17,18,27,22,23,24,25,4]
-
def gpio_setup():
-
-
GPIO.setmode(GPIO.BCM)
-
-
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))
-
-
client.subscribe("gpio")
-
-
-
def on_message(client, userdata, msg):
-
print(msg.topic+" "+str(msg.payload))
-
-
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:
-
-
client.connect("192.168.1.110", 1883, 60)
-
client.loop_forever()
-
except KeyboardInterrupt:
-
client.disconnect()
-
gpio_destroy()
3.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机推送的消息点亮或熄灭。
4 总结
本文说明了如何在树莓派中使用MQTT客户端,通过paho-mqtt实现GPIO的远程控制。本例在局域网中运行并能不能体现出MQTT协议在远程控制中的优越性。后期还将花更多的时间实践和分析MQTT协议。