Mia Mia
Mia Mia

Reputation: 153

python paho client MQTT subscriber not getting

I am using python Paho client.

I am using this in to my function.

my code is showing

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)

and in 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)

Here the topic 'test' is published but not subscribing.

subscriber

please check views output

view

in my views.py on_message function is not called by the topic test. how can I solve this. I am totally stuck here.. in view.py subscribe function is not calling.

I am very new to mqtt. please help

Upvotes: 2

Views: 1802

Answers (1)

hardillb
hardillb

Reputation: 59608

You need to start the client loop in your views.py code otherwise there is nothing to actually run your on_message() callback.

You should also move all your calls to client.subscribe() to into the on_connect callback and remove most of the calls to time.sleep()

Upvotes: 1

Related Questions