6
6
from micropython import const
7
7
8
8
class ESP_SPIcontrol :
9
+ SET_NET_CMD = const (0x10 )
10
+ SET_PASSPHRASE_CMD = const (0x11 )
11
+
9
12
GET_CONN_STATUS_CMD = const (0x20 )
13
+ GET_IPADDR_CMD = const (0x21 )
10
14
GET_MACADDR_CMD = const (0x22 )
15
+ GET_CURR_SSID_CMD = const (0x23 )
16
+ GET_CURR_RSSI_CMD = const (0x25 )
17
+ GET_CURR_ENCT_CMD = const (0x26 )
18
+
11
19
SCAN_NETWORKS = const (0x27 )
12
20
GET_IDX_RSSI_CMD = const (0x32 )
13
21
GET_IDX_ENCT_CMD = const (0x33 )
14
22
START_SCAN_NETWORKS = const (0x36 )
23
+
15
24
GET_FW_VERSION_CMD = const (0x37 )
16
25
17
26
START_CMD = const (0xE0 )
@@ -23,9 +32,12 @@ class ESP_SPIcontrol:
23
32
WL_NO_SHIELD = const (0xFF )
24
33
WL_NO_MODULE = const (0xFF )
25
34
WL_IDLE_STATUS = const (0 )
35
+ WL_NO_SSID_AVAIL = const (1 )
36
+ WL_SCAN_COMPLETED = const (2 )
37
+ WL_CONNECTED = const (3 )
26
38
27
- def __init__ (self , spi , cs_pin , ready_pin , reset_pin , gpio0_pin , * , debug = True ):
28
- self .debug = debug
39
+ def __init__ (self , spi , cs_pin , ready_pin , reset_pin , gpio0_pin , * , debug = False ):
40
+ self ._debug = debug
29
41
self ._buffer = bytearray (10 )
30
42
self ._pbuf = bytearray (1 ) # buffer for param read
31
43
@@ -69,17 +81,19 @@ def slave_ready(self):
69
81
return self ._ready .value == False
70
82
71
83
def wait_for_slave_ready (self ):
72
- print ("Wait for slave ready" , end = '' )
84
+ if self ._debug :
85
+ print ("Wait for slave ready" , end = '' )
73
86
while not self .slave_ready ():
74
- print ('.' , end = '' )
87
+ if self ._debug :
88
+ print ('.' , end = '' )
75
89
time .sleep (0.01 )
76
- print ()
90
+ if self ._debug :
91
+ print ()
77
92
78
93
def wait_for_slave_select (self ):
79
94
self .wait_for_slave_ready ()
80
95
self .spi_slave_select ()
81
96
82
-
83
97
def send_command (self , cmd , params = None ):
84
98
if not params :
85
99
params = []
@@ -94,18 +108,21 @@ def send_command(self, cmd, params=None):
94
108
packet += (param )
95
109
96
110
packet .append (END_CMD )
97
- print ("packet len:" , len (packet ))
111
+ if self ._debug :
112
+ print ("packet len:" , len (packet ))
98
113
while len (packet ) % 4 != 0 :
99
114
packet .append (0xFF )
100
115
101
116
self .wait_for_slave_select ()
102
117
self ._spi .write (bytearray (packet ))
103
- print ("Wrote: " , [hex (b ) for b in packet ])
118
+ if self ._debug :
119
+ print ("Wrote: " , [hex (b ) for b in packet ])
104
120
self .slave_deselect ()
105
121
106
122
def get_param (self ):
107
123
self ._spi .readinto (self ._pbuf )
108
- print ("Read param" , hex (self ._pbuf [0 ]))
124
+ if self ._debug :
125
+ print ("Read param" , hex (self ._pbuf [0 ]))
109
126
return self ._pbuf [0 ]
110
127
111
128
def wait_spi_char (self , desired ):
@@ -151,18 +168,21 @@ def send_command_get_response(self, cmd, params=None, *, reply_params=1):
151
168
self .send_command (cmd , params )
152
169
return self .wait_response_cmd (cmd , reply_params )
153
170
154
- def get_connection_status (self ):
171
+ @property
172
+ def status (self ):
155
173
print ("Connection status" )
156
174
resp = self .send_command_get_response (GET_CONN_STATUS_CMD )
157
175
print ("Status:" , resp [0 ][0 ])
158
176
return resp [0 ][0 ] # one byte response
159
177
160
- def get_firmware_version (self ):
178
+ @property
179
+ def firmware_version (self ):
161
180
print ("Firmware version" )
162
181
resp = self .send_command_get_response (GET_FW_VERSION_CMD )
163
182
return resp [0 ]
164
183
165
- def get_MAC (self ):
184
+ @property
185
+ def MAC_address (self ):
166
186
print ("MAC address" )
167
187
resp = self .send_command_get_response (GET_MACADDR_CMD , [b'\xFF ' ])
168
188
return resp [0 ]
@@ -196,3 +216,53 @@ def scan_networks(self):
196
216
if len (APs ):
197
217
break
198
218
return APs
219
+
220
+ def wifi_set_network (self , ssid ):
221
+ print ("Set Network" )
222
+ resp = self .send_command_get_response (SET_NET_CMD , [ssid ])
223
+ if resp [0 ][0 ] != 1 :
224
+ raise RuntimeError ("Failed to set network" )
225
+
226
+ def wifi_set_passphrase (self , ssid , passphrase ):
227
+ print ("Set passphrase" )
228
+ resp = self .send_command_get_response (SET_PASSPHRASE_CMD , [ssid , passphrase ])
229
+ print (resp )
230
+ if resp [0 ][0 ] != 1 :
231
+ raise RuntimeError ("Failed to set passphrase" )
232
+
233
+ @property
234
+ def ssid (self ):
235
+ resp = self .send_command_get_response (GET_CURR_SSID_CMD , [b'\xFF ' ])
236
+ return resp [0 ]
237
+
238
+ @property
239
+ def rssi (self ):
240
+ resp = self .send_command_get_response (GET_CURR_RSSI_CMD , [b'\xFF ' ])
241
+ return struct .unpack ('<i' , resp [0 ])[0 ]
242
+
243
+ @property
244
+ def network_data (self ):
245
+ resp = self .send_command_get_response (GET_IPADDR_CMD , [b'\xFF ' ], reply_params = 3 )
246
+ return {'ip_addr' : resp [0 ], 'netmask' : resp [1 ], 'gateway' : resp [2 ]}
247
+
248
+ @property
249
+ def ip_address (self ):
250
+ raw_ip = self .network_data ['ip_addr' ]
251
+ return "%d.%d.%d.%d" % (raw_ip [0 ], raw_ip [1 ], raw_ip [2 ], raw_ip [3 ])
252
+
253
+ def connect_AP (self , ssid , password ):
254
+ print ("Connect AP" )
255
+ if password :
256
+ self .wifi_set_passphrase (ssid , password )
257
+ else :
258
+ self .wifi_set_network (ssid )
259
+ for i in range (10 ): # retries
260
+ stat = self .status
261
+ if stat == WL_CONNECTED :
262
+ return stat
263
+ time .sleep (1 )
264
+ if stat in (WL_CONNECT_FAILED , WL_CONNECTION_LOST , WL_DISCONNECTED ):
265
+ raise RuntimeError ("Failed to connect to ssid" , ssid )
266
+ if stat == WL_NO_SSID_AVAIL :
267
+ raise RuntimeError ("No such ssid" , ssid )
268
+ raise RuntimeError ("Unknown error 0x%02X" % stat )
0 commit comments