attempt at start_server method
This commit is contained in:
parent
45cf5c1df7
commit
c72cd97f02
3 changed files with 50 additions and 1 deletions
|
|
@ -66,6 +66,7 @@ _GET_CURR_RSSI_CMD = const(0x25)
|
|||
_GET_CURR_ENCT_CMD = const(0x26)
|
||||
|
||||
_SCAN_NETWORKS = const(0x27)
|
||||
_START_SERVER_TCP_CMD = const(0x28)
|
||||
_GET_SOCKET_CMD = const(0x3F)
|
||||
_GET_STATE_TCP_CMD = const(0x29)
|
||||
_DATA_SENT_TCP_CMD = const(0x2A)
|
||||
|
|
@ -662,6 +663,25 @@ class ESP_SPIcontrol: # pylint: disable=too-many-public-methods
|
|||
if resp[0][0] != 1:
|
||||
raise RuntimeError("Failed to close socket")
|
||||
|
||||
def start_server(self, port, socket_num, conn_mode=TCP_MODE, ip=None):
|
||||
if self._debug:
|
||||
print("*** starting server")
|
||||
self._socknum_ll[0][0] = socket_num
|
||||
port_param = struct.pack('>H', port)
|
||||
if ip: # use the 4 arg version
|
||||
resp = self._send_command_get_response(_START_SERVER_TCP_CMD,
|
||||
(ip,
|
||||
port_param,
|
||||
self._socknum_ll[0],
|
||||
(conn_mode,)))
|
||||
else: # use the 3 arg version
|
||||
resp = self._send_command_get_response(_START_SERVER_TCP_CMD,
|
||||
(port_param,
|
||||
self._socknum_ll[0],
|
||||
(conn_mode,)))
|
||||
if resp[0][0] != 1:
|
||||
raise RuntimeError("Could not start server")
|
||||
|
||||
def set_esp_debug(self, enabled):
|
||||
"""Enable/disable debug mode on the ESP32. Debug messages will be
|
||||
written to the ESP32's UART."""
|
||||
|
|
|
|||
|
|
@ -66,6 +66,7 @@ class socket:
|
|||
raise RuntimeError("Only SOCK_STREAM type supported")
|
||||
self._buffer = b''
|
||||
self._socknum = _the_interface.get_socket()
|
||||
print("socknum: ", self._socknum)
|
||||
self.settimeout(0)
|
||||
|
||||
def connect(self, address, conntype=None):
|
||||
|
|
@ -148,6 +149,9 @@ class socket:
|
|||
"""Set the read timeout for sockets, if value is 0 it will block"""
|
||||
self._timeout = value
|
||||
|
||||
def get_sock_num(self):
|
||||
return self._socknum
|
||||
|
||||
def close(self):
|
||||
"""Close the socket, after reading whatever remains"""
|
||||
_the_interface.socket_close(self._socknum)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from secrets import secrets
|
|||
from adafruit_esp32spi import adafruit_esp32spi
|
||||
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
|
||||
import adafruit_esp32spi.adafruit_esp32spi_wifimanager as wifimanager
|
||||
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
|
||||
|
||||
print("ESP32 SPI web server test!!!!!!")
|
||||
|
||||
|
|
@ -19,6 +20,30 @@ esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset,
|
|||
|
||||
wifi = wifimanager.ESPSPI_WiFiManager(esp, secrets, debug=True)
|
||||
|
||||
wifi.create_ap();
|
||||
wifi.create_ap()
|
||||
time.sleep(10)
|
||||
|
||||
sock = socket.socket() # gets and creates a socket
|
||||
sock_num = sock.get_sock_num() # returns socket number
|
||||
|
||||
esp.start_server(sock_num, 80)
|
||||
print("socket num: ", sock_num)
|
||||
print("socket status?: ", esp.socket_status(sock_num))
|
||||
print("IP addr: ", esp.pretty_ip(esp.ip_address))
|
||||
|
||||
status = 0
|
||||
while True:
|
||||
if status != esp.status:
|
||||
status = esp.status
|
||||
|
||||
if status == 8:
|
||||
print("Device connected!") ## works
|
||||
else:
|
||||
print("Device disconnected status=", status) ## works
|
||||
|
||||
print("socket available?: ", esp.socket_available(sockNum))
|
||||
print("socket_status: ", esp.socket_status(sockNum))
|
||||
print(sock.read())
|
||||
|
||||
|
||||
print("done!")
|
||||
|
|
|
|||
Loading…
Reference in a new issue