打算用树莓派控制灯开关、电视、空调,通过siri语音控制,web远程控制等。
待实现。
如果有静态ip最好了,一般家庭宽带都是动态ip,需要路由器设置端口,采用DHCP的方式,借助花生壳等来实现。或者写个脚本定时查询外网ip推送出来。
通过vnc或ssh,待续。。
实现设备与客户端手机通信
http://mosquitto.org/download/
cd /etc/yum.repos.d/
wget http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/home:oojah:mqtt.repo
yum install -y mosquitto mosquitto-clients libmosquitto1 libmosquitto-devel libmosquittopp1 libmosquittopp-devel python-mosquitto
#The available packages are: mosquitto, mosquitto-clients, libmosquitto1, libmosquitto-devel, libmosquittopp1, libmosquittopp-devel, python-mosquitto.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import paho.mqtt.client as mqtt
import wiringpi
# 连接成功回调函数
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# 连接完成之后订阅serial主题
client.subscribe("serial")
# 消息推送回调函数
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
# 将负载通过串口发出
wiringpi.serialPuts(serial,str(msg.payload))
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
wiringpi.wiringPiSetup()
serial = wiringpi.serialOpen('/dev/ttyAMA0',9600)
try:
# 请更改为自己MQTT代理服务器的公网IP
client.connect("127.0.0.1", 1883, 60)
client.loop_forever()
except KeyboardInterrupt:
client.disconnect()
wiringpi.serialClose(serial)
pip install paho-mqtt
pip install wiringpi2
python MQTT.py
安装 MqttClient 测试。
控制灯开关,为了方便改造现有开关,购买86型开关,带学习型315M射频模块的。
更新记录:
参考
[1] MQTT协议应用:外网手机控制内网树莓派
[2] MQTT官网
标签: 树莓派
评论:
发表评论 登录:
2017-12-12 14:04