@@ -19,6 +19,8 @@ class ESP_SPIcontrol:
19
19
SCAN_NETWORKS = const (0x27 )
20
20
GET_IDX_RSSI_CMD = const (0x32 )
21
21
GET_IDX_ENCT_CMD = const (0x33 )
22
+ REQ_HOST_BY_NAME_CMD = const (0x34 )
23
+ GET_HOST_BY_NAME_CMD = const (0x35 )
22
24
START_SCAN_NETWORKS = const (0x36 )
23
25
24
26
GET_FW_VERSION_CMD = const (0x37 )
@@ -155,7 +157,8 @@ def wait_response_cmd(self, cmd, num_responses=None):
155
157
for num in range (num_responses ):
156
158
response = []
157
159
param_len = self .get_param ()
158
- print ("parameter #%d length is %d" % (num , param_len ))
160
+ if self ._debug :
161
+ print ("parameter #%d length is %d" % (num , param_len ))
159
162
for j in range (param_len ):
160
163
response .append (self .get_param ())
161
164
responses .append (bytes (response ))
@@ -170,25 +173,30 @@ def send_command_get_response(self, cmd, params=None, *, reply_params=1):
170
173
171
174
@property
172
175
def status (self ):
173
- print ("Connection status" )
176
+ if self ._debug :
177
+ print ("Connection status" )
174
178
resp = self .send_command_get_response (GET_CONN_STATUS_CMD )
175
- print ("Status:" , resp [0 ][0 ])
179
+ if self ._debug :
180
+ print ("Status:" , resp [0 ][0 ])
176
181
return resp [0 ][0 ] # one byte response
177
182
178
183
@property
179
184
def firmware_version (self ):
180
- print ("Firmware version" )
185
+ if self ._debug :
186
+ print ("Firmware version" )
181
187
resp = self .send_command_get_response (GET_FW_VERSION_CMD )
182
188
return resp [0 ]
183
189
184
190
@property
185
191
def MAC_address (self ):
186
- print ("MAC address" )
192
+ if self ._debug :
193
+ print ("MAC address" )
187
194
resp = self .send_command_get_response (GET_MACADDR_CMD , [b'\xFF ' ])
188
195
return resp [0 ]
189
196
190
197
def start_scan_networks (self ):
191
- print ("Start scan" )
198
+ if self ._debug :
199
+ print ("Start scan" )
192
200
resp = self .send_command_get_response (START_SCAN_NETWORKS )
193
201
if resp [0 ][0 ] != 1 :
194
202
raise RuntimeError ("Failed to start AP scan" )
@@ -218,15 +226,12 @@ def scan_networks(self):
218
226
return APs
219
227
220
228
def wifi_set_network (self , ssid ):
221
- print ("Set Network" )
222
229
resp = self .send_command_get_response (SET_NET_CMD , [ssid ])
223
230
if resp [0 ][0 ] != 1 :
224
231
raise RuntimeError ("Failed to set network" )
225
232
226
233
def wifi_set_passphrase (self , ssid , passphrase ):
227
- print ("Set passphrase" )
228
234
resp = self .send_command_get_response (SET_PASSPHRASE_CMD , [ssid , passphrase ])
229
- print (resp )
230
235
if resp [0 ][0 ] != 1 :
231
236
raise RuntimeError ("Failed to set passphrase" )
232
237
@@ -247,11 +252,11 @@ def network_data(self):
247
252
248
253
@property
249
254
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 ])
255
+ return self .network_data ['ip_addr' ]
252
256
253
257
def connect_AP (self , ssid , password ):
254
- print ("Connect AP" )
258
+ if self ._debug :
259
+ print ("Connect to AP" )
255
260
if password :
256
261
self .wifi_set_passphrase (ssid , password )
257
262
else :
@@ -266,3 +271,16 @@ def connect_AP(self, ssid, password):
266
271
if stat == WL_NO_SSID_AVAIL :
267
272
raise RuntimeError ("No such ssid" , ssid )
268
273
raise RuntimeError ("Unknown error 0x%02X" % stat )
274
+
275
+ def pretty_ip (self , ip ):
276
+ return "%d.%d.%d.%d" % (ip [0 ], ip [1 ], ip [2 ], ip [3 ])
277
+
278
+ def get_host_by_name (self , hostname ):
279
+ if isinstance (hostname , str ):
280
+ hostname = bytes (hostname , 'utf-8' )
281
+ self ._debug = True
282
+ resp = self .send_command_get_response (REQ_HOST_BY_NAME_CMD , [hostname ])
283
+ if resp [0 ][0 ] != 1 :
284
+ raise RuntimeError ("Failed to request hostname" )
285
+ resp = self .send_command_get_response (GET_HOST_BY_NAME_CMD )
286
+ return resp [0 ]
0 commit comments