![](/img/trans.png)
[英]PAHO MQTT Python client - acknowledgement missing, guaranteed delivery for subscriber
[英]python paho client MQTT subscriber not getting
我正在使用 python Paho 客户端。
我在我的 function 中使用它。
我的代码正在显示
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)
此处主题“测试”已发布但未订阅。
请查看意见 output
在我的views.py on_message function 没有被主题测试调用。 我该如何解决这个问题。 我完全被困在这里..在 view.py 订阅 function 没有打电话。
我对 mqtt 很陌生。 请帮忙
您需要在您的views.py
代码中启动客户端循环,否则实际上没有任何东西可以运行您的on_message()
回调。
您还应该将所有对 client.subscribe client.subscribe()
的调用移到on_connect
回调中,并删除对 time.sleep time.sleep()
) 的大部分调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.