Python Forum

Full Version: my function is stuck on loop - even when it not supposed to be
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello ,
can someone explain why my code is stuck on loop?
even if I don't press the button it run the button_callback function (like I press the button)
I use the configuration from here (very simple one)
https://raspberrypihq.com/use-a-push-button-with-raspberry-pi-gpio/
and this is the code :
from twilio.rest import Client
import socket  #Import udp
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import json  # import json read\write
import time
import logging
def button_callback(channel):
    print("Button was pushed!")
    CurrentTime = str(time.ctime())
    MESSAGE = (CurrentTime + " Test")
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
    sock.sendto(MESSAGE.encode(), (UDP_IP, UDP_PORT))
    print (MESSAGE)
    whatsupp()
    logging.basicConfig(filename='/home/pi/Desktop/message.log',level=logging.DEBUG)
    logging.debug(MESSAGE)
    time.sleep(1)

def whatsupp():
    account_sid = "********"
    auth_token  = "********"
    client = Client(account_sid, auth_token)
    message = client.messages.create(
        to="whatsapp:+12346578912329",
        from_="whatsapp:+14155299999",
        body="test")
    print(message.sid)


UDP_IP = "10.0.0.51"
UDP_PORT = 9051

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
message = input("Press enter to quit\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up
I replace the resistor\cables - the same resualt .
want to make sure the problem is not in the code

Thanks ,
I'm not an Rpi expert, but since no one else has answered, I'll take a stab at it:

I don't see a loop that you might be stuck in. Am I missing something there?

I see you have a debug log enabled, is that working? Can you see the log?

Can you run this with the debugging serial console enabled? What does that show?
Thanks you for trying to help -
How to run it with debugging serial console enabled?