Skip to content

Commit f33420f

Browse files
committed
nslookup
1 parent 3e19631 commit f33420f

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

adafruit_esp32spi.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ESP_SPIcontrol:
1919
SCAN_NETWORKS = const(0x27)
2020
GET_IDX_RSSI_CMD = const(0x32)
2121
GET_IDX_ENCT_CMD = const(0x33)
22+
REQ_HOST_BY_NAME_CMD = const(0x34)
23+
GET_HOST_BY_NAME_CMD = const(0x35)
2224
START_SCAN_NETWORKS = const(0x36)
2325

2426
GET_FW_VERSION_CMD = const(0x37)
@@ -155,7 +157,8 @@ def wait_response_cmd(self, cmd, num_responses=None):
155157
for num in range(num_responses):
156158
response = []
157159
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))
159162
for j in range(param_len):
160163
response.append(self.get_param())
161164
responses.append(bytes(response))
@@ -170,25 +173,30 @@ def send_command_get_response(self, cmd, params=None, *, reply_params=1):
170173

171174
@property
172175
def status(self):
173-
print("Connection status")
176+
if self._debug:
177+
print("Connection status")
174178
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])
176181
return resp[0][0] # one byte response
177182

178183
@property
179184
def firmware_version(self):
180-
print("Firmware version")
185+
if self._debug:
186+
print("Firmware version")
181187
resp = self.send_command_get_response(GET_FW_VERSION_CMD)
182188
return resp[0]
183189

184190
@property
185191
def MAC_address(self):
186-
print("MAC address")
192+
if self._debug:
193+
print("MAC address")
187194
resp = self.send_command_get_response(GET_MACADDR_CMD, [b'\xFF'])
188195
return resp[0]
189196

190197
def start_scan_networks(self):
191-
print("Start scan")
198+
if self._debug:
199+
print("Start scan")
192200
resp = self.send_command_get_response(START_SCAN_NETWORKS)
193201
if resp[0][0] != 1:
194202
raise RuntimeError("Failed to start AP scan")
@@ -218,15 +226,12 @@ def scan_networks(self):
218226
return APs
219227

220228
def wifi_set_network(self, ssid):
221-
print("Set Network")
222229
resp = self.send_command_get_response(SET_NET_CMD, [ssid])
223230
if resp[0][0] != 1:
224231
raise RuntimeError("Failed to set network")
225232

226233
def wifi_set_passphrase(self, ssid, passphrase):
227-
print("Set passphrase")
228234
resp = self.send_command_get_response(SET_PASSPHRASE_CMD, [ssid, passphrase])
229-
print(resp)
230235
if resp[0][0] != 1:
231236
raise RuntimeError("Failed to set passphrase")
232237

@@ -247,11 +252,11 @@ def network_data(self):
247252

248253
@property
249254
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']
252256

253257
def connect_AP(self, ssid, password):
254-
print("Connect AP")
258+
if self._debug:
259+
print("Connect to AP")
255260
if password:
256261
self.wifi_set_passphrase(ssid, password)
257262
else:
@@ -266,3 +271,16 @@ def connect_AP(self, ssid, password):
266271
if stat == WL_NO_SSID_AVAIL:
267272
raise RuntimeError("No such ssid", ssid)
268273
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

Comments
 (0)