Sun, 16 Mar 2025 12:53:12 +0100
Added the Adafruit Feather nRF52840 to the list of known NRF52 boards and changed the list of known CircuitPython boards to be more explicit with respect to Adafruit boards (i.e. VID 0x239A).
11038 | 1 | def has_ntp(): |
2 | try: | |
3 | import ntptime | |
4 | return True | |
5 | except ImportError: | |
6 | return False | |
7 | ||
8 | def set_ntp_time(server, tz_offset, timeout): | |
9 | import ntptime | |
10 | import machine | |
11 | ||
12 | ntptime.host = server | |
13 | ntptime.timeout = timeout | |
14 | ntptime.settime() | |
15 | ||
16 | rtc = machine.RTC() | |
17 | t = list(rtc.datetime()) | |
18 | t[4] += tz_offset | |
19 | rtc.datetime(t) | |
20 | ||
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9828
diff
changeset
|
21 | def connect_wifi(): |
9795 | 22 | import esp32 |
23 | import network | |
24 | from time import sleep | |
25 | ||
26 | try: | |
27 | nvs = esp32.NVS("wifi_creds") | |
28 | buf = bytearray(1024) | |
29 | size = nvs.get_blob("ssid", buf) | |
30 | ssid = buf[:size].decode() | |
31 | size = nvs.get_blob("password", buf) | |
32 | password = buf[:size].decode() | |
10153 | 33 | size = nvs.get_blob("hostname", buf) |
34 | hostname = buf[:size].decode() | |
35 | size = nvs.get_blob("country", buf) | |
36 | country = buf[:size].decode() | |
37 | ||
38 | print("Connecting WiFi to '{0}'...".format(ssid)) | |
39 | ||
40 | if country: | |
41 | try: | |
42 | network.country(country) | |
43 | except AttributeError: | |
44 | pass | |
45 | ||
46 | if hostname: | |
47 | try: | |
48 | network.hostname(hostname) | |
49 | except AttributeError: | |
50 | pass | |
9795 | 51 | |
52 | wifi = network.WLAN(network.STA_IF) | |
53 | wifi.active(False) | |
54 | wifi.active(True) | |
55 | wifi.connect(ssid, password) | |
56 | max_wait = 140 | |
11038 | 57 | while max_wait and wifi.status() != network.STAT_GOT_IP: |
9795 | 58 | max_wait -= 1 |
59 | sleep(0.1) | |
11038 | 60 | if wifi.isconnected(): |
61 | print("WiFi connected:", wifi.ifconfig()[0]) | |
62 | if has_ntp(): | |
63 | set_ntp_time("pool.ntp.org", 0, 10) | |
64 | print("Time snchronized to network time (UTC).") | |
65 | else: | |
66 | print("WiFi connection failed. Status:", wifi.status()) | |
9795 | 67 | except: |
9828 | 68 | print("WiFi secrets are kept in NVM. Please store them there!") |
9795 | 69 | |
9890
66a6d3f131cc
Changed the MCU script to a pythonic naming.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9828
diff
changeset
|
70 | connect_wifi() |