|
| 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: |
0 commit comments