Skip to content

Commit 3e19631

Browse files
committed
connect and get network settings/ip
1 parent ac9976b commit 3e19631

File tree

1 file changed

+82
-12
lines changed

1 file changed

+82
-12
lines changed

adafruit_esp32spi.py

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
from micropython import const
77

88
class ESP_SPIcontrol:
9+
SET_NET_CMD = const(0x10)
10+
SET_PASSPHRASE_CMD = const(0x11)
11+
912
GET_CONN_STATUS_CMD = const(0x20)
13+
GET_IPADDR_CMD = const(0x21)
1014
GET_MACADDR_CMD = const(0x22)
15+
GET_CURR_SSID_CMD = const(0x23)
16+
GET_CURR_RSSI_CMD = const(0x25)
17+
GET_CURR_ENCT_CMD = const(0x26)
18+
1119
SCAN_NETWORKS = const(0x27)
1220
GET_IDX_RSSI_CMD = const(0x32)
1321
GET_IDX_ENCT_CMD = const(0x33)
1422
START_SCAN_NETWORKS = const(0x36)
23+
1524
GET_FW_VERSION_CMD = const(0x37)
1625

1726
START_CMD = const(0xE0)
@@ -23,9 +32,12 @@ class ESP_SPIcontrol:
2332
WL_NO_SHIELD = const(0xFF)
2433
WL_NO_MODULE = const(0xFF)
2534
WL_IDLE_STATUS = const(0)
35+
WL_NO_SSID_AVAIL = const(1)
36+
WL_SCAN_COMPLETED = const(2)
37+
WL_CONNECTED = const(3)
2638

27-
def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=True):
28-
self.debug = debug
39+
def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=False):
40+
self._debug = debug
2941
self._buffer = bytearray(10)
3042
self._pbuf = bytearray(1) # buffer for param read
3143

@@ -69,17 +81,19 @@ def slave_ready(self):
6981
return self._ready.value == False
7082

7183
def wait_for_slave_ready(self):
72-
print("Wait for slave ready", end='')
84+
if self._debug:
85+
print("Wait for slave ready", end='')
7386
while not self.slave_ready():
74-
print('.', end='')
87+
if self._debug:
88+
print('.', end='')
7589
time.sleep(0.01)
76-
print()
90+
if self._debug:
91+
print()
7792

7893
def wait_for_slave_select(self):
7994
self.wait_for_slave_ready()
8095
self.spi_slave_select()
8196

82-
8397
def send_command(self, cmd, params=None):
8498
if not params:
8599
params = []
@@ -94,18 +108,21 @@ def send_command(self, cmd, params=None):
94108
packet += (param)
95109

96110
packet.append(END_CMD)
97-
print("packet len:", len(packet))
111+
if self._debug:
112+
print("packet len:", len(packet))
98113
while len(packet) % 4 != 0:
99114
packet.append(0xFF)
100115

101116
self.wait_for_slave_select()
102117
self._spi.write(bytearray(packet))
103-
print("Wrote: ", [hex(b) for b in packet])
118+
if self._debug:
119+
print("Wrote: ", [hex(b) for b in packet])
104120
self.slave_deselect()
105121

106122
def get_param(self):
107123
self._spi.readinto(self._pbuf)
108-
print("Read param", hex(self._pbuf[0]))
124+
if self._debug:
125+
print("Read param", hex(self._pbuf[0]))
109126
return self._pbuf[0]
110127

111128
def wait_spi_char(self, desired):
@@ -151,18 +168,21 @@ def send_command_get_response(self, cmd, params=None, *, reply_params=1):
151168
self.send_command(cmd, params)
152169
return self.wait_response_cmd(cmd, reply_params)
153170

154-
def get_connection_status(self):
171+
@property
172+
def status(self):
155173
print("Connection status")
156174
resp = self.send_command_get_response(GET_CONN_STATUS_CMD)
157175
print("Status:", resp[0][0])
158176
return resp[0][0] # one byte response
159177

160-
def get_firmware_version(self):
178+
@property
179+
def firmware_version(self):
161180
print("Firmware version")
162181
resp = self.send_command_get_response(GET_FW_VERSION_CMD)
163182
return resp[0]
164183

165-
def get_MAC(self):
184+
@property
185+
def MAC_address(self):
166186
print("MAC address")
167187
resp = self.send_command_get_response(GET_MACADDR_CMD, [b'\xFF'])
168188
return resp[0]
@@ -196,3 +216,53 @@ def scan_networks(self):
196216
if len(APs):
197217
break
198218
return APs
219+
220+
def wifi_set_network(self, ssid):
221+
print("Set Network")
222+
resp = self.send_command_get_response(SET_NET_CMD, [ssid])
223+
if resp[0][0] != 1:
224+
raise RuntimeError("Failed to set network")
225+
226+
def wifi_set_passphrase(self, ssid, passphrase):
227+
print("Set passphrase")
228+
resp = self.send_command_get_response(SET_PASSPHRASE_CMD, [ssid, passphrase])
229+
print(resp)
230+
if resp[0][0] != 1:
231+
raise RuntimeError("Failed to set passphrase")
232+
233+
@property
234+
def ssid(self):
235+
resp = self.send_command_get_response(GET_CURR_SSID_CMD, [b'\xFF'])
236+
return resp[0]
237+
238+
@property
239+
def rssi(self):
240+
resp = self.send_command_get_response(GET_CURR_RSSI_CMD, [b'\xFF'])
241+
return struct.unpack('<i', resp[0])[0]
242+
243+
@property
244+
def network_data(self):
245+
resp = self.send_command_get_response(GET_IPADDR_CMD, [b'\xFF'], reply_params=3)
246+
return {'ip_addr': resp[0], 'netmask': resp[1], 'gateway': resp[2]}
247+
248+
@property
249+
def ip_address(self):
250+
raw_ip = self.network_data['ip_addr']
251+
return "%d.%d.%d.%d" % (raw_ip[0], raw_ip[1], raw_ip[2], raw_ip[3])
252+
253+
def connect_AP(self, ssid, password):
254+
print("Connect AP")
255+
if password:
256+
self.wifi_set_passphrase(ssid, password)
257+
else:
258+
self.wifi_set_network(ssid)
259+
for i in range(10): # retries
260+
stat = self.status
261+
if stat == WL_CONNECTED:
262+
return stat
263+
time.sleep(1)
264+
if stat in (WL_CONNECT_FAILED, WL_CONNECTION_LOST, WL_DISCONNECTED):
265+
raise RuntimeError("Failed to connect to ssid", ssid)
266+
if stat == WL_NO_SSID_AVAIL:
267+
raise RuntimeError("No such ssid", ssid)
268+
raise RuntimeError("Unknown error 0x%02X" % stat)

0 commit comments

Comments
 (0)