Raspberry Pi 1, aktuellstes Raspbian Stretch
Moin,
Ich habe mir über die python-library nach Anleitung einen sehr simplen Server erstellt, über den ich ein Relais öffnen und schließen kann:
Python
#!/usr/bin/python3
import RPi.GPIO as GPIO
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
host_name = '192.168.2.129' # Change this to your Raspberry Pi IP address
host_port = 8000
class MyServer(BaseHTTPRequestHandler):
""" A special implementation of BaseHTTPRequestHander for reading data from
and control GPIO of a Raspberry Pi
"""
def do_HEAD(self):
""" do_HEAD() can be tested use curl command
'curl -I http://server-ip-address:port'
"""
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def _redirect(self, path):
self.send_response(303)
self.send_header('Content-type', 'text/html')
self.send_header('Location', path)
self.end_headers()
def do_GET(self):
""" do_GET() can be tested using curl command
'curl http://server-ip-address:port'
"""
html = '''
<html>
<body style="width:960px; margin: 20px auto;">
<h1>Welcome to my Raspberry Pi</h1>
<p>Current GPU temperature is {}</p>
<form action="/" method="POST">
Turn LED :
<input type="submit" name="submit" value="On">
<input type="submit" name="submit" value="Off">
</form>
</body>
</html>
'''
temp = os.popen("/opt/vc/bin/vcgencmd measure_temp").read()
self.do_HEAD()
self.wfile.write(html.format(temp[5:]).encode("utf-8"))
def do_POST(self):
""" do_POST() can be tested using curl command
'curl -d "submit=On" http://server-ip-address:port'
"""
content_length = int(self.headers['Content-Length']) # Get the size of data
post_data = self.rfile.read(content_length).decode("utf-8") # Get the data
post_data = post_data.split("=")[1] # Only keep the value
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
if post_data == 'On':
GPIO.output(17, GPIO.HIGH)
else:
GPIO.output(17, GPIO.LOW)
print("LED is {}".format(post_data))
self._redirect('/') # Redirect back to the root url
if __name__ == '__main__':
http_server = HTTPServer((host_name, host_port), MyServer)
print("Server Starts - %s:%s" % (host_name, host_port))
try:
http_server.serve_forever()
except KeyboardInterrupt:
http_server.server_close()
Alles anzeigen
Das funktioniert soweit ohne Probleme. Allerdings schaffe ich es einfach nicht, den über crontab beim boot zu starten. Folgendes habe ich versucht:
Damit das user directory richtig ist habe ich ein sh-script erstellt, das von crontab geöffnet werden soll:
relais_boot.sh
Wenn ich das Script von Hand öffne startet der Server ganz normal. Wenn crontab allerdings das Script beim Boot öffnet bekomme ich folgende Fehlermeldung:
Code
Traceback (most recent call last):
File "klingelserver.py", line 72, in <module>
http_server = HTTPServer((host_name, host_port), MyServer)
File "/usr/lib/python3.5/socketserver.py", line 440, in __init__
self.server_bind()
File "/usr/lib/python3.5/http/server.py", line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.5/socketserver.py", line 454, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 99] Cannot assign requested address
Weiß jemand warum? Braucht crontab irgendeine besondere Berechtigung?
Vielen Dank