Tue, 06 May 2025 15:32:29 +0200
Various changes and optimizations to the MicroPython support.
9878 | 1 | try: |
2 | import wiznet_config | |
3 | ||
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
4 | def connect_lan(): |
9878 | 5 | import network |
6 | import time | |
7 | from machine import Pin, SPI | |
8 | ||
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
9 | try: |
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
10 | ifconfig = wiznet_config.ifconfig |
10153 | 11 | hostname = wiznet_config.hostname |
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
12 | except AttributeError: |
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
13 | print("The network configuration in 'wiznet_config.py' is invalid.") |
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
14 | return None |
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
15 | |
10153 | 16 | if hostname: |
17 | try: | |
18 | network.hostname(hostname) | |
19 | except AttributeError: | |
20 | pass | |
21 | ||
9878 | 22 | spi = SPI(0, 2_000_000, mosi=Pin(19), miso=Pin(16), sck=Pin(18)) |
23 | nic = network.WIZNET5K(spi, Pin(17), Pin(20)) | |
24 | ||
25 | nic.active(False) | |
26 | nic.active(True) | |
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
27 | nic.ifconfig(ifconfig) |
9878 | 28 | max_wait = 140 |
11270
0e220c26e60e
Various changes and optimizations to the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10153
diff
changeset
|
29 | while max_wait and not nic.isconnected(): |
9878 | 30 | max_wait -= 1 |
31 | time.sleep(0.1) | |
32 | ||
33 | if nic.isconnected(): | |
34 | print("Connected to LAN:", nic.ifconfig()) | |
35 | else: | |
36 | print("Connection to LAN failed.") | |
37 | ||
38 | return nic | |
39 | ||
40 | except ImportError: | |
41 | print( | |
42 | "The network configuration is kept in 'wiznet_config.py'. Please add it there." | |
43 | ) | |
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
44 | def connect_lan(): |
9878 | 45 | return None |