35 lines
964 B
Python
35 lines
964 B
Python
import RPi.GPIO as GPIO
|
|
import requests
|
|
import time
|
|
|
|
# Set up GPIO
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
|
|
|
def send_message():
|
|
try:
|
|
response = requests.post(
|
|
'http://beere5:5020/send',
|
|
json={'message': 'klingel geht'},
|
|
headers={'Content-Type': 'application/json'}
|
|
)
|
|
return f"Message sent with status code {response.status_code}"
|
|
except Exception as e:
|
|
return f"Error sending message: {str(e)}"
|
|
|
|
# Main loop
|
|
if __name__ == "__main__":
|
|
try:
|
|
while True:
|
|
current_state = GPIO.input(19)
|
|
if current_state == 1:
|
|
print("GPIO Pin 19 is HIGH")
|
|
result = send_message()
|
|
print(result)
|
|
time.sleep(5)
|
|
time.sleep(0.5) # Check pin every 5 seconds
|
|
except KeyboardInterrupt:
|
|
print("\nProgram stopped by user")
|
|
finally:
|
|
GPIO.cleanup()
|