on high signal send to telegram

This commit is contained in:
2025-06-14 17:45:47 +02:00
parent ea4720e3f8
commit 0f13eee484
5 changed files with 136 additions and 18 deletions

View File

@ -1,24 +1,34 @@
from RPi import GPIO
import RPi.GPIO as GPIO
import requests
import time
# Set up GPIO pin (example using BCM pin 14)
GPIO_PIN = 26
# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN, GPIO.IN)
def loop():
Messwert = GPIO.input(GPIO_PIN) # Read analog value from the pin
Spannung = ((Messwert * 250) / 1023) / 10 # Map and scale to desired range (0-2.5V)
print(f"Spannung: {Spannung:.2f} V") # Print with two decimal places
time.sleep(1)
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__":
setup()
try:
while True:
loop()
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:
GPIO.cleanup() # Clean up when Ctrl+C is pressed
print("\nProgram stopped by user")
finally:
GPIO.cleanup()