gpt4 book ai didi

树莓派上的python mqtt脚本发送和接收消息

转载 作者:太空狗 更新时间:2023-10-30 00:48:43 25 4
gpt4 key购买 nike

MQTT问题:

您好,我正在尝试在多个 Raspberry Pi(从两个开始)之间建立一个 MQTT 网络。我有一个覆盆子 pi (RPi-A),MQTT 客户端,连接了一个热敏电阻传感器和一个覆盆子 (RPi-B),MQTT 代理/客户端,作为我的网络的集线器。通过 python 脚本,我希望每 30 分钟从 RPi-A 通过 MQTT 将温度发送到主题传感器/数据,并由 RPi-B 接收。当 RPi-B 通过主题传感器/数据从 RPi-A 收到消息时,我希望它通过 MQTT 主题传感器/指令向 RPi-A 响应指令。下面是我的脚本,到目前为止,RPi-A 可以发送消息,RPi-B 可以接收消息,但我不知道 RPi-B 如何响应。

基本上,我想了解的是,MQTT 设备是否可以同时充当代理和客户端?而且,客户端可以发送和接收消息吗?如果可以,如何通过 python 实现上述所有内容?我已经阅读了很多博客、官方 MQTT 文章和 paho 模块文档(对我来说很难理解),但仍然无法弄清楚。您的帮助将非常有用/非常感谢。

代码 RPi-A(带热敏电阻传感器):

from sense_hat import SenseHat
import time
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
sense = SenseHat()

Broker = "192.168.1.252"

sub_topic = "sensor/instructions" # receive messages on this topic

pub_topic = "sensor/data" # send messages to this topic


############### sensehat inputs ##################

def read_temp():
t = sense.get_temperature()
t = round(t)
return t

def read_humidity():
h = sense.get_humidity()
h = round(h)
return h

def read_pressure():
p = sense.get_pressure()
p = round(p)
return p

def display_sensehat(message):
sense.show_message(message)
time.sleep(10)

############### MQTT section ##################

# when connecting to mqtt do this;

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(sub_topic)

# when receiving a mqtt message do this;

def on_message(client, userdata, msg):
message = str(msg.payload)
print(msg.topic+" "+message)
display_sensehat(message)

def publish_mqtt(sensor_data):
mqttc = mqtt.Client("python_pub")
mqttc.connect(Broker, 1883)
mqttc.publish(pub_topic, sensor_data)
#mqttc.loop(2) //timeout = 2s

def on_publish(mosq, obj, mid):
print("mid: " + str(mid))


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)


while True:
sensor_data = [read_temp(), read_humidity(), read_pressure()]
publish.single("monto/solar/sensors", str(sensor_data), hostname = Broker)
time.sleep(1*60)

代码 RPi-B(网络集线器):

import time
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

Broker = "192.168.1.252"

sub_topic = "sensor/data" # receive messages on this topic

pub_topic = "sensor/instructions" # send messages to this topic


# mqtt section

# when connecting to mqtt do this;

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(sub_topic)

# when receiving a mqtt message do this;

def on_message(client, userdata, msg):
message = str(msg.payload)
print(msg.topic+" "+message)
publish_mqtt(‘got your message’)

# to send a message

def publish_mqtt(sensor_data):
mqttc = mqtt.Client("monto_hub")
mqttc.connect(Broker, 1883)
mqttc.publish(pub_topic, "this is the master speaking")
#mqttc.loop(2) //timeout = 2s

def on_publish(mosq, obj, mid):
print("mid: " + str(mid))


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.loop_forever()

最佳答案

最简单的方法是使用client.loop_start() 函数在单独的线程上启动网络循环,然后使用普通的client.publish 方法

from sense_hat import SenseHat
import time
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
sense = SenseHat()

Broker = "192.168.1.252"

sub_topic = "sensor/instructions" # receive messages on this topic

pub_topic = "sensor/data" # send messages to this topic


############### sensehat inputs ##################

def read_temp():
t = sense.get_temperature()
t = round(t)
return t

def read_humidity():
h = sense.get_humidity()
h = round(h)
return h

def read_pressure():
p = sense.get_pressure()
p = round(p)
return p

def display_sensehat(message):
sense.show_message(message)
time.sleep(10)

############### MQTT section ##################

# when connecting to mqtt do this;

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(sub_topic)

# when receiving a mqtt message do this;

def on_message(client, userdata, msg):
message = str(msg.payload)
print(msg.topic+" "+message)
display_sensehat(message)

def on_publish(mosq, obj, mid):
print("mid: " + str(mid))


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.loop_start()

while True:
sensor_data = [read_temp(), read_humidity(), read_pressure()]
client.publish("monto/solar/sensors", str(sensor_data))
time.sleep(1*60)

关于树莓派上的python mqtt脚本发送和接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37006863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com