首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中如何交替切换连接和断开mqtt

在python中如何交替切换连接和断开mqtt
EN

Stack Overflow用户
提问于 2022-06-19 16:59:58
回答 1查看 38关注 0票数 0

因此,我想知道是否可以在连接到代理之间切换,获取一些消息,断开连接,执行一些其他代码,然后再连接到代理,然后从那里重复所有的过程。

我想做一个简单的闩锁。但不起作用。以下是代码:

时间锁存器= 0

代码语言:javascript
复制
import paho.mqtt.client as mqtt
import time

def on_connect(client, userdata, flags, rc):  # The callback for when the client connects to the broker
    print("Connected with result code {0}".format(str(rc)))  # Print result of connection attempt
    client.subscribe("trilaterasi/ESP1")
    client.subscribe("trilaterasi/ESP2")
    client.subscribe("trilaterasi/ESP3")
    
def on_message(client, userdata, msg):
    if msg.topic == 'trilaterasi/ESP1':
        global ESP1
        ESP1 = float(msg.payload)
        print("Msg rcvd" + msg.topic + " " + str(ESP1))
    elif msg.topic == 'trilaterasi/ESP2':
        global ESP2
        ESP2 = float(msg.payload)
        print("Msg rcvd" + msg.topic + " " + str(ESP2))
    elif msg.topic == 'trilaterasi/ESP3':
        global ESP3
        ESP3 = float(msg.payload)
        print("Msg rcvd" + msg.topic + " " + str(ESP3))
        
broker = "ipaddress"
latch = 0
while latch == 0:
    client = mqtt.Client("Python")
    client.loop_start()
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(broker)
    time.sleep(5)
    client.disconnect()
    client.loop_stop()
    latch = 1

时间锁存器= 1

代码语言:javascript
复制
while latch == 1:
    #This is the coordinates of ESPs that are mounted to the wall
    ESP1x = 300
    ESP1y = 0
    ESP2x = 0
    ESP2y = 130
    ESP3x = 150
    ESP3y = 260
    #This is the Trilateration Formula
    a = 2*(-ESP1x + ESP2x)
    b = 2*(-ESP1y + ESP2y)
    c = (ESP1**2 - ESP2**2 - ESP1x**2 + ESP2x**2 - ESP1y**2 + ESP2y**2)
    d = 2*(-ESP2x + ESP3x)
    e = 2*(-ESP2y + ESP3y)
    f = (ESP2**2 - ESP3**2 - ESP2x**2 + ESP3x**2 - ESP2y**2 + ESP3y**2)
    #This is the estimated coordinate of the BLE device
    x = ((c*e-f*b)/(e*a-b*d))
    y = ((c*d-a*f)/(b*d-a*e))
    print(x)
    print(y)
    latch = 0

它给出了结果,但它只运行了一次代码。我对python和mqtt非常陌生,所以我不知道自己做错了什么。我想知道是否因为while循环并尝试将其更改为if,但它仍然只运行了一次代码。我觉得我在这里错过了什么,所以任何建议都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-19 23:04:45

如果我正确地理解了您的问题,那么您就有两个while循环:

代码语言:javascript
复制
latch = 0
while latch == 0:
   # Do stuff
   latch = 1
while latch == 1:
   # Do other stuff
   latch = 0

为什么每个循环只运行一次?如果您希望代码持续运行,那么需要一个外部循环。

代码语言:javascript
复制
latch = 0
while True:
   while latch == 0:
      # Do stuff
      latch = 1
   while latch == 1:
      # Do other stuff
      latch = 0

在这种情况下,实际上不需要latch,因为latch只(而且总是)在每个段的末尾被更改(因此while只运行一次)。您可以简化为:

代码语言:javascript
复制
while True:
   # Do stuff from first while loop (connect to MQTT broker etc)
   # Do stuff from second while loop (formula etc)

但是,需要重新考虑应用程序的结构。连接到代理涉及一些开销(建立连接、MQTT CONNECT握手,在您的情况下是SUBSCRIBE数据包等),因此通常最好保持连接处于打开状态(而不是经常断开连接)。这有一个额外的好处:丢失更少的消息(另一种解决此问题的方法是通过setting the QOS)。也许可以这样做(粗略的伪代码):

代码语言:javascript
复制
def recalculate():
    #This is the coordinates of ESPs that are mounted to the wall
    ESP1x = 300
    # Do calculation required when values change...

def on_message(client, userdata, msg):
    if msg.topic == 'trilaterasi/ESP1':
        global ESP1
        ESP1 = float(msg.payload)
        print("Msg rcvd" + msg.topic + " " + str(ESP1))
        recalculate()
    # Handle other messages 

client = mqtt.Client("Python")
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
client.loop_forever()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72678776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档