############################################################################### ########################## IMPORT LIBRARIES ################################### ############################################################################### import RPi.GPIO as GPIO import time import signal import sys import socket ############################################################################### ########################## GLOBAL CONSTANTS ################################### ############################################################################### # Network Constants TARGET_IP = sys.argv[1] TARGET_PORT = 50000 UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ############################################################################### ########################## CLASS DEFINITION ################################### ############################################################################### class Buzzer: def __init__(self, number, board_pin, bounce=300): self.number = number self.pin = board_pin self.bounce = bounce GPIO.setmode(GPIO.BOARD) GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) self.create_event() def create_event(self): GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.event, bouncetime=self.bounce) def event(self, channel): msg = "Button " + str(self.number) + " pressed" print(msg) UDP.sendto(str.encode(msg), (TARGET_IP, TARGET_PORT)) ############################################################################### ########################## DEFINITION FUNCTIONS ############################### ############################################################################### def init(): # Initialize target ip adress try: socket.inet_aton(TARGET_IP) except socket.error: print("First Argument was no valid IP-Address") sys.exit(0) else: # Initialize signal handling for interrupt by CTRL+C or kill command signal.signal(signal.SIGINT, quit) # Things to be done while ending programm def quit(signal, frame): GPIO.cleanup() UDP.close() print("\n Bye Bye") sys.exit(0) ############################################################################### ################################## MAIN ####################################### ############################################################################### # Mainfunction which includes the whole program def main(): # Initialize init() # Initialize GPIO pins b1 = Buzzer(1, 35) b2 = Buzzer(2, 36) b3 = Buzzer(3, 37) b4 = Buzzer(4, 38) while True: # Do nothing time.sleep(0.1) ############################################################################### ################################# PROGRAM ##################################### ############################################################################### # Start Server main() ############################################################################### #################################### END ###################################### ###############################################################################