Repository navigation
Expand file tree
/
Copy pathboot.py
More file actions
55 lines (46 loc) · 1.61 KB
/
Copy pathboot.py
File metadata and controls
55 lines (46 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import webrepl
import esp32
import network
import time
from machine import Pin
def connect_to_WiFi():
# If we're running on a Seeed C6 switch to the external antenna
#WIFI_SWITCH_ENABLE = Pin(3, Pin.OUT)
#WIFI_ANT_CONFIG = Pin(14, Pin.OUT)
#WIFI_SWITCH_ENABLE.value(0)
#WIFI_ANT_CONFIG.value(1)
# Get the access point and password from non-volatile storage
try:
nvs = esp32.NVS("keys")
buf = bytearray(32)
nvs.get_blob("ap", buf)
ap = buf.rstrip(b'\x00').decode('utf-8')
buf[:] = b'\x00' * 32 # Clear before reuse
nvs.get_blob("pw", buf)
pw = buf.rstrip(b'\x00').decode('utf-8')
except OSError as e:
print(f"Failed to read WiFi credentials from NVS: {e}")
return
network.hostname("tempmon")
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
sta_if.active(True)
print(f"Connecting to WiFi {ap}...")
#mac = sta_if.config('mac')
#host = 'esp32-' + ''.join('{:02x}'.format(b) for b in mac[3:])
#host = 'tempmon'
#sta_if.config(dhcp_hostname = host)
sta_if.connect(ap, pw)
timeout = 15
start = time.time()
while not sta_if.isconnected():
if time.time() - start > timeout:
print("WiFi connection timed out")
break
#host = sta_if.config('dhcp_hostname')
print('Wifi connected as {}/{}, net={}, gw={}, dns={}'.format(network.hostname(), *sta_if.ifconfig()))
# Connect to the local WiFi
connect_to_WiFi()
# Start the webrepl server on port 8266
webrepl.stop()
webrepl.start()