android - Raspberry Pi 作为 MQTT 代理以及订阅者或发布者
问题描述
场景:我有一个带有一些连接设备的本地网络,所以我想将命令从 android 应用程序发送到 Raspberry Pi,它将通过 MQTT 协议控制这些连接的设备,所以我可以使用 Raspberry Pi 作为代理以及客户端(发布者/订阅者) .
解决方案
是的。您可以在同一个 Raspberry Pi 上运行代理和客户端。
import time
import paho.mqtt.client as paho
#broker="broker.hivemq.com"
broker="iot.eclipse.org"
#define callback
def on_message(client, userdata, message):
time.sleep(1)
print("received message =",str(message.payload.decode("utf-8")))
client= paho.Client("client-001") #create client object
#client1.on_publish = on_publish #assign function to callback
#client1.connect(broker,port) #establish connection
#client1.publish("house/bulb1","on")
######Bind function to callback
client.on_message=on_message
#####
print("connecting to broker ",broker)
client.connect(broker)#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("house/bulb1")#subscribe
time.sleep(2)
print("publishing ")
client.publish("house/bulb1","on")#publish
time.sleep(4)
client.disconnect() #disconnect
client.loop_stop() #stop loop
http://www.steves-internet-guide.com/into-mqtt-python-client/
http://www.steves-internet-guide.com/python-mqtt-publish-subscribe/
以上链接可以指导您解决问题。
推荐阅读
- javascript - 下拉菜单中 SVG 的颜色变化
- python - 如何在第三维的中间创建一个全零和一个 1 的 PyTorch 张量?
- java - NullPointerException:尝试调用虚拟方法 'void android.widget.ListView.setAdapter 无法修复
- php - 如何将 PHP cURL 连接拆分为文件?
- java - Microsoft.WindowsAzure.Storage:未找到有效的帐户信息组合 - 尝试编辑 conn 字符串,但没有成功
- javascript - Javascript 检查是否
存在于
- blazor - 如何在 Blazor 中调用子组件的方法
- javascript - Javascript打字机片段使浏览器没有响应
- c# - 覆盖继承属性的默认值的正确方法是什么?
- reactjs - 使用 React(dropzone 和 Formik)进行弹簧休息