Skip to content

Commit e2e396b

Browse files
committed
Updated existing and added more examples
1 parent f0b61a7 commit e2e396b

File tree

7 files changed

+240
-50
lines changed

7 files changed

+240
-50
lines changed

docs/examples.rst

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,64 @@
1-
Simple test
2-
------------
1+
Simple file serving
2+
-------------------
33

4-
Ensure your device works with this simple test.
4+
Serving the content of index.html from the filesystem.
55

6-
.. literalinclude:: ../examples/httpserver_simpletest.py
7-
:caption: examples/httpserver_simpletest.py
6+
.. literalinclude:: ../examples/httpserver_simple_serve.py
7+
:caption: examples/httpserver_simple_serve.py
88
:linenos:
99

10-
Temperature test
11-
--------------------
10+
If you want your code to do more than just serve web pages,
11+
use the ``.start()``/``.poll()`` methods as shown in this example.
1212

13-
Send the microcontroller temperature back to the browser with this simple test.
13+
Between calling ``.poll()`` you can do something useful,
14+
for example read a sensor and capture an average or
15+
a running total of the last 10 samples.
1416

15-
.. literalinclude:: ../examples/httpserver_temperature.py
16-
:caption: examples/httpserver_temperature.py
17+
.. literalinclude:: ../examples/httpserver_simple_poll.py
18+
:caption: examples/httpserver_simple_poll.py
1719
:linenos:
1820

19-
Simple polling test
20-
-------------------
21+
Server with MDNS
22+
----------------
23+
24+
It is possible to use the MDNS protocol to make the server
25+
accessible via a hostname in addition to an IP address.
26+
27+
In this example, the server is accessible via ``http://custom-mdns-hostname/`` and ``http://custom-mdns-hostname.local/``.
28+
29+
.. literalinclude:: ../examples/httpserver_cpu_information.py
30+
:caption: examples/httpserver_cpu_information.py
31+
:linenos:
32+
33+
Change NeoPixel color
34+
---------------------
2135

2236
If you want your code to do more than just serve web pages,
2337
use the start/poll methods as shown in this example.
2438

25-
.. literalinclude:: ../examples/httpserver_simplepolling.py
26-
:caption: examples/httpserver_simplepolling.py
39+
For example by going to ``/change-neopixel-color?r=255&g=0&b=0`` you can change the color of the NeoPixel to red.
40+
Tested on ESP32-S2 Feather.
41+
42+
.. literalinclude:: ../examples/httpserver_neopixel.py
43+
:caption: examples/httpserver_neopixel.py
44+
:linenos:
45+
46+
Get CPU information
47+
---------------------
48+
49+
You can return data from sensors or any computed value as JSON.
50+
That makes it easy to use the data in other applications.
51+
52+
.. literalinclude:: ../examples/httpserver_cpu_information.py
53+
:caption: examples/httpserver_cpu_information.py
54+
:linenos:
55+
56+
Chunked response
57+
---------------------
58+
59+
Libraries supports chunked responses. This is useful for streaming data.
60+
To use it, you need to set the ``chunked=True`` when creating a ``HTTPResponse`` object.
61+
62+
.. literalinclude:: ../examples/httpserver_chunked.py
63+
:caption: examples/httpserver_chunked.py
2764
:linenos:

examples/httpserver_temperature.py renamed to examples/httpserver_chunked.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,38 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
from secrets import secrets # pylint: disable=no-name-in-module
6-
7-
import microcontroller
85
import socketpool
96
import wifi
107

11-
from adafruit_httpserver.server import HTTPServer
128
from adafruit_httpserver.response import HTTPResponse
9+
from adafruit_httpserver.server import HTTPServer
10+
11+
import secrets
12+
13+
14+
ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD
1315

14-
ssid = secrets["ssid"]
1516
print("Connecting to", ssid)
16-
wifi.radio.connect(ssid, secrets["password"])
17+
wifi.radio.connect(ssid, password)
1718
print("Connected to", ssid)
18-
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
1919

2020
pool = socketpool.SocketPool(wifi.radio)
2121
server = HTTPServer(pool)
2222

2323

24-
@server.route("/temperature")
25-
def base(request): # pylint: disable=unused-argument
26-
"""Return the current temperature"""
27-
# pylint: disable=no-member
28-
return HTTPResponse(body=f"{str(microcontroller.cpu.temperature)}")
24+
@server.route("/chunked")
25+
def chunked(request):
26+
"""
27+
Return the response with ``Transfer-Encoding: chunked``.
28+
"""
2929

30+
with HTTPResponse(request, chunked=True) as response:
31+
response.send_chunk("Adaf")
32+
response.send_chunk("ruit")
33+
response.send_chunk(" Indus")
34+
response.send_chunk("tr")
35+
response.send_chunk("ies")
3036

31-
# Never returns
37+
38+
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
3239
server.serve_forever(str(wifi.radio.ipv4_address))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import json
6+
import microcontroller
7+
import socketpool
8+
import wifi
9+
10+
from adafruit_httpserver.mime_type import MIMEType
11+
from adafruit_httpserver.response import HTTPResponse
12+
from adafruit_httpserver.server import HTTPServer
13+
14+
import secrets
15+
16+
17+
ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD
18+
19+
print("Connecting to", ssid)
20+
wifi.radio.connect(ssid, password)
21+
print("Connected to", ssid)
22+
23+
pool = socketpool.SocketPool(wifi.radio)
24+
server = HTTPServer(pool)
25+
26+
27+
@server.route("/cpu-information")
28+
def cpu_information_handler(request):
29+
"""
30+
Return the current CPU temperature, frequency, and voltage as JSON.
31+
"""
32+
33+
data = {
34+
"temperature": microcontroller.cpu.temperature,
35+
"frequency": microcontroller.cpu.frequency,
36+
"voltage": microcontroller.cpu.voltage,
37+
}
38+
39+
with HTTPResponse(request, content_type=MIMEType.TYPE_JSON) as response:
40+
response.send(json.dumps(data))
41+
42+
43+
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
44+
server.serve_forever(str(wifi.radio.ipv4_address))

examples/httpserver_mdns.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import mdns
6+
import socketpool
7+
import wifi
8+
9+
from adafruit_httpserver.mime_type import MIMEType
10+
from adafruit_httpserver.response import HTTPResponse
11+
from adafruit_httpserver.server import HTTPServer
12+
13+
import secrets
14+
15+
16+
ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD
17+
18+
print("Connecting to", ssid)
19+
wifi.radio.connect(ssid, password)
20+
print("Connected to", ssid)
21+
22+
mdns_server = mdns.Server(wifi.radio)
23+
mdns_server.hostname = "custom-mdns-hostname"
24+
mdns_server.advertise_service(
25+
service_type="_http",
26+
protocol="_tcp",
27+
port=80
28+
)
29+
30+
pool = socketpool.SocketPool(wifi.radio)
31+
server = HTTPServer(pool)
32+
33+
34+
@server.route("/")
35+
def base(request):
36+
"""
37+
Serve the default index.html file.
38+
"""
39+
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
40+
response.send_file("index.html")
41+
42+
43+
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
44+
server.serve_forever(str(wifi.radio.ipv4_address))

examples/httpserver_neopixel.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import board
6+
import neopixel
7+
import socketpool
8+
import wifi
9+
10+
from adafruit_httpserver.mime_type import MIMEType
11+
from adafruit_httpserver.response import HTTPResponse
12+
from adafruit_httpserver.server import HTTPServer
13+
14+
import secrets
15+
16+
17+
ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD
18+
19+
print("Connecting to", ssid)
20+
wifi.radio.connect(ssid, password)
21+
print("Connected to", ssid)
22+
23+
pool = socketpool.SocketPool(wifi.radio)
24+
server = HTTPServer(pool)
25+
26+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
27+
28+
29+
@server.route("/change-neopixel-color")
30+
def change_neopixel_color_handler(request):
31+
"""
32+
Changes the color of the built-in NeoPixel.
33+
"""
34+
r = request.query_params.get("r")
35+
g = request.query_params.get("g")
36+
b = request.query_params.get("b")
37+
38+
pixel.fill((int(r or 0), int(g or 0), int(b or 0)))
39+
40+
with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response:
41+
response.send(f"Changed NeoPixel to color ({r}, {g}, {b})")
42+
43+
44+
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
45+
server.serve_forever(str(wifi.radio.ipv4_address))

examples/httpserver_simplepolling.py renamed to examples/httpserver_simple_poll.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,48 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
from secrets import secrets # pylint: disable=no-name-in-module
6-
75
import socketpool
86
import wifi
97

10-
from adafruit_httpserver.server import HTTPServer
8+
from adafruit_httpserver.mime_type import MIMEType
119
from adafruit_httpserver.response import HTTPResponse
10+
from adafruit_httpserver.server import HTTPServer
11+
12+
import secrets
13+
14+
15+
ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD
1216

13-
ssid = secrets["ssid"]
1417
print("Connecting to", ssid)
15-
wifi.radio.connect(ssid, secrets["password"])
18+
wifi.radio.connect(ssid, password)
1619
print("Connected to", ssid)
17-
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
1820

1921
pool = socketpool.SocketPool(wifi.radio)
2022
server = HTTPServer(pool)
2123

2224

2325
@server.route("/")
24-
def base(request): # pylint: disable=unused-argument
25-
"""Default reponse is /index.html"""
26-
return HTTPResponse(filename="/index.html")
26+
def base(request):
27+
"""
28+
Serve the default index.html file.
29+
"""
30+
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
31+
response.send_file("index.html")
2732

2833

29-
# startup the server
34+
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
35+
36+
# Start the server.
3037
server.start(str(wifi.radio.ipv4_address))
3138

3239
while True:
3340
try:
34-
# do something useful in this section,
41+
# Do something useful in this section,
3542
# for example read a sensor and capture an average,
3643
# or a running total of the last 10 samples
3744

38-
# process any waiting requests
45+
# Process any waiting requests
3946
server.poll()
40-
except OSError:
47+
except OSError as error:
48+
print(error)
4149
continue

examples/httpserver_simpletest.py renamed to examples/httpserver_simple_serve.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
from secrets import secrets # pylint: disable=no-name-in-module
6-
75
import socketpool
86
import wifi
97

10-
from adafruit_httpserver.server import HTTPServer
8+
from adafruit_httpserver.mime_type import MIMEType
119
from adafruit_httpserver.response import HTTPResponse
10+
from adafruit_httpserver.server import HTTPServer
11+
12+
import secrets
13+
14+
15+
ssid, password = secrets.WIFI_SSID, secrets.WIFI_PASSWORD
1216

13-
ssid = secrets["ssid"]
1417
print("Connecting to", ssid)
15-
wifi.radio.connect(ssid, secrets["password"])
18+
wifi.radio.connect(ssid, password)
1619
print("Connected to", ssid)
17-
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
1820

1921
pool = socketpool.SocketPool(wifi.radio)
2022
server = HTTPServer(pool)
2123

2224

2325
@server.route("/")
24-
def base(request): # pylint: disable=unused-argument
25-
"""Default reponse is /index.html"""
26-
return HTTPResponse(filename="/index.html")
26+
def base(request):
27+
"""
28+
Serve the default index.html file.
29+
"""
30+
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
31+
response.send_file("index.html")
2732

2833

29-
# Never returns
34+
print(f"Listening on http://{wifi.radio.ipv4_address}:80")
3035
server.serve_forever(str(wifi.radio.ipv4_address))

0 commit comments

Comments
 (0)