Skip to content

Commit ea630d2

Browse files
author
Matt Costi
committed
Working hello world server example
1 parent c72cd97 commit ea630d2

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,11 @@ def start_server(self, port, socket_num, conn_mode=TCP_MODE, ip=None):
682682
if resp[0][0] != 1:
683683
raise RuntimeError("Could not start server")
684684

685+
def get_server_state(self, socket_num):
686+
self._socknum_ll[0][0] = socket_num
687+
resp = self._send_command_get_response(_GET_STATE_TCP_CMD, self._socknum_ll)
688+
return resp[0][0]
689+
685690
def set_esp_debug(self, enabled):
686691
"""Enable/disable debug mode on the ESP32. Debug messages will be
687692
written to the ESP32's UART."""

examples/esp32spi_server.py

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import board
22
import busio
3+
import time
34
from digitalio import DigitalInOut
45
from secrets import secrets
56

@@ -13,37 +14,73 @@
1314
esp32_cs = DigitalInOut(board.D10)
1415
esp32_ready = DigitalInOut(board.D9)
1516
esp32_reset = DigitalInOut(board.D7)
17+
esp32_gpio0 = DigitalInOut(board.D12)
1618

1719

1820
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
19-
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=True)
21+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, gpio0_pin=esp32_gpio0, debug=False)
2022

23+
## Create Access Point from SSID and optional password in secrets
2124
wifi = wifimanager.ESPSPI_WiFiManager(esp, secrets, debug=True)
22-
2325
wifi.create_ap()
2426
time.sleep(10)
2527

26-
sock = socket.socket() # gets and creates a socket
27-
sock_num = sock.get_sock_num() # returns socket number
28+
socket.set_interface(esp)
29+
sock = socket.socket() # Request a socket for the server
30+
curr_sock = sock
31+
sockNum = sock.get_sock_num()
32+
print("server status: ", esp.get_server_state(sockNum))
33+
34+
# Start the server on port 80 with the socket number we just requested for it.
35+
esp.start_server(80, sockNum)
2836

29-
esp.start_server(sock_num, 80)
30-
print("socket num: ", sock_num)
31-
print("socket status?: ", esp.socket_status(sock_num))
37+
print("socket num: ", sockNum)
38+
print("server status: ", esp.get_server_state(sockNum))
3239
print("IP addr: ", esp.pretty_ip(esp.ip_address))
40+
print("info: ", esp.network_data)
41+
print("done!")
42+
3343

3444
status = 0
45+
last_sock = 255
46+
def server_avail(): # TODO: make a server helper class
47+
global last_sock
48+
sock = 255;
49+
50+
if (curr_sock != 255):
51+
# if (last_sock != 255):
52+
# TODO: if last sock, check that last_sock is still connected and available
53+
# sock = last_sock
54+
if (sock == 255):
55+
sock = esp.socket_available(sockNum)
56+
if (sock != 255):
57+
last_sock = sock
58+
return sock
59+
60+
return 255
61+
3562
while True:
36-
if status != esp.status:
63+
if status != esp.status: # TODO: Move device connected check to server class ?
3764
status = esp.status
3865

3966
if status == 8:
40-
print("Device connected!") ## works
67+
print("Device connected! status=", status)
4168
else:
42-
print("Device disconnected status=", status) ## works
69+
print("Device disconnected! status=", status)
4370

44-
print("socket available?: ", esp.socket_available(sockNum))
45-
print("socket_status: ", esp.socket_status(sockNum))
46-
print(sock.read())
4771

72+
avail = server_avail()
73+
if (avail != 255):
74+
sock.set_sock_num(avail) # TODO: Server class should return a new client socket
75+
data = sock.read()
76+
if (len(data)):
77+
print(data)
78+
sock.write(b"HTTP/1.1 200 OK\r\n");
79+
sock.write(b"Content-type:text/html\r\n");
80+
sock.write(b"\r\n");
4881

49-
print("done!")
82+
sock.write(b"Click <a href=\"/H\">here</a> turn the LED on!!!<br>\r\n");
83+
sock.write(b"Click <a href=\"/L\">here</a> turn the LED off!!!!<br>\r\n");
84+
85+
sock.write(b"\r\n")
86+
sock.close()

0 commit comments

Comments
 (0)