Manual WiFi
This is the minimal example of using the library with CircuitPython. This example is serving a simple static text message.
It also manually connects to the WiFi network. SSID and password are stored in the code, but they
can as well be stored in the settings.toml
file, and then read from there using os.getenv()
.
1# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4
5import socketpool
6import wifi
7
8from adafruit_httpserver import Server, Request, Response
9
10WIFI_SSID = "..."
11WIFI_PASSWORD = "..."
12
13print(f"Connecting to {WIFI_SSID}...")
14wifi.radio.connect(WIFI_SSID, WIFI_PASSWORD)
15print(f"Connected to {WIFI_SSID}")
16
17pool = socketpool.SocketPool(wifi.radio)
18
19server = Server(pool, "/static", debug=True)
20
21
22@server.route("/")
23def 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
30server.serve_forever(str(wifi.radio.ipv4_address))
Manual AP (access point)
If there is no external network available, it is possible to create an access point (AP) and run a server on it. It is important to note that only devices connected to the AP will be able to access the server and depending on the device, it may not be able to access the internet.
1# SPDX-FileCopyrightText: 2024 Michał Pokusa
2#
3# SPDX-License-Identifier: Unlicense
4
5import socketpool
6import wifi
7
8from adafruit_httpserver import Server, Request, Response
9
10
11AP_SSID = "..."
12AP_PASSWORD = "..."
13
14print("Creating access point...")
15wifi.radio.start_ap(ssid=AP_SSID, password=AP_PASSWORD)
16print(f"Created access point {AP_SSID}")
17
18pool = socketpool.SocketPool(wifi.radio)
19server = Server(pool, "/static", debug=True)
20
21
22@server.route("/")
23def 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
30server.serve_forever(str(wifi.radio.ipv4_address_ap))
Manual Ethernet
Most of the time, the WiFi will be a preferred way of connecting to the network.
Nevertheless it is also possible to use Ethernet instead of WiFi.
The only difference in usage is related to configuring the socket_source
differently.
1# SPDX-FileCopyrightText: 2023 Tim C for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4
5import board
6import digitalio
7
8from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
9from adafruit_wiznet5k import adafruit_wiznet5k_socket as socket
10from adafruit_httpserver import Server, Request, Response
11
12
13# For Adafruit Ethernet FeatherWing
14cs = digitalio.DigitalInOut(board.D10)
15
16# For Particle Ethernet FeatherWing
17# cs = digitalio.DigitalInOut(board.D5)
18
19spi_bus = board.SPI()
20
21# Initialize ethernet interface with DHCP
22eth = WIZNET5K(spi_bus, cs)
23
24# Set the interface on the socket source
25socket.set_interface(eth)
26
27server = Server(socket, "/static", debug=True)
28
29
30@server.route("/")
31def base(request: Request):
32 """
33 Serve a default static plain text message.
34 """
35 return Response(request, "Hello from the CircuitPython HTTP Server!")
36
37
38server.serve_forever(str(eth.pretty_ip(eth.ip_address)))
Automatic WiFi using settings.toml
From the version 8.0.0 of CircuitPython,
it is possible to use the environment variables
defined in settings.toml
file to store secrets and configure the WiFi network
using the CIRCUITPY_WIFI_SSID
and CIRCUITPY_WIFI_PASSWORD
variables.
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.
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
.
This is the same example as above, but it uses the settings.toml
file to configure the WiFi network.
Note
From now on, all the examples will use the settings.toml
file to configure the WiFi network.
1# Setting these variables will automatically connect board to WiFi on boot
2CIRCUITPY_WIFI_SSID="Your WiFi SSID Here"
3CIRCUITPY_WIFI_PASSWORD="Your WiFi Password Here"
Note that we still need to import socketpool
and wifi
modules.
1# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4
5import socketpool
6import wifi
7
8from adafruit_httpserver import Server, Request, Response
9
10
11pool = socketpool.SocketPool(wifi.radio)
12server = Server(pool, "/static", debug=True)
13
14
15@server.route("/")
16def 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
23server.serve_forever(str(wifi.radio.ipv4_address))
Helper for socket pool using adafruit_connection_manager
If you do not want to configure the socket pool manually, you can use the adafruit_connection_manager
library,
which provides helpers for getting socket pool and SSL context for common boards.
Note that it is not installed by default. You can read more about it here.
1# SPDX-FileCopyrightText: 2024 DJDevon3
2#
3# SPDX-License-Identifier: MIT
4
5import wifi
6
7from adafruit_connection_manager import get_radio_socketpool
8from adafruit_httpserver import Server, Request, Response
9
10
11pool = get_radio_socketpool(wifi.radio)
12server = Server(pool, "/static", debug=True)
13
14
15@server.route("/")
16def 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
23server.serve_forever(str(wifi.radio.ipv4_address))