Skip to content

Commit 5b28118

Browse files
committed
separate server code from AP code
1 parent 8f964cc commit 5b28118

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
# pylint: disable=bad-whitespace
5555
_SET_NET_CMD = const(0x10)
5656
_SET_PASSPHRASE_CMD = const(0x11)
57+
_SET_AP_PASSPHRASE_CMD = const(0x19)
5758
_SET_DEBUG_CMD = const(0x1A)
5859

5960
_GET_CONN_STATUS_CMD = const(0x20)
@@ -64,6 +65,7 @@
6465
_GET_CURR_ENCT_CMD = const(0x26)
6566

6667
_SCAN_NETWORKS = const(0x27)
68+
_START_SERVER_TCP_CMD = const(0x28)
6769
_GET_SOCKET_CMD = const(0x3F)
6870
_GET_STATE_TCP_CMD = const(0x29)
6971
_DATA_SENT_TCP_CMD = const(0x2A)
@@ -622,6 +624,30 @@ def socket_close(self, socket_num):
622624
if resp[0][0] != 1:
623625
raise RuntimeError("Failed to close socket")
624626

627+
def start_server(self, port, socket_num, conn_mode=TCP_MODE, ip=None):
628+
if self._debug:
629+
print("*** starting server")
630+
self._socknum_ll[0][0] = socket_num
631+
port_param = struct.pack('>H', port)
632+
if ip: # use the 4 arg version
633+
resp = self._send_command_get_response(_START_SERVER_TCP_CMD,
634+
(ip,
635+
port_param,
636+
self._socknum_ll[0],
637+
(conn_mode,)))
638+
else: # use the 3 arg version
639+
resp = self._send_command_get_response(_START_SERVER_TCP_CMD,
640+
(port_param,
641+
self._socknum_ll[0],
642+
(conn_mode,)))
643+
if resp[0][0] != 1:
644+
raise RuntimeError("Could not start server")
645+
646+
def get_server_state(self, socket_num):
647+
self._socknum_ll[0][0] = socket_num
648+
resp = self._send_command_get_response(_GET_STATE_TCP_CMD, self._socknum_ll)
649+
return resp[0][0]
650+
625651
def set_esp_debug(self, enabled):
626652
"""Enable/disable debug mode on the ESP32. Debug messages will be
627653
written to the ESP32's UART."""

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ def settimeout(self, value):
148148
"""Set the read timeout for sockets, if value is 0 it will block"""
149149
self._timeout = value
150150

151+
def get_sock_num(self):
152+
return self._socknum
153+
154+
def set_sock_num(self, sock_num):
155+
self._socknum = sock_num
151156
def close(self):
152157
"""Close the socket, after reading whatever remains"""
153158
_the_interface.socket_close(self._socknum)

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ESPSPI_WiFiManager:
3838
"""
3939
A class to help manage the Wifi connection
4040
"""
41-
def __init__(self, esp, secrets, status_pixel=None, attempts=2):
41+
def __init__(self, esp, secrets, status_pixel=None, attempts=2, debug=False):
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)
@@ -49,7 +49,7 @@ def __init__(self, esp, secrets, status_pixel=None, attempts=2):
4949
"""
5050
# Read the settings
5151
self.esp = esp
52-
self.debug = False
52+
self.debug = debug
5353
self.ssid = secrets['ssid']
5454
self.password = secrets['password']
5555
self.attempts = attempts

examples/esp32spi_server.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import board
2+
import busio
3+
from digitalio import DigitalInOut
4+
5+
from adafruit_esp32spi import adafruit_esp32spi
6+
import adafruit_esp32spi.adafruit_esp32spi_wifimanager as wifimanager
7+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
8+
9+
# Get wifi details and more from a secrets.py file
10+
try:
11+
from secrets import secrets
12+
except ImportError:
13+
print("WiFi secrets are kept in secrets.py, please add them there!")
14+
raise
15+
16+
print("ESP32 SPI simple web server test!")
17+
18+
esp32_cs = DigitalInOut(board.D10)
19+
esp32_ready = DigitalInOut(board.D9)
20+
esp32_reset = DigitalInOut(board.D7)
21+
esp32_gpio0 = DigitalInOut(board.D12)
22+
23+
24+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
25+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, gpio0_pin=esp32_gpio0, debug=False)
26+
27+
## Connect to wifi with secrets
28+
wifi = wifimanager.ESPSPI_WiFiManager(esp, secrets, debug=True)
29+
wifi.connect()
30+
31+
socket.set_interface(esp)
32+
sock = socket.socket() # Request a socket for the server
33+
curr_sock = sock
34+
sockNum = sock.get_sock_num()
35+
print("server status: ", esp.get_server_state(sockNum))
36+
37+
# Start the server on port 80 with the socket number we just requested for it.
38+
esp.start_server(80, sockNum)
39+
40+
print("socket num: ", sockNum)
41+
print("server status: ", esp.get_server_state(sockNum))
42+
print("IP addr: ", esp.pretty_ip(esp.ip_address))
43+
print("info: ", esp.network_data)
44+
print("done!")
45+
46+
47+
status = 0
48+
last_sock = 255
49+
def server_avail(): # TODO: make a server helper class
50+
global last_sock
51+
sock = 255;
52+
53+
if (curr_sock != 255):
54+
# if (last_sock != 255):
55+
# TODO: if last sock, check that last_sock is still connected and available
56+
# sock = last_sock
57+
if (sock == 255):
58+
sock = esp.socket_available(sockNum)
59+
if (sock != 255):
60+
last_sock = sock
61+
return sock
62+
63+
return 255
64+
65+
while True:
66+
67+
avail = server_avail()
68+
if (avail != 255):
69+
sock.set_sock_num(avail) # TODO: Server class should return a new client socket
70+
data = sock.read()
71+
if len(data):
72+
print(data)
73+
sock.write(b"HTTP/1.1 200 OK\r\n")
74+
sock.write(b"Content-type:text/html\r\n")
75+
sock.write(b"\r\n")
76+
77+
sock.write(b"Click <a href=\"/H\">here</a> turn the LED on!!!<br>\r\n")
78+
sock.write(b"Click <a href=\"/L\">here</a> turn the LED off!!!!<br>\r\n")
79+
80+
sock.write(b"\r\n")
81+
sock.close()

0 commit comments

Comments
 (0)