Skip to content

Commit 2990061

Browse files
Adding to the functionality of the ESP32 library in order to provide a printable echo of a remote IP connection in addition to optional code to control specific IP access to the server as the server can only take one connection at a time.
1 parent 0d382ef commit 2990061

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
_GET_HOST_BY_NAME_CMD = const(0x35)
8383
_START_SCAN_NETWORKS = const(0x36)
8484
_GET_FW_VERSION_CMD = const(0x37)
85+
_GET_REMOTE_DATA_CMD = const(0x3A)
8586
_GET_TIME = const(0x3B)
8687
_PING_CMD = const(0x3E)
8788

@@ -768,3 +769,8 @@ def get_time(self):
768769
resp = self._send_command_get_response(_GET_TIME)
769770
return struct.unpack('<i', resp[0])
770771
raise RuntimeError("Must be connected to WiFi before obtaining NTP.")
772+
773+
def get_remote_data(self, socket_num):
774+
self._socknum_ll[0][0] = socket_num
775+
resp = self._send_command_get_response(_GET_REMOTE_DATA_CMD, self._socknum_ll, reply_params=2)
776+
return resp[0]

adafruit_esp32spi/adafruit_esp32spi_wsgiserver.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,23 @@ def update_poll(self):
9898
"""
9999
self.client_available()
100100
if (self._client_sock and self._client_sock.available()):
101+
self.print_remote_ip()
101102
environ = self._get_environ(self._client_sock)
102103
result = self.application(environ, self._start_response)
103104
self.finish_response(result)
105+
"""
106+
Optional routine to control what IP connects to your ESP server
107+
as it forces a single connection as the server can't handle more
108+
than one request at a time.
109+
"""
110+
# self.client_available()
111+
# if (self._client_sock and self._client_sock.available()):
112+
# result = self.check_remote_ip()
113+
# if result == "192.168.4.2":
114+
# self.print_remote_ip()
115+
# environ = self._get_environ(self._client_sock)
116+
# result = self.application(environ, self._start_response)
117+
# self.finish_response(result)
104118

105119
def finish_response(self, result):
106120
"""
@@ -216,3 +230,13 @@ def _get_environ(self, client):
216230
env[key] = value
217231

218232
return env
233+
234+
def check_remote_ip(self):
235+
sock_num = self._client_sock.socknum
236+
remote_ip = _the_interface.get_remote_data(sock_num)
237+
return _the_interface.pretty_ip(remote_ip)
238+
239+
def print_remote_ip(self):
240+
sock_num = self._client_sock.socknum
241+
remote_ip = _the_interface.get_remote_data(sock_num)
242+
print("Remote Connection Established: " + _the_interface.pretty_ip(remote_ip))

0 commit comments

Comments
 (0)