-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnetconfig.py
163 lines (133 loc) · 4.94 KB
/
netconfig.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Connect a MicroPython board to an IP network."""
import json
import network
import time
# Configuration defaults
# ----------------------
#
# Which interface to connect with ('wifi', 'lan', 'wiznet')
# Available interfaces depend on MicroPython board / firmware used
INTERFACE = "wifi"
# Enable network. If False, interface is deactivated
ENABLED = True
# Try to connect with the custom network config for this many seconds
# A value of -1 means re-try forever until connected
TIMEOUT = 5
# How to configure the IP network ('static' / 'dhcp')
IP_CONFIG = "dhcp"
# Static IP configuration (ignored if IP_CONFIG is 'dhcp')
IP_ADDRESS = "192.168.1.100"
SUBNET_MASK = "255.255.255.0"
GATEWAY = "192.168.1.0"
DNS = "192.168.1.0"
# For WiFi
# Try to connect using the default network config saved in flash
# to reduce flash wear
USE_DEFAULT = True
# Try to connect with the saved default network config for this many seconds
# A value of -1 means re-try forever until connected
TIMEOUT_DEFAULT_CONFIG = 5
# For WIZNET5K interface
# Number of the SPI bus the Wiznet 5x00 chip/board is connected to
SPI_BUS = 1
# The board pin connected to the Wiznet's reset (RST) pin
PIN_RST = "A3"
# The board pin connected to the Wiznet's slave select (SS/SCS) pin
PIN_SS = "A4"
def connect(configfile, reconfigure=False):
"""Try to connect to an Ethernet or WiFi network.
Reads the network configuration (interface, IP config, SSID, password,
etc.) from the JSON file specified with the `configfile` argument.
If the network interface is not already connected or if the `reconfigure`
argument is True, it sets up the network according to the settings in that
file.
Returns the network interface object instance.
Example configuration file contents:
{
"interface": "wifi"
"ssid": "My WLAN",
"password": "XXXXXXXX",
"use_default": 1,
"timeout_default_config": 3,
"timeout": 8
}
or, for ethernet using the WIZNET5K driver with DHCP:
{
"interface": "wiznet",
"ip_config": "dhcp"
"spi_bus": 2,
"pin_rst": "B11",
"pin_ss": "B12",
"timeout": 3,
}
or with static IP configuration:
{
"interface": "wiznet",
"ip_config": "static"
"spi_bus": 1,
"pin_rst": "A3",
"pin_ss": "A4",
"ip_address": "192.168.1.100",
"subnet_mask": "255.255.255.0",
"gateway": "192.168.1.1",
"dns": "192.168.1.1",
}
"""
try:
with open(configfile) as fp:
config = json.loads(fp.read())
if not isinstance(config, dict):
raise ValueError
except (OSError, ValueError):
config = {}
interface = config.get("interface", INTERFACE).lower()
print("Using '%s' interface." % interface)
if interface == "wifi":
net_if = network.WLAN(network.STA_IF)
elif interface == "lan":
net_if = network.LAN()
elif interface == "wiznet":
from machine import Pin, SPI
pin_rst = config.get("pin_rst", PIN_RST)
pin_ss = config.get("pin_ss", PIN_SS)
spi_bus = int(config.get("spi_bus", SPI_BUS))
net_if = network.WIZNET5K(SPI(spi_bus), Pin(pin_ss), Pin(pin_rst))
if not config.get("enabled", ENABLED):
net_if.active(False)
print("Interface disabled.")
return False
else:
print("Interface enabled.")
net_if.active(True)
if interface == "wifi" and config.get("use_default", USE_DEFAULT):
print("Trying saved default WiFi configuration.")
timeout = int(config.get("timeout_default_config", TIMEOUT_DEFAULT_CONFIG))
while timeout != 0 and not net_if.isconnected():
time.sleep(1)
timeout = max(-1, timeout - 1)
if not net_if.isconnected() and interface == "wifi" and "ssid" in config:
# If we can't use default wifi config, use wifi from config
print("Connecting to WiFi network '%s'..." % config["ssid"])
net_if.connect(
config["ssid"], config.get("password"), bssid=config.get("bssid")
)
timeout = int(config.get("timeout", TIMEOUT))
while timeout != 0 and not net_if.isconnected():
time.sleep(1)
timeout = max(-1, timeout - 1)
ip_config = config.get("ip_config", IP_CONFIG).lower()
if reconfigure or not net_if.isconnected():
if ip_config == "static":
print("Setting static IP network configuration.")
net_if.ifconfig(
(
config.get("ip_address", IP_ADDRESS),
config.get("subnet_mask", SUBNET_MASK),
config.get("gateway", GATEWAY),
config.get("dns", DNS),
)
)
else:
print("Configuring IP network via DHCP...")
net_if.ifconfig("dhcp")
return net_if