Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理循环\u stop()Paho MQTT_Python_Loops_Mqtt_Mosquitto - Fatal编程技术网

Python 如何处理循环\u stop()Paho MQTT

Python 如何处理循环\u stop()Paho MQTT,python,loops,mqtt,mosquitto,Python,Loops,Mqtt,Mosquitto,我尝试通过MQTT将小型模型车连接到代理。每次他们通过轨道的某个区段==20,他们就会连接到代理,订阅,发送一条带有当前位置的消息。在收到来自代理的消息后,他们应该断开与代理的连接。到目前为止,这很有效,但这只是他们第一次穿过工件==20。一旦客户车辆断开连接,且环路以loop_stop终止,如果再次穿过该段,则不会再发生任何情况。如果我不断开并停止循环,一切都会正常工作——但我希望他们断开并停止循环。 你知道这里有什么问题吗 from overdrive import Overdrive im

我尝试通过MQTT将小型模型车连接到代理。每次他们通过轨道的某个区段==20,他们就会连接到代理,订阅,发送一条带有当前位置的消息。在收到来自代理的消息后,他们应该断开与代理的连接。到目前为止,这很有效,但这只是他们第一次穿过工件==20。一旦客户车辆断开连接,且环路以loop_stop终止,如果再次穿过该段,则不会再发生任何情况。如果我不断开并停止循环,一切都会正常工作——但我希望他们断开并停止循环。 你知道这里有什么问题吗

from overdrive import Overdrive
import time
import paho.mqtt.client as mqtt
自动分类:

def __init__(self, macaddr):
    self.car = Overdrive(macaddr)
    self.client = CarClient(macaddr)
    self.check = 0


def startEngine(self):


    self.car.changeSpeed(300, 1000)
    self.car.setLocationChangeCallback(self.locationChangeCallback) # Set location change callback to function above


def locationChangeCallback(self, addr, location, piece, speed, clockwise):

    if piece == 20 :

        self.client.run(piece)
类别客户机:

def __init__(self, id):
    self.client = mqtt.Client(id)
    self.id = id


def on_connect(self, mqttc, obj, flags, rc):
    print("connected" + str(rc))

def on_message(self, mqttc, obj, msg):
    print("received message" + " " + str(msg.payload))
    self.client.disconnect() #if i remove this and the following line, everything works fine
    self.client.loop_stop() #but i have to disconnect and stop the loop after they received a message


def on_publish(self, mqttc, obj, mid):
    print("Data published " + str(mid))

def on_subscribe(self, mqttc, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(self, mqttc, obj, level, string):
    print(string)

def run(self, position):

        self.position = position

        self.client.on_message = self.on_message
        self.client.connect("localhost", 1883, 60)  # connect to broker
        time.sleep(0.1)
        self.client.subscribe("test_channel")  # subscribe topic
        self.client.loop_start()

        self.client.publish("test_channel1", "ID:" + str(self.id) + " Position: " + str(position))
主要


我终于找到了问题所在。停止循环的函数不是loop\u stop,而是loop.stop

因此,代码段必须更改为以下内容:

def on_message(self, mqttc, obj, msg):
    print("received message" + " " + str(msg.payload))
    self.client.disconnect()
    self.client.loop.stop() 

无论如何,谢谢

不,这就是为什么您在mqtt客户端收到消息时试图断开它的连接?为什么您希望客户机在与代理断开连接后能够继续接收消息?因为我尝试编写一种真实场景的代码。一辆汽车来到十字路口,订阅一个主题,并通过MQTT将其位置/速度数据发布到“十字路口控制逻辑”“。该逻辑处理数据,计算汽车能够在不发生碰撞的情况下通过交叉口的速度,并将其返回给汽车。汽车通过交叉口后,它应该断开连接,因为不需要保持连接。代码应该能够处理断开连接,因为每次car在一个特殊的轨道区段行驶。在大写部分==20这不是断开连接的理由,只是取消订阅相关主题。这意味着现实世界中的汽车将一直连接到同一台服务器上?这不是不现实的吗?在我看来,汽车应该在街上穿过一个联系环路,获得地址通过RFID,连接和订阅服务器,将数据发布到服务器。服务器计算车速,并将防撞速度自适应发布回汽车。一旦汽车通过十字路口,它应该取消订阅并断开连接。你现在明白我的意思了吗?我希望它更清楚一点现在我试着编码什么
def on_message(self, mqttc, obj, msg):
    print("received message" + " " + str(msg.payload))
    self.client.disconnect()
    self.client.loop.stop()