-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
58 lines (47 loc) · 1.84 KB
/
server.py
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
import board
import json
import neopixel
import os
import sys
import time
from http.server import BaseHTTPRequestHandler, HTTPServer, SimpleHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
hostName = "0.0.0.0"
serverPort = 3001
# initialize NeoPixel board
num_pixels = 90 # 3 strips x 30 lights/strip
pixels = neopixel.NeoPixel(board.D18, num_pixels, brightness=1)
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
# Route serve the main HTML page
if self.path == '/':
with open(os.path.join(sys.path[0], "index.html"), "r") as fh:
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(fh.read().encode())
# Route serving the app.js scripts
if self.path == '/app.js':
self.send_response(200)
self.send_header("Content-type", "application/javascript")
self.end_headers()
with open(os.path.join(sys.path[0], "app.js"), "r") as fh:
self.wfile.write(fh.read().encode())
# Route to set color of NeoPixel strips
if self.path.split('?')[0] == '/set-color':
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
query_components = parse_qs(urlparse(self.path).query)
color = eval(query_components['color'][0])
pixels.fill(color)
self.wfile.write(json.dumps({'success': True }).encode())
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")