Skip to content

Commit 0f016f9

Browse files
committed
fix ping, prettier example
1 parent a86c896 commit 0f016f9

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class ESP_SPIcontrol: # pylint: disable=too-many-public-methods
123123
TLS_MODE = const(2)
124124

125125
# pylint: disable=too-many-arguments
126-
def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=False):
126+
def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin=None, *, debug=False):
127127
self._debug = debug
128128
self._buffer = bytearray(10)
129129
self._pbuf = bytearray(1) # buffer for param read
@@ -138,22 +138,25 @@ def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=False)
138138
self._cs.direction = Direction.OUTPUT
139139
self._ready.direction = Direction.INPUT
140140
self._reset.direction = Direction.OUTPUT
141-
self._gpio0.direction = Direction.INPUT
141+
if self._gpio0:
142+
self._gpio0.direction = Direction.INPUT
142143
self.reset()
143144
# pylint: enable=too-many-arguments
144145

145146
def reset(self):
146147
"""Hard reset the ESP32 using the reset pin"""
147-
self._gpio0.direction = Direction.OUTPUT
148148
if self._debug:
149149
print("Reset ESP32")
150-
self._gpio0.value = True # not bootload mode
150+
if self._gpio0:
151+
self._gpio0.direction = Direction.OUTPUT
152+
self._gpio0.value = True # not bootload mode
151153
self._cs.value = True
152154
self._reset.value = False
153155
time.sleep(0.01) # reset
154156
self._reset.value = True
155157
time.sleep(0.75) # wait for it to boot up
156-
self._gpio0.direction = Direction.INPUT
158+
if self._gpio0:
159+
self._gpio0.direction = Direction.INPUT
157160

158161
def _wait_for_ready(self):
159162
"""Wait until the ready pin goes low"""
@@ -338,7 +341,7 @@ def get_scan_networks(self):
338341
'ssid', 'rssi' and 'encryption' entries, one for each AP found"""
339342
self._send_command(_SCAN_NETWORKS)
340343
names = self._wait_response_cmd(_SCAN_NETWORKS)
341-
print("SSID names:", names)
344+
#print("SSID names:", names)
342345
APs = [] # pylint: disable=invalid-name
343346
for i, name in enumerate(names):
344347
a_p = {'ssid': name}
@@ -466,7 +469,7 @@ def ping(self, dest, ttl=250):
466469
dest = self.get_host_by_name(dest)
467470
# ttl must be between 0 and 255
468471
ttl = max(0, min(ttl, 255))
469-
resp = self._send_command_get_response(_PING_CMD, (dest, (ttl)))
472+
resp = self._send_command_get_response(_PING_CMD, (dest, (ttl,)))
470473
return struct.unpack('<H', resp[0])[0]
471474

472475
def get_socket(self):

examples/esp32spi_simpletest.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,47 @@
1010
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
1111
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
1212

13-
esp32_cs = DigitalInOut(board.ESP_CS)
14-
esp32_ready = DigitalInOut(board.ESP_BUSY)
15-
esp32_gpio0 = DigitalInOut(board.ESP_GPIO0)
16-
esp32_reset = DigitalInOut(board.ESP_RESET)
13+
#esp32_cs = DigitalInOut(board.ESP_CS)
14+
#esp32_ready = DigitalInOut(board.ESP_BUSY)
15+
#esp32_reset = DigitalInOut(board.ESP_RESET)
16+
17+
import microcontroller
18+
esp32_cs = DigitalInOut(microcontroller.pin.PB14)
19+
esp32_ready = DigitalInOut(microcontroller.pin.PB16)
20+
esp32_reset = DigitalInOut(microcontroller.pin.PB17)
21+
1722
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
18-
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, esp32_gpio0)
23+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
1924

2025
requests.set_interface(esp)
2126

2227
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
2328
print("ESP32 found and in idle mode")
2429
print("Firmware vers.", esp.firmware_version)
2530
print("MAC addr:", [hex(i) for i in esp.MAC_address])
26-
print("APs: ", esp.scan_networks())
31+
for ap in esp.scan_networks():
32+
print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))
2733
print("Connecting to AP...")
2834
esp.connect_AP(b'adafruit', b'ffffffff')
29-
print("Connected to", esp.ssid, "RSSI", esp.rssi)
35+
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
3036
print("My IP address is", esp.pretty_ip(esp.ip_address))
3137
print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")))
3238
print("Ping google.com: %d ms" % esp.ping("google.com"))
3339

3440
#esp._debug = True
3541
print("Fetching text from", TEXT_URL)
3642
r = requests.get(TEXT_URL)
43+
print('-'*40)
3744
print(r.text)
45+
print('-'*40)
3846
r.close()
3947

48+
print()
4049
print("Fetching json from", JSON_URL)
4150
r = requests.get(JSON_URL)
51+
print('-'*40)
4252
print(r.json())
53+
print('-'*40)
4354
r.close()
4455

4556
print("Done!")

0 commit comments

Comments
 (0)