【问题标题】:python paho client MQTT subscriber not gettingpython paho客户端MQTT订阅者没有得到
【发布时间】:2021-03-11 08:12:56
【问题描述】:

我正在使用 python Paho 客户端。

我在我的函数中使用它。

我的代码正在显示

import paho.mqtt.client as mqtt
import time, logging


broker = "127.0.0.1"

port = 1883
QOS = 0

CLEAN_SESSION = True
# error logging


# use DEBUG,INFO,WARNING
def on_subscribe(client, userdata, mid, granted_qos):  # create function for callback
    # print("subscribed with qos",granted_qos, "\n")
    time.sleep(1)
    print("sub acknowledge message id=" + str(mid))
    pass


def on_disconnect(client, userdata, rc=0):
    print("DisConnected result code " + str(rc))


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


def on_message(client, userdata, message):
    msg = str(message.payload.decode("utf-8"))
    print("message received in mqtt_subscriber  " + msg)


def on_publish(client, userdata, mid):
    print("message published " + str(mid))


topic1 = "test"
client = mqtt.Client("RDAresp", False)  # create client object

client.on_subscribe = on_subscribe  # assign function to callback
client.on_disconnect = on_disconnect  # assign function to callback
client.on_connect = on_connect  # assign function to callback
client.on_message = on_message
client.connect(broker, port)  # establish connection
time.sleep(1)
client.loop_start()
client.subscribe("RemoteDoorAccess")
count = 1
while True:  # runs forever break with CTRL+C
    print("publishing on topic ", topic1)
    msg = "message : RemoteDoorAccess_resp is published "
    client.publish(topic1, msg)
    count += 1
    time.sleep(5)

在views.py中

def on_message(client, userdata, message):
    msg = str(message.payload.decode("utf-8"))
    print("message  authority resp module  " + msg)


def on_subscribe(client, userdata, mid, granted_qos):  # create function for callback
    print("subscribed with qos", granted_qos, "\n")
    time.sleep(1)

    pass


def on_disconnect(client, userdata, rc=0):
    print("DisConnected result code " + str(rc))


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


def on_publish(client, userdata, mid):
    print("message published " + str(mid))


def mqttConnection():
    topic = "RemoteDoorAccess"
    client = mqtt.Client("RDA", False)  # create client object

    client.on_subscribe = on_subscribe  # assign function to callback
    client.on_disconnect = on_disconnect  # assign function to callback
    client.on_connect = on_connect  # assign function to callback
    client.on_message = on_message
    client.connect(broker, port)  # establish connection
    time.sleep(1)
    client.subscribe("test")
    time.sleep(1)
    print("publishing on topic ", topic)
    msg = "RemoteDoor Access published"
    client.publish(topic, msg)
    time.sleep(10)

@api_view(['GET'])
@permission_classes([])
def remotedooraccess_mobile(request):
        mqttConnection()
        return Response({msg: validation["FDP34"]}, status=status.HTTP_200_OK)

此处主题“测试”已发布但未订阅。

请检查视图输出

在我的views.py 中,主题测试没有调用on_message 函数。 我该如何解决这个问题。 我完全被困在这里..在 view.py 订阅功能没有调用。

我对 mqtt 很陌生。 请帮忙

【问题讨论】:

  • 请不要张贴文字图片。发布实际文本,然后使用工具对其进行格式化。图片可能难以阅读,并且对于使用屏幕阅读器的人来说是不可能的。
  • @hardillb 好的。我会努力的

标签: python mqtt mosquitto paho


【解决方案1】:

您需要在您的 views.py 代码中启动客户端循环,否则将无法实际运行您的 on_message() 回调。

您还应该将所有对client.subscribe() 的调用移至on_connect 回调中,并删除对time.sleep() 的大部分调用

【讨论】:

  • 感谢您的重播。它工作正常。谢谢谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
相关资源
最近更新 更多