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.
@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.
@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.
@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.
Iād like to thank you for you ideas. I have resolved the issue 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.
@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.