MQtt send commands to change state

hi everyone.
first post here

ive setup myself with a new arduino unit with Ethernet setup with 2 relays…im trying to understand the mqtt
can anyone point me to where in the documentation i can information on push commands to cayenne

my goal is to use tasker on an android device, with the mqtt plugin to send commands on certain actions

for example on this one i was when i pickup my tablet from the dock with will turn off the two relays though cayenne to turn off the light in my lounge room…

relays are working direct thought cayenne so now im just looking for that part of the injection of commands from external…
any replys are welcome and helpfull, if i can provide any more information im happy to

I have a pretty good explanation here Using Node-RED as a Local Fallback Server

Let me know if you need help!

ok so im going to need a few extra pieces…
a http post option would be pretty cool as an api as they tend to be rather simple…
but i get the idea of mqtt its alot more versatile once you wrap your head around it…ill order another pi, or perhaps use my orange pi for this job as it doesnt need to be compatible with cayenne itself

MQTT is just as easy, you just need to have a client installed. You can do this from the same Pi, so won’t have to get a second one. I’ll see if I can come up with a Python script quick.

You can give this a try:

import paho.mqtt.client as mqtt
import time
import sys

MQTTpayload = 0

mqttc = mqtt.Client(client_id="")
mqttc.username_pw_set("username", password="")
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
mqttc.loop_start()

topic = "v1/username/things/clientid/digital/22"
while True:
    try:
        MQTTpayload = MQTTpayload + 1
        print(MQTTpayload)
        mqttc.loop()
        mqttc.publish(topic, payload=MQTTpayload, retain=True)
        time.sleep(5)
    except KeyboardInterrupt:
        mqttc.loop_stop()
        mqttc.disconnect()
        sys.exit()

For me it works for about 15 seconds and then starts sending a 1 on channel 1. I tested with a local MQTT server and it appears to be working there so I’m not sure if it’s a Cayenne issue or something with the code?

My disconnect issue was due to 2 devices connecting to the same MQTT device. This code should work fine if you want to give it a try. Just update the line MQTTpayload = MQTTpayload + 1 with the value you want to send and update username/password/clientid.

1 Like