Kapitel 15 GPIO
This commit is contained in:
3
teil15/.gitignore
vendored
Normal file
3
teil15/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
./__pycache__
|
||||
*.log
|
||||
./network/__pycache__/*
|
37
teil15/button.py
Executable file
37
teil15/button.py
Executable file
@@ -0,0 +1,37 @@
|
||||
#! /usr/bin/python3
|
||||
import RPi.GPIO as GPIO
|
||||
from time import sleep
|
||||
import logging
|
||||
|
||||
|
||||
__PIN__ = 16 # GPIO Pin, für den Taster
|
||||
__WAIT__ = 0.5 # Warten für 0,5 Sekunden
|
||||
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
||||
|
||||
if __name__ =='__main__':
|
||||
GPIO.setwarnings(False)
|
||||
# benutze Broadcom Pin Nummerierung
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
logging.debug(f'konfiguriere Pin ${__PIN__} als Input.')
|
||||
# Pin 16 als Input mit Pull-Up-Widerstand schalten.
|
||||
GPIO.setup(__PIN__, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
pressed = False
|
||||
|
||||
logging.debug('Start der Schleife.')
|
||||
try:
|
||||
while True:
|
||||
if not GPIO.input(__PIN__):
|
||||
if not pressed:
|
||||
logging.info('Button wurde gedrückt.')
|
||||
pressed = True
|
||||
else:
|
||||
logging.debug('Button nicht gedrückt.')
|
||||
pressed = False
|
||||
|
||||
sleep(__WAIT__)
|
||||
except KeyboardInterrupt:
|
||||
logging.debug('Abbruch durch Benutzer.')
|
||||
finally:
|
||||
pass
|
||||
|
50
teil15/led.py
Executable file
50
teil15/led.py
Executable file
@@ -0,0 +1,50 @@
|
||||
#! /usr/bin/python3
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
from time import sleep
|
||||
|
||||
__PIN__ = 23 # GPIO Pin, den wir nutzen
|
||||
__WAIT__ = 0.5 # Warten für 0,5 Sekunden
|
||||
|
||||
GPIO.setwarnings(False)
|
||||
|
||||
# benutze Broadcom Pin Nummerierung
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
# Pin 23 als Output schalten.
|
||||
GPIO.setup(__PIN__,GPIO.OUT)
|
||||
|
||||
#Zustand der LED setzen
|
||||
def setLED(state):
|
||||
|
||||
GPIO.output(__PIN__,state)
|
||||
|
||||
# Dauerschleife, die die LED im Wechsel ein- und ausschaltet.
|
||||
# Wird gestoppt mit CTRL-C
|
||||
def blinkloop():
|
||||
try:
|
||||
while True:
|
||||
|
||||
# Pin auf HIGH setzen schaltet die LED ein
|
||||
setLED(GPIO.HIGH)
|
||||
print('LED ein')
|
||||
|
||||
sleep(__WAIT__)
|
||||
|
||||
# Pin auf LOW setzen schaltet die LED aus
|
||||
setLED(GPIO.LOW)
|
||||
print('LED aus')
|
||||
sleep(__WAIT__)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
# Zum schluss immer die LED ausschalten.
|
||||
setLED(GPIO.LOW)
|
||||
|
||||
if __name__ =='__main__':
|
||||
blinkloop()
|
||||
GPIO.cleanup(__PIN__)
|
||||
|
||||
|
||||
|
||||
|
69
teil15/ledserver.py
Executable file
69
teil15/ledserver.py
Executable file
@@ -0,0 +1,69 @@
|
||||
#! /usr/bin/python3
|
||||
from http.server import SimpleHTTPRequestHandler
|
||||
import socketserver
|
||||
import logging
|
||||
from led import setLED
|
||||
|
||||
responsehtml = '<html><body> <h3>LED-Server</h3><p> LED ist $ledstate_str</p></body></html>'
|
||||
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
||||
|
||||
# Da http ein stateless Protokoll ist, wird der Handler bei jedem Request neu
|
||||
# aufgebaut! Deswegen benutzen wir hier setup() statt __init__()
|
||||
class LEDHttpRequestHandler(SimpleHTTPRequestHandler):
|
||||
def setup(self):
|
||||
logging.debug('setting up Handler')
|
||||
self.ledstate = False
|
||||
super().setup()
|
||||
|
||||
def do_GET(self):
|
||||
logging.debug(f'path= {self.path}')
|
||||
logging.debug('GET: empfangen')
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type:','text/html')
|
||||
# Ende der Header markieren, dass ist wichtig, sonst kommt der Response nicht zurück!
|
||||
self.end_headers()
|
||||
|
||||
#Wenn im Requestpath 'led='on'enthalten ist, schalten wir die LED ein. Bei off aus
|
||||
if '?led=' in self.path:
|
||||
if 'on' in self.path:
|
||||
logging.debug('schalte LED ein')
|
||||
self.ledstate = True
|
||||
setLED(self.ledstate)
|
||||
elif 'off' in self.path:
|
||||
logging.debug('schalte LED aus')
|
||||
self.ledstate = False
|
||||
setLED(self.ledstate)
|
||||
|
||||
logging.debug(f'ledstate={self.ledstate}')
|
||||
|
||||
response = responsehtml.replace('$ledstate_str',('aus','an')[self.ledstate])
|
||||
|
||||
self.wfile.write(response.encode())
|
||||
|
||||
|
||||
|
||||
class http_server:
|
||||
def __init__(self):
|
||||
def handler(*args):
|
||||
myHandler(*args)
|
||||
server = HTTPServer(('', 8080), handler)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.debug('MAIN: start')
|
||||
setLED(False) # Beim Start LED ausschalten
|
||||
#Request-Handler aufbauen
|
||||
handler = LEDHttpRequestHandler
|
||||
handler.ledstate = False
|
||||
#Socketserver mit unserem Handler starten
|
||||
with socketserver.TCPServer(('',8080),handler) as httpd:
|
||||
logging.info('SERVER: start')
|
||||
try:
|
||||
#Server läuft bis CTRL-C gedrückt wird.
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
setLED(False)
|
||||
|
||||
|
3
teil15/network/__init__.py
Normal file
3
teil15/network/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .const import __ADDRESSLOCAL__
|
||||
#from .const import __ADDRESSREMOTE__
|
||||
|
BIN
teil15/network/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
teil15/network/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
teil15/network/__pycache__/const.cpython-39.pyc
Normal file
BIN
teil15/network/__pycache__/const.cpython-39.pyc
Normal file
Binary file not shown.
10
teil15/network/code.py
Normal file
10
teil15/network/code.py
Normal file
@@ -0,0 +1,10 @@
|
||||
def receive(conn):
|
||||
msg = conn.recv(64).decode('utf-8')
|
||||
|
||||
return msg
|
||||
|
||||
def send(client, msg):
|
||||
message = msg.encode('utf-8')
|
||||
client.send(message)
|
||||
|
||||
|
19
teil15/network/const.py
Normal file
19
teil15/network/const.py
Normal file
@@ -0,0 +1,19 @@
|
||||
#network/const.py
|
||||
|
||||
import socket
|
||||
|
||||
# TCP Port
|
||||
__PORT__ = 6554
|
||||
|
||||
#Server-IP
|
||||
# für den Server (immer localhost)
|
||||
__SERVERLOCAL__ = socket.gethostbyname(socket.gethostname())
|
||||
|
||||
#für Server auf einem Remote-Pi
|
||||
#__SERVERREMOTE__ = socket.gethostbyname('gabbo')
|
||||
|
||||
# Server-Adresse (IP,Port)
|
||||
#lokal
|
||||
__ADDRESSLOCAL__ = (__SERVERLOCAL__,__PORT__)
|
||||
#remote
|
||||
#__ADDRESSREMOTE__ = (__SERVERREMOTE__,__PORT__)
|
Reference in New Issue
Block a user