Tue, 14 Mar 2023 13:19:33 +0100
Changed the MCU script to a pythonic naming.
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 |
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
11 | except AttributeError: |
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
12 | 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
|
13 | return None |
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
14 | |
9878 | 15 | spi = SPI(0, 2_000_000, mosi=Pin(19), miso=Pin(16), sck=Pin(18)) |
16 | nic = network.WIZNET5K(spi, Pin(17), Pin(20)) | |
17 | ||
18 | nic.active(False) | |
19 | nic.active(True) | |
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
20 | nic.ifconfig(ifconfig) |
9878 | 21 | max_wait = 140 |
22 | while max_wait: | |
23 | if nic.isconnected(): | |
24 | break | |
25 | max_wait -= 1 | |
26 | time.sleep(0.1) | |
27 | ||
28 | if nic.isconnected(): | |
29 | print("Connected to LAN:", nic.ifconfig()) | |
30 | else: | |
31 | print("Connection to LAN failed.") | |
32 | ||
33 | return nic | |
34 | ||
35 | except ImportError: | |
36 | print( | |
37 | "The network configuration is kept in 'wiznet_config.py'. Please add it there." | |
38 | ) | |
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9878
diff
changeset
|
39 | def connect_lan(): |
9878 | 40 | return None |