MQTT subscribing is not working [solved]

Hi All,

I have followed Cayenne Docs but I was not able to subscribe for commad published by Cayenne.

Snipped of my code:

import paho.mqtt.client as mqtt
import bluetooth
import subprocess

from time import sleep

from MyThreading import SimpleThread

def running_loop():
    while True:
        mac = "7C:91:22:CE:C4:54"
        r = bluetooth.lookup_name(mac,timeout=5)
        p = subprocess.Popen(["ping","-c","4","192.168.25.8"],stdout=subprocess.PIPE)
        result =  p.communicate()[0].decode("utf-8")
        if r:
            client.publish("v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/data/0", "motion,d=1")
        else:
            client.publish("v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/data/0", "motion,d=0")
        if "100% packet loss" in result:
            client.publish("v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/data/1", "motion,d=0")
        else:
            client.publish("v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/data/1", "motion,d=1")
        sleep(5)

def msg_rcvd(client, userdata, msg):
    print("Yeep received from cloud")
    print("topic=\'%s\'" % msg.topic)
    str_data = msg.payload.decode("utf-8")
    print("message=\'%s\'" % str_data)

if __name__ == "__main__":
    client = mqtt.Client(client_id="XXX", clean_session=False)
    client.qos = 1
    client.username_pw_set("YYY",password="ZZZ")

    try:
        client.connect("mqtt.mydevices.com", 1883)
    except OSError as e:
        print("Unable to connect to broker \"%s\" on port \"%d\"" % (broker_address, broker_port))
        print(e)
        print("Exiting!")
        sys.exit()
    else:
        print("Connected to myDevices.com")
    client.subscribe("v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/#")
    client.on_message = msg_rcvd
    run_loop = SimpleThread(1,"Running Loop",running_loop)
    run_loop.start()
    client.loop_forever()

Since publishing is working like charm - I can see status of the ā€œfound bluetooth deviceā€ and status of my PC Iā€™m not able to receive any message from Cayenne.

Any advice is highly appreciated.

Thank you.

Y

Hi @michal.z4nd4r

please try:
ā€œv1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/+ā€

instead of:
ā€œv1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/#ā€

@rau I can try there is no problem with that, but I donā€™t believe it will help as I have tried subscribing for topic with individuals channel number as well - like subscribing for v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/5. Of course I created a simple switch-actuator coudpled with channel 5. I didnā€™t receive single message.

Y

@rau I have performered your suggested change, but unfortunatelly nothing changed. There needs to be another root casue.

Anyone esle have some idea?

Y

Hi @michal.z4nd4r

maybe itā€™s a timing problem. Please try to call the subscribe method in the on_connect function.

MQTT Subscribe Example

@rau I have performed suggested change, but it didnā€™t help:

def on_connect(client, userdata, rc):
    logging.info("Connected to myDevices.com. Code=\'%s\'" % str(rc))
    client.subscribe("v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/+")

Log file:

18.03.2017 13:18:12  INFO  MyThreading.py: Starting function="Running Loop" in thread
18.03.2017 13:18:12  INFO  test.py: Connected to myDevices.com. Code='0'

And in log file I can see that client did connect successfully, but the msg_rcvd is never called.

Any ideas?

Y

@michal.z4nd4r
in the next step, I suggest to check the command widget and subscription in principal. For that:

  1. install mqtt.fx
  2. connect to the server with your credentials
  3. subscribe to the cmd topic
  4. actuate your button or slider in cayenne dashboard
  5. check if you receive the message in mqtt.fx

MQTT.fx

@rau I have performed another test. I have subscribed for topic like v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/# and whenever I do publishing message to MQTT I will also receive it.

19.03.2017 16:53:40  DEBUG  test.py: Yeep received from cloud
19.03.2017 16:53:40  DEBUG  test.py: topic='v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/data/0'
19.03.2017 16:53:40  DEBUG  test.py: message='motion,d=1'
19.03.2017 16:53:40  DEBUG  test.py: Yeep received from cloud
19.03.2017 16:53:40  DEBUG  test.py: topic='v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/data/1'
19.03.2017 16:53:40  DEBUG  test.py: message='motion,d=1'

Based on this test I believe the subscribing itself is working just fine. For me it looks like the Cayenne will simpli not publish correct command into topics.

Y

Have you tried stripping code back to basics to see if there is something in other parts of your code interfering? Try this and see what happens

import paho.mqtt.client as mqtt

from time import sleep

def msg_rcvd(client, userdata, msg):
    print("Yeep received from cloud")
    print("topic=\'%s\'" % msg.topic)
    str_data = msg.payload.decode("utf-8")
    print("message=\'%s\'" % str_data)

client = mqtt.Client(client_id="XXX", clean_session=False)
client.qos = 1
client.username_pw_set("YYY",password="ZZZ")

try:
	client.connect("mqtt.mydevices.com", 1883)
except OSError as e:
	print("Unable to connect to broker \"%s\" on port \"%d\"" % (broker_address, broker_port))
	print(e)
	print("Exiting!")
	sys.exit()
else:
	print("Connected to myDevices.com")
client.subscribe("v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/#")
client.on_message = msg_rcvd
client.loop_forever()

@adam There is no significat change in the code. But in order to see if ā€œbasicā€ code works the same way I tried your suggested code - the same result. The msg_rcvd is never called, Cayenne wonā€™t publish anything to v1/e9010910-05b8-11e7-8ef4-bdbd62fda600/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/# or Iā€™m missing smthg here, but I donā€™t know what.

Y

Ok, Iā€™ll try this code when I get a chance and see if I have the same problem

@adam great. Donā€™t hesitate to ask form some debug if needed.

Hello All,

Iā€™d like to thank you for you ideas. I have resolved the issue :wink: in fact it was not even issue with Cayenne, but with my misunderstanding of the concept.

Eventhough the issue is resolved I have another one, which I will post in another topic if Iā€™m not able to resolve it by my own.

Y

We are glad that you have solve the problem!

1 Like

Cool, I was just going through old posts and was going to try. What did you end up changing to fix the problem?

@adam, frenkly? I did nothing, I just had to realised that I can expect MQTT message related only to my MQTT device (which is a bit pitty - Maybe feature request?).

As simple as that I have 2 devices - RPI3 and my MQTT device and I was expecting to receive MQTT message when the LED or any device changes on RPI3.

Y