MQTT Subsribe and Multiple Button


#1

Hi All,

I am using umqtt library for Python to interface with Cayenne MQTT server. I have the following issue: I want to control two buttons B1 and B2 (controlling two different pins on my ESP32) on my Cayenne dashboard. In my ESP32 I subscribe two the command topics for both of the channels associated with the buttons. I also create the necessary callback function to handle the received messages. My question is: how is it possible to know which button (B1 or B2 sent the command) so I parse the command in the proper way?

Thanks and regards!


#2

Hi @ivlachos

You should definitely check this (Cayenne MQTT Manual Publish / Subscribe)…
https://github.com/myDevicesIoT/cayenne-docs/blob/master/docs/MQTTAPIS.md#manually-publishing–subscribing.

You probably already know that, your MQTT action consists of a TOPIC, and a PAYLOAD (the actual message). I’ve never used that particular library, but it shouldn’t matter. Specifically in Cayenne, (for an actuator) things might look something like…

TOPIC = v1/MQTT_USERNAME/things/CLIENT_ID/cmd/ChannelNumber
PAYLOAD = Sequence_ID, value

Expanding…
MQTT TOPIC = v1/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/things/xxxxxxxxxxxxxx-xxxxx-xxxxx-xxxxxxxxxxxx/cmd/5
PAYLOAD = xxxxxxxxxxxxxxx,1

So, in order to find out which button you are sending commands from, inside your Callback function, you should check the last TOPIC’s character, in this case “5” (Actuator on channel 5). Then you should check the PAYLOAD’s last character to get the value (1 in this case, ON). For example…

if ((char)topic[85] == ‘5’) && ((char)payload[16] == ‘0’) {
doSomething();
}

topic[85] refers to the character in “position” number 86
payload[16] refers to the character in “position” number 17

Please let me know if I wasn’t clear (It might definitely be the case :laughing:)

Cheers!
Marc


#3

Thinking of it twice…
to read the payload´s value, you should do as Adam suggested in this post…

:sunglasses: