Skip to content

Commit 1fe7e85

Browse files
authored
Merge pull request #40 from cpforbes/wifi_fix
Fix debug in WiFiManager and socket leak in request().
2 parents 318c7ae + 4ebc502 commit 1fe7e85

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@
6666
_SCAN_NETWORKS = const(0x27)
6767
_GET_SOCKET_CMD = const(0x3F)
6868
_GET_STATE_TCP_CMD = const(0x29)
69-
_DATA_SENT_TCP_CMD = const(0x2A)
70-
_AVAIL_DATA_TCP_CMD = const(0x2B)
71-
_GET_DATA_TCP_CMD = const(0x2C)
69+
_DATA_SENT_TCP_CMD = const(0x2A)
70+
_AVAIL_DATA_TCP_CMD = const(0x2B)
71+
_GET_DATA_TCP_CMD = const(0x2C)
7272
_START_CLIENT_TCP_CMD = const(0x2D)
7373
_STOP_CLIENT_TCP_CMD = const(0x2E)
7474
_GET_CLIENT_STATE_TCP_CMD = const(0x2F)
75-
_DISCONNECT_CMD = const(0x30)
75+
_DISCONNECT_CMD = const(0x30)
7676
_GET_IDX_RSSI_CMD = const(0x32)
7777
_GET_IDX_ENCT_CMD = const(0x33)
7878
_REQ_HOST_BY_NAME_CMD = const(0x34)
7979
_GET_HOST_BY_NAME_CMD = const(0x35)
8080
_START_SCAN_NETWORKS = const(0x36)
8181
_GET_FW_VERSION_CMD = const(0x37)
82-
_PING_CMD = const(0x3E)
82+
_PING_CMD = const(0x3E)
8383

8484
_SEND_DATA_TCP_CMD = const(0x44)
8585
_GET_DATABUF_TCP_CMD = const(0x45)
@@ -612,6 +612,8 @@ def socket_connect(self, socket_num, dest, port, conn_mode=TCP_MODE):
612612

613613
def socket_close(self, socket_num):
614614
"""Close a socket using the ESP32's internal reference number"""
615+
if self._debug:
616+
print("*** Closing socket #%d" % socket_num)
615617
self._socknum_ll[0][0] = socket_num
616618
resp = self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
617619
if resp[0][0] != 1:

adafruit_esp32spi/adafruit_esp32spi_requests.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def __init__(self, sock):
6969
self._read_so_far = 0
7070
self.headers = {}
7171

72+
def __enter__(self):
73+
return self
74+
75+
def __exit__(self, exc_type, exc_value, traceback):
76+
self.close()
77+
7278
def close(self):
7379
"""Close, delete and collect the response data"""
7480
if self.socket:
@@ -216,7 +222,7 @@ def request(method, url, data=None, json=None, headers=None, stream=False):
216222
elif line.startswith(b"Location:") and not 200 <= status <= 299:
217223
raise NotImplementedError("Redirects not yet supported")
218224

219-
except OSError:
225+
except:
220226
sock.close()
221227
raise
222228

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131

3232
# pylint: disable=no-name-in-module
3333

34-
import adafruit_esp32spi
34+
from adafruit_esp32spi import adafruit_esp32spi
3535
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
3636

3737
class ESPSPI_WiFiManager:
3838
"""
3939
A class to help manage the Wifi connection
4040
"""
41-
def __init__(self, esp, secrets, status_pixel, attempts=2):
41+
def __init__(self, esp, secrets, status_pixel=None, attempts=2):
4242
"""
4343
:param ESP_SPIcontrol esp: The ESP object we are using
4444
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)

0 commit comments

Comments
 (0)