Skip to content

Commit c72cd97

Browse files
author
Matt Costi
committed
attempt at start_server method
1 parent 45cf5c1 commit c72cd97

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
_GET_CURR_ENCT_CMD = const(0x26)
6767

6868
_SCAN_NETWORKS = const(0x27)
69+
_START_SERVER_TCP_CMD = const(0x28)
6970
_GET_SOCKET_CMD = const(0x3F)
7071
_GET_STATE_TCP_CMD = const(0x29)
7172
_DATA_SENT_TCP_CMD = const(0x2A)
@@ -662,6 +663,25 @@ def socket_close(self, socket_num):
662663
if resp[0][0] != 1:
663664
raise RuntimeError("Failed to close socket")
664665

666+
def start_server(self, port, socket_num, conn_mode=TCP_MODE, ip=None):
667+
if self._debug:
668+
print("*** starting server")
669+
self._socknum_ll[0][0] = socket_num
670+
port_param = struct.pack('>H', port)
671+
if ip: # use the 4 arg version
672+
resp = self._send_command_get_response(_START_SERVER_TCP_CMD,
673+
(ip,
674+
port_param,
675+
self._socknum_ll[0],
676+
(conn_mode,)))
677+
else: # use the 3 arg version
678+
resp = self._send_command_get_response(_START_SERVER_TCP_CMD,
679+
(port_param,
680+
self._socknum_ll[0],
681+
(conn_mode,)))
682+
if resp[0][0] != 1:
683+
raise RuntimeError("Could not start server")
684+
665685
def set_esp_debug(self, enabled):
666686
"""Enable/disable debug mode on the ESP32. Debug messages will be
667687
written to the ESP32's UART."""

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None):
6666
raise RuntimeError("Only SOCK_STREAM type supported")
6767
self._buffer = b''
6868
self._socknum = _the_interface.get_socket()
69+
print("socknum: ", self._socknum)
6970
self.settimeout(0)
7071

7172
def connect(self, address, conntype=None):
@@ -148,6 +149,9 @@ def settimeout(self, value):
148149
"""Set the read timeout for sockets, if value is 0 it will block"""
149150
self._timeout = value
150151

152+
def get_sock_num(self):
153+
return self._socknum
154+
151155
def close(self):
152156
"""Close the socket, after reading whatever remains"""
153157
_the_interface.socket_close(self._socknum)

examples/esp32spi_server.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from adafruit_esp32spi import adafruit_esp32spi
77
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
88
import adafruit_esp32spi.adafruit_esp32spi_wifimanager as wifimanager
9+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
910

1011
print("ESP32 SPI web server test!!!!!!")
1112

@@ -19,6 +20,30 @@
1920

2021
wifi = wifimanager.ESPSPI_WiFiManager(esp, secrets, debug=True)
2122

22-
wifi.create_ap();
23+
wifi.create_ap()
24+
time.sleep(10)
25+
26+
sock = socket.socket() # gets and creates a socket
27+
sock_num = sock.get_sock_num() # returns socket number
28+
29+
esp.start_server(sock_num, 80)
30+
print("socket num: ", sock_num)
31+
print("socket status?: ", esp.socket_status(sock_num))
32+
print("IP addr: ", esp.pretty_ip(esp.ip_address))
33+
34+
status = 0
35+
while True:
36+
if status != esp.status:
37+
status = esp.status
38+
39+
if status == 8:
40+
print("Device connected!") ## works
41+
else:
42+
print("Device disconnected status=", status) ## works
43+
44+
print("socket available?: ", esp.socket_available(sockNum))
45+
print("socket_status: ", esp.socket_status(sockNum))
46+
print(sock.read())
47+
2348

2449
print("done!")

0 commit comments

Comments
 (0)