MQTT subscribing is not working [solved]


#1

Hi All,

I have followed http://mydevices.com/cayenne/docs/#bring-your-own-thing-api-manually-publishing-subscribing-control-a-light-actuator-testing-the-actuator 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


#2

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/#”


#3

@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


#4

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

Anyone esle have some idea?

Y


#5

Hi @michal.z4nd4r

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

MQTT Subscribe Example


#6

@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


#7

@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


#8

@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


#9

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()

#10

@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


#11

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


#12

@adam great. Don’t hesitate to ask form some debug if needed.


#13

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


#14

We are glad that you have solve the problem!


#15

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


#16

@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