-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (70 loc) · 2.08 KB
/
main.py
File metadata and controls
85 lines (70 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# main.py — Connect to WiFi and run the USB Ethernet bridge
import network
import machine
import time
import gc
import config
from netstack import Bridge
from ecm_interface import ECMInterface
ecm = ECMInterface._instance
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print("[wifi] Connecting to", config.WIFI_SSID)
wlan.connect(config.WIFI_SSID, config.WIFI_PASS)
for _ in range(30):
if wlan.isconnected():
break
time.sleep(1)
if wlan.isconnected():
ip_info = wlan.ifconfig()
print("[wifi] Connected:", ip_info[0])
return wlan, ip_info
else:
print("[wifi] Failed to connect")
return wlan, None
def main():
led = None
try:
led = machine.Pin("LED", machine.Pin.OUT)
except Exception:
pass
if led:
led.on()
wlan, ip_info = connect_wifi()
if not ip_info:
while not wlan.isconnected():
if led:
led.toggle()
time.sleep(2)
wlan.connect(config.WIFI_SSID, config.WIFI_PASS)
for _ in range(15):
if wlan.isconnected():
break
time.sleep(1)
ip_info = wlan.ifconfig()
print("[wifi] Connected:", ip_info[0])
if led:
led.off()
dns = ip_info[3] if ip_info[3] != "0.0.0.0" else config.DNS_FALLBACK
print("[bridge] DNS server:", dns)
print(
"[bridge] USB network: host={} gw={}".format(config.USB_HOST_IP, config.USB_IP)
)
bridge = Bridge(ecm, dns_server=dns)
ecm._bridge = bridge
print("[bridge] Running — plug USB into host computer")
gc.collect()
gc_counter = 0
while True:
bridge.process()
time.sleep(0)
gc_counter += 1
if gc_counter >= 100:
gc.collect()
gc_counter = 0
if gc_counter == 50 and not wlan.isconnected():
print("[wifi] Reconnecting...")
wlan.connect(config.WIFI_SSID, config.WIFI_PASS)
main()