1
- import network
1
+ """
2
+ The network manager class, manages reads/writes/connections, etc..
3
+ It supports static addresses and dhcp, but only supports ipv4
4
+ That is due to the underlying micropython methods
5
+ """
2
6
import socket
3
7
from time import sleep
4
8
import machine
9
+ import network
5
10
6
11
WIFI_TIMEOUT = 10
7
12
8
13
class NetMgr :
9
-
10
14
led = None
11
15
wlan = None
12
16
ssid = ""
@@ -15,7 +19,7 @@ class NetMgr:
15
19
connection = None
16
20
PAGE_SIZE = 4096
17
21
verbose = False
18
-
22
+
19
23
def __init__ (self ,ssid ,passwd ,ifconfig = None ,verbose = False ):
20
24
self .ssid = ssid
21
25
self .passw = passwd
@@ -30,17 +34,17 @@ def __init__(self,ssid,passwd,ifconfig=None,verbose=False):
30
34
self .wlan .connect (ssid ,passwd )
31
35
if verbose :
32
36
print ("NetMgr::__init__" )
33
-
37
+
34
38
def __del__ (self ):
35
39
if verbose :
36
40
print ("NetMgr::__del__" )
37
41
self .connection .close ()
38
42
self .wlan .disconnect ()
39
43
self .led .off ()
40
-
44
+
41
45
def is_connected (self ):
42
46
return self .wlan .isconnected ()
43
-
47
+
44
48
def if_config (self ):
45
49
return self .wlan .ifconfig ()
46
50
@@ -64,7 +68,7 @@ def wait_for_connected(self):
64
68
self .net_fail ()
65
69
66
70
return self .wlan .isconnected ()
67
-
71
+
68
72
def bind_to_port (self ,port ):
69
73
if self .verbose :
70
74
print (f"NetMgr::bind_to_port({ port } )" )
@@ -94,7 +98,7 @@ def next_request(self):
94
98
if self .verbose :
95
99
print (f"NetMgr::next_request Connection from { acc [1 ]} " )
96
100
return acc [0 ]
97
-
101
+
98
102
def read_all (self ,sock ):
99
103
rv = ""
100
104
inbytes = sock .recv (NetMgr .PAGE_SIZE )
@@ -105,7 +109,7 @@ def read_all(self,sock):
105
109
if self .verbose :
106
110
print (f"NetMgr::read_all Read { len (rv )} bytes" )
107
111
return rv
108
-
112
+
109
113
def read (self ,sock ,sz ):
110
114
if sz == - 1 :
111
115
return self .read_all (sock )
@@ -118,7 +122,7 @@ def read(self,sock,sz):
118
122
if self .verbose :
119
123
print (f"NetMgr::read Read { len (rv )} bytes" )
120
124
return rv
121
-
125
+
122
126
def write (self ,sock : socket .socket ,data : bytes ):
123
127
sz = len (data )
124
128
sent = sock .write (data )
@@ -127,7 +131,7 @@ def write(self,sock: socket.socket,data: bytes):
127
131
if self .verbose :
128
132
print (f"NetMgr::write Wrote { sent } bytes" )
129
133
return sent
130
-
134
+
131
135
def close (self ):
132
136
if self .verbose :
133
137
print ("NetMgr::close" )
@@ -136,7 +140,7 @@ def close(self):
136
140
self .connection = None
137
141
self .wlan .disconnect ()
138
142
self .led .off ()
139
-
143
+
140
144
def net_fail (self ):
141
145
while ( 1 == 1 ):
142
146
sleep (0.25 )
0 commit comments