Change Raspberry Pi GPIO state by manually publishing MQTT

Hi,

I have just started using Cayenne and I’m working my way through it. I have managed to get an Arduino Yun connected and I can control the I/O from either the dashboard or using MQTT.fx.

I have also managed to setup a Raspberry Pi on the dashboard and have multiple DS18B20 devices pushing data from the Pi and I can control the GPIO though dashboard widgets.

What I can’t figure out is how to send a command using MQTT.fx to Cayenne to control the GPIO. I have tried using:

v1/xxx-my-user-id-xxx/things/xxx-pi-device-id-xxx/cmd/dev:wq12KGELzpo1vtD/1

with dev:wq12KGELzpo1vtD being the channel ID assigned to the GPIO button in the dashboard but all that happens is that MQTT.fx says the command was published correctly, then disconnects!

Could someone point me in the right direction, either by example or a link to a page that explains how to do it.

Thanks in advance

Andy Hodges

first of all, you cannot connect two instances with same client id, hence you are getting the disconnect.

Are you trying to change the state of actuator widget like button? then this is not possible. You can publish the sensor widget data.

Hi,

Thanks for your response.

I have setup a client in Cayenne specifically for MQTT.fx by going to “Add New -> Device/Widget -> Bring your own thing” in order to avoid using a duplicate client ID, so MQTT.fx is it’s own entity.

I then try and publish as per my original post to:

v1/xxx-my-user-id-xxx/things/xxx-pi-device-id-xxx/cmd/dev:wq12KGELzpo1vtD/1

with the Pi’s device ID in the MQTT publish command. This disconnects me and does not change the relay on the Pi GPIO. If I use the MQTT.fx client ID then my command gets published (under the MQTT.fx device) and I don’t get disconnected.

The workaround I have at the moment involves creating two triggers using the virtual actuator under the MQTT.fx device to control the actual actuator on the Raspberry Pi device. Something like:

Trigger 1
IF
MQTT.fx channel 1 > 0
THEN
Raspberry Pi dev:wq12KGELzpo1vtD = ON

Trigger 2
IF
MQTT.fx channel 1 < 1
THEN
Raspberry Pi dev:wq12KGELzpo1vtD = OFF

That works but it seems a very long way round the issue if I have to do that for all the relays I have connected.

Am I not able to control the Pi’s GPIO directly from a different device in my Cayenne dashboard?

Thanks again for your help.

Regards

Andy Hodges

We do not support this.

Hi,

Thanks again for your reply. Good to know, at least I won’t be chasing my tail trying to get it working.

Regards

Andy Hodges

But why do you want to control it from another device? You can use trigger like you mentioned above.

Hi,

From what I can tell a Cayenne trigger can only have one input and one output.

For one project I need to be able to control multispeed fans (Off, Low, Medium, High) based on a temperature range rather than just “If greater than x turn y on, if less than x turn y off”. I am not going to be able to do that with the options available. Another project is to control a fridge / freezer during the winter months to ensure that the freezer stays at a low enough temperature. In that instance, the compressor needs to be turned on / off based on the results of three temperatures (Fridge, Freezer, Plate) which again does not appear something I can easily do within the realms of Cayenne triggers.

I had hoped to do the main interfacing with Cayenne and the extended processing with NodeRED but as you have pointed out, Cayenne does not support different MQTT devices controlling each other.

The easiest solution will be to leave the data collection / processing / control etc in NodeRED and just publish temperatures to Cayenne for data logging purposes.

Regards

Andy Hodges

First of all, do not use raspberry pi cayenne agent for this. Better will be to shift to using cayenne python library https://github.com/myDevicesIoT/Cayenne-MQTT-Python which will give you must more control to do what you want manually by adding the appropriate code.

This is possible by using 4 different triggers for each mode. Though as you mentioned it won’t be a straight forward and will be a long way.
Same for other project.
If you want we can try doing some try and error methods to get it working.

Unfortunately I am not familiar with Python at this point so using the Python library is more of a headache than it may seem.

I am not sure that triggers will work with the fan solution, purely due to the way I have the hardware setup, currently I use two GPIO / relays to select between the four modes:

Mode: Off Relay1: Off Relay2: Off
Mode: Low Relay1: On Relay2: Off
Mode: Med Relay1: Off Relay2: On
Mode: High Relay1: On Relay2: On

The current projects are already running in NodeRED, coded and working. I liked the look and feel of Cayenne and wanted to take advantage of the historic data logging, something I don’t currently have the facility for. It looks like I would need to start these projects almost from scratch in order to implement them within this system and that is sadly something current commitments will not allow.

Regards

Andy Hodges

1 Like