Skip to content

Commit 7c6ae2a

Browse files
committed
Updated the NeoPixel example to also show using values from POST data
1 parent dbdbacd commit 7c6ae2a

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

docs/examples.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,23 @@ In example below, handler for ``/api`` route will be called when any of ``GET``,
109109
Change NeoPixel color
110110
---------------------
111111

112-
In your handler function you can access the query/GET parameters using ``request.query_params``.
113-
This allows you to pass data to the handler function and use it in your code.
114-
Alternatively you can use URL parameters, which are described later in this document.
112+
There are several ways to pass data to the handler function:
113+
114+
- In your handler function you can access the query/GET parameters using ``request.query_params``
115+
- You can also access the POST data directly using ``request.body`` or if you data is in JSON format,
116+
you can use ``request.json()`` to parse it into a dictionary
117+
- Alternatively for short pieces of data you can use URL parameters, which are described later in this document
118+
For more complex data, it is recommended to use JSON format.
119+
120+
All of these approaches allow you to pass data to the handler function and use it in your code.
115121

116122
For example by going to ``/change-neopixel-color?r=255&g=0&b=0`` or ``/change-neopixel-color/255/0/0``
117123
you can change the color of the NeoPixel to red.
118124
Tested on ESP32-S2 Feather.
119125

120126
.. literalinclude:: ../examples/httpserver_neopixel.py
121127
:caption: examples/httpserver_neopixel.py
122-
:emphasize-lines: 24-26,34-35
128+
:emphasize-lines: 25-27,39-40,52-53,61,69
123129
:linenos:
124130

125131
Get CPU information

examples/httpserver_neopixel.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import socketpool
88
import wifi
99

10-
from adafruit_httpserver import Server, Request, Response
10+
from adafruit_httpserver import Server, Request, Response, GET, POST
1111

1212

1313
pool = socketpool.SocketPool(wifi.radio)
@@ -16,27 +16,57 @@
1616
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
1717

1818

19-
@server.route("/change-neopixel-color")
19+
@server.route("/change-neopixel-color", GET)
2020
def change_neopixel_color_handler_query_params(request: Request):
21-
"""
22-
Changes the color of the built-in NeoPixel using query/GET params.
23-
"""
24-
r = request.query_params.get("r")
25-
g = request.query_params.get("g")
26-
b = request.query_params.get("b")
21+
"""Changes the color of the built-in NeoPixel using query/GET params."""
2722

28-
pixel.fill((int(r or 0), int(g or 0), int(b or 0)))
23+
# e.g. /change-neopixel-color?r=255&g=0&b=0
24+
25+
r = request.query_params.get("r") or 0
26+
g = request.query_params.get("g") or 0
27+
b = request.query_params.get("b") or 0
28+
29+
pixel.fill((int(r), int(g), int(b)))
30+
31+
with Response(request, content_type="text/plain") as response:
32+
response.send(f"Changed NeoPixel to color ({r}, {g}, {b})")
33+
34+
35+
@server.route("/change-neopixel-color", POST)
36+
def change_neopixel_color_handler_post_body(request: Request):
37+
"""Changes the color of the built-in NeoPixel using POST body."""
38+
39+
data = request.body # e.g b"255,0,0"
40+
r, g, b = data.decode().split(",") # ["255", "0", "0"]
41+
42+
pixel.fill((int(r), int(g), int(b)))
2943

3044
with Response(request, content_type="text/plain") as response:
3145
response.send(f"Changed NeoPixel to color ({r}, {g}, {b})")
3246

3347

34-
@server.route("/change-neopixel-color/<r>/<g>/<b>")
35-
def change_neopixel_color_handler_url_params(request: Request, r: str, g: str, b: str):
36-
"""
37-
Changes the color of the built-in NeoPixel using URL params.
38-
"""
39-
pixel.fill((int(r or 0), int(g or 0), int(b or 0)))
48+
@server.route("/change-neopixel-color/json", POST)
49+
def change_neopixel_color_handler_post_json(request: Request):
50+
"""Changes the color of the built-in NeoPixel using JSON POST body."""
51+
52+
data = request.json() # e.g {"r": 255, "g": 0, "b": 0}
53+
r, g, b = data.get("r", 0), data.get("g", 0), data.get("b", 0)
54+
55+
pixel.fill((r, g, b))
56+
57+
with Response(request, content_type="text/plain") as response:
58+
response.send(f"Changed NeoPixel to color ({r}, {g}, {b})")
59+
60+
61+
@server.route("/change-neopixel-color/<r>/<g>/<b>", GET)
62+
def change_neopixel_color_handler_url_params(
63+
request: Request, r: str = "0", g: str = "0", b: str = "0"
64+
):
65+
"""Changes the color of the built-in NeoPixel using URL params."""
66+
67+
# e.g. /change-neopixel-color/255/0/0
68+
69+
pixel.fill((int(r), int(g), int(b)))
4070

4171
with Response(request, content_type="text/plain") as response:
4272
response.send(f"Changed NeoPixel to color ({r}, {g}, {b})")

0 commit comments

Comments
 (0)