81 lines
1.9 KiB
Python
81 lines
1.9 KiB
Python
#
|
|
# This is an example of a (sub)application, which can be made a part of
|
|
# bigger site using "app mount" feature, see example_app_router.py.
|
|
#
|
|
import picoweb
|
|
import network
|
|
from time import sleep
|
|
import ulogging
|
|
import traceback
|
|
import sdcard
|
|
import uos
|
|
from ws2812b import WS2812B
|
|
|
|
|
|
ssid = ''
|
|
password = ''
|
|
|
|
ws = WS2812B(256, 28, 0.1)
|
|
|
|
# Assign chip select (CS) pin (and start it high)
|
|
cs = machine.Pin(13, machine.Pin.OUT)
|
|
|
|
# Intialize SPI peripheral (start with 1 MHz)
|
|
spi = machine.SPI(1,
|
|
baudrate=1000000,
|
|
polarity=0,
|
|
phase=0,
|
|
bits=8,
|
|
firstbit=machine.SPI.MSB,
|
|
sck=machine.Pin(10),
|
|
mosi=machine.Pin(11),
|
|
miso=machine.Pin(12))
|
|
|
|
# Initialize SD card
|
|
sd = sdcard.SDCard(spi, cs)
|
|
|
|
# Mount filesystem
|
|
vfs = uos.VfsFat(sd)
|
|
uos.mount(vfs, "/sd")
|
|
|
|
|
|
def connect():
|
|
#Connect to WLAN
|
|
wlan = network.WLAN(network.STA_IF)
|
|
wlan.active(True)
|
|
wlan.connect(ssid, password)
|
|
i = 0
|
|
while wlan.isconnected() == False:
|
|
if i>10:
|
|
print("Failed to connect")
|
|
break
|
|
i += 1
|
|
print('Waiting for connection...')
|
|
sleep(1)
|
|
ip = wlan.ifconfig()[0]
|
|
print(f'Connected on {ip}')
|
|
return ip
|
|
|
|
ip = connect()
|
|
|
|
app = picoweb.WebApp(__name__)
|
|
|
|
@app.route("/")
|
|
def index(request, response):
|
|
size = int(request.headers[b"Content-Length"])
|
|
data = yield from request.reader.readexactly(size)
|
|
data = data.split(b'\r\n\r\n')[-1].split(b'------')[0]
|
|
|
|
for i in range(256):
|
|
offset = i * 3
|
|
green, red, blue = data[offset:offset + 3]
|
|
#print(red, green, blue)
|
|
ws.pixels_set(i, (red, green, blue))
|
|
ws.pixels_show()
|
|
|
|
yield from picoweb.start_response(response)
|
|
yield from response.awrite("HTTP/1.0 200 OK\r\n")
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True, host="0.0.0.0", port=80)
|