|
5 | 5 | .. module:: network |
6 | 6 | :synopsis: network configuration |
7 | 7 |
|
8 | | -This module provides network drivers and routing configuration. |
| 8 | +This module provides network drivers and routing configuration. Network |
| 9 | +drivers for specific hardware are available within this module and are |
| 10 | +used to configure a hardware network interface. Configured interfaces |
| 11 | +are then available for use via the :mod:`socket` module. |
| 12 | + |
| 13 | +For example:: |
| 14 | + |
| 15 | + # configure a specific network interface |
| 16 | + # see below for examples of specific drivers |
| 17 | + import network |
| 18 | + nic = network.Driver(...) |
| 19 | + print(nic.ifconfig()) |
| 20 | + |
| 21 | + # now use socket as usual |
| 22 | + import socket |
| 23 | + addr = socket.getaddrinfo('micropython.org', 80)[0][-1] |
| 24 | + s = socket.socket() |
| 25 | + s.connect(addr) |
| 26 | + s.send(b'GET / HTTP/1.1\r\nHost: micropython.org\r\n\r\n') |
| 27 | + data = s.recv(1000) |
| 28 | + s.close() |
| 29 | + |
| 30 | +class CC3K |
| 31 | +========== |
9 | 32 |
|
| 33 | +This class provides a driver for CC3000 wifi modules. Example usage:: |
10 | 34 |
|
11 | | -class CC3k |
12 | | -========== |
| 35 | + import network |
| 36 | + nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3) |
| 37 | + nic.connect('your-ssid', 'your-password') |
| 38 | + while not nic.isconnected(): |
| 39 | + pyb.delay(50) |
| 40 | + print(nic.ifconfig()) |
| 41 | + |
| 42 | + # now use socket as usual |
| 43 | + ... |
| 44 | + |
| 45 | +For this example to work the CC3000 module must have the following connections: |
| 46 | + |
| 47 | + - MOSI connected to Y8 |
| 48 | + - MISO connected to Y7 |
| 49 | + - CLK connected to Y6 |
| 50 | + - CS connected to Y5 |
| 51 | + - VBEN connected to Y4 |
| 52 | + - IRQ connected to Y3 |
| 53 | + |
| 54 | +It is possible to use other SPI busses and other pins for CS, VBEN and IRQ. |
13 | 55 |
|
14 | 56 | Constructors |
15 | 57 | ------------ |
16 | 58 |
|
17 | | -.. class:: CC3k(spi, pin_cs, pin_en, pin_irq) |
| 59 | +.. class:: CC3K(spi, pin_cs, pin_en, pin_irq) |
| 60 | + |
| 61 | + Create a CC3K driver object, initialise the CC3000 module using the given SPI bus |
| 62 | + and pins, and return the CC3K object. |
| 63 | + |
| 64 | + Arguments are: |
18 | 65 |
|
19 | | - Initialise the CC3000 using the given SPI bus and pins and return a CC3k object. |
| 66 | + - ``spi`` is an :ref:`SPI object <pyb.SPI>` which is the SPI bus that the CC3000 is |
| 67 | + connected to (the MOSI, MISO and CLK pins). |
| 68 | + - ``pin_cs`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 CS pin. |
| 69 | + - ``pin_en`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 VBEN pin. |
| 70 | + - ``pin_irq`` is a :ref:`Pin object <pyb.Pin>` which is connected to the CC3000 IRQ pin. |
20 | 71 |
|
| 72 | + All of these objects will be initialised by the driver, so there is no need to |
| 73 | + initialise them yourself. For example, you can use:: |
| 74 | + |
| 75 | + nic = network.CC3K(pyb.SPI(2), pyb.Pin.board.Y5, pyb.Pin.board.Y4, pyb.Pin.board.Y3) |
21 | 76 |
|
22 | 77 | Methods |
23 | 78 | ------- |
24 | 79 |
|
25 | 80 | .. method:: cc3k.connect(ssid, key=None, \*, security=WPA2, bssid=None) |
26 | 81 |
|
| 82 | + Connect to a wifi access point using the given SSID, and other security |
| 83 | + parameters. |
| 84 | + |
| 85 | +.. method:: cc3k.disconnect() |
| 86 | + |
| 87 | + Disconnect from the wifi access point. |
| 88 | + |
| 89 | +.. method:: cc3k.isconnected() |
| 90 | + |
| 91 | + Returns True if connected to a wifi access point and has a valid IP address, |
| 92 | + False otherwise. |
| 93 | + |
| 94 | +.. method:: cc3k.ifconfig() |
| 95 | + |
| 96 | + Returns a 7-tuple with (ip, subnet mask, gateway, DNS server, DHCP server, |
| 97 | + MAC address, SSID). |
| 98 | + |
| 99 | +.. method:: cc3k.patch_version() |
27 | 100 |
|
28 | | -class WIZnet5k |
| 101 | + Return the version of the patch program (firmware) on the CC3000. |
| 102 | + |
| 103 | +.. method:: cc3k.patch_program('pgm') |
| 104 | + |
| 105 | + Upload the current firmware to the CC3000. You must pass 'pgm' as the first |
| 106 | + argument in order for the upload to proceed. |
| 107 | + |
| 108 | +Constants |
| 109 | +--------- |
| 110 | + |
| 111 | +.. data:: CC3K.WEP |
| 112 | +.. data:: CC3K.WPA |
| 113 | +.. data:: CC3K.WPA2 |
| 114 | + |
| 115 | + security type to use |
| 116 | + |
| 117 | +class WIZNET5K |
29 | 118 | ============== |
30 | 119 |
|
31 | 120 | This class allows you to control WIZnet5x00 Ethernet adaptors based on |
32 | 121 | the W5200 and W5500 chipsets (only W5200 tested). |
33 | 122 |
|
34 | 123 | Example usage:: |
35 | 124 |
|
36 | | - import wiznet5k |
37 | | - w = wiznet5k.WIZnet5k() |
38 | | - print(w.ipaddr()) |
39 | | - w.gethostbyname('micropython.org') |
40 | | - s = w.socket() |
41 | | - s.connect(('192.168.0.2', 8080)) |
42 | | - s.send('hello') |
43 | | - print(s.recv(10)) |
| 125 | + import network |
| 126 | + nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4) |
| 127 | + print(nic.ifconfig()) |
| 128 | + |
| 129 | + # now use socket as usual |
| 130 | + ... |
| 131 | + |
| 132 | +For this example to work the WIZnet5x00 module must have the following connections: |
44 | 133 |
|
| 134 | + - MOSI connected to X8 |
| 135 | + - MISO connected to X7 |
| 136 | + - SCLK connected to X6 |
| 137 | + - nSS connected to X5 |
| 138 | + - nRESET connected to X4 |
| 139 | + |
| 140 | +It is possible to use other SPI busses and other pins for nSS and nRESET. |
45 | 141 |
|
46 | 142 | Constructors |
47 | 143 | ------------ |
48 | 144 |
|
49 | | -.. class:: WIZnet5k(spi, pin_cs, pin_rst) |
| 145 | +.. class:: WIZNET5K(spi, pin_cs, pin_rst) |
| 146 | + |
| 147 | + Create a WIZNET5K driver object, initialise the WIZnet5x00 module using the given |
| 148 | + SPI bus and pins, and return the WIZNET5K object. |
| 149 | + |
| 150 | + Arguments are: |
| 151 | + |
| 152 | + - ``spi`` is an :ref:`SPI object <pyb.SPI>` which is the SPI bus that the WIZnet5x00 is |
| 153 | + connected to (the MOSI, MISO and SCLK pins). |
| 154 | + - ``pin_cs`` is a :ref:`Pin object <pyb.Pin>` which is connected to the WIZnet5x00 nSS pin. |
| 155 | + - ``pin_rst`` is a :ref:`Pin object <pyb.Pin>` which is connected to the WIZnet5x00 nRESET pin. |
50 | 156 |
|
51 | | - Create and return a WIZnet5k object. |
| 157 | + All of these objects will be initialised by the driver, so there is no need to |
| 158 | + initialise them yourself. For example, you can use:: |
52 | 159 |
|
| 160 | + nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4) |
53 | 161 |
|
54 | 162 | Methods |
55 | 163 | ------- |
56 | 164 |
|
57 | | -.. method:: wiznet5k.ipaddr([(ip, subnet, gateway, dns)]) |
| 165 | +.. method:: wiznet5k.ifconfig([(ip, subnet, gateway, dns)]) |
58 | 166 |
|
59 | 167 | Get/set IP address, subnet mask, gateway and DNS. |
60 | 168 |
|
| 169 | + When called with no arguments, this method returns a 4-tuple with the above information. |
| 170 | + |
| 171 | + To set the above values, pass a 4-tuple with the required information. For example:: |
| 172 | + |
| 173 | + nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')) |
| 174 | + |
61 | 175 | .. method:: wiznet5k.regs() |
62 | 176 |
|
63 | | - Dump WIZnet5k registers. |
| 177 | + Dump the WIZnet5x00 registers. Useful for debugging. |
0 commit comments