summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Trentini <matt.trentini@gmail.com>2024-12-22 20:08:54 +1100
committerDamien George <damien@micropython.org>2025-03-25 23:28:56 +1100
commit39452dbeed27423fd207d3cc48c52b4e591deea5 (patch)
tree2bfba2a9fed2de58f0f06374c9792e65e2d50cdf
parent93a8c53d64e63da4b965265a24ccdd33806055db (diff)
docs/rp2: Add network information to the rp2 quickref.
Some rp2 boards include WiFi, at least with the very popular Pico W and Pico 2 W. New users frequently ask how to set up WiFi and are confused because it's not covered in the quickref. This commit adds the wlan section, copied and modified with notes from the ESP32 quickref. Signed-off-by: Matt Trentini <matt.trentini@gmail.com>
-rw-r--r--docs/esp32/quickref.rst24
-rw-r--r--docs/rp2/quickref.rst51
2 files changed, 63 insertions, 12 deletions
diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst
index d65782e50..ccc01099d 100644
--- a/docs/esp32/quickref.rst
+++ b/docs/esp32/quickref.rst
@@ -83,30 +83,30 @@ The :class:`network.WLAN` class in the :mod:`network` module::
import network
- wlan = network.WLAN(network.WLAN.IF_STA) # create station interface
- wlan.active(True) # activate the interface
- wlan.scan() # scan for access points
- wlan.isconnected() # check if the station is connected to an AP
+ wlan = network.WLAN() # create station interface (the default, see below for an access point interface)
+ wlan.active(True) # activate the interface
+ wlan.scan() # scan for access points
+ wlan.isconnected() # check if the station is connected to an AP
wlan.connect('ssid', 'key') # connect to an AP
- wlan.config('mac') # get the interface's MAC address
- wlan.ipconfig('addr4') # get the interface's IPv4 addresses
+ wlan.config('mac') # get the interface's MAC address
+ wlan.ipconfig('addr4') # get the interface's IPv4 addresses
ap = network.WLAN(network.WLAN.IF_AP) # create access-point interface
- ap.config(ssid='ESP-AP') # set the SSID of the access point
- ap.config(max_clients=10) # set how many clients can connect to the network
- ap.active(True) # activate the interface
+ ap.config(ssid='ESP-AP') # set the SSID of the access point
+ ap.config(max_clients=10) # set how many clients can connect to the network
+ ap.active(True) # activate the interface
A useful function for connecting to your local WiFi network is::
def do_connect():
- import network
- wlan = network.WLAN(network.WLAN.IF_STA)
+ import machine, network
+ wlan = network.WLAN()
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('ssid', 'key')
while not wlan.isconnected():
- pass
+ machine.idle()
print('network config:', wlan.ipconfig('addr4'))
Once the network is established the :mod:`socket <socket>` module can be used
diff --git a/docs/rp2/quickref.rst b/docs/rp2/quickref.rst
index 6be318050..23071d772 100644
--- a/docs/rp2/quickref.rst
+++ b/docs/rp2/quickref.rst
@@ -55,6 +55,57 @@ The :mod:`rp2` module::
import rp2
+Networking
+----------
+
+WLAN
+^^^^
+
+.. note::
+ This section applies only to devices that include WiFi support, such as the `Pico W`_ and `Pico 2 W`_.
+
+The :class:`network.WLAN` class in the :mod:`network` module::
+
+ import network
+
+ wlan = network.WLAN() # create station interface (the default, see below for an access point interface)
+ wlan.active(True) # activate the interface
+ wlan.scan() # scan for access points
+ wlan.isconnected() # check if the station is connected to an AP
+ wlan.connect('ssid', 'key') # connect to an AP
+ wlan.config('mac') # get the interface's MAC address
+ wlan.ipconfig('addr4') # get the interface's IPv4 addresses
+
+ ap = network.WLAN(network.WLAN.IF_AP) # create access-point interface
+ ap.config(ssid='RP2-AP') # set the SSID of the access point
+ ap.config(max_clients=10) # set how many clients can connect to the network
+ ap.active(True) # activate the interface
+
+A useful function for connecting to your local WiFi network is::
+
+ def do_connect():
+ import machine, network
+ wlan = network.WLAN()
+ wlan.active(True)
+ if not wlan.isconnected():
+ print('connecting to network...')
+ wlan.connect('ssid', 'key')
+ while not wlan.isconnected():
+ machine.idle()
+ print('network config:', wlan.ipconfig('addr4'))
+
+Once the network is established the :mod:`socket <socket>` module can be used
+to create and use TCP/UDP sockets as usual, and the ``requests`` module for
+convenient HTTP requests.
+
+After a call to ``wlan.connect()``, the device will by default retry to connect
+**forever**, even when the authentication failed or no AP is in range.
+``wlan.status()`` will return ``network.STAT_CONNECTING`` in this state until a
+connection succeeds or the interface gets disabled.
+
+.. _Pico W: https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#picow-technical-specification
+.. _Pico 2 W: https://www.raspberrypi.com/documentation/microcontrollers/pico-series.html#pico2w-technical-specification
+
Delay and timing
----------------