Digital Value off/on switch doesn't update

so basically you are done.
what you want to achieve with push button??

Thanks for your help up to now. What I want to achieve is to send the state of my push button to cayenne dashboard. Meaning when my gpio and ground, which each are connected to one leg of the button, are connected, I get an on button on the dashboard, and if they are disconnected the dashboard displays the off status.

But right now, with your solution, the widget is on at all times

@rushtkb with push button :-1:
what you want can be achieved with switch
as push button changes state as soon as release it

Yes, maybe its more like a switch function. If its on, it shows the on status, and if its off it shows the off status

right now I just have a jumper wire cut in half and bared at the ends. One is connected to a gpio port and the other is connected to a ground. To test this I’m just connecting the bare ends together to see the status change.

Ultimately I will connect the ends to a switch.

import cayenne.client #Cayenne MQTT Client
from time import sleep
import time

Cayenne authentication info. This should be obtained from the Cayenne Dashboa$
MQTT_USERNAME = “x”
MQTT_PASSWORD = “x”
MQTT_CLIENT_ID = “x”

client = cayenne.client.CayenneMQTTClient()

client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

import RPi.GPIO as GPIO
from time import sleep # this lets us have a time delay (see line 12)
GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO 25 as input
i=0
timestamp = 0

while True:
client.loop()
if (time.time() > timestamp + 10):
client.virtualWrite(22, i, “digital_sensor”, “d”)
timestamp = time.time()

if (GPIO.input(25)==GPIO.LOW): # if port 25 == 0
print “Port 25 is 0/GPIO.LOW - button pressed”
i = 1

if (GPIO.input(25)==GPIO.HIGH): # if port 25 == 1
print “Port 25 is 0/GPIO,HIGH - button pressed”
i = 0

try this code and keep button pressed for some time.

1 Like

Its working!!! Thank you very much. Just one more thing if you can help me. Now I need 20 switches connected. Is there an optimize way of doing the code?? Instead of just copying the if statements for every pin?

1 Like

Great.
i think you have to do this way only or someone else might help you optimize the code.

1 Like

Ok wonderful Thanks again for your help :slight_smile:

welcome :slightly_smiling_face:. cant wait to see what you will be doing with this project. once done post it in the community.

1 Like

I did NOT test this, just whipped it up quick. Let me know if there are errors you cannot figure out. It could probably be optimized even more, I’ll think about it some more and post back if I can think of anything. This uses board numbering. You can change it if you like but board numbering is much easier to keep track of.

import cayenne.client #Cayenne MQTT Client
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) # set up BOARD numbering

MQTT_USERNAME = "x"
MQTT_PASSWORD = "x"
MQTT_CLIENT_ID = "x"

client = cayenne.client.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

unusedpins = [1,2,4,5,9,14,17,20,25,30,34,39] #add all pins you do NOT want to check here

for i in range(1,40):
    if (i not in unusedpins):
        GPIO.setup(i, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    try:
        client.loop()
        for i in range(1,40):
            if (i not in unusedpins):
                if (GPIO.input(i)==GPIO.LOW): # if port 25 == 0
                    print "Port " + str(i) + " is 0/GPIO.LOW - button pressed"
                    client.virtualWrite(i, 1, "digital_sensor", "d")

                if (GPIO.input(i)==GPIO.HIGH): # if port 25 == 1
                    print "Port " + str(i) + " is 0/GPIO,HIGH - button pressed"
                    client.virtualWrite(i, 0, "digital_sensor", "d")
                
        time.sleep(2) #Increase based on your sensitivity needs.  Don't go lower than 2 seconds.
    except (EOFError, SystemExit, KeyboardInterrupt):
        client.disconnect()
        GPIO.cleanup()
        sys.exit()
2 Likes

I don’t think its liking that loop for the gpio setup:
GPIO.setup(i, GPIO.IN, pull_up_down=GPIO.PUD_UP)
ValueError: The channel sent is invalid on a Raspberry Pi
but it was a good approach. I really liked it.

Did you add all the excluded pins in the unusedpins array? Probably some pin I missed that either can’t be an input or doesn’t have a pull-up.

1 Like

hmm no, I just added the GPIOs not the ground and others. Ill give that a try now

I can’t believe it. Its working perfectly. Many thanks to you guys @shramik_salgaonkar @adam.

3 Likes

You guys are probably going to hate me, but I do have one more request. I posted in another thread that I wanted to run this program at boot and I found the solution for my initial code to start at boot which was to run the python code out of a shell and assign PATH to environmental variables which loads the modules. But I’

Ok I got it working you guys…Never mind, I had to put a delay for my client to connect to cayenne.

1 Like

Haha, as I was reading your post I was thinking exactly that. Especially if you are wireless it takes too long to get an internet connection and the script fails. Good job figuring that out!