MQTT syntax


#1

I have managed to set up my RasPi and DHT11 and DHT22 sensors on channels 1-4 as per the community tutorial but would now like to add a digital input sensor on GPIO 17. Can anyone assist with Syntax?


#2

have a look at this : Executing custom code on your Raspberry Pi from Cayenne


#3

@shovelheadman I think the post that might be of more interest to you is this one: Data types for Cayenne MQTT API

Specifically, using the chart in that thread, I can add a digital input via MQTT like the following:

virtualWrite(5, 1, "digital_sensor", "d")

That passes digital value 1 on MQTT channel 5 to a 2-state sensor. Please note that the MQTT channel doesn’t have to match the GPIO pin, just being a virtual construct that can be used to populate Cayenne widgets with data. Many users keep it that way for the sake of code simplicity. The ‘1’ in this case is just my publishing a raw integer value, but this could of course be replaced with a variable that contains the 0/1 data you’re interested in publishing to Cayenne and tracking via this widget.


#4

I managed to add the sensor now thanks, and moved on to adding a digital actuator: Based upon my setting a GPIO output high or low, I can use virtualWrite to write the status of the pin to the dashboard widget, but how can I set the status of the GPIO pin from that widget by pushing it as in the standard Cayenne dashboard? Can you share any sample code for this? Thanks


#5

I’m not following what you are trying to do. If you have a digital actuator widget you will know what the value of that pin is. Can you try to explain differently?


#6

In Non-MQTT Cayenne, you simply add a digital actuator, specify the channel and ensure you connect relay to that GPIO pin. Pushing the widget in the dashboard, will set the pin and enable/disable the relay, and show the status of the actuator on the dashboard evertime it is pressed
With MQTT, I can code the enabling/disabling the relay in some code and publish that pin value to widget via virtualWrite - but pushing the button on the dashboard does not change the state of the widget, nor the GPIO pin,0. I’m thinking I’m looking for some kind of Read function from the dashboard…?


#7

I think I understand. You want to click a button on the dashboard and have the pin change value? If so that’s fairly straight forward. You can see an example here also some additional info here Basically in that on_message function you want to do a pin write. You can see a little better example here I’m using a PiFace so the command is pdf.output_pins but you can replace that with your preferred library to write your pins.