@@ -14,21 +14,124 @@ module.
1414
1515For example::
1616
17- # configure a specific network interface
17+ # connect/ show IP config a specific network interface
1818 # see below for examples of specific drivers
1919 import network
20+ import utime
2021 nic = network.Driver(...)
22+ if not nic.isconnected():
23+ nic.connect()
24+ print("Waiting for connection...")
25+ while not nic.isconnected():
26+ utime.sleep(1)
2127 print(nic.ifconfig())
2228
23- # now use socket as usual
24- import socket
29+ # now use usocket as usual
30+ import usocket as socket
2531 addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
2632 s = socket.socket()
2733 s.connect(addr)
2834 s.send(b'GET / HTTP/1.1\r\nHost: micropython.org\r\n\r\n')
2935 data = s.recv(1000)
3036 s.close()
3137
38+ Common network adapter interface
39+ ================================
40+
41+ This section describes an (implied) abstract base class for all network
42+ interface classes implemented by different ports of MicroPython for
43+ different hardware. This means that MicroPython does not actually
44+ provide `AbstractNIC ` class, but any actual NIC class, as described
45+ in the following sections, implements methods as described here.
46+
47+ .. class :: AbstractNIC(id=None, ...)
48+
49+ Instantiate a network interface object. Parameters are network interface
50+ dependent. If there are more than one interface of the same type, the first
51+ parameter should be `id `.
52+
53+ .. method :: active([is_active])
54+
55+ Activate ("up") or deactivate ("down") the network interface, if
56+ a boolean argument is passed. Otherwise, query current state if
57+ no argument is provided. Most other methods require an active
58+ interface (behavior of calling them on inactive interface is
59+ undefined).
60+
61+ .. method :: connect([service_id, key=None, \*, ...])
62+
63+ Connect the interface to a network. This method is optional, and
64+ available only for interfaces which are not "always connected".
65+ If no parameters are given, connect to the default (or the only)
66+ service. If a single parameter is given, it is the primary identifier
67+ of a service to connect to. It may be accompanied by a key
68+ (password) required to access said service. There can be further
69+ arbitrary keyword-only parameters, depending on the networking medium
70+ type and/or particular device. Parameters can be used to: a)
71+ specify alternative service identifer types; b) provide additional
72+ connection parameters. For various medium types, there are different
73+ sets of predefined/recommended parameters, among them:
74+
75+ * WiFi: `bssid ` keyword to connect by BSSID (MAC address) instead
76+ of access point name
77+
78+ .. method :: disconnect()
79+
80+ Disconnect from network.
81+
82+ .. method :: isconnected()
83+
84+ Returns ``True `` if connected to network, otherwise returns ``False ``.
85+
86+ .. method :: scan(\*, ...)
87+
88+ Scan for the available network services/connections. Returns a
89+ list of tuples with discovered service parameters. For various
90+ network media, there are different variants of predefined/
91+ recommended tuple formats, among them:
92+
93+ * WiFi: (ssid, bssid, channel, RSSI, authmode, hidden). There
94+ may be further fields, specific to a particular device.
95+
96+ The function may accept additional keyword arguments to filter scan
97+ results (e.g. scan for a particular service, on a particular channel,
98+ for services of a particular set, etc.), and to affect scan
99+ duration and other parameters. Where possible, parameter names
100+ should match those in connect().
101+
102+ .. method :: status()
103+
104+ Return detailed status of the interface, values are dependent
105+ on the network medium/technology.
106+
107+ .. method :: ifconfig([(ip, subnet, gateway, dns)])
108+
109+ Get/set IP-level network interface parameters: IP address, subnet mask,
110+ gateway and DNS server. When called with no arguments, this method returns
111+ a 4-tuple with the above information. To set the above values, pass a
112+ 4-tuple with the required information. For example::
113+
114+ nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
115+
116+ .. method :: config('param')
117+ config(param=value, ...)
118+
119+ Get or set general network interface parameters. These methods allow to work
120+ with additional parameters beyond standard IP configuration (as dealt with by
121+ ``ifconfig() ``). These include network-specific and hardware-specific
122+ parameters and status values. For setting parameters, the keyword argument
123+ syntax should be used, and multiple parameters can be set at once. For
124+ querying, a parameter name should be quoted as a string, and only one
125+ parameter can be queried at a time::
126+
127+ # Set WiFi access point name (formally known as ESSID) and WiFi channel
128+ ap.config(essid='My AP', channel=11)
129+ # Query params one by one
130+ print(ap.config('essid'))
131+ print(ap.config('channel'))
132+ # Extended status information also available this way
133+ print(sta.config('rssi'))
134+
32135.. only :: port_pyboard
33136
34137 class CC3K
0 commit comments