|
8 | 8 |
|
9 | 9 | import neopixel |
10 | 10 |
|
| 11 | + |
| 12 | +# This example depends on the 'static' folder in the examples folder |
| 13 | +# being copied to the root of the circuitpython filesystem. |
| 14 | +# This is where our static assets like html, js, and css live. |
| 15 | + |
| 16 | + |
11 | 17 | # Get wifi details and more from a secrets.py file |
12 | 18 | try: |
13 | 19 | from secrets import secrets |
14 | 20 | except ImportError: |
15 | 21 | print("WiFi secrets are kept in secrets.py, please add them there!") |
16 | 22 | raise |
17 | 23 |
|
| 24 | +try: |
| 25 | + import json as json_module |
| 26 | +except ImportError: |
| 27 | + import ujson as json_module |
| 28 | + |
18 | 29 | print("ESP32 SPI simple web server test!") |
19 | 30 |
|
20 | 31 | esp32_cs = DigitalInOut(board.D10) |
|
41 | 52 | server = server.server(80, debug=False) |
42 | 53 |
|
43 | 54 |
|
44 | | -def onLedHigh(headers, body, client): |
| 55 | +def onLedHigh(headers, body, client): # pylint: disable=unused-argument |
45 | 56 | print("led on!") |
46 | | - print("headers: ", headers) |
47 | | - print("body: ", body) |
48 | 57 | status_light.fill((0, 0, 100)) |
49 | | - respond(headers, body, client) |
| 58 | + server.serve_file("static/index.html") |
50 | 59 |
|
51 | | -def onLedLow(headers, body, client): |
| 60 | +def onLedLow(headers, body, client): # pylint: disable=unused-argument |
52 | 61 | print("led off!") |
53 | | - print("headers: ", headers) |
54 | | - print("body: ", body) |
55 | 62 | status_light.fill(0) |
56 | | - respond(headers, body, client) |
57 | | - |
58 | | -def respond(headers, body, client): |
59 | | - print("headers: ", headers) |
60 | | - print("body: ", body) |
61 | | - |
62 | | - client.write(b"HTTP/1.1 200 OK\r\n") |
63 | | - client.write(b"Content-type:text/html\r\n") |
64 | | - client.write(b"\r\n") |
| 63 | + server.serve_file("static/index.html") |
65 | 64 |
|
66 | | - client.write(b"Click <a href=\"/H\">here</a> turn the LED on!!!<br>\r\n") |
67 | | - client.write(b"Click <a href=\"/L\">here</a> turn the LED off!!!!<br>\r\n") |
68 | | - |
69 | | - client.write(b"\r\n") |
| 65 | +def onLedColor(headers, body, client): # pylint: disable=unused-argument |
| 66 | + rgb = json_module.loads(body) |
| 67 | + print("led color: " + rgb) |
| 68 | + status_light.fill((rgb.get("r"), rgb.get("g"), rgb.get("b"))) |
| 69 | + client.write(b"HTTP/1.1 200 OK") |
70 | 70 | client.close() |
71 | 71 |
|
72 | | -server.on("GET", "/", respond) |
| 72 | +server.set_static_dir("/static") |
73 | 73 | server.on("GET", "/H", onLedHigh) |
74 | 74 | server.on("GET", "/L", onLedLow) |
| 75 | +server.on("POST", "/ajax/ledcolor", onLedColor) |
75 | 76 |
|
76 | 77 |
|
77 | | -print("IP addr: ", esp.pretty_ip(esp.ip_address)) |
| 78 | +print("open this IP in your browser: ", esp.pretty_ip(esp.ip_address)) |
78 | 79 |
|
79 | 80 | server.start() |
80 | | -print("server started!") |
| 81 | + |
81 | 82 | while True: |
82 | 83 | server.update_poll() |
0 commit comments