Skip to content

Commit 9b16e16

Browse files
authored
Merge pull request #94 from michalpokusa/connection-manager-and-ap-examples
AP example and docs about ConnectionManager example
2 parents 4cc9503 + aaa640e commit 9b16e16

9 files changed

+161
-85
lines changed

docs/examples.rst

+13-42
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,21 @@
1-
Simple Test
2-
-----------
1+
.. note::
2+
All examples in this document are using ``Server`` in ``debug`` mode.
3+
This mode is useful for development, but it is not recommended to use it in production.
4+
More about Debug mode at the end of Examples section.
35

4-
**All examples in this document are using** ``Server`` **in** ``debug`` **mode.**
5-
**This mode is useful for development, but it is not recommended to use it in production.**
6-
**More about Debug mode at the end of Examples section.**
6+
Different ways of starting the server
7+
-------------------------------------
78

8-
This is the minimal example of using the library with CircuitPython.
9-
This example is serving a simple static text message.
9+
There are several ways to start the server on CircuitPython, mostly depending on the device you are using and
10+
whether you have access to external network.
1011

11-
It also manually connects to the WiFi network.
12+
Functionally, all of them are the same, not features of the server are limited or disabled in any way.
1213

13-
.. literalinclude:: ../examples/httpserver_simpletest_manual.py
14-
:caption: examples/httpserver_simpletest_manual.py
15-
:emphasize-lines: 12-17
16-
:linenos:
17-
18-
It is also possible to use Ethernet instead of WiFi.
19-
The only difference in usage is related to configuring the ``socket_source`` differently.
20-
21-
.. literalinclude:: ../examples/httpserver_ethernet_simpletest.py
22-
:caption: examples/httpserver_ethernet_simpletest.py
23-
:emphasize-lines: 13-23
24-
:linenos:
25-
26-
Although there is nothing wrong with this approach, from the version 8.0.0 of CircuitPython,
27-
`it is possible to use the environment variables <https://docs.circuitpython.org/en/latest/docs/environment.html#circuitpython-behavior>`_
28-
defined in ``settings.toml`` file to store secrets and configure the WiFi network.
29-
30-
By default the library uses ``0.0.0.0`` and port ``5000`` for the server, as port ``80`` is reserved for the CircuitPython Web Workflow.
31-
If you want to use port ``80`` , you need to set ``CIRCUITPY_WEB_API_PORT`` to any other port, and then set ``port`` parameter in ``Server`` constructor to ``80`` .
14+
Below you can find examples of different ways to start the server:
3215

33-
This is the same example as above, but it uses the ``settings.toml`` file to configure the WiFi network.
16+
.. toctree::
3417

35-
**From now on, all the examples will use the** ``settings.toml`` **file to configure the WiFi network.**
36-
37-
.. literalinclude:: ../examples/settings.toml
38-
:caption: settings.toml
39-
:lines: 5-
40-
:linenos:
41-
42-
Note that we still need to import ``socketpool`` and ``wifi`` modules.
43-
44-
.. literalinclude:: ../examples/httpserver_simpletest_auto.py
45-
:caption: examples/httpserver_simpletest_auto.py
46-
:emphasize-lines: 11
47-
:linenos:
18+
starting_methods
4819

4920
CPython usage
5021
--------------------
@@ -210,7 +181,7 @@ You can find more information about the template syntax in the
210181

211182
.. literalinclude:: ../examples/httpserver_templates.py
212183
:caption: examples/httpserver_templates.py
213-
:emphasize-lines: 12-15,49-55
184+
:emphasize-lines: 12-15,51-59
214185
:linenos:
215186

216187
Form data parsing

docs/starting_methods.rst

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
Manual WiFi
3+
-----------
4+
5+
This is the minimal example of using the library with CircuitPython.
6+
This example is serving a simple static text message.
7+
8+
It also manually connects to the WiFi network. SSID and password are stored in the code, but they
9+
can as well be stored in the ``settings.toml`` file, and then read from there using ``os.getenv()``.
10+
11+
.. literalinclude:: ../examples/httpserver_simpletest_manual_wifi.py
12+
:caption: examples/httpserver_simpletest_manual_wifi.py
13+
:emphasize-lines: 10-17
14+
:linenos:
15+
16+
Manual AP (access point)
17+
------------------------
18+
19+
If there is no external network available, it is possible to create an access point (AP) and run a server on it.
20+
It is important to note that only devices connected to the AP will be able to access the server and depending on the device,
21+
it may not be able to access the internet.
22+
23+
.. literalinclude:: ../examples/httpserver_simpletest_manual_ap.py
24+
:caption: examples/httpserver_simpletest_manual_ap.py
25+
:emphasize-lines: 11-16,30
26+
:linenos:
27+
28+
Manual Ethernet
29+
---------------
30+
31+
Most of the time, the WiFi will be a preferred way of connecting to the network.
32+
Nevertheless it is also possible to use Ethernet instead of WiFi.
33+
The only difference in usage is related to configuring the ``socket_source`` differently.
34+
35+
.. literalinclude:: ../examples/httpserver_simpletest_manual_ethernet.py
36+
:caption: examples/httpserver_simpletest_manual_ethernet.py
37+
:emphasize-lines: 9-10,13-25,38
38+
:linenos:
39+
40+
Automatic WiFi using ``settings.toml``
41+
--------------------------------------
42+
43+
From the version 8.0.0 of CircuitPython,
44+
`it is possible to use the environment variables <https://docs.circuitpython.org/en/latest/docs/environment.html#circuitpython-behavior>`_
45+
defined in ``settings.toml`` file to store secrets and configure the WiFi network
46+
using the ``CIRCUITPY_WIFI_SSID`` and ``CIRCUITPY_WIFI_PASSWORD`` variables.
47+
48+
By default the library uses ``0.0.0.0`` and port ``5000`` for the server, as port ``80`` is reserved for the CircuitPython Web Workflow.
49+
If you want to use port ``80`` , you need to set ``CIRCUITPY_WEB_API_PORT`` to any other port, and then set ``port`` parameter in ``Server`` constructor to ``80`` .
50+
51+
This is the same example as above, but it uses the ``settings.toml`` file to configure the WiFi network.
52+
53+
.. note::
54+
From now on, all the examples will use the ``settings.toml`` file to configure the WiFi network.
55+
56+
.. literalinclude:: ../examples/settings.toml
57+
:caption: settings.toml
58+
:lines: 5-
59+
:linenos:
60+
61+
Note that we still need to import ``socketpool`` and ``wifi`` modules.
62+
63+
.. literalinclude:: ../examples/httpserver_simpletest_auto_settings_toml.py
64+
:caption: examples/httpserver_simpletest_auto_settings_toml.py
65+
:emphasize-lines: 11
66+
:linenos:
67+
68+
69+
Helper for socket pool using ``adafruit_connection_manager``
70+
------------------------------------------------------------
71+
72+
If you do not want to configure the socket pool manually, you can use the ``adafruit_connection_manager`` library,
73+
which provides helpers for getting socker pool and SSL context for common boards.
74+
75+
Note that it is not installed by default.
76+
You can read `more about the it here <https://docs.circuitpython.org/projects/connectionmanager/en/latest/index.html>`_.
77+
78+
79+
.. literalinclude:: ../examples/httpserver_simpletest_auto_connection_manager.py
80+
:caption: examples/httpserver_simpletest_auto_connection_manager.py
81+
:emphasize-lines: 7,11
82+
:linenos:

docs/starting_methods.rst.license

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2024 Michał Pokusa
2+
3+
SPDX-License-Identifier: MIT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-FileCopyrightText: 2024 DJDevon3
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import wifi
6+
7+
from adafruit_connection_manager import get_radio_socketpool
8+
from adafruit_httpserver import Server, Request, Response
9+
10+
11+
pool = get_radio_socketpool(wifi.radio)
12+
server = Server(pool, "/static", debug=True)
13+
14+
15+
@server.route("/")
16+
def base(request: Request):
17+
"""
18+
Serve a default static plain text message.
19+
"""
20+
return Response(request, "Hello from the CircuitPython HTTP Server!")
21+
22+
23+
server.serve_forever(str(wifi.radio.ipv4_address))

examples/httpserver_simpletest_connectionmanager.py

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-FileCopyrightText: 2024 Michał Pokusa
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import socketpool
6+
import wifi
7+
8+
from adafruit_httpserver import Server, Request, Response
9+
10+
11+
AP_SSID = "..."
12+
AP_PASSWORD = "..."
13+
14+
print("Creating access point...")
15+
wifi.radio.start_ap(ssid=AP_SSID, password=AP_PASSWORD)
16+
print(f"Created access point {AP_SSID}")
17+
18+
pool = socketpool.SocketPool(wifi.radio)
19+
server = Server(pool, "/static", debug=True)
20+
21+
22+
@server.route("/")
23+
def base(request: Request):
24+
"""
25+
Serve a default static plain text message.
26+
"""
27+
return Response(request, "Hello from the CircuitPython HTTP Server!")
28+
29+
30+
server.serve_forever(str(wifi.radio.ipv4_address_ap))

examples/httpserver_ethernet_simpletest.py renamed to examples/httpserver_simpletest_manual_ethernet.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# SPDX-FileCopyrightText: 2023 Tim C for Adafruit Industries
2+
#
23
# SPDX-License-Identifier: MIT
34

45
import board
56
import digitalio
67

78
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
8-
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
9+
from adafruit_wiznet5k import adafruit_wiznet5k_socket as socket
910
from adafruit_httpserver import Server, Request, Response
1011

11-
print("Wiznet5k HTTPServer Test")
1212

1313
# For Adafruit Ethernet FeatherWing
1414
cs = digitalio.DigitalInOut(board.D10)
15+
1516
# For Particle Ethernet FeatherWing
1617
# cs = digitalio.DigitalInOut(board.D5)
18+
1719
spi_bus = board.SPI()
1820

1921
# Initialize ethernet interface with DHCP
@@ -22,7 +24,6 @@
2224
# Set the interface on the socket source
2325
socket.set_interface(eth)
2426

25-
# Initialize the server
2627
server = Server(socket, "/static", debug=True)
2728

2829

examples/httpserver_simpletest_manual.py renamed to examples/httpserver_simpletest_manual_wifi.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
import os
6-
75
import socketpool
86
import wifi
97

108
from adafruit_httpserver import Server, Request, Response
119

12-
ssid = os.getenv("WIFI_SSID")
13-
password = os.getenv("WIFI_PASSWORD")
10+
WIFI_SSID = "..."
11+
WIFI_PASSWORD = "..."
1412

15-
print("Connecting to", ssid)
16-
wifi.radio.connect(ssid, password)
17-
print("Connected to", ssid)
13+
print(f"Connecting to {WIFI_SSID}...")
14+
wifi.radio.connect(WIFI_SSID, WIFI_PASSWORD)
15+
print(f"Connected to {WIFI_SSID}")
1816

1917
pool = socketpool.SocketPool(wifi.radio)
18+
2019
server = Server(pool, "/static", debug=True)
2120

2221

0 commit comments

Comments
 (0)