Momentary switch on RaspberryPi


#1

Hello,is there anyway to make a momentary switch on a RaspberryPi?


#2

do you want the momentary switch on dashboard? what you are using it for? i do not see any use of an momentary switch. you can send OFF command back once it is pressed after X seconds.


#3

Ok, I wanted to use it to pulse a relay connected to an older garage door opener to open and close it. It’s and older opener that just needs a few seconds close on the control wire to open and close it.

Thanks.


#4

you can do this in your python code if you are using a MQTT cayenne python library.


#5

Ok, do you have and example of what that code would look like? I was thinking the momentary switch would also be useful to build a fireworks launcher with a relay hatt. Although it’s a little to late this year.
Thanks.


#6

first you need add a device to cayenne using this https://github.com/myDevicesIoT/Cayenne-MQTT-Python.
once done you can write your own code to turn ON relay and turn OFF after X seconds while sending back OFF command to button.


#7

Ok, something like this code I found on the internet?

import cayenne.client #Cayenne MQTT Client<br>
from gpiozero import LED
led=LED(17) #Declaring button pin 17</p><p>
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "YOUR MQTT USERNAME"
MQTT_PASSWORD  = "YOUR MQTT PASSWORD "
MQTT_CLIENT_ID = "YOUR CLIENT ID"</p><p>

# The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
    if message.channel==1: #Dashboard Led widget channel. They must be same. 
        if message.value=="1": #If led command "1", turn led on(message.value must be string)
            led.on()
        elif message.value=="0": #If led command "0", turn led off(message.value must be string)
            led.off()</p><p>
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message #When message recieved from Cayenne run on_message function
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)</p><p>

while True:
  client.loop()

Thanks.


#8

yup that should wok and then send OFF value back to cayenne.