-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_gpio.py
More file actions
104 lines (89 loc) · 3.06 KB
/
server_gpio.py
File metadata and controls
104 lines (89 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
##
# PROJECT, 2021
# test
# File description:
# test
##
import RPi.GPIO as GPIO
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
# Change this to your Raspberry Pi IP address (ip -4 address | grep inet)
host_name = '10.61.2.181'
# Port
host_port = 8000
state = "Off"
class MyServer(BaseHTTPRequestHandler):
""" A special implementation of BaseHTTPRequestHander for reading data from
and control GPIO of a Raspberry Pi
"""
def do_HEAD(self, type):
""" do_HEAD() can be tested use curl command
'curl -I http://server-ip-address:port'
"""
self.send_response(200)
self.send_header('Content-type', type)
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 checkRequestedFiles(self, request_extension):
if request_extension != ".py":
f = open(self.path[1:]).read()
self.do_HEAD("text/" + request_extension[1:])
if (request_extension == ".html"):
self.wfile.write(f.format(state, state).encode("utf-8"))
else:
self.wfile.write(bytes(f, 'utf-8'))
else:
f = "File not found"
self.send_error(404, f)
def do_GET(self):
""" do_GET() can be tested using curl command
'curl http://server-ip-address:port'
"""
if (self.path == "/"):
self.path = '/index.html'
try:
split_path = os.path.splitext(self.path)
request_extension = split_path[1]
self.checkRequestedFiles(request_extension)
except:
f = "File not found"
self.send_error(404, f)
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
if state == 'On':
GPIO.output(18, GPIO.HIGH)
else:
GPIO.output(18, GPIO.LOW)
def do_POST(self):
""" do_POST() can be tested using curl command
'curl -d "submit=On" http://server-ip-address:port'
"""
global state
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
try:
post_data = post_data.split("=")[1] # Only keep the value
except:
post_data = "Off"
state = post_data
# print(post_data)
print("LED is {}".format(post_data))
self._redirect('/') # Redirect back to the root url
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()