-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
143 lines (110 loc) · 3.58 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
#import secrets # pylint: disable=no-name-in-module
import socketpool
import wifi
from adafruit_httpserver.mime_type import MIMEType
from adafruit_httpserver.request import HTTPRequest
from adafruit_httpserver.response import HTTPResponse
from adafruit_httpserver.server import HTTPServer
import neopixel
import touchio
import json
import board
from digitalio import DigitalInOut, Direction
import time
from ledPixelsPico import *
nPix = 32
pix = ledPixels(nPix, board.GP15)
ledMode="rainbow"
with open("index.html") as f:
webpage = f.read()
#ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD # pylint: disable=no-member
ssid, password = "TFS Students", "Fultoneagles" # pylint: disable=no-member
print("Connecting to", ssid)
wifi.radio.connect(ssid, password)
print("Connected to", ssid)
pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool)
def requestToArray(request):
raw_text = request.body.decode("utf8")
print("Raw")
data = json.loads(raw_text)
return data
@server.route("/")
def base(request: HTTPRequest):
"""
Serve the default index.html file.
"""
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
#response.send(f"{webpage()}")
response.send(webpage)
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
led.value = False
@server.route("/led", "POST")
def ledButton(request: HTTPRequest):
# raw_text = request.body.decode("utf8")
global ledMode
print("Raw")
# data = json.loads(raw_text)
data = requestToArray(request)
print(f"data: {data} & action: {data['action']}")
rData = {}
# SETTINGS HERE
if (data['action'] == 'ON'):
led.value = True
ledMode="rainbow"
if (data['action'] == 'OFF'):
led.value = False
ledMode="OFF"
rData['item'] = "led"
rData['status'] = data['action']
print("ledMode:", ledMode)
with HTTPResponse(request) as response:
response.send(json.dumps(rData))
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
# Start the server.
server.start(str(wifi.radio.ipv4_address))
touch = touchio.TouchIn(board.GP16)
print("Start touch", touch.value)
def touchCheck():
if touch.value:
pix.light(0, (200,0,0))
while touch.value:
time.sleep(0.1)
return True
else:
return False
while True:
try:
# BEHAVIOR
if ledMode == "rainbow":
# rainbow
for j in range(255):
for i in range(pix.nPix):
pixel_index = (i * 256 // pix.nPix) + j
pix.pixels[i] = pix.wheel(pixel_index & 255, 0.5)
pix.pixels.show()
# check for anything from the webpage
server.poll()
if ledMode == "rainbow":
time.sleep(0.01)
else:
break
# check for a touch signal
if touchCheck():
ledMode = "OFF"
break
elif ledMode == "OFF":
pix.off()
if touchCheck():
ledMode = "rainbow"
else:
pix.off()
# Process any waiting requests
server.poll()
except OSError as error:
print(error)
continue